proxygen
folly::AttachDetachClient Class Reference
Inheritance diagram for folly::AttachDetachClient:
folly::AsyncSocket::ConnectCallback folly::AsyncWriter::WriteCallback folly::AsyncReader::ReadCallback

Public Member Functions

 AttachDetachClient (const folly::SocketAddress &address)
 
Future< bool > getFuture ()
 
void connect ()
 
void connectSuccess () noexceptoverride
 
void connectErr (const AsyncSocketException &ex) noexceptoverride
 
void writeSuccess () noexceptoverride
 
void writeErr (size_t, const AsyncSocketException &ex) noexceptoverride
 
void getReadBuffer (void **bufReturn, size_t *lenReturn) override
 
void readEOF () noexceptoverride
 
void readErr (const AsyncSocketException &ex) noexceptoverride
 
void readDataAvailable (size_t len) noexceptoverride
 
- Public Member Functions inherited from folly::AsyncSocket::ConnectCallback
virtual ~ConnectCallback ()=default
 
- Public Member Functions inherited from folly::AsyncWriter::WriteCallback
virtual ~WriteCallback ()=default
 
- Public Member Functions inherited from folly::AsyncReader::ReadCallback
virtual ~ReadCallback ()=default
 
virtual bool isBufferMovable () noexcept
 
virtual size_t maxBufferSize () const
 
virtual void readBufferAvailable (std::unique_ptr< IOBuf >) noexcept
 

Private Member Functions

void detach ()
 

Private Attributes

EvbAndContext t1_
 
EvbAndContext t2_
 
std::shared_ptr< AsyncSSLSocketsslSocket_
 
folly::SocketAddress address_
 
char buf_ [128]
 
char readbuf_ [128]
 
uint32_t bytesRead_
 
folly::Promise< bool > promise_
 

Detailed Description

Definition at line 61 of file AsyncSSLSocketTest2.cpp.

Constructor & Destructor Documentation

folly::AttachDetachClient::AttachDetachClient ( const folly::SocketAddress address)
inlineexplicit

Definition at line 83 of file AsyncSSLSocketTest2.cpp.

84  : address_(address), bytesRead_(0) {}
folly::SocketAddress address_

Member Function Documentation

void folly::AttachDetachClient::connect ( )
inline

Definition at line 90 of file AsyncSSLSocketTest2.cpp.

References folly::EvbAndContext::attach(), folly::EvbAndContext::createSocket(), folly::EvbAndContext::ctx_, folly::EvbAndContext::getEventBase(), i, and folly::EventBase::runInEventBaseThread().

90  {
91  // create in one and then move to another
92  auto t1Evb = t1_.getEventBase();
93  t1Evb->runInEventBaseThread([this] {
95  // ensure we can detach and reattach the context before connecting
96  for (int i = 0; i < 1000; ++i) {
97  sslSocket_->detachSSLContext();
98  sslSocket_->attachSSLContext(t1_.ctx_);
99  }
100  // detach from t1 and connect in t2
101  detach();
102  auto t2Evb = t2_.getEventBase();
103  t2Evb->runInEventBaseThread([this] {
105  sslSocket_->connect(this, address_);
106  });
107  });
108  }
std::shared_ptr< SSLContext > ctx_
std::shared_ptr< AsyncSSLSocket > createSocket()
bool runInEventBaseThread(void(*fn)(T *), T *arg)
Definition: EventBase.h:794
folly::SocketAddress address_
std::shared_ptr< AsyncSSLSocket > sslSocket_
void attach(AsyncSSLSocket &socket)
void folly::AttachDetachClient::connectErr ( const AsyncSocketException ex)
inlineoverridevirtualnoexcept

connectErr() will be invoked if the connection attempt fails.

Parameters
exAn exception describing the error that occurred.

Implements folly::AsyncSocket::ConnectCallback.

Definition at line 133 of file AsyncSSLSocketTest2.cpp.

133  {
134  cerr << "AttachDetachClient::connectError: " << ex.what() << endl;
135  sslSocket_.reset();
136  }
std::shared_ptr< AsyncSSLSocket > sslSocket_
void folly::AttachDetachClient::connectSuccess ( )
inlineoverridevirtualnoexcept

connectSuccess() will be invoked when the connection has been successfully established.

Implements folly::AsyncSocket::ConnectCallback.

Definition at line 110 of file AsyncSSLSocketTest2.cpp.

References folly::EvbAndContext::attach(), folly::EvbAndContext::ctx_, EXPECT_TRUE, folly::EvbAndContext::getEventBase(), i, and folly::EventBase::runInEventBaseThread().

110  {
111  auto t2Evb = t2_.getEventBase();
112  EXPECT_TRUE(t2Evb->isInEventBaseThread());
113  cerr << "client SSL socket connected" << endl;
114  for (int i = 0; i < 1000; ++i) {
115  sslSocket_->detachSSLContext();
116  sslSocket_->attachSSLContext(t2_.ctx_);
117  }
118 
119  // detach from t2 and then read/write in t1
120  t2Evb->runInEventBaseThread([this] {
121  detach();
122  auto t1Evb = t1_.getEventBase();
123  t1Evb->runInEventBaseThread([this] {
125  sslSocket_->write(this, buf_, sizeof(buf_));
126  sslSocket_->setReadCB(this);
127  memset(readbuf_, 'b', sizeof(readbuf_));
128  bytesRead_ = 0;
129  });
130  });
131  }
std::shared_ptr< SSLContext > ctx_
bool runInEventBaseThread(void(*fn)(T *), T *arg)
Definition: EventBase.h:794
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
std::shared_ptr< AsyncSSLSocket > sslSocket_
void attach(AsyncSSLSocket &socket)
void folly::AttachDetachClient::detach ( )
inlineprivate

Definition at line 77 of file AsyncSSLSocketTest2.cpp.

77  {
78  sslSocket_->detachEventBase();
79  sslSocket_->detachSSLContext();
80  }
std::shared_ptr< AsyncSSLSocket > sslSocket_
Future<bool> folly::AttachDetachClient::getFuture ( )
inline

Definition at line 86 of file AsyncSSLSocketTest2.cpp.

References folly::Promise< T >::getFuture().

86  {
87  return promise_.getFuture();
88  }
folly::Promise< bool > promise_
Future< T > getFuture()
Definition: Promise-inl.h:97
void folly::AttachDetachClient::getReadBuffer ( void **  bufReturn,
size_t *  lenReturn 
)
inlineoverridevirtual

When data becomes available, getReadBuffer() will be invoked to get the buffer into which data should be read.

This method allows the ReadCallback to delay buffer allocation until data becomes available. This allows applications to manage large numbers of idle connections, without having to maintain a separate read buffer for each idle connection.

It is possible that in some cases, getReadBuffer() may be called multiple times before readDataAvailable() is invoked. In this case, the data will be written to the buffer returned from the most recent call to readDataAvailable(). If the previous calls to readDataAvailable() returned different buffers, the ReadCallback is responsible for ensuring that they are not leaked.

If getReadBuffer() throws an exception, returns a nullptr buffer, or returns a 0 length, the ReadCallback will be uninstalled and its readError() method will be invoked.

getReadBuffer() is not allowed to change the transport state before it returns. (For example, it should never uninstall the read callback, or set a different read callback.)

Parameters
bufReturngetReadBuffer() should update *bufReturn to contain the address of the read buffer. This parameter will never be nullptr.
lenReturngetReadBuffer() should update *lenReturn to contain the maximum number of bytes that may be written to the read buffer. This parameter will never be nullptr.

Implements folly::AsyncReader::ReadCallback.

Definition at line 148 of file AsyncSSLSocketTest2.cpp.

148  {
149  *bufReturn = readbuf_ + bytesRead_;
150  *lenReturn = sizeof(readbuf_) - bytesRead_;
151  }
void folly::AttachDetachClient::readDataAvailable ( size_t  len)
inlineoverridevirtualnoexcept

readDataAvailable() will be invoked when data has been successfully read into the buffer returned by the last call to getReadBuffer().

The read callback remains installed after readDataAvailable() returns. It must be explicitly uninstalled to stop receiving read events. getReadBuffer() will be called at least once before each call to readDataAvailable(). getReadBuffer() will also be called before any call to readEOF().

Parameters
lenThe number of bytes placed in the buffer.

Implements folly::AsyncReader::ReadCallback.

Definition at line 161 of file AsyncSSLSocketTest2.cpp.

References EXPECT_EQ, EXPECT_TRUE, folly::EvbAndContext::getEventBase(), folly::EventBase::isInEventBaseThread(), and folly::Promise< T >::setValue().

161  {
163  EXPECT_EQ(sslSocket_->getEventBase(), t1_.getEventBase());
164  cerr << "client read data: " << len << endl;
165  bytesRead_ += len;
166  if (len == sizeof(buf_)) {
167  EXPECT_EQ(memcmp(buf_, readbuf_, bytesRead_), 0);
168  sslSocket_->closeNow();
169  sslSocket_.reset();
170  promise_.setValue(true);
171  }
172  }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
bool isInEventBaseThread() const
Definition: EventBase.h:504
folly::Promise< bool > promise_
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
std::enable_if< std::is_same< Unit, B >::value, void >::type setValue()
Definition: Promise.h:326
std::shared_ptr< AsyncSSLSocket > sslSocket_
void folly::AttachDetachClient::readEOF ( )
inlineoverridevirtualnoexcept

readEOF() will be invoked when the transport is closed.

The read callback will be automatically uninstalled immediately before readEOF() is invoked.

Implements folly::AsyncReader::ReadCallback.

Definition at line 152 of file AsyncSSLSocketTest2.cpp.

152  {
153  cerr << "client readEOF" << endl;
154  }
void folly::AttachDetachClient::readErr ( const AsyncSocketException ex)
inlineoverridevirtualnoexcept

readError() will be invoked if an error occurs reading from the transport.

The read callback will be automatically uninstalled immediately before readError() is invoked.

Parameters
exAn exception describing the error that occurred.

Implements folly::AsyncReader::ReadCallback.

Definition at line 156 of file AsyncSSLSocketTest2.cpp.

References folly::Promise< T >::setException().

156  {
157  cerr << "client readError: " << ex.what() << endl;
159  }
void setException(exception_wrapper ew)
Definition: Promise-inl.h:111
folly::Promise< bool > promise_
void folly::AttachDetachClient::writeErr ( size_t  bytesWritten,
const AsyncSocketException ex 
)
inlineoverridevirtualnoexcept

writeError() will be invoked if an error occurs writing the data.

Parameters
bytesWrittenThe number of bytes that were successfull
exAn exception describing the error that occurred.

Implements folly::AsyncWriter::WriteCallback.

Definition at line 142 of file AsyncSSLSocketTest2.cpp.

144  {
145  cerr << "client writeError: " << ex.what() << endl;
146  }
void folly::AttachDetachClient::writeSuccess ( )
inlineoverridevirtualnoexcept

writeSuccess() will be invoked when all of the data has been successfully written.

Note that this mainly signals that the buffer containing the data to write is no longer needed and may be freed or re-used. It does not guarantee that the data has been fully transmitted to the remote endpoint. For example, on socket-based transports, writeSuccess() only indicates that the data has been given to the kernel for eventual transmission.

Implements folly::AsyncWriter::WriteCallback.

Definition at line 138 of file AsyncSSLSocketTest2.cpp.

138  {
139  cerr << "client write success" << endl;
140  }

Member Data Documentation

folly::SocketAddress folly::AttachDetachClient::address_
private

Definition at line 70 of file AsyncSSLSocketTest2.cpp.

char folly::AttachDetachClient::buf_[128]
private

Definition at line 71 of file AsyncSSLSocketTest2.cpp.

uint32_t folly::AttachDetachClient::bytesRead_
private

Definition at line 73 of file AsyncSSLSocketTest2.cpp.

folly::Promise<bool> folly::AttachDetachClient::promise_
private

Definition at line 75 of file AsyncSSLSocketTest2.cpp.

char folly::AttachDetachClient::readbuf_[128]
private

Definition at line 72 of file AsyncSSLSocketTest2.cpp.

std::shared_ptr<AsyncSSLSocket> folly::AttachDetachClient::sslSocket_
private

Definition at line 69 of file AsyncSSLSocketTest2.cpp.

EvbAndContext folly::AttachDetachClient::t1_
private

Definition at line 67 of file AsyncSSLSocketTest2.cpp.

EvbAndContext folly::AttachDetachClient::t2_
private

Definition at line 68 of file AsyncSSLSocketTest2.cpp.


The documentation for this class was generated from the following file: