proxygen
folly::test::TestAcceptCallback Class Reference

#include <AsyncSocketTest2.h>

Inheritance diagram for folly::test::TestAcceptCallback:
folly::AsyncServerSocket::AcceptCallback

Classes

struct  EventInfo
 

Public Types

enum  EventType { TYPE_START, TYPE_ACCEPT, TYPE_ERROR, TYPE_STOP }
 
typedef std::deque< EventInfoEventList
 

Public Member Functions

 TestAcceptCallback ()
 
std::deque< EventInfo > * getEvents ()
 
void setConnectionAcceptedFn (const std::function< void(int, const folly::SocketAddress &)> &fn)
 
void setAcceptErrorFn (const std::function< void(const std::exception &)> &fn)
 
void setAcceptStartedFn (const std::function< void()> &fn)
 
void setAcceptStoppedFn (const std::function< void()> &fn)
 
void connectionAccepted (int fd, const folly::SocketAddress &clientAddr) noexceptoverride
 
void acceptError (const std::exception &ex) noexceptoverride
 
void acceptStarted () noexceptoverride
 
void acceptStopped () noexceptoverride
 
- Public Member Functions inherited from folly::AsyncServerSocket::AcceptCallback
virtual ~AcceptCallback ()=default
 

Private Attributes

std::function< void(int, const folly::SocketAddress &)> connectionAcceptedFn_
 
std::function< void(const std::exception &)> acceptErrorFn_
 
std::function< void()> acceptStartedFn_
 
std::function< void()> acceptStoppedFn_
 
std::deque< EventInfoevents_
 

Detailed Description

Helper AcceptCallback class for the test code It records the callbacks that were invoked, and also supports calling generic std::function objects in each callback.

Definition at line 142 of file AsyncSocketTest2.h.

Member Typedef Documentation

Definition at line 158 of file AsyncSocketTest2.h.

Member Enumeration Documentation

Constructor & Destructor Documentation

folly::test::TestAcceptCallback::TestAcceptCallback ( )
inline

Definition at line 160 of file AsyncSocketTest2.h.

162  acceptErrorFn_(),
164  events_() {}
std::function< void(const std::exception &)> acceptErrorFn_
std::function< void()> acceptStoppedFn_
std::function< void(int, const folly::SocketAddress &)> connectionAcceptedFn_
std::deque< EventInfo > events_

Member Function Documentation

void folly::test::TestAcceptCallback::acceptError ( const std::exception &  ex)
inlineoverridevirtualnoexcept

acceptError() is called if an error occurs while accepting.

The AcceptCallback will remain installed even after an accept error, as the errors are typically somewhat transient, such as being out of file descriptors. The server socket must be explicitly stopped if you wish to stop accepting after an error.

Parameters
exAn exception representing the error.

Implements folly::AsyncServerSocket::AcceptCallback.

Definition at line 193 of file AsyncSocketTest2.h.

193  {
194  events_.emplace_back(ex.what());
195 
196  if (acceptErrorFn_) {
197  acceptErrorFn_(ex);
198  }
199  }
std::function< void(const std::exception &)> acceptErrorFn_
std::deque< EventInfo > events_
void folly::test::TestAcceptCallback::acceptStarted ( )
inlineoverridevirtualnoexcept

acceptStarted() will be called in the callback's EventBase thread after this callback has been added to the AsyncServerSocket.

acceptStarted() will be called before any calls to connectionAccepted() or acceptError() are made on this callback.

acceptStarted() makes it easier for callbacks to perform initialization inside the callback thread. (The call to addAcceptCallback() must always be made from the AsyncServerSocket's primary EventBase thread. acceptStarted() provides a hook that will always be invoked in the callback's thread.)

Note that the call to acceptStarted() is made once the callback is added, regardless of whether or not the AsyncServerSocket is actually accepting at the moment. acceptStarted() will be called even if the AsyncServerSocket is paused when the callback is added (including if the initial call to startAccepting() on the AsyncServerSocket has not been made yet).

Reimplemented from folly::AsyncServerSocket::AcceptCallback.

Definition at line 200 of file AsyncSocketTest2.h.

200  {
201  events_.emplace_back(TYPE_START);
202 
203  if (acceptStartedFn_) {
205  }
206  }
std::deque< EventInfo > events_
std::function< void()> acceptStartedFn_
void folly::test::TestAcceptCallback::acceptStopped ( )
inlineoverridevirtualnoexcept

acceptStopped() will be called when this AcceptCallback is removed from the AsyncServerSocket, or when the AsyncServerSocket is destroyed, whichever occurs first.

No more calls to connectionAccepted() or acceptError() will be made after acceptStopped() is invoked.

Reimplemented from folly::AsyncServerSocket::AcceptCallback.

Definition at line 207 of file AsyncSocketTest2.h.

207  {
208  events_.emplace_back(TYPE_STOP);
209 
210  if (acceptStoppedFn_) {
212  }
213  }
std::function< void()> acceptStoppedFn_
std::deque< EventInfo > events_
void folly::test::TestAcceptCallback::connectionAccepted ( int  fd,
const folly::SocketAddress clientAddr 
)
inlineoverridevirtualnoexcept

connectionAccepted() is called whenever a new client connection is received.

The AcceptCallback will remain installed after connectionAccepted() returns.

Parameters
fdThe newly accepted client socket. The AcceptCallback assumes ownership of this socket, and is responsible for closing it when done. The newly accepted file descriptor will have already been put into non-blocking mode.
clientAddrA reference to a SocketAddress struct containing the client's address. This struct is only guaranteed to remain valid until connectionAccepted() returns.

Implements folly::AsyncServerSocket::AcceptCallback.

Definition at line 184 of file AsyncSocketTest2.h.

186  {
187  events_.emplace_back(fd, clientAddr);
188 
189  if (connectionAcceptedFn_) {
190  connectionAcceptedFn_(fd, clientAddr);
191  }
192  }
std::function< void(int, const folly::SocketAddress &)> connectionAcceptedFn_
std::deque< EventInfo > events_
std::deque<EventInfo>* folly::test::TestAcceptCallback::getEvents ( )
inline

Definition at line 166 of file AsyncSocketTest2.h.

Referenced by serverSocketSanityTest(), and TEST().

166  {
167  return &events_;
168  }
std::deque< EventInfo > events_
void folly::test::TestAcceptCallback::setAcceptErrorFn ( const std::function< void(const std::exception &)> &  fn)
inline

Definition at line 174 of file AsyncSocketTest2.h.

Referenced by serverSocketSanityTest(), and TEST().

174  {
175  acceptErrorFn_ = fn;
176  }
std::function< void(const std::exception &)> acceptErrorFn_
void folly::test::TestAcceptCallback::setAcceptStartedFn ( const std::function< void()> &  fn)
inline

Definition at line 177 of file AsyncSocketTest2.h.

Referenced by TEST().

177  {
178  acceptStartedFn_ = fn;
179  }
std::function< void()> acceptStartedFn_
void folly::test::TestAcceptCallback::setAcceptStoppedFn ( const std::function< void()> &  fn)
inline

Definition at line 180 of file AsyncSocketTest2.h.

Referenced by TEST().

180  {
181  acceptStoppedFn_ = fn;
182  }
std::function< void()> acceptStoppedFn_
void folly::test::TestAcceptCallback::setConnectionAcceptedFn ( const std::function< void(int, const folly::SocketAddress &)> &  fn)
inline

Definition at line 170 of file AsyncSocketTest2.h.

Referenced by serverSocketSanityTest(), and TEST().

171  {
173  }
std::function< void(int, const folly::SocketAddress &)> connectionAcceptedFn_

Member Data Documentation

std::function<void(const std::exception&)> folly::test::TestAcceptCallback::acceptErrorFn_
private

Definition at line 217 of file AsyncSocketTest2.h.

std::function<void()> folly::test::TestAcceptCallback::acceptStartedFn_
private

Definition at line 218 of file AsyncSocketTest2.h.

std::function<void()> folly::test::TestAcceptCallback::acceptStoppedFn_
private

Definition at line 219 of file AsyncSocketTest2.h.

std::function<void(int, const folly::SocketAddress&)> folly::test::TestAcceptCallback::connectionAcceptedFn_
private

Definition at line 216 of file AsyncSocketTest2.h.

std::deque<EventInfo> folly::test::TestAcceptCallback::events_
private

Definition at line 221 of file AsyncSocketTest2.h.


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