ZNC  trunk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Types | Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
CSocketManager Class Reference

Best class to use to interact with the sockets. More...

#include <Csocket.h>

+ Inheritance diagram for CSocketManager:
+ Collaboration diagram for CSocketManager:

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 CsockGetSockObj (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 CsockFindSockByRemotePort (uint16_t iPort)
 returns a pointer to the FIRST sock found by port or NULL on no match More...
 
virtual CsockFindSockByLocalPort (uint16_t iPort)
 returns a pointer to the FIRST sock found by port or NULL on no match More...
 
virtual CsockFindSockByName (const CS_STRING &sName)
 returns a pointer to the FIRST sock found by name or NULL on no match More...
 
virtual CsockFindSockByFD (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

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
 

Detailed Description

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 :).

See Also
TSocketManager
Author
Jim Hull csock.nosp@m.et@j.nosp@m.imloc.nosp@m.o.co.nosp@m.m

Member Enumeration Documentation

this is a strict wrapper around C-api select(). Added in the event you need to do special work here

Enumerator
ECT_Read 
ECT_Write 
Enumerator
SUCCESS 

Select returned at least 1 fd ready for action.

SELECT_ERROR 

An Error Happened, Probably dead socket.

That socket is returned if available

SELECT_TIMEOUT 

Select Timeout.

SELECT_TRYAGAIN 

Select calls for you to try again.

Constructor & Destructor Documentation

CSocketManager::CSocketManager ( )
virtual CSocketManager::~CSocketManager ( )
virtual

Member Function Documentation

virtual void CSockCommon::AddCron ( CCron pcCron)
virtualinherited

insert a newly created cron

virtual void CSocketManager::AddSock ( Csock pcSock,
const CS_STRING sSockName 
)
virtual

Make this method virtual, so you can override it when a socket is added.

Assuming you might want to do some extra stuff

void CSockCommon::AssignFDs ( std::map< cs_sock_t, short > &  miiReadyFds,
struct timeval *  tvtimeout 
)
inherited
void CSockCommon::CheckFDs ( const std::map< cs_sock_t, short > &  miiReadyFds)
inherited
virtual void CSocketManager::Cleanup ( )
virtual
void CSockCommon::CleanupCrons ( )
inherited
void CSockCommon::CleanupFDMonitors ( )
inherited
virtual void CSocketManager::clear ( )
virtual
void CSocketManager::Connect ( const CSConnection cCon,
Csock pcSock = NULL 
)

Create a connection.

Parameters
cConthe connection which should be established
pcSockthe socket used for the connection, can be NULL
virtual void CSockCommon::Cron ( )
virtualinherited

This has a garbage collecter, and is used internall to call the jobs.

virtual void CSockCommon::DelCron ( const CS_STRING sName,
bool  bDeleteAll = true,
bool  bCaseSensitive = true 
)
virtualinherited

deletes a cron by name

Parameters
sNamethe name of the cron
bDeleteAlldelete all crons that match sName
bCaseSensitiveuse strcmp or strcasecmp
virtual void CSockCommon::DelCron ( uint32_t  iPos)
virtualinherited

delete cron by idx

virtual void CSockCommon::DelCronByAddr ( CCron pcCron)
virtualinherited

delete cron by address

virtual void CSocketManager::DelSock ( size_t  iPos)
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 void CSocketManager::DelSockByAddr ( Csock pcSock)
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
Parameters
iLowerBoundsthe lower bounds to use in MICROSECONDS
iUpperBoundsthe upper bounds to use in MICROSECONDS
iMaxResolutionthe 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 
)
virtual Csock* CSocketManager::FindSockByFD ( cs_sock_t  iFD)
virtual

returns a pointer to the FIRST sock found by filedescriptor or NULL on no match

virtual Csock* CSocketManager::FindSockByLocalPort ( uint16_t  iPort)
virtual

returns a pointer to the FIRST sock found by port or NULL on no match

virtual Csock* CSocketManager::FindSockByName ( const CS_STRING sName)
virtual

returns a pointer to the FIRST sock found by name or NULL on no match

virtual Csock* CSocketManager::FindSockByRemotePort ( uint16_t  iPort)
virtual

returns a pointer to the FIRST sock found by port or NULL on no match

virtual std::vector<Csock *> CSocketManager::FindSocksByName ( const CS_STRING sName)
virtual
virtual std::vector<Csock *> CSocketManager::FindSocksByRemoteHost ( const CS_STRING sHostname)
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.

const std::vector<CCron *>& CSockCommon::GetCrons ( ) const
inlineinherited

returns a const reference to the crons associated to this socket

int CSocketManager::GetErrno ( ) const
inline

return the last known error as set by this class

uint64_t CSocketManager::GetSelectTimeout ( ) const
inline

Get the Select Timeout in MICROSECONDS ( 1000 == 1 millisecond )

virtual Csock* CSocketManager::GetSockObj ( const CS_STRING sHostname,
uint16_t  uPort,
int  iTimeout = 60 
)
virtual
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 bool CSocketManager::Listen ( const CSListener cListen,
Csock pcSock = NULL,
uint16_t *  piRandPort = NULL 
)
virtual

Sets up a listening socket.

Parameters
cListenthe listener configuration
pcSockpreconfigured sock to use
piRandPortif 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 void CSocketManager::Loop ( )
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.

void CSockCommon::MonitorFD ( CSMonitorFD pMonitorFD)
inlineinherited

add an FD set to monitor

virtual int CSocketManager::Select ( std::map< cs_sock_t, short > &  miiReadyFds,
struct timeval *  tvtimeout 
)
protectedvirtual
void CSocketManager::SetSelectTimeout ( uint64_t  iTimeout)
inline

Set the Select Timeout in MICROSECONDS ( 1000 == 1 millisecond ) Setting this to 0 will cause no timeout to happen, Select() will return instantly.

virtual bool CSocketManager::SwapSockByAddr ( Csock pNewSock,
Csock pOrigSock 
)
virtual

swaps out a sock with a copy of the original sock

Parameters
pNewSockthe new sock to change out with. (this should be constructed by you with the default ctor)
pOrigSockthe address of the original socket
Returns
true on success
virtual bool CSocketManager::SwapSockByIdx ( Csock pNewSock,
size_t  iOrginalSockIdx 
)
virtual

swaps out a sock with a copy of the original sock

Parameters
pNewSockthe new sock to change out with. (this should be constructed by you with the default ctor)
iOrginalSockIdxthe position in this sockmanager of the original sock
Returns
true on success

Member Data Documentation

T std::vector< T >::elements
inherited

STL member.

std::vector<CCron *> CSockCommon::m_vcCrons
protectedinherited
std::vector<CSMonitorFD *> CSockCommon::m_vcMonitorFD
protectedinherited

The documentation for this class was generated from the following file: