GNE  0.75.0
GNE::ClientConnection Class Reference

A class that can connect to a remote target. More...

#include <ClientConnection.h>

Inheritance diagram for GNE::ClientConnection:
GNE::Connection GNE::Thread GNE::ListServerConnection

Public Types

typedef SmartPtr
< ClientConnection
sptr
typedef WeakPtr< ClientConnectionwptr
- 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

bool open (const Address &dest, const ConnectionParams &p)
 Opens the socket, ready for connect, but does not yet connect.
void connect ()
 Starts connection to the specified target.
Error waitForConnect ()
 Waits indefinitely for the connection attempt started by connect to finish.
- 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.
virtual void shutDown ()
 Makes a request that this thread should shutdown.
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 ()
 Creates a new ClientConnection object, managed by a SmartPtr.
- 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

 ClientConnection ()
 Initializes this ClientConnection.
- 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.
virtual void run ()=0
 This function is the starting point for this thread.

Friends

class SyncConnection
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 class that can connect to a remote target.

The fact that a ClientConnection 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.

Member Function Documentation

void GNE::ClientConnection::connect ( )

Starts connection to the specified target.

This method does not block, and a thread will be started to handle the connection process. onConnect() or onConnectFailure() will be called depending on the outcome of this process. The exception is that if an error occurs during onConnect or it chooses to reject the connection, onConnectFailure will also be called.

You can call waitForConnect after connect to wait until the connection is finished, when onConnect or onConnectFailure will be called. When the called function exits, the thread will stop and waitForConnect will return.

The version number for the library and your own version number will be checked during this phase. If either protocol versions mismatch, onConnectFailure() will be triggered.

Precondition
Connection's state is ReadyToConnect
Connection's state is Connecting (but may change immedately after to another state).
the open method succeeded.
See Also
ConnectionListener::onConnect()
ConnectionListener::onConnectFailure()
ClientConnection::sptr GNE::ClientConnection::create ( )
static

Creates a new ClientConnection object, managed by a SmartPtr.

The listener is passed in the open method.

bool GNE::ClientConnection::open ( const Address dest,
const ConnectionParams p 
)

Opens the socket, ready for connect, but does not yet connect.

All of the relvant parameters for establishing the connection are passed into this function. If there is an error, the function returns true.

Precondition
Connection's state is NeedsInitialization
Postcondition
Connection's state is ReadyToConnect
Parameters
destthe destination address.
Error GNE::ClientConnection::waitForConnect ( )

Waits indefinitely for the connection attempt started by connect to finish.

When the attempt finishes, the result of the connection attempt is returned. If the connection was successful, an error code of Error::NoError is returned.

This is the same Error that is passed to onConnectFailure.

Multiple threads may call this method at any time, even after connection completes (and the return will always be the same). If the connection has already completed this method will return instantly.

Precondition
connect has been called on this object successfully.

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