GNE  0.75.0
GNE::ServerConnection Class Reference

A GNE "internal" class. More...

#include <ServerConnection.h>

Inheritance diagram for GNE::ServerConnection:
GNE::Connection GNE::Thread

Public Types

typedef SmartPtr
< ServerConnection
sptr
typedef WeakPtr< ServerConnectionwptr
- Public Types inherited from GNE::Connection
enum  State {
  NeedsInitialization, ReadyToConnect, Connecting, Connected,
  Disconnecting, Disconnected
}
 Represents the states a Connection object can be in. More...
typedef SmartPtr< Connectionsptr
typedef WeakPtr< Connectionwptr
- Public Types inherited from GNE::Thread
enum  ThreadType {
  USER, TIMER, SYSTEM, CONNECTION,
  ALL
}
 An enum for specifying the thread type. More...
typedef SmartPtr< Threadsptr
typedef WeakPtr< Threadwptr

Public Member Functions

virtual ~ServerConnection ()
 Destructs this ServerConnection object.
- Public Member Functions inherited from GNE::Connection
virtual ~Connection ()
 Destructor.
SmartPtr< ConnectionListenergetListener () const
 Returns the currently registered event Listener.
void setListener (const SmartPtr< ConnectionListener > &listener)
 Sets a new event listener.
int getTimeout ()
 Returns the timeout for this connection.
void setTimeout (int ms)
 Sets the timeout for this connection in milliseconds.
PacketStreamstream ()
 Returns the PacketStream associated with this class.
ConnectionStats getStats (int reliable) const
 If stats is enabled, returns Connection stats.
Address getLocalAddress (bool reliable) const
 Returns the local address of this connection.
Address getRemoteAddress (bool reliable) const
 Returns the remote address of this connection.
State getState () const
 Returns the state this Connection is in.
bool isConnected () const
 Returns true if this Connection is active and ready to send/recieve.
void disconnect ()
 Immediately disconnects this socket.
void disconnectSendAll (int waitTime=10000)
 A blocking call that disconnects nicely.
- Public Member Functions inherited from GNE::Thread
 Thread (std::string name=DEF_NAME, int priority=DEF_PRI)
 Creates a new thread, ready to run but not yet running.
std::string getName () const
 Returns the name of this thread.
void join ()
 A blocking call that returns when this thread terminates, or returns immediately if the thread has terminated.
bool hasStarted () const
 Returns true if this Thread has ever been started.
bool isRunning () const
 Determine the running state of the thread.
void start ()
 Starts this thread running.
int getPriority () const
 Returns the priority of this class.

Static Public Member Functions

static sptr create (const ConnectionParams &p, NLsocket rsocket, const SmartPtr< ServerConnectionListener > &creator)
 Intializes this class.
- Static Public Member Functions inherited from GNE::Connection
static void disconnectAll ()
 Disconnects all active connections in the GNE system.
- Static Public Member Functions inherited from GNE::Thread
static sptr currentThread ()
 Returns the Thread object that represents this calling thread.
static void sleep (int ms)
 The currently running thread sleeps for the time given in milliseconds.
static void yield ()
 Makes a request to the operating system to give up the remainder of this thread's timeslice to another appropriate thread.
static bool waitForAllThreads (int ms)
 This method will wait for all threads that have a type != SYSTEM.
static void requestAllShutdown (ThreadType threadType)
 Calls the shutDown method of all running threads of the given type.

Protected Member Functions

 ServerConnection ()
 We need information gained from setThisPointer to initialize, so the real work is done in the init method, which should be called right after constructon and setThisPointer has been called.
void init (const ConnectionParams &p, NLsocket rsocket, const SmartPtr< ServerConnectionListener > &creator)
void run ()
 This thread performs the connection process.
void shutDown ()
 Makes a request that this thread should shutdown.
- Protected Member Functions inherited from GNE::Connection
 Connection ()
 Initializes this class with a NULL listener, which must be set before any operations are performed on this instance.
void setThisPointer (const wptr &weakThis)
 This method must be called and set to a weak pointer referencing this object before the end of the static create function of the child class.
void addHeader (Buffer &raw)
 A utility function for ServerConnection and ClientConnection to add the standard header onto the connection packets.
void addVersions (Buffer &raw)
 A utility function for ServerConnection and ClientConnection to add the version information to a packet.
void checkHeader (Buffer &raw, ProtocolViolation::ViolationType t=ProtocolViolation::OtherViolation)
 A utility function for ServerConnection and ClientConnection to check to verify the GNE header of the connecting packets.
void checkVersions (Buffer &raw)
 A utility function for ServerConnection and ClientConnection to check the GNE version, game name, and user versions in the Buffer.
void onReceive ()
void finishedInit ()
 The Connection has finished initializing and the state now can change to ReadyToConnect.
void startConnecting ()
 Moves from ReadyToConnect to Connecting state.
void startThreads ()
 Does the work needed to start up the PacketStream and EventThread threads.
void finishedConnecting ()
 The connecting has just finished and the state needs to be changed.
void reg (bool reliable, bool unreliable)
 Register this Connection object's sockets with the ConnectionEventGenerator.
void unreg (bool reliable, bool unreliable)
 Unregistered the sockets which are specified by passing true to the appropriate parameter.
- Protected Member Functions inherited from GNE::Thread
void setThisPointer (const wptr &thisPtr)
 IMPORTANT: call this method in your static create function.
sptr getThisPointer () const
 Returns a SmartPtr to this object.
void setType (ThreadType newType)
 Sets this Thread's type.

Friends

class boost::weak_ptr< Thread >
class boost::shared_ptr< Thread >
class boost::weak_ptr< Connection >
class boost::shared_ptr< Connection >

Additional Inherited Members

- Static Public Attributes inherited from GNE::Thread
static const int DEF_PRI = 0
 The default priority of a thread.
static const std::string DEF_NAME = "Thread"
 The default name for a thread.
static const int LOW_PRI = -1
 A lowered priority for a thread.
static const int LOWER_PRI = -2
 A lower priority for a thread than LOW_PRI.
static const int HIGH_PRI = 1
 A "boosted priority" for a thread.
static const int HIGHER_PRI = 2
 Even higher priority thread than HIGH_PRI.
- Protected Attributes inherited from GNE::Connection
wptr this_
 A weak pointer to this own object.
SocketPair sockets
 The pair of sockets.
PacketStream::sptr ps
 The PacketStream associated with this Connection.
- Protected Attributes inherited from GNE::Thread
volatile bool shutdown
 This variable is set to true when this thread should exit.

Detailed Description

A GNE "internal" class.

Users will use this class, but probably only as its base class – a Connection. This class is created by ServerConnectionListener when incoming connections are comming in.

The fact that a ServerConnection is a Thread is an implementation detail and should not be used outside of this class's implementation. Originally it was private, but this presented a compatibility issue with Boost 1.36. Long-term, the Thread will be encapsulated as a private object, rather than inheritance abuse.

Constructor & Destructor Documentation

GNE::ServerConnection::~ServerConnection ( )
virtual

Destructs this ServerConnection object.

The user need not worry about the fact that ServerConnection is a thread (in the sense that the user never need to call detach or join), to do a proper cleanup.

Member Function Documentation

ServerConnection::sptr GNE::ServerConnection::create ( const ConnectionParams p,
NLsocket  rsocket,
const SmartPtr< ServerConnectionListener > &  creator 
)
static

Intializes this class.

Note that you won't create a ServerConnection directly. The ServerConnectionListener does that for you.

Parameters
rsocket2the reliable socket received from the accept command.
creatorthe ServerConnectionListener that created us, so that we may call its onListenFailure event. This strong pointer will be released after the connection finished, as to not worry about cycles.
See Also
ServerConnectionListener
void GNE::ServerConnection::run ( )
protectedvirtual

This thread performs the connection process.

If an error occurs:

Before onNewConn: ServerConnectionListener::onListenFailure() is called.

During onNewConn: Only onNewConn is called and is reponsible for catching the exception and cleaning up anything it has done. onDisconnect will NOT be called, but onListenFailure will be.

After onNewConn: onFailure then onDisconnect.

After onNewConn is called successfully, then ServerConnectionListener::onListenSuccess is called.

Todo:
better test GNE shutting down while connection is being made.
Bug:
The PacketFeeder is set after onNewConn, so it is possible that if the user sets a different PacketFeeder right after connecting, it might "get lost" and if set during onNewConn, is definitely lost. I do provide a check do this is only a problem if a feeder is set in the ConnectionParams AND in onNewConn.

Implements GNE::Thread.

void GNE::ServerConnection::shutDown ( )
protectedvirtual

Makes a request that this thread should shutdown.

Tells this thread to shutdown, if it is in an infinite loop. You will probably want to call join right after calling this to wait for the shutdown to complete which is dependent on the thread you are shutting down.

This function is virtual if the thread needs any additional actions to notify itself to shutdown, for example if it is waiting for some event on a ConditionVariable.

You will want to call this function from the override to make sure that shutdown is set to true.

This function is safe to call multiple times, but you cannot undo a shutdown once it has been called.

Reimplemented from GNE::Thread.


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