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

#include <Socket.h>

+ Inheritance diagram for CSockManager:
+ Collaboration diagram for CSockManager:

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

 CSockManager ()
 
virtual ~CSockManager ()
 
bool ListenHost (u_short iPort, const CString &sSockName, const CString &sBindHost, bool bSSL=false, int iMaxConns=SOMAXCONN, CZNCSock *pcSock=nullptr, u_int iTimeout=0, EAddrType eAddr=ADDR_ALL)
 
bool ListenAll (u_short iPort, const CString &sSockName, bool bSSL=false, int iMaxConns=SOMAXCONN, CZNCSock *pcSock=nullptr, u_int iTimeout=0, EAddrType eAddr=ADDR_ALL)
 
u_short ListenRand (const CString &sSockName, const CString &sBindHost, bool bSSL=false, int iMaxConns=SOMAXCONN, CZNCSock *pcSock=nullptr, u_int iTimeout=0, EAddrType eAddr=ADDR_ALL)
 
u_short ListenAllRand (const CString &sSockName, bool bSSL=false, int iMaxConns=SOMAXCONN, CZNCSock *pcSock=nullptr, u_int iTimeout=0, EAddrType eAddr=ADDR_ALL)
 
void Connect (const CString &sHostname, u_short iPort, const CString &sSockName, int iTimeout=60, bool bSSL=false, const CString &sBindHost="", CZNCSock *pcSock=nullptr)
 
unsigned int GetAnonConnectionCount (const CString &sIP) const
 
void DelSockByAddr (Csock *pcSock) override
 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 CZNCSockGetSockObj (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 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 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
 

Friends

class CThreadMonitorFD
 

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 
enum CSocketManager::EMessages
inherited
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

CSockManager::CSockManager ( )
virtual CSockManager::~CSockManager ( )
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 
)
virtualinherited

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 ( )
virtualinherited
void CSockCommon::CleanupCrons ( )
inherited
void CSockCommon::CleanupFDMonitors ( )
inherited
virtual void CSocketManager::clear ( )
virtualinherited
void CSockManager::Connect ( const CString sHostname,
u_short  iPort,
const CString sSockName,
int  iTimeout = 60,
bool  bSSL = false,
const CString sBindHost = "",
CZNCSock pcSock = nullptr 
)
void CSocketManager::Connect ( const CSConnection cCon,
Csock pcSock = NULL 
)
inherited

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)
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– );

void CSockManager::DelSockByAddr ( Csock pcSock)
overridevirtual

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 from CSocketManager.

void CSocketManager::DynamicSelectLoop ( uint64_t  iLowerBounds,
uint64_t  iUpperBounds,
time_t  iMaxResolution = 3600 
)
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
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 
)
inherited
void CSocketManager::FDSetCheck ( cs_sock_t  iFd,
std::map< cs_sock_t, short > &  miiReadyFds,
ECheckType  eType 
)
inherited
virtual Csock* CSocketManager::FindSockByFD ( cs_sock_t  iFD)
virtualinherited

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

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

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

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

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

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

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

returns a vector of pointers to socks with sHostname as being connected

unsigned int CSockManager::GetAnonConnectionCount ( const CString sIP) const
uint64_t CSocketManager::GetBytesRead ( ) const
inherited

Get the bytes read from all sockets current and past.

uint64_t CSocketManager::GetBytesWritten ( ) const
inherited

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
inlineinherited

return the last known error as set by this class

uint64_t CSocketManager::GetSelectTimeout ( ) const
inlineinherited

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

virtual CZNCSock * TSocketManager< CZNCSock >::GetSockObj ( const CS_STRING sHostname,
uint16_t  uPort,
int  iTimeout = 60 
)
inlinevirtualinherited

Reimplemented from CSocketManager.

bool CSocketManager::HasFDs ( ) const
inherited

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 
)
virtualinherited

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 ListenHost(), and ListenRand().

bool CSockManager::ListenAll ( u_short  iPort,
const CString sSockName,
bool  bSSL = false,
int  iMaxConns = SOMAXCONN,
CZNCSock pcSock = nullptr,
u_int  iTimeout = 0,
EAddrType  eAddr = ADDR_ALL 
)
inline

References ListenHost().

u_short CSockManager::ListenAllRand ( const CString sSockName,
bool  bSSL = false,
int  iMaxConns = SOMAXCONN,
CZNCSock pcSock = nullptr,
u_int  iTimeout = 0,
EAddrType  eAddr = ADDR_ALL 
)
inline

References ListenRand().

bool CSockManager::ListenHost ( u_short  iPort,
const CString sSockName,
const CString sBindHost,
bool  bSSL = false,
int  iMaxConns = SOMAXCONN,
CZNCSock pcSock = nullptr,
u_int  iTimeout = 0,
EAddrType  eAddr = ADDR_ALL 
)
inline
u_short CSockManager::ListenRand ( const CString sSockName,
const CString sBindHost,
bool  bSSL = false,
int  iMaxConns = SOMAXCONN,
CZNCSock pcSock = nullptr,
u_int  iTimeout = 0,
EAddrType  eAddr = ADDR_ALL 
)
inline
virtual void CSocketManager::Loop ( )
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.

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 
)
protectedvirtualinherited
void CSocketManager::SetSelectTimeout ( uint64_t  iTimeout)
inlineinherited

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 
)
virtualinherited

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 
)
virtualinherited

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

Friends And Related Function Documentation

friend class CThreadMonitorFD
friend

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: