dtNet::NetMgr Class Reference

This class is used as the base of all networking applications. More...

#include <netmgr.h>

Inheritance diagram for dtNet::NetMgr:

dtCore::Base

List of all members.

Public Member Functions

 NetMgr ()
void InitializeGame (const std::string &gameName, int gameVersion, const std::string &logFile)
 Initialize the networking and game environment.
bool SetupServer (int portNum)
 Setup and create a server.
bool SetupClient (const std::string &host, int portNum)
 Setup and create a client to connect to the server.
void Shutdown ()
 Shutdown the networking.
void SendPacket (const std::string &address, GNE::Packet &packet)
 Send a packet to the given address.
int GetNumConnections () const
 Get the number of connections to the network.
bool GetIsServer () const
 Is this instance setup as a server?
virtual void OnListenSuccess ()
 virtual methods
virtual void OnListenFailure (const GNE::Error &error, const GNE::Address &from, const GNE::ConnectionListener::sptr &listener)
 callback to signal the connection to the socket failed
virtual void OnDisconnect (GNE::Connection &conn)
 The GNE::Connection has been disconnected.
virtual void OnExit (GNE::Connection &conn)
 called when the remote has gracefully closed the connection
virtual void OnNewConn (GNE::SyncConnection &conn)
 called when the server receives a new connection
virtual void OnConnect (GNE::SyncConnection &conn)
 called when the client is connected to the server
virtual void OnReceive (GNE::Connection &conn)
 one or more GNE::Packets have been received
virtual void OnFailure (GNE::Connection &conn, const GNE::Error &error)
 A fatal error has occurred in the connection.
virtual void OnError (GNE::Connection &conn, const GNE::Error &error)
 A non-fatal error has occurred in the connection.
virtual void OnConnectFailure (GNE::Connection &conn, const GNE::Error &error)
 A connection failed before or during the onConnect event.

Protected Types

typedef std::map< std::string,
GNE::Connection * >::iterator 
ConnectionIterator

Protected Member Functions

virtual ~NetMgr ()
void AddConnection (GNE::Connection *connection)
 Add a new GNE::Connection to the list of existing connections.
void RemoveConnection (GNE::Connection *connection)
 Remove an existing GNE::Connection from the list of connections.

Protected Attributes

bool mInitialized
 has the network been inititialed yet?
bool mIsServer
 are we a server?
std::map< std::string,
GNE::Connection * > 
mConnections
 A map of network address strings to Connections.
GNE::Mutex mMutex


Detailed Description

This class is used as the base of all networking applications.

It handles creating a server or a client and provides a convenient place to implement application-specific functionality.

It can be used as-is, but it's anticipated that the end-user will derive from this class and override the virtual methods as needed.

To use this class, create an instance and call InitializeGame(); this will setup the internals of the network. Then call either SetupServer() or SetupClient() to start the networking. To end the networking, just call the Shutdown() method.

To pass data packets through the network, supply a GNE::Packet to SendPacketToAll(). A custom GNE::Packet must be registered with GNE after InitilizeGame() using:

 GNE::PacketParser::defaultRegisterPacket<MyCustomPacket>(); 

Member Typedef Documentation

typedef std::map<std::string, GNE::Connection*>::iterator dtNet::NetMgr::ConnectionIterator [protected]


Constructor & Destructor Documentation

NetMgr::NetMgr (  ) 

NetMgr::~NetMgr (  )  [protected, virtual]


Member Function Documentation

void NetMgr::InitializeGame ( const std::string &  gameName,
int  gameVersion,
const std::string &  logFile 
)

Initialize the networking and game environment.

Initialize the network and setup the game parameters.

This method must be called before any other NetMgr methods. The supplied game name and game version are used to during the connection process to verify if the client/server match.

Parameters:
gameName : the name of the network game
gameVersion : the version number of the game
logFile : a filename to log networking debug information

bool NetMgr::SetupServer ( int  portNum  ) 

Setup and create a server.

Create and start the server network.

Parameters:
portNum : the socket port number to listen to
Returns:
true if successful, false otherwise

bool NetMgr::SetupClient ( const std::string &  host,
int  portNum 
)

Setup and create a client to connect to the server.

Create a client and try to connect to the supplied host name.

Parameters:
host : the name of the host to connect to
portNum : the socket port number to use
Returns:
true if successful, false otherwise

void NetMgr::Shutdown ( void   ) 

Shutdown the networking.

Perform a graceful shutdown of the network.

This will attempt to disconnect all currently active connections.

void NetMgr::SendPacket ( const std::string &  address,
GNE::Packet &  packet 
)

Send a packet to the given address.

Sends the supplied packet to all connections in the list.

If this is a server, it will send the packet to all existing connections. If this is a client, typically there will be only one connection: to the server.

Parameters:
address : the string representation of the address to send to or "all"
packet : the GNE::Packet to send to the world
See also:
AddConnection()

int dtNet::NetMgr::GetNumConnections (  )  const [inline]

Get the number of connections to the network.

bool dtNet::NetMgr::GetIsServer (  )  const [inline]

Is this instance setup as a server?

void NetMgr::OnListenSuccess (  )  [virtual]

virtual methods

callback to signal a connection is successful

void NetMgr::OnListenFailure ( const GNE::Error &  error,
const GNE::Address &  from,
const GNE::ConnectionListener::sptr &  listener 
) [virtual]

callback to signal the connection to the socket failed

Parameters:
error : The GNE:Error describing the failure
from : The GNE::Address of the problem
listener The GNE::ConnectionListen who triggered this failure

void NetMgr::OnDisconnect ( GNE::Connection &  conn  )  [virtual]

The GNE::Connection has been disconnected.

Parameters:
conn : the GNE::Connection that was just disconnected

void NetMgr::OnExit ( GNE::Connection &  conn  )  [virtual]

called when the remote has gracefully closed the connection

Parameters:
conn : the GNE::Connetion that just exited

void NetMgr::OnNewConn ( GNE::SyncConnection &  conn  )  [virtual]

called when the server receives a new connection

Typically, this new connection gets stored in a list for future reference.

Parameters:
conn : the new connection
See also:
AddConnection()

void NetMgr::OnConnect ( GNE::SyncConnection &  conn  )  [virtual]

called when the client is connected to the server

Typically, this connection gets stored in a list for future reference.

Parameters:
conn : the new connection
See also:
AddConnection()

void NetMgr::OnReceive ( GNE::Connection &  conn  )  [virtual]

one or more GNE::Packets have been received

Parameters:
: conn : the GNE::Connection which contains the GNE::Packets to be read

void NetMgr::OnFailure ( GNE::Connection &  conn,
const GNE::Error &  error 
) [virtual]

A fatal error has occurred in the connection.

Parameters:
conn,: The GNE::Connection that caused the failure
error : The error describing the failure

void NetMgr::OnError ( GNE::Connection &  conn,
const GNE::Error &  error 
) [virtual]

A non-fatal error has occurred in the connection.

Parameters:
conn,: The GNE::Connection that caused the failure
error : The error describing the failure

void NetMgr::OnConnectFailure ( GNE::Connection &  conn,
const GNE::Error &  error 
) [virtual]

A connection failed before or during the onConnect event.

Parameters:
conn,: The GNE::Connection that caused the failure
error : The error describing the failure

void NetMgr::AddConnection ( GNE::Connection *  connection  )  [protected]

Add a new GNE::Connection to the list of existing connections.

Internal method used to store the connection in a map.

Typically gets called from OnConnect() and OnNewConn() to save the connection for later use.

Parameters:
connection : the connection to add to the list

void NetMgr::RemoveConnection ( GNE::Connection *  connection  )  [protected]

Remove an existing GNE::Connection from the list of connections.

Internal method used to remove an existing connection from the list.

If the supplied connection is not in the list, it won't be removed.

Parameters:
connection : the connection to remove from the list


Member Data Documentation

bool dtNet::NetMgr::mInitialized [protected]

has the network been inititialed yet?

bool dtNet::NetMgr::mIsServer [protected]

are we a server?

std::map<std::string, GNE::Connection*> dtNet::NetMgr::mConnections [protected]

A map of network address strings to Connections.

GNE::Mutex dtNet::NetMgr::mMutex [protected]


http://www.delta3d.org
2.0.0 generated 14 Feb 2008