ZNC
trunk
|
Ease of use templated socket manager. More...
#include <Csocket.h>
Public Types | |
enum | EMessages { SUCCESS = 0 , SELECT_ERROR = -1 , SELECT_TIMEOUT = -2 , SELECT_TRYAGAIN = -3 } |
enum | ECheckType { ECT_Read = 1 , ECT_Write = 2 } |
this is a strict wrapper around C-api select(). Added in the event you need to do special work here More... | |
Public Member Functions | |
TSocketManager () | |
virtual | ~TSocketManager () |
virtual T * | GetSockObj (const CS_STRING &sHostname, uint16_t uPort, int iTimeout=60) |
virtual void | clear () |
virtual void | Cleanup () |
void | Connect (const CSConnection &cCon, Csock *pcSock=NULL) |
Create a connection. More... | |
virtual bool | Listen (const CSListener &cListen, Csock *pcSock=NULL, uint16_t *piRandPort=NULL) |
Sets up a listening socket. More... | |
bool | HasFDs () const |
simple method to see if there are file descriptors being processed, useful to know if all the work is done in the manager More... | |
virtual void | Loop () |
Best place to call this class for running, all the call backs are called. More... | |
void | DynamicSelectLoop (uint64_t iLowerBounds, uint64_t iUpperBounds, time_t iMaxResolution=3600) |
this is similar to loop, except that it dynamically adjusts the select time based on jobs and timeouts in sockets More... | |
virtual void | AddSock (Csock *pcSock, const CS_STRING &sSockName) |
Make this method virtual, so you can override it when a socket is added. More... | |
virtual Csock * | FindSockByRemotePort (uint16_t iPort) |
returns a pointer to the FIRST sock found by port or NULL on no match More... | |
virtual Csock * | FindSockByLocalPort (uint16_t iPort) |
returns a pointer to the FIRST sock found by port or NULL on no match More... | |
virtual Csock * | FindSockByName (const CS_STRING &sName) |
returns a pointer to the FIRST sock found by name or NULL on no match More... | |
virtual Csock * | FindSockByFD (cs_sock_t iFD) |
returns a pointer to the FIRST sock found by filedescriptor or NULL on no match More... | |
virtual std::vector< Csock * > | FindSocksByName (const CS_STRING &sName) |
virtual std::vector< Csock * > | FindSocksByRemoteHost (const CS_STRING &sHostname) |
returns a vector of pointers to socks with sHostname as being connected More... | |
int | GetErrno () const |
return the last known error as set by this class More... | |
uint64_t | GetSelectTimeout () const |
Get the Select Timeout in MICROSECONDS ( 1000 == 1 millisecond ) More... | |
void | SetSelectTimeout (uint64_t iTimeout) |
Set the Select Timeout in MICROSECONDS ( 1000 == 1 millisecond ) Setting this to 0 will cause no timeout to happen, Select() will return instantly. More... | |
virtual void | DelSockByAddr (Csock *pcSock) |
Delete a sock by addr its position is looked up the socket is deleted, the appropriate call backs are peformed and its instance is removed from the manager. More... | |
virtual void | DelSock (size_t iPos) |
Delete a sock by position in the vector the socket is deleted, the appropriate call backs are peformed and its instance is removed from the manager deleting in a loop can be tricky, be sure you watch your position. More... | |
virtual bool | SwapSockByIdx (Csock *pNewSock, size_t iOrginalSockIdx) |
swaps out a sock with a copy of the original sock More... | |
virtual bool | SwapSockByAddr (Csock *pNewSock, Csock *pOrigSock) |
swaps out a sock with a copy of the original sock More... | |
uint64_t | GetBytesRead () const |
Get the bytes read from all sockets current and past. More... | |
uint64_t | GetBytesWritten () const |
Get the bytes written to all sockets current and past. More... | |
void | FDSetCheck (cs_sock_t iFd, std::map< cs_sock_t, short > &miiReadyFds, ECheckType eType) |
bool | FDHasCheck (cs_sock_t iFd, std::map< cs_sock_t, short > &miiReadyFds, ECheckType eType) |
void | CleanupCrons () |
void | CleanupFDMonitors () |
const std::vector< CCron * > & | GetCrons () const |
returns a const reference to the crons associated to this socket More... | |
virtual void | Cron () |
This has a garbage collecter, and is used internall to call the jobs. More... | |
virtual void | AddCron (CCron *pcCron) |
insert a newly created cron More... | |
virtual void | DelCron (const CS_STRING &sName, bool bDeleteAll=true, bool bCaseSensitive=true) |
deletes a cron by name More... | |
virtual void | DelCron (uint32_t iPos) |
delete cron by idx More... | |
virtual void | DelCronByAddr (CCron *pcCron) |
delete cron by address More... | |
void | CheckFDs (const std::map< cs_sock_t, short > &miiReadyFds) |
void | AssignFDs (std::map< cs_sock_t, short > &miiReadyFds, struct timeval *tvtimeout) |
void | MonitorFD (CSMonitorFD *pMonitorFD) |
add an FD set to monitor More... | |
Public Attributes | |
T | elements |
STL member. More... | |
Protected Member Functions | |
virtual int | Select (std::map< cs_sock_t, short > &miiReadyFds, struct timeval *tvtimeout) |
Protected Attributes | |
std::vector< CCron * > | m_vcCrons |
std::vector< CSMonitorFD * > | m_vcMonitorFD |
Ease of use templated socket manager.
class CBlahSock : public TSocketManager<SomeSock>
|
inherited |
|
inherited |
|
inline |
|
inlinevirtual |
|
virtualinherited |
insert a newly created cron
|
virtualinherited |
Make this method virtual, so you can override it when a socket is added.
Assuming you might want to do some extra stuff
|
inherited |
|
inherited |
|
virtualinherited |
|
inherited |
|
inherited |
|
virtualinherited |
|
inherited |
Create a connection.
cCon | the connection which should be established |
pcSock | the socket used for the connection, can be NULL |
|
virtualinherited |
This has a garbage collecter, and is used internall to call the jobs.
|
virtualinherited |
deletes a cron by name
sName | the name of the cron |
bDeleteAll | delete all crons that match sName |
bCaseSensitive | use strcmp or strcasecmp |
|
virtualinherited |
delete cron by idx
|
virtualinherited |
delete cron by address
|
virtualinherited |
Delete a sock by position in the vector the socket is deleted, the appropriate call backs are peformed and its instance is removed from the manager deleting in a loop can be tricky, be sure you watch your position.
ie for( uint32_t a = 0; a < size(); a++ ) DelSock( a– );
|
virtualinherited |
Delete a sock by addr its position is looked up the socket is deleted, the appropriate call backs are peformed and its instance is removed from the manager.
Reimplemented in CSockManager.
|
inherited |
this is similar to loop, except that it dynamically adjusts the select time based on jobs and timeouts in sockets
- This type of behavior only works well in a scenario where there is low traffic. If you use this then its because you - are trying to spare yourself some of those idle loops where nothing is done. If you try to use this code where you have lots of - connections and/or lots of traffic you might end up causing more CPU usage than just a plain Loop() with a static sleep of 500ms - its a trade off at some point, and you'll probably find out that the vast majority of the time and in most cases Loop() works fine - by itself. I've tried to mitigate that as much as possible by not having it change the select if the previous call to select - was not a timeout. Anyways .... Caveat Emptor. - Sample useage is cFoo.DynamicSelectLoop( 500000, 5000000 ); which basically says min of 500ms and max of 5s
iLowerBounds | the lower bounds to use in MICROSECONDS |
iUpperBounds | the upper bounds to use in MICROSECONDS |
iMaxResolution | the maximum time to calculate overall in seconds |
|
inherited |
|
inherited |
returns a pointer to the FIRST sock found by filedescriptor or NULL on no match
|
virtualinherited |
returns a pointer to the FIRST sock found by port or NULL on no match
returns a pointer to the FIRST sock found by name or NULL on no match
|
virtualinherited |
returns a pointer to the FIRST sock found by port or NULL on no match
|
virtualinherited |
|
virtualinherited |
returns a vector of pointers to socks with sHostname as being connected
|
inherited |
Get the bytes read from all sockets current and past.
|
inherited |
Get the bytes written to all sockets current and past.
|
inlineinherited |
returns a const reference to the crons associated to this socket
|
inlineinherited |
return the last known error as set by this class
|
inlineinherited |
Get the Select Timeout in MICROSECONDS ( 1000 == 1 millisecond )
|
inlinevirtual |
Reimplemented from CSocketManager.
|
inherited |
simple method to see if there are file descriptors being processed, useful to know if all the work is done in the manager
|
virtualinherited |
Sets up a listening socket.
cListen | the listener configuration |
pcSock | preconfigured sock to use |
piRandPort | if listener is set to port 0, then a random port is used and this is assigned. |
IF you provide piRandPort to be assigned, AND you set bDetach to true, then Listen() still blocks. If you don't want this behavior, then look for the port assignment to be called in Csock::Listening
Referenced by CSockManager::ListenHost(), and CSockManager::ListenRand().
|
virtualinherited |
Best place to call this class for running, all the call backs are called.
You should through this in your main while loop (long as its not blocking) all the events are called as needed.
|
inlineinherited |
add an FD set to monitor
|
protectedvirtualinherited |
|
inlineinherited |
Set the Select Timeout in MICROSECONDS ( 1000 == 1 millisecond ) Setting this to 0 will cause no timeout to happen, Select() will return instantly.
|
virtualinherited |
swaps out a sock with a copy of the original sock
pNewSock | the new sock to change out with. (this should be constructed by you with the default ctor) |
pOrigSock | the address of the original socket |
|
virtualinherited |
swaps out a sock with a copy of the original sock
pNewSock | the new sock to change out with. (this should be constructed by you with the default ctor) |
iOrginalSockIdx | the position in this sockmanager of the original sock |
|
inherited |
STL member.
|
protectedinherited |
|
protectedinherited |