Classes | Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
GNet::Server Class Referenceabstract

A network server class which listens on a specific port and spins off ServerPeer objects for each incoming connection. More...

#include <gserver.h>

Inheritance diagram for GNet::Server:
GNet::EventHandler GNet::MultiServerImp

Classes

struct  PeerInfo
 A structure used in GNet::Server::newPeer(). More...
 

Public Member Functions

 Server (unsigned int listening_port, ConnectionLookup *=NULL)
 Constructor taking a port number. More...
 
 Server (const Address &listening_address, ConnectionLookup *=NULL)
 Constructor. More...
 
 Server ()
 Default constructor. Initialise with init(). More...
 
void init (unsigned int listening_port)
 Initilisation after default construction. More...
 
void init (const Address &listening_address)
 Initilisation after default construction. More...
 
virtual ~Server ()
 Destructor. More...
 
std::pair< bool, Addressaddress () const
 Returns the listening address. More...
 
virtual void readEvent ()
 Final override from GNet::EventHandler. More...
 
virtual void writeEvent ()
 Final override from GNet::EventHandler. More...
 
virtual void onException (std::exception &e)
 Final override from GNet::EventHandler. More...
 
- Public Member Functions inherited from GNet::EventHandler
virtual ~EventHandler ()
 Destructor. More...
 
virtual void exceptionEvent ()
 Called for an exception event. More...
 

Static Public Member Functions

static bool canBind (const Address &listening_address, bool do_throw)
 Checks that the specified address can be bound. More...
 

Protected Member Functions

virtual ServerPeernewPeer (PeerInfo)=0
 A factory method which new()s a ServerPeer-derived object. More...
 
void serverCleanup ()
 May be called from the derived class destructor in order to trigger early deletion of peer objects, before the derived part of the server disappears. More...
 

Detailed Description

A network server class which listens on a specific port and spins off ServerPeer objects for each incoming connection.

See also
GNet::ServerPeer

Definition at line 83 of file gserver.h.

Constructor & Destructor Documentation

GNet::Server::Server ( unsigned int  listening_port,
ConnectionLookup connection_lookup = NULL 
)
explicit

Constructor taking a port number.

The server listens on all local interfaces.

Definition at line 136 of file gserver.cpp.

References init().

GNet::Server::Server ( const Address listening_address,
ConnectionLookup connection_lookup = NULL 
)
explicit

Constructor.

The server listens only on the specific (local) interface.

Definition at line 142 of file gserver.cpp.

References init().

GNet::Server::Server ( )

Default constructor. Initialise with init().

Definition at line 148 of file gserver.cpp.

GNet::Server::~Server ( )
virtual

Destructor.

Definition at line 182 of file gserver.cpp.

Member Function Documentation

std::pair< bool, GNet::Address > GNet::Server::address ( ) const

Returns the listening address.

Pair.first is false if not properly init()ialised.

Definition at line 221 of file gserver.cpp.

References GNet::Address::invalidAddress().

Referenced by GNet::MultiServer::serverReport().

bool GNet::Server::canBind ( const Address listening_address,
bool  do_throw 
)
static

Checks that the specified address can be bound.

Throws CannotBind if the address cannot be bound and 'do_throw' is true.

Definition at line 172 of file gserver.cpp.

References GNet::Socket::canBindHint(), and GNet::Address::displayString().

Referenced by GNet::MultiServer::canBind().

void GNet::Server::init ( unsigned int  listening_port)

Initilisation after default construction.

Definition at line 154 of file gserver.cpp.

Referenced by Server().

void GNet::Server::init ( const Address listening_address)

Initilisation after default construction.

Definition at line 159 of file gserver.cpp.

References GNet::Address::displayString(), G_DEBUG, and G::limits::net_listen_queue.

virtual ServerPeer* GNet::Server::newPeer ( PeerInfo  )
protectedpure virtual

A factory method which new()s a ServerPeer-derived object.

This method is called when a new connection comes into this server. The new ServerPeer object is used to represent the state of the client/server connection.

The new ServerPeer object manages its own lifetime doing a "delete this" when the network connection disappears. But the Server also deletes remaining peers during its destruction.

The implementation should pass the 'PeerInfo' parameter through to the ServerPeer base-class constructor.

Should return NULL for non-fatal errors. It is expected that a typical server process will terminate if newPeer() throws, so most implementations will catch any exceptions and return NULL.

Implemented in GNet::MultiServerImp.

void GNet::Server::onException ( std::exception &  e)
virtual

Final override from GNet::EventHandler.

Implements GNet::EventHandler.

Definition at line 215 of file gserver.cpp.

References G_ERROR.

void GNet::Server::readEvent ( )
virtual
void GNet::Server::serverCleanup ( )
protected

May be called from the derived class destructor in order to trigger early deletion of peer objects, before the derived part of the server disappears.

If this is called from the most-derived Server class then it allows ServerPeer objects to use their Server object safely during destruction.

Definition at line 187 of file gserver.cpp.

void GNet::Server::writeEvent ( )
virtual

Final override from GNet::EventHandler.

Reimplemented from GNet::EventHandler.

Definition at line 313 of file gserver.cpp.

References G_DEBUG.


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