proxygen
|
#include <EventHandler.h>
Public Types | |
enum | EventFlags { NONE = 0, READ = EV_READ, WRITE = EV_WRITE, READ_WRITE = (READ | WRITE), PERSIST = EV_PERSIST } |
Public Member Functions | |
EventHandler (EventBase *eventBase, int fd) | |
EventHandler (EventBase *eventBase=nullptr, NetworkSocket fd=NetworkSocket()) | |
virtual | ~EventHandler () |
virtual void | handlerReady (uint16_t events) noexcept=0 |
bool | registerHandler (uint16_t events) |
void | unregisterHandler () |
bool | isHandlerRegistered () const |
void | attachEventBase (EventBase *eventBase) |
void | detachEventBase () |
void | changeHandlerFD (int fd) |
void | changeHandlerFD (NetworkSocket fd) |
void | initHandler (EventBase *eventBase, int fd) |
void | initHandler (EventBase *eventBase, NetworkSocket fd) |
uint16_t | getRegisteredEvents () const |
bool | registerInternalHandler (uint16_t events) |
bool | isPending () const |
Private Member Functions | |
bool | registerImpl (uint16_t events, bool internal) |
void | ensureNotRegistered (const char *fn) |
void | setEventBase (EventBase *eventBase) |
Static Private Member Functions | |
static void | libeventCallback (libevent_fd_t fd, short events, void *arg) |
Private Attributes | |
struct event | event_ |
EventBase * | eventBase_ |
The EventHandler class is used to asynchronously wait for events on a file descriptor.
Users that wish to wait on I/O events should derive from EventHandler and implement the handlerReady() method.
Definition at line 39 of file EventHandler.h.
Enumerator | |
---|---|
NONE | |
READ | |
WRITE | |
READ_WRITE | |
PERSIST |
Definition at line 41 of file EventHandler.h.
|
inlineexplicit |
Create a new EventHandler object.
eventBase | The EventBase to use to drive this event handler. This may be nullptr, in which case the EventBase must be set separately using initHandler() or attachEventBase() before the handler can be registered. |
fd | The file descriptor that this EventHandler will monitor. This may be -1, in which case the file descriptor must be set separately using initHandler() or changeHandlerFD() before the handler can be registered. |
Definition at line 65 of file EventHandler.h.
References handlerReady(), folly::pushmi::__adl::noexcept(), uint16_t, and ~EventHandler().
|
explicit |
Definition at line 25 of file EventHandler.cpp.
References event_, eventBase_, folly::folly_event_set(), libeventCallback(), setEventBase(), and folly::NetworkSocket::toFd().
|
virtual |
EventHandler destructor.
The event will be automatically unregistered if it is still registered.
Definition at line 38 of file EventHandler.cpp.
References unregisterHandler().
Referenced by EventHandler().
void folly::EventHandler::attachEventBase | ( | EventBase * | eventBase | ) |
Attach the handler to a EventBase.
This may only be called if the handler is not currently attached to a EventBase (either by using the default constructor, or by calling detachEventBase()).
This method must be invoked in the EventBase's thread.
Definition at line 109 of file EventHandler.cpp.
References folly::EventBase::dcheckIsInEventBaseThread(), event_, isHandlerRegistered(), and setEventBase().
Referenced by folly::AsyncUDPSocket::attachEventBase(), folly::AsyncSocket::attachEventBase(), and isHandlerRegistered().
|
inline |
Change the file descriptor that this handler is associated with.
This may only be called when the handler is not currently registered.
Definition at line 143 of file EventHandler.h.
References folly::NetworkSocket::fromFd().
Referenced by folly::AsyncUDPSocket::bind(), folly::AsyncPipeReader::close(), folly::AsyncPipeWriter::closeNow(), folly::AsyncSocket::closeNow(), folly::AsyncSocket::connect(), folly::AsyncSocket::detachFd(), folly::AsyncSocket::handleWrite(), folly::AsyncUDPSocket::setFD(), and folly::AsyncSocket::startFail().
void folly::EventHandler::changeHandlerFD | ( | NetworkSocket | fd | ) |
Definition at line 124 of file EventHandler.cpp.
References ensureNotRegistered(), event_, folly::folly_event_set(), libeventCallback(), and folly::NetworkSocket::toFd().
void folly::EventHandler::detachEventBase | ( | ) |
Detach the handler from its EventBase.
This may only be called when the handler is not currently registered. Once detached, the handler may not be registered again until it is re-attached to a EventBase by calling attachEventBase().
This method must be called from the current EventBase's thread.
Definition at line 119 of file EventHandler.cpp.
References ensureNotRegistered(), and event_.
Referenced by folly::AsyncUDPSocket::detachEventBase(), folly::AsyncSocket::detachEventBase(), and isHandlerRegistered().
|
private |
Definition at line 138 of file EventHandler.cpp.
References isHandlerRegistered().
Referenced by changeHandlerFD(), detachEventBase(), initHandler(), and registerInternalHandler().
|
inline |
Return the set of events that we're currently registered for.
Definition at line 165 of file EventHandler.h.
References event_, isHandlerRegistered(), and uint16_t.
|
pure virtualnoexcept |
handlerReady() is invoked when the handler is ready.
events | A bitset indicating the events that are ready. |
Implemented in PipeHandler, TerminateTestCallback, folly::AsyncSocket::IoHandler, folly::AsyncServerSocket::ServerEventHandler, PartialWriteHandler, PartialReadHandler, folly::AsyncUDPSocket, folly::NotificationQueue< MessageT >::Consumer, folly::AsyncPipeWriter, TestHandler, folly::AsyncPipeReader, fizz::tool::TerminalInputHandler, and EventHandlerMock.
Referenced by EventHandler(), and libeventCallback().
|
inline |
Attach the handler to a EventBase, and change the file descriptor.
This method may only be called if the handler is not currently attached to a EventBase. This is primarily intended to be used to initialize EventHandler objects created using the default constructor.
Definition at line 156 of file EventHandler.h.
References folly::NetworkSocket::fromFd().
void folly::EventHandler::initHandler | ( | EventBase * | eventBase, |
NetworkSocket | fd | ||
) |
Definition at line 132 of file EventHandler.cpp.
References ensureNotRegistered(), event_, folly::folly_event_set(), libeventCallback(), setEventBase(), and folly::NetworkSocket::toFd().
|
inline |
Returns true if the handler is currently registered.
Definition at line 112 of file EventHandler.h.
References attachEventBase(), detachEventBase(), event_, and folly::EventUtil::isEventRegistered().
Referenced by attachEventBase(), folly::AsyncPipeWriter::closeOnEmpty(), ensureNotRegistered(), getRegisteredEvents(), registerImpl(), folly::AsyncPipeReader::setReadCB(), unregisterHandler(), and folly::AsyncPipeWriter::write().
bool folly::EventHandler::isPending | ( | ) | const |
Definition at line 173 of file EventHandler.cpp.
References event_, and folly::event_ref_flags().
Referenced by folly::AsyncSocket::isPending(), and registerInternalHandler().
|
staticprivate |
Definition at line 148 of file EventHandler.cpp.
References folly::EventBase::bumpHandlingTime(), event_, eventBase_, folly::EventBase::getExecutionObserver(), handler(), handlerReady(), folly::ExecutionObserver::starting(), and uint16_t.
Referenced by changeHandlerFD(), EventHandler(), initHandler(), registerImpl(), and registerInternalHandler().
|
inline |
Register the handler.
If the handler is already registered, the registration will be updated to wait on the new set of events.
events | A bitset specifying the events to monitor. If the PERSIST bit is set, the handler will remain registered even after handlerReady() is called. |
Definition at line 100 of file EventHandler.h.
References registerImpl(), and unregisterHandler().
Referenced by folly::AsyncUDPSocket::detachEventBase(), folly::AsyncPipeWriter::handleWrite(), folly::AsyncSocket::registerForConnectEvents(), folly::AsyncPipeReader::setReadCB(), fizz::tool::TerminalInputHandler::TerminalInputHandler(), TEST(), TEST_F(), folly::AsyncSocket::updateEventRegistration(), and folly::AsyncUDPSocket::updateRegistration().
|
private |
Definition at line 42 of file EventHandler.cpp.
References folly::errnoStr(), event_, folly::event_ref_flags(), isHandlerRegistered(), and libeventCallback().
Referenced by registerHandler(), and registerInternalHandler().
|
inline |
Register the handler as an internal event.
This event will not count as an active event for determining if the EventBase loop has more events to process. The EventBase loop runs only as long as there are active EventHandlers, however "internal" event handlers are not counted. Therefore this event handler will not prevent EventBase loop from exiting with no more work to do if there are no other non-internal event handlers registered.
This is intended to be used only in very rare cases by the internal EventBase code. This API is not guaranteed to remain stable or portable in the future.
Definition at line 183 of file EventHandler.h.
References ensureNotRegistered(), isPending(), libeventCallback(), registerImpl(), setEventBase(), and uint16_t.
|
private |
Definition at line 168 of file EventHandler.cpp.
References event_, eventBase_, and folly::EventBase::getLibeventBase().
Referenced by attachEventBase(), EventHandler(), initHandler(), and registerInternalHandler().
void folly::EventHandler::unregisterHandler | ( | ) |
Unregister the handler, if it is registered.
Definition at line 103 of file EventHandler.cpp.
References event_, and isHandlerRegistered().
Referenced by folly::AsyncPipeReader::close(), folly::AsyncUDPSocket::close(), folly::AsyncPipeWriter::closeNow(), folly::AsyncSocket::detachEventBase(), folly::AsyncPipeReader::handlerReady(), folly::AsyncPipeWriter::handleWrite(), registerHandler(), folly::AsyncPipeReader::setReadCB(), folly::AsyncSocket::startFail(), TEST(), TEST_F(), folly::AsyncSocket::updateEventRegistration(), and ~EventHandler().
|
private |
Definition at line 197 of file EventHandler.h.
Referenced by attachEventBase(), changeHandlerFD(), detachEventBase(), EventHandler(), getRegisteredEvents(), initHandler(), isHandlerRegistered(), isPending(), libeventCallback(), registerImpl(), setEventBase(), and unregisterHandler().
|
private |
Definition at line 198 of file EventHandler.h.
Referenced by EventHandler(), libeventCallback(), and setEventBase().