Loading...
Searching...
No Matches
Reactor.h
Go to the documentation of this file.
// -*- c++ -*-
//------------------------------------------------------------------------------
// Reactor.h
//------------------------------------------------------------------------------
// Copyright (C) 1997-2002,2005 Vladislav Grinchenko
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//------------------------------------------------------------------------------
// Created: 05/25/1999
//------------------------------------------------------------------------------
#ifndef REACTOR_H
#define REACTOR_H
#include <sys/time.h> // select(2)
#include <map>
#if !defined(WIN32)
# include <sys/resource.h> // getrlimit(2)
#endif
#include "assa/EventHandler.h"
#include "assa/Singleton.h"
#include "assa/MaskSet.h"
#include "assa/TimerQueue.h"
#include "assa/TimerCountdown.h"
namespace ASSA {
class Reactor
{
public:
Reactor ();
~Reactor();
TimerId registerTimerHandler (EventHandler* eh_,
const TimeVal& tv_,
const std::string& name_ = "<unknown>");
bool registerIOHandler (EventHandler* eh_,
handler_t fd_,
EventType et_ = RWE_EVENTS);
bool removeHandler (EventHandler* eh_, EventType et_ = ALL_EVENTS);
bool removeTimerHandler (TimerId id_);
bool removeIOHandler (handler_t fd_);
void waitForEvents (void);
void waitForEvents (TimeVal* tv_);
void stopReactor (void);
void deactivate (void);
private:
Reactor (const Reactor&);
Reactor& operator= (const Reactor&);
private:
typedef std::map<u_int, EventHandler*> Fd2Eh_Map_Type;
typedef Fd2Eh_Map_Type::iterator Fd2Eh_Map_Iter;
private:
void adjust_maxfdp1 (handler_t fd_);
bool handleError (void);
bool dispatch (int minimum_);
int isAnyReady (void);
bool checkFDs (void);
void dispatchHandler ( FdSet& mask_,
Fd2Eh_Map_Type& fdSet_,
EH_IO_Callback callback_);
void calculateTimeout (TimeVal*& howlong_, TimeVal* maxwait_);
private:
int m_fd_setsize;
handler_t m_maxfd_plus1;
bool m_active;
Fd2Eh_Map_Type m_readSet;
Fd2Eh_Map_Type m_writeSet;
Fd2Eh_Map_Type m_exceptSet;
MaskSet m_waitSet;
MaskSet m_readySet;
TimerQueue m_tqueue;
};
//-----------------------------------------------------------------------------
// Inline functions
//-----------------------------------------------------------------------------
inline void Reactor::deactivate (void) { m_active = false; }
} // end namespace ASSA
#endif /* REACTOR_H */
EventHandler.h
An abstract interface for handling I/O events, timers, and such.
handler_t
int handler_t
Definition Logger_Impl.h:82
MaskSet.h
Bundles file descriptor mask sets to be used with select().
Singleton.h
Singleton template class allows to turn any new or existing class T into Singleton Pattern.
TimerCountdown.h
TimerCountdown class keep the track of the elapsed time.
TimerQueue.h
TimerQueue class represents the queue of Timers that application can install for Reactor to dispatch.
ASSA::EventHandler
EventHandler class.
Definition EventHandler.h:103
ASSA::FdSet
Class FdSet.
Definition FdSet.h:52
ASSA::MaskSet
Definition MaskSet.h:25
ASSA::Reactor::calculateTimeout
void calculateTimeout(TimeVal *&howlong_, TimeVal *maxwait_)
Calculate closest timeout.
Definition Reactor.cpp:421
ASSA::Reactor::registerIOHandler
bool registerIOHandler(EventHandler *eh_, handler_t fd_, EventType et_=RWE_EVENTS)
Register I/O Event handler with Reactor.
Definition Reactor.cpp:93
ASSA::Reactor::Reactor
Reactor(const Reactor &)
ASSA::Reactor::m_tqueue
TimerQueue m_tqueue
The queue of Timers.
Definition Reactor.h:227
ASSA::Reactor::m_waitSet
MaskSet m_waitSet
Handlers to wait for event on.
Definition Reactor.h:221
ASSA::Reactor::m_writeSet
Fd2Eh_Map_Type m_writeSet
Event handlers awaiting on WRITE_EVENT.
Definition Reactor.h:215
ASSA::Reactor::Reactor
Reactor()
Constructor.
Definition Reactor.cpp:24
ASSA::Reactor::Fd2Eh_Map_Iter
Fd2Eh_Map_Type::iterator Fd2Eh_Map_Iter
Definition Reactor.h:155
ASSA::Reactor::m_readySet
MaskSet m_readySet
Handlers that are ready for processing.
Definition Reactor.h:224
ASSA::Reactor::waitForEvents
void waitForEvents(void)
Main waiting loop that blocks indefinitely processing events.
Definition Reactor.cpp:470
ASSA::Reactor::m_maxfd_plus1
handler_t m_maxfd_plus1
Max file descriptor number (in all sets) plus 1.
Definition Reactor.h:206
ASSA::Reactor::dispatchHandler
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.
Definition Reactor.cpp:568
ASSA::Reactor::m_fd_setsize
int m_fd_setsize
Max number of open files per process.
Definition Reactor.h:200
ASSA::Reactor::handleError
bool handleError(void)
Handle error in select(2) loop appropriately.
Definition Reactor.cpp:341
ASSA::Reactor::Fd2Eh_Map_Type
std::map< u_int, EventHandler * > Fd2Eh_Map_Type
no cloning
Definition Reactor.h:154
ASSA::Reactor::deactivate
void deactivate(void)
Deactivate Reactor.
Definition Reactor.h:234
ASSA::Reactor::dispatch
bool dispatch(int minimum_)
Notify all EventHandlers registered on respecful events occured.
Definition Reactor.cpp:626
ASSA::Reactor::operator=
Reactor & operator=(const Reactor &)
no cloning
ASSA::Reactor::m_readSet
Fd2Eh_Map_Type m_readSet
Event handlers awaiting on READ_EVENT.
Definition Reactor.h:212
ASSA::Reactor::m_active
bool m_active
Flag that indicates whether Reactor is active or had been stopped.
Definition Reactor.h:209
ASSA::Reactor::stopReactor
void stopReactor(void)
Stop Reactor's activity.
Definition Reactor.cpp:667
ASSA::Reactor::registerTimerHandler
TimerId registerTimerHandler(EventHandler *eh_, const TimeVal &tv_, const std::string &name_="<unknown>")
Register Timer Event handler with Reactor.
Definition Reactor.cpp:67
ASSA::Reactor::checkFDs
bool checkFDs(void)
Check mask for bad file descriptors.
Definition Reactor.cpp:317
ASSA::Reactor::isAnyReady
int isAnyReady(void)
Return number of file descriptors ready accross all sets.
Definition Reactor.cpp:404
ASSA::Reactor::removeHandler
bool removeHandler(EventHandler *eh_, EventType et_=ALL_EVENTS)
Remove Event handler from reactor for either all I/O events or timeout event or both.
Definition Reactor.cpp:173
ASSA::Reactor::~Reactor
~Reactor()
Destructor.
Definition Reactor.cpp:55
ASSA::Reactor::removeTimerHandler
bool removeTimerHandler(TimerId id_)
Remove Timer event from the queue.
Definition Reactor.cpp:152
ASSA::Reactor::adjust_maxfdp1
void adjust_maxfdp1(handler_t fd_)
Adjust maxfdp1 in a portable way (win32 ignores maxfd alltogether).
Definition Reactor.cpp:701
ASSA::Reactor::removeIOHandler
bool removeIOHandler(handler_t fd_)
Remove IO Event handler from reactor.
Definition Reactor.cpp:247
ASSA::Reactor::m_exceptSet
Fd2Eh_Map_Type m_exceptSet
Event handlers awaiting on EXCEPT_EVENT.
Definition Reactor.h:218
ASSA::TimeVal
Definition TimeVal.h:31
ASSA::TimerQueue
Definition TimerQueue.h:36
ASSA
Definition Acceptor.h:40
ASSA::EventType
EventType
EventType defines events types that Reactor understands.
Definition EventHandler.h:35
ASSA::ALL_EVENTS
@ ALL_EVENTS
Mask that includes all events.
Definition EventHandler.h:47
ASSA::RWE_EVENTS
@ RWE_EVENTS
READ_EVENT | WRITE_EVENT | EXCEPT_EVENT.
Definition EventHandler.h:46
ASSA::EH_IO_Callback
int(EventHandler::* EH_IO_Callback)(int)
A type for the pointer to I/O-related callback member function of class EventHandler.
Definition EventHandler.h:236
ASSA::TimerId
unsigned long TimerId
Timer Id is used in handle_timeout() calls.
Definition EventHandler.h:27