ZNC
trunk
|
Best class to use to interact with the sockets. 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 | |
CSocketManager () | |
virtual | ~CSocketManager () |
virtual void | clear () |
virtual void | Cleanup () |
virtual Csock * | GetSockObj (const CS_STRING &sHostname, uint16_t uPort, int iTimeout=60) |
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 |
Best class to use to interact with the sockets.
Handles SSL and NON Blocking IO. Rather then use it directly, you'll probably get more use deriving from it. Override GetSockObj since Csock derivatives need to be new'd correctly. Makes it easier to use overall. Another thing to note, is that all sockets are deleted implicitly, so obviously you can't pass in Csock classes created on the stack. For those of you who don't know STL very well, the reason I did this is because whenever you add to certain stl containers (e.g. vector, or map), it is completely rebuilt using the copy constructor on each element. That then means the constructor and destructor are called on every item in the container. Not only is this more overhead then just moving pointers around, its dangerous as if you have an object that is newed and deleted in the destructor the value of its pointer is copied in the default copy constructor. This means everyone has to know better and create a copy constructor, or I just make everyone new their object :).
CSocketManager::CSocketManager | ( | ) |
|
virtual |
|
virtualinherited |
insert a newly created cron
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 |
|
virtual |
|
inherited |
|
inherited |
|
virtual |
void CSocketManager::Connect | ( | const CSConnection & | cCon, |
Csock * | pcSock = NULL |
||
) |
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
|
virtual |
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– );
|
virtual |
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.
void CSocketManager::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
- 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 |
bool CSocketManager::FDHasCheck | ( | cs_sock_t | iFd, |
std::map< cs_sock_t, short > & | miiReadyFds, | ||
ECheckType | eType | ||
) |
void CSocketManager::FDSetCheck | ( | cs_sock_t | iFd, |
std::map< cs_sock_t, short > & | miiReadyFds, | ||
ECheckType | eType | ||
) |
returns a pointer to the FIRST sock found by filedescriptor or NULL on no match
|
virtual |
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
|
virtual |
returns a pointer to the FIRST sock found by port or NULL on no match
|
virtual |
returns a vector of pointers to socks with sHostname as being connected
uint64_t CSocketManager::GetBytesRead | ( | ) | const |
Get the bytes read from all sockets current and past.
uint64_t CSocketManager::GetBytesWritten | ( | ) | const |
Get the bytes written to all sockets current and past.
|
inlineinherited |
returns a const reference to the crons associated to this socket
|
inline |
return the last known error as set by this class
|
inline |
Get the Select Timeout in MICROSECONDS ( 1000 == 1 millisecond )
|
virtual |
Reimplemented in TSocketManager< T >, and TSocketManager< CZNCSock >.
bool CSocketManager::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
|
virtual |
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().
|
virtual |
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
|
protectedvirtual |
|
inline |
Set the Select Timeout in MICROSECONDS ( 1000 == 1 millisecond ) Setting this to 0 will cause no timeout to happen, Select() will return instantly.
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 |
|
virtual |
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 |