libassa
3.5.1
|
#include <Reactor.h>
Public Member Functions | |
Reactor () | |
Constructor. More... | |
~Reactor () | |
Destructor. More... | |
TimerId | registerTimerHandler (EventHandler *eh_, const TimeVal &tv_, const std::string &name_="<unknown>") |
Register Timer Event handler with Reactor. More... | |
bool | registerIOHandler (EventHandler *eh_, handler_t fd_, EventType et_=RWE_EVENTS) |
Register I/O Event handler with Reactor. More... | |
bool | removeHandler (EventHandler *eh_, EventType et_=ALL_EVENTS) |
Remove Event handler from reactor for either all I/O events or timeout event or both. More... | |
bool | removeTimerHandler (TimerId id_) |
Remove Timer event from the queue. More... | |
bool | removeIOHandler (handler_t fd_) |
Remove IO Event handler from reactor. More... | |
void | waitForEvents (void) |
Main waiting loop that blocks indefinitely processing events. More... | |
void | waitForEvents (TimeVal *tv_) |
Wait for events for time specified. More... | |
void | stopReactor (void) |
Stop Reactor's activity. More... | |
void | deactivate (void) |
Deactivate Reactor. More... | |
Private Types | |
typedef std::map< u_int, EventHandler * > | Fd2Eh_Map_Type |
no cloning More... | |
typedef Fd2Eh_Map_Type::iterator | Fd2Eh_Map_Iter |
Private Member Functions | |
Reactor (const Reactor &) | |
Reactor & | operator= (const Reactor &) |
no cloning More... | |
void | adjust_maxfdp1 (handler_t fd_) |
Adjust maxfdp1 in a portable way (win32 ignores maxfd alltogether). More... | |
bool | handleError (void) |
Handle error in select(2) loop appropriately. More... | |
bool | dispatch (int minimum_) |
Notify all EventHandlers registered on respecful events occured. More... | |
int | isAnyReady (void) |
Return number of file descriptors ready accross all sets. More... | |
bool | checkFDs (void) |
Check mask for bad file descriptors. More... | |
void | dispatchHandler (FdSet &mask_, Fd2Eh_Map_Type &fdSet_, EH_IO_Callback callback_) |
Call handler's callback and, if callback returns negative value, remove it from the Reactor. More... | |
void | calculateTimeout (TimeVal *&howlong_, TimeVal *maxwait_) |
Calculate closest timeout. More... | |
Private Attributes | |
int | m_fd_setsize |
Max number of open files per process. More... | |
handler_t | m_maxfd_plus1 |
Max file descriptor number (in all sets) plus 1. More... | |
bool | m_active |
Flag that indicates whether Reactor is active or had been stopped. More... | |
Fd2Eh_Map_Type | m_readSet |
Event handlers awaiting on READ_EVENT. More... | |
Fd2Eh_Map_Type | m_writeSet |
Event handlers awaiting on WRITE_EVENT. More... | |
Fd2Eh_Map_Type | m_exceptSet |
Event handlers awaiting on EXCEPT_EVENT. More... | |
MaskSet | m_waitSet |
Handlers to wait for event on. More... | |
MaskSet | m_readySet |
Handlers that are ready for processing. More... | |
TimerQueue | m_tqueue |
The queue of Timers. More... | |
|
private |
|
private |
Reactor::Reactor | ( | ) |
Constructor.
Maximum number of sockets supported (per process) Win32 defines it to 64 in winsock2.h.
Initialize winsock2 library
Definition at line 23 of file Reactor.cpp.
References m_fd_setsize, ASSA::REACTTRACE, and trace_with_mask.
Reactor::~Reactor | ( | ) |
Destructor.
Definition at line 54 of file Reactor.cpp.
References deactivate(), m_exceptSet, m_readSet, m_writeSet, ASSA::REACTTRACE, and trace_with_mask.
|
private |
|
private |
Adjust maxfdp1 in a portable way (win32 ignores maxfd alltogether).
If the socket descriptor that has just been eliminated was the maxfd+1, we readjust to the next highest.
Win32 implementation of select() ignores this value altogether.
Definition at line 700 of file Reactor.cpp.
References DL, m_maxfd_plus1, m_waitSet, ASSA::MaskSet::max_fd(), ASSA::REACT, ASSA::REACTTRACE, and trace_with_mask.
Referenced by removeHandler(), and removeIOHandler().
Calculate closest timeout.
If TimerQueue is not empty, then return smallest of maxtimeout and first in the queue. Otherwise, return maxtimeout.
maxwait_ | (in) how long we are expected to wait for event(s). |
howlong_ | (out) how long we are going to wait. |
Definition at line 420 of file Reactor.cpp.
References DL, ASSA::TimerQueue::dump(), ASSA::TimeVal::gettimeofday(), ASSA::TimerQueue::isEmpty(), m_tqueue, ASSA::REACT, ASSA::REACTTRACE, ASSA::TimerQueue::top(), trace_with_mask, and ASSA::TimeVal::zeroTime().
Referenced by waitForEvents().
|
private |
Check mask for bad file descriptors.
Definition at line 316 of file Reactor.cpp.
References ASSA::FdSet::clear(), DL, m_fd_setsize, m_readSet, ASSA::REACT, ASSA::REACTTRACE, removeIOHandler(), ASSA::FdSet::setFd(), and trace_with_mask.
Referenced by handleError().
|
inline |
Deactivate Reactor.
This function sets internal flag which notifies Reactor's internal event handling loop to abort its activity. It is mostly used when a slow system call is interrupted by the signal handler. The system call will be restarted by OS after control returns from the signal handler. Signal handler (GenServer::handle_signal()) should call this method to delay Reactor's deactivation.
Definition at line 234 of file Reactor.h.
References m_active.
Referenced by ASSA::GenServer::handle_signal(), ASSA::GenServer::stop_service(), and ~Reactor().
|
private |
Notify all EventHandlers registered on respecful events occured.
Many UNIX systems will count a particular file descriptor in the ready_ only ONCE, even if it was flagged by select(2) in, say, both read and write masks.
minimum_ | number of file descriptors ready. |
Definition at line 625 of file Reactor.cpp.
References ASSA::ASSAERR, dispatchHandler(), DL, ASSA::MaskSet::dump(), EL, ASSA::TimerQueue::expire(), ASSA::TimeVal::gettimeofday(), ASSA::EventHandler::handle_except(), ASSA::EventHandler::handle_read(), ASSA::EventHandler::handle_write(), ASSA::MaskSet::m_eset, m_exceptSet, m_readSet, m_readySet, ASSA::MaskSet::m_rset, m_tqueue, m_writeSet, ASSA::MaskSet::m_wset, ASSA::REACT, ASSA::REACTTRACE, and trace_with_mask.
Referenced by waitForEvents().
|
private |
Call handler's callback and, if callback returns negative value, remove it from the Reactor.
This spot needs re-thinking.
When you have several high data-rate connections sending data at the same time, the one that had connected first would get lower FD number and would get data transfer preference over everybody else who has connected later on.
WIN32 HACK: Without having restarted scan from the beginning, this causes crash due to the fact that firing a callback of EventHandler might have invalidated the iterator (happens with Connector's in a sync mode).
Definition at line 567 of file Reactor.cpp.
References ASSA::FdSet::clear(), DL, ASSA::EventHandler::get_id(), ASSA::FdSet::isSet(), ASSA::REACT, ASSA::REACTTRACE, removeIOHandler(), and trace_with_mask.
Referenced by dispatch().
|
private |
Handle error in select(2) loop appropriately.
If commanded to stop, do so
Definition at line 340 of file Reactor.cpp.
References ASSA::ASSAERR, checkFDs(), DL, EL, m_active, ASSA::REACT, ASSA::REACTTRACE, and trace_with_mask.
Referenced by waitForEvents().
|
private |
Return number of file descriptors ready accross all sets.
Definition at line 403 of file Reactor.cpp.
References DL, ASSA::MaskSet::dump(), ASSA::MaskSet::m_eset, m_readySet, ASSA::MaskSet::m_rset, ASSA::MaskSet::m_wset, ASSA::FdSet::numSet(), ASSA::REACT, ASSA::REACTTRACE, and trace_with_mask.
Referenced by waitForEvents().
bool Reactor::registerIOHandler | ( | EventHandler * | eh_, |
handler_t | fd_, | ||
EventType | et_ = RWE_EVENTS |
||
) |
Register I/O Event handler with Reactor.
Reactor will dispatch appropriate callback when event of EventType is received.
eh_ | Pointer to the EventHandler |
fd_ | File descriptor |
et_ | Event Type |
Definition at line 92 of file Reactor.cpp.
References ASSA::ASSAERR, Assure_return, DL, ASSA::MaskSet::dump(), ASSA::ends(), ASSA::EventHandler::get_id(), ASSA::isExceptEvent(), ASSA::isReadEvent(), ASSA::isSignalEvent(), ASSA::isTimeoutEvent(), ASSA::isWriteEvent(), ASSA::MaskSet::m_eset, m_exceptSet, m_maxfd_plus1, m_readSet, ASSA::MaskSet::m_rset, m_waitSet, m_writeSet, ASSA::MaskSet::m_wset, ASSA::REACT, ASSA::REACTTRACE, ASSA::FdSet::setFd(), and trace_with_mask.
Referenced by ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::doSync(), and ASSA::RemoteLogger::log_open().
TimerId Reactor::registerTimerHandler | ( | EventHandler * | eh_, |
const TimeVal & | tv_, | ||
const std::string & | name_ = "<unknown>" |
||
) |
Register Timer Event handler with Reactor.
Reactor will dispatch appropriate callback when event of EventType is received.
eh_ | Pointer to the EventHandler |
tv_ | Timeout value |
name_ | Name of the timer |
Definition at line 66 of file Reactor.cpp.
References Assure_return, DL, ASSA::TimerQueue::dump(), ASSA::TimeVal::fmtString(), ASSA::TimeVal::gettimeofday(), ASSA::TimerQueue::insert(), m_tqueue, ASSA::TimeVal::msec(), ASSA::REACT, ASSA::REACTTRACE, ASSA::TimeVal::sec(), and trace_with_mask.
bool Reactor::removeHandler | ( | EventHandler * | eh_, |
EventType | et_ = ALL_EVENTS |
||
) |
Remove Event handler from reactor for either all I/O events or timeout event or both.
Remove handler from all events that matches event_.
If et_ is TIMEOUT_EVENT, all timers associated with Event Handler eh_ will be removed.
eh_ | Pointer to the EventHandler |
et_ | Event Type to remove. Default will remove Event Handler for all events. |
Definition at line 172 of file Reactor.cpp.
References adjust_maxfdp1(), ASSA::FdSet::clear(), DL, ASSA::MaskSet::dump(), ASSA::EventHandler::get_id(), ASSA::EventHandler::handle_close(), ASSA::isExceptEvent(), ASSA::isReadEvent(), ASSA::isTimeoutEvent(), ASSA::isWriteEvent(), ASSA::MaskSet::m_eset, m_exceptSet, m_readSet, ASSA::MaskSet::m_rset, m_tqueue, m_waitSet, m_writeSet, ASSA::MaskSet::m_wset, ASSA::REACT, ASSA::REACTTRACE, ASSA::TimerQueue::remove(), and trace_with_mask.
Referenced by ASSA::RemoteLogger::log_close(), and stopReactor().
bool Reactor::removeIOHandler | ( | handler_t | fd_ | ) |
Remove IO Event handler from reactor.
This will remove handler from receiving all I/O events.
fd_ | File descriptor |
We clear m_readySet mask here as well, because if we don't, it will be erroneously used by isAnyReady() before select().
Definition at line 246 of file Reactor.cpp.
References adjust_maxfdp1(), Assure_return, ASSA::FdSet::clear(), DL, ASSA::MaskSet::dump(), ASSA::EventHandler::get_id(), ASSA::EventHandler::handle_close(), ASSA::is_valid_handler(), ASSA::MaskSet::m_eset, m_exceptSet, m_readSet, m_readySet, ASSA::MaskSet::m_rset, m_waitSet, m_writeSet, ASSA::MaskSet::m_wset, ASSA::REACT, ASSA::REACTTRACE, and trace_with_mask.
Referenced by checkFDs(), and dispatchHandler().
bool Reactor::removeTimerHandler | ( | TimerId | id_ | ) |
Remove Timer event from the queue.
This removes particular event.
id_ | Timer Id returned by registerTimer. |
Definition at line 151 of file Reactor.cpp.
References ASSA::ASSAERR, DL, ASSA::TimerQueue::dump(), EL, m_tqueue, ASSA::REACT, ASSA::REACTTRACE, ASSA::TimerQueue::remove(), and trace_with_mask.
void Reactor::stopReactor | ( | void | ) |
Stop Reactor's activity.
This effectively removes all handlers from under Reactor's supervision. As of now, there is no way to re-activate the Reactor. This method is typically called from method other then EventHandler::signal_handler(). EventHandler::handle_read () is a good candidate. Calling it from EventHandler::handle_close () will most likely cause an infinite loop of recursive calls.
Definition at line 666 of file Reactor.cpp.
References m_active, m_exceptSet, m_readSet, m_writeSet, ASSA::REACTTRACE, removeHandler(), and trace_with_mask.
void Reactor::waitForEvents | ( | TimeVal * | tv_ | ) |
Wait for events for time specified.
Passing NULL replicates behavior of waitForEvents(void). Passing tv_ {0, 0} will cause non-blocking polling for all events. This method blocks up to tv_ time interval processing event. If an event occurs, it will process event(s) and return. tv_ time is adjusted by substracting time spent in event processing.
tv_ | [RW] is time to wait for. |
| select() | errno | Events | Behavior | |===================================================================| | < 0 | EINTR | Interrup by signal | Retry | +-------—+----—+------------------—+-----------------------—+ | < 0 | EBADF | Bad file descriptor | Remove bad fds and retry | | | | | and retry | +-------—+----—+------------------—+-----------------------—+ | < 0 | others| Some other error | Fall through | +-------—+----—+------------------—+-----------------------—+ | == 0 | 0 | Timed out | Fall through | +-------—+----—+------------------—+-----------------------—+ | > 0 | 0 | Got some work to do | Fall through | +----------------------------------------------------------------—+
Definition at line 494 of file Reactor.cpp.
References calculateTimeout(), dispatch(), DL, ASSA::MaskSet::dump(), ASSA::TimerQueue::expire(), ASSA::TimeVal::gettimeofday(), handleError(), isAnyReady(), m_active, ASSA::MaskSet::m_eset, m_maxfd_plus1, m_readySet, ASSA::MaskSet::m_rset, m_tqueue, m_waitSet, ASSA::MaskSet::m_wset, ASSA::REACT, ASSA::REACTTRACE, ASSA::MaskSet::reset(), ASSA::MaskSet::sync(), and trace_with_mask.
void Reactor::waitForEvents | ( | void | ) |
Main waiting loop that blocks indefinitely processing events.
Block forever version.
Definition at line 469 of file Reactor.cpp.
References m_active.
|
private |
Flag that indicates whether Reactor is active or had been stopped.
Definition at line 209 of file Reactor.h.
Referenced by deactivate(), handleError(), stopReactor(), and waitForEvents().
|
private |
Event handlers awaiting on EXCEPT_EVENT.
Definition at line 218 of file Reactor.h.
Referenced by dispatch(), registerIOHandler(), removeHandler(), removeIOHandler(), stopReactor(), and ~Reactor().
|
private |
Max number of open files per process.
This is the soft limit enforced by the kernel. It can be obtained/manipulated from the shell with ulimit/limit utilities, but may not exceed the hard limit.
Definition at line 200 of file Reactor.h.
Referenced by checkFDs(), and Reactor().
|
private |
Max file descriptor number (in all sets) plus 1.
This value is ignored by WIN32 implementation of select()
Definition at line 206 of file Reactor.h.
Referenced by adjust_maxfdp1(), registerIOHandler(), and waitForEvents().
|
private |
Event handlers awaiting on READ_EVENT.
Definition at line 212 of file Reactor.h.
Referenced by checkFDs(), dispatch(), registerIOHandler(), removeHandler(), removeIOHandler(), stopReactor(), and ~Reactor().
|
private |
Handlers that are ready for processing.
Definition at line 224 of file Reactor.h.
Referenced by dispatch(), isAnyReady(), removeIOHandler(), and waitForEvents().
|
private |
The queue of Timers.
Definition at line 227 of file Reactor.h.
Referenced by calculateTimeout(), dispatch(), registerTimerHandler(), removeHandler(), removeTimerHandler(), and waitForEvents().
|
private |
Handlers to wait for event on.
Definition at line 221 of file Reactor.h.
Referenced by adjust_maxfdp1(), registerIOHandler(), removeHandler(), removeIOHandler(), and waitForEvents().
|
private |
Event handlers awaiting on WRITE_EVENT.
Definition at line 215 of file Reactor.h.
Referenced by dispatch(), registerIOHandler(), removeHandler(), removeIOHandler(), stopReactor(), and ~Reactor().