Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
GrayLib::cNetServer Class Reference

#include <cNetServer.h>

Inheritance diagram for GrayLib::cNetServer:
GrayLib::cProtocolFactories GrayLib::cNetServerGroup Gray::cStreamStats GrayLib::cNetServerUnitTest

Public Member Functions

 cNetServer (const char *pszName="NetServer", bool bAsyncEvents=false)
 
virtual ~cNetServer ()
 
const char * get_ServerName () const noexcept
 
ITERATE_t get_ConnectionQty () const noexcept
 
HRESULT AddListenPort (cNetServerPort *pServerPort)
 
HRESULT AddListenPort (const cNetAddress &SockAddr)
 
HRESULT AddListenPort2 (const cNetAddress &SockAddr, OUT cNetServerPortPtr *ppServerPort=nullptr)
 
bool RemoveListenPort (cNetServerPort *pServerPort)
 
ITERATE_t get_ListenPortQty () const noexcept
 
cNetServerPortPtr EnumListenPort (ITERATE_t i) const
 
HRESULT CreateServerStream (const cNetAddress &addr, IProtocolFactory &pf, cNetServerConnectionPtr &rpStream)
 
virtual HRESULT CheckAddressBeforeAccept (const cNetAddress &Addr) const
 
bool InitServer ()
 
void CloseServer ()
 
virtual void OnTickClients () override
 
void OnTickServer (TIMESYSD_t tWaitMax=0)
 
 UNITTEST_FRIEND (cNetServer)
 
- Public Member Functions inherited from GrayLib::cProtocolFactories
bool AddProtocol (IProtocolFactory *pProtocolFactory)
 
HRESULT TestProtocol (const BYTE *pPrefixData, size_t iPrefixLen, OUT IProtocolFactory *&rpProtocolFactory) const
 
- Public Member Functions inherited from GrayLib::cNetServerGroup
 cNetServerGroup (cNetServer &server)
 
virtual ~cNetServerGroup ()
 
const char * get_ServerName () const
 
ITERATE_t get_ClientQty () const
 
bool RemoveClient (cNetServerConnection *pClient)
 
void MoveClientToGroup (cNetServerConnection *pClient)
 
void MoveClientToClose (cNetServerConnection *pClient)
 
- Public Member Functions inherited from Gray::cStreamStats
void Add (const cStreamStats &n)
 

Public Attributes

const char * m_pszServerName
 Server type name. (not host name) More...
 
const bool m_bAsyncEvents
 _WIN32 only. use async callbacks. More...
 
int m_nMaxConnections
 total allowed incoming connections. More...
 
int m_nMaxPerAddr
 max connections per host/address. More...
 
int m_nMaxPerAddrUnresolved
 max unresolved protocol connections per host/address. More...
 
- Public Attributes inherited from GrayLib::cNetServerGroup
cNetServerm_server
 My top level cNetServer owner/parent. More...
 
cValueStatsRate< float > m_TickLatency
 How long did the last OnTickSelect and OnTickClients take ? TIMESYS_t. More...
 
- Public Attributes inherited from Gray::cStreamStats
cStreamStat m_StatOut
 
cStreamStat m_StatInp
 

Protected Member Functions

void OnClientCloseComplete ()
 
HRESULT TestProtocolClient (cNetServerConnection *pClient)
 
HRESULT OnEventAccept (cNetServerPort *pListenPort, LSTATUS wError)
 
HRESULT OnClientConnect (cNetServerConnection *pClient)
 
virtual HRESULT OnClientReady (cNetServerConnection *pClient)
 
virtual void OnClientClose (cNetServerConnection *pClient)
 
virtual HRESULT OnClientRead (cNetServerConnection *pClient) override
 
- Protected Member Functions inherited from GrayLib::cNetServerGroup
HRESULT OnClientWrite (cNetServerConnection *pClient)
 
HRESULT OnTickSelect (TIMESYSD_t tWaitMax, cNetSocketSet &readfds, cNetSocketSet &writefds)
 

Protected Attributes

cNetSystemPtr m_NetSystem
 I use the network. More...
 
cArrayRef< cNetServerThreadm_aThreads
 Alternate threads used to process clients. More...
 
cArraySortHash< cNetServerConnectionm_aConnecting
 My Outgoing connections. From this server to some place. More...
 
cThreadLockArrayHash< cNetServerConnectionm_aClosing
 Call OnClientClose these connections. More...
 
cArraySortHash< cNetServerPortm_aListenPorts
 The server SOCKET(s) we are currently listening on. 1 for each port. More...
 
- Protected Attributes inherited from GrayLib::cProtocolFactories
cArrayPtr< IProtocolFactorym_aProtocols
 array of possible protocols we support. More...
 
- Protected Attributes inherited from GrayLib::cNetServerGroup
cThreadLockArrayHash< cNetServerConnectionm_aConnections
 Collection of active clients. sorted by socket id. More...
 

Friends

class cNetServerGroup
 

Detailed Description

A generic TCP Internet server for things based on cProtocol. Accept incoming TCP connections and manage them. Can act as a polled server or just waits for incoming connections.

Note
Use cUPnPNAT to expose ports outside the local firewall.
Use cNetAddrTracker/cNetAddrMgr to track DoS/password/etc attacks.
Todo:
support UDP based protocols.

Constructor & Destructor Documentation

◆ cNetServer()

GrayLib::cNetServer::cNetServer ( const char *  pszName = "NetServer",
bool  bAsyncEvents = false 
)

◆ ~cNetServer()

GrayLib::cNetServer::~cNetServer ( )
virtual

Member Function Documentation

◆ AddListenPort() [1/2]

HRESULT GrayLib::cNetServer::AddListenPort ( cNetServerPort pServerPort)

Monitor/listen on this new local socket address/port.

Returns
The index for the listen socket. <0 = error.

◆ AddListenPort() [2/2]

HRESULT GrayLib::cNetServer::AddListenPort ( const cNetAddress SockAddr)

Monitor/listen on this new local socket address/port.

Returns
The index for the listen socket. <0 = error.

◆ AddListenPort2()

HRESULT GrayLib::cNetServer::AddListenPort2 ( const cNetAddress SockAddr,
OUT cNetServerPortPtr ppServerPort = nullptr 
)

Monitor/listen on this new local socket address/port.

Returns
<0 = failed to get a port. e.g. No permissions to open low ports?

◆ CheckAddressBeforeAccept()

HRESULT GrayLib::cNetServer::CheckAddressBeforeAccept ( const cNetAddress Addr) const
virtual

Is the address on my blocked list ? override to use cNetAddrMgr/cNetAddrTracker Different clients behind a NAT IP host router can all look like the same address to me. just different ports. Does this host address have other unresolved connections? (only c_MaxUnresolvedPerAddr allowed at a time) Prevent DoS attacks.

◆ CloseServer()

void GrayLib::cNetServer::CloseServer ( )

server is going away.

Note
this is only called on the server thread.

remove server SOCKET(s) we are currently listening on. 1 for each port.

◆ CreateServerStream()

HRESULT GrayLib::cNetServer::CreateServerStream ( const cNetAddress addr,
IProtocolFactory pf,
cNetServerConnectionPtr rpStream 
)

Create an outgoing stream/protocol connection. Weird behavior for a server. But treat it like any other server connection. e.g. FTP server does outgoing connect for NOT PASV data connection.

◆ EnumListenPort()

cNetServerPortPtr GrayLib::cNetServer::EnumListenPort ( ITERATE_t  i) const
inline

◆ get_ConnectionQty()

ITERATE_t GrayLib::cNetServer::get_ConnectionQty ( ) const
inlinenoexcept

Sum of all ports I must poll.

◆ get_ListenPortQty()

ITERATE_t GrayLib::cNetServer::get_ListenPortQty ( ) const
inlinenoexcept

How many ports/sockets am i listening to ?

◆ get_ServerName()

const char* GrayLib::cNetServer::get_ServerName ( ) const
inlinenoexcept

◆ InitServer()

bool GrayLib::cNetServer::InitServer ( )

Make sure the server has connected to the network.

◆ OnClientClose()

void GrayLib::cNetServer::OnClientClose ( cNetServerConnection pClient)
protectedvirtual

The socket has disconnected so clean it up. a remote disconnect or graceful disconnect. Assume debug message already posted.

Note
Called on server thread only.

◆ OnClientCloseComplete()

void GrayLib::cNetServer::OnClientCloseComplete ( )
protected

call OnClientClose all the clients that are m_aClosing.

Note
this is only called on the server thread.

◆ OnClientConnect()

HRESULT GrayLib::cNetServer::OnClientConnect ( cNetServerConnection pClient)
protected

Outgoing (non blocking) connection is now connected. FD_CONNECT Failure to complete connection just calls OnClientClose

◆ OnClientRead()

HRESULT GrayLib::cNetServer::OnClientRead ( cNetServerConnection pClient)
overrideprotectedvirtual

Data is ready for read on a client connection. FD_READ

Returns
<0 = HRESULT HRESULT_WIN32_C(ERROR_IO_INCOMPLETE) Number of bytes received.

Reimplemented from GrayLib::cNetServerGroup.

◆ OnClientReady()

HRESULT GrayLib::cNetServer::OnClientReady ( cNetServerConnection pClient)
protectedvirtual

The connections/clients m_pProtocol has been resolved and/or connected. override this to reject protocols on certain addresses/protocol combos ?

Returns
HRESULT_WIN32_C(ERROR_IO_INCOMPLETE) = need more. Incomplete packet.

◆ OnEventAccept()

HRESULT GrayLib::cNetServer::OnEventAccept ( cNetServerPort pListenPort,
LSTATUS  wError 
)
protected

The system says a new connection is incoming via ::select() or asynchronous FD_ACCEPT message. Accept incoming connection and find out more about this connection.

  • pListenPort=the port the new connection is coming in on.
    Note
    Windows has a better mechanism for filtering bad source hosts earlier/cheaper. AsyncAccept call CheckAddressBeforeAccept.
    Returns
    HRESULT. HRESULT_WIN32_C(ERROR_IO_INCOMPLETE)

◆ OnTickClients()

void GrayLib::cNetServer::OnTickClients ( )
overridevirtual

OnTick + Final cleanup of closed clients.

Reimplemented from GrayLib::cNetServerGroup.

◆ OnTickServer()

void GrayLib::cNetServer::OnTickServer ( TIMESYSD_t  tWaitMax = 0)

Poll of listen sockets and process data from clients.

  • tWaitMax = mSec to wait for new data.
    Note
    Don't call OnTickStream() or OnTickSelect() if using OnEvent() asynchronous callbacks.

◆ RemoveListenPort()

bool GrayLib::cNetServer::RemoveListenPort ( cNetServerPort pServerPort)

When a particular listen socket is no longer needed. remove it.

◆ TestProtocolClient()

HRESULT GrayLib::cNetServer::TestProtocolClient ( cNetServerConnection pClient)
protected

We just got data for this new incoming client. Try to resolve its protocol.

Note
we even call this with no data on first connection. allow non prefix dedicated port protocols. (e.g.telnet)
Returns
HRESULT_WIN32_C(ERROR_IO_INCOMPLETE) = need more. Incomplete packet. not enough data to determine protocol yet. S_OK = got it.

◆ UNITTEST_FRIEND()

GrayLib::cNetServer::UNITTEST_FRIEND ( cNetServer  )

Friends And Related Function Documentation

◆ cNetServerGroup

friend class cNetServerGroup
friend

Member Data Documentation

◆ m_aClosing

cThreadLockArrayHash<cNetServerConnection> GrayLib::cNetServer::m_aClosing
protected

Call OnClientClose these connections.

◆ m_aConnecting

cArraySortHash<cNetServerConnection> GrayLib::cNetServer::m_aConnecting
protected

My Outgoing connections. From this server to some place.

◆ m_aListenPorts

cArraySortHash<cNetServerPort> GrayLib::cNetServer::m_aListenPorts
protected

The server SOCKET(s) we are currently listening on. 1 for each port.

◆ m_aThreads

cArrayRef<cNetServerThread> GrayLib::cNetServer::m_aThreads
protected

Alternate threads used to process clients.

◆ m_bAsyncEvents

const bool GrayLib::cNetServer::m_bAsyncEvents

_WIN32 only. use async callbacks.

◆ m_NetSystem

cNetSystemPtr GrayLib::cNetServer::m_NetSystem
protected

I use the network.

◆ m_nMaxConnections

int GrayLib::cNetServer::m_nMaxConnections

total allowed incoming connections.

◆ m_nMaxPerAddr

int GrayLib::cNetServer::m_nMaxPerAddr

max connections per host/address.

◆ m_nMaxPerAddrUnresolved

int GrayLib::cNetServer::m_nMaxPerAddrUnresolved

max unresolved protocol connections per host/address.

◆ m_pszServerName

const char* GrayLib::cNetServer::m_pszServerName

Server type name. (not host name)


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