GClasses
GClasses::GTCPServer Class Reference

Detailed Description

This class is an abstraction of a TCP server, which maintains a set of socket connections.

#include <GSocket.h>

Public Member Functions

 GTCPServer (unsigned short port)
 
virtual ~GTCPServer ()
 
void checkForNewConnections ()
 Accept any new incoming connections. More...
 
std::set< GTCPConnection * > & connections ()
 Returns a reference to the current set of connections. More...
 
void disconnect (GTCPConnection *pConn)
 Disconnect from the specified client. More...
 
size_t receive (char *buf, size_t len, GTCPConnection **pOutConn)
 This method receives any data that is ready to be received. It returns the number of bytes received. It immediately returns 0 if nothing is ready to be recevied. The value at pOutConn will be set to indicate which client it received the data from. More...
 
void send (const char *buf, size_t len, GTCPConnection *pConn)
 Send some data to the specified client. Throws an exception if the send fails for any reason (including common reasons, such as if the client has closed the connection), so it is generally a good idea to send within a try/catch block. More...
 

Static Public Member Functions

static void hostName (char *buf, size_t len)
 Obtains the name of this host. More...
 

Protected Member Functions

virtual GTCPConnectionmakeConnection (SOCKET s)
 This is called just before a new connection is accepted. It returns a pointer to a new GTCPConnection object to associate with this connection. (The connection, however, isn't yet fully established, so it might cause an error if you send something to the client in an overload of this method.) If you want to associate some data with each connection, you can overload this method to create a custom object. More...
 
virtual void onDisconnect (GTCPConnection *pConn)
 This is called when a connection is first known to have disconnected. More...
 
virtual void onReceiveBadData (const char *message)
 This is called when a client sends some bad data. More...
 

Protected Attributes

SOCKET m_sock
 
std::set< GTCPConnection * > m_socks
 

Constructor & Destructor Documentation

GClasses::GTCPServer::GTCPServer ( unsigned short  port)
virtual GClasses::GTCPServer::~GTCPServer ( )
virtual

Member Function Documentation

void GClasses::GTCPServer::checkForNewConnections ( )

Accept any new incoming connections.

std::set<GTCPConnection*>& GClasses::GTCPServer::connections ( )
inline

Returns a reference to the current set of connections.

void GClasses::GTCPServer::disconnect ( GTCPConnection pConn)

Disconnect from the specified client.

static void GClasses::GTCPServer::hostName ( char *  buf,
size_t  len 
)
static

Obtains the name of this host.

virtual GTCPConnection* GClasses::GTCPServer::makeConnection ( SOCKET  s)
inlineprotectedvirtual

This is called just before a new connection is accepted. It returns a pointer to a new GTCPConnection object to associate with this connection. (The connection, however, isn't yet fully established, so it might cause an error if you send something to the client in an overload of this method.) If you want to associate some data with each connection, you can overload this method to create a custom object.

virtual void GClasses::GTCPServer::onDisconnect ( GTCPConnection pConn)
inlineprotectedvirtual

This is called when a connection is first known to have disconnected.

virtual void GClasses::GTCPServer::onReceiveBadData ( const char *  message)
inlineprotectedvirtual

This is called when a client sends some bad data.

size_t GClasses::GTCPServer::receive ( char *  buf,
size_t  len,
GTCPConnection **  pOutConn 
)

This method receives any data that is ready to be received. It returns the number of bytes received. It immediately returns 0 if nothing is ready to be recevied. The value at pOutConn will be set to indicate which client it received the data from.

void GClasses::GTCPServer::send ( const char *  buf,
size_t  len,
GTCPConnection pConn 
)

Send some data to the specified client. Throws an exception if the send fails for any reason (including common reasons, such as if the client has closed the connection), so it is generally a good idea to send within a try/catch block.

Member Data Documentation

SOCKET GClasses::GTCPServer::m_sock
protected
std::set<GTCPConnection*> GClasses::GTCPServer::m_socks
protected