GNE  0.75.0
GNE::EventThread Class Reference

Internal class used by GNE to manage Connection events. More...

#include <EventThread.h>

Inheritance diagram for GNE::EventThread:
GNE::Thread

Public Types

typedef SmartPtr< EventThreadsptr
typedef WeakPtr< EventThreadwptr
- 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

SmartPtr< ConnectionListenergetListener () const
 Use GNE::Connection::getListener.
void setListener (const SmartPtr< ConnectionListener > &listener)
 Use GNE::Connection::setListener.
int getTimeout () const
 Use GNE::Connection::getTimeout.
void setTimeout (int ms)
 Use GNE::Connection::setTimeout.
void onDisconnect ()
 For more information about these events, see ConnectionListener.
void onExit ()
 For more information about these events, see ConnectionListener.
void onFailure (const Error &error)
 For more information about these events, see ConnectionListener.
void onError (const Error &error)
 For more information about these events, see ConnectionListener.
void onReceive ()
 For more information about these events, see ConnectionListener.
void shutDown ()
 Overrides Thread::shutDown so that the daemon thread will be woken up since it might be waiting on a ConditionVariable.
- 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 SmartPtr< Connection > &conn)
 Initializes this class as a event thread for a listener.
- 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

 EventThread (const SmartPtr< Connection > &conn)
void run ()
 This thread serializes events for a Connection.
- 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.

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::Thread
volatile bool shutdown
 This variable is set to true when this thread should exit.

Detailed Description

Internal class used by GNE to manage Connection events.

Each Connection has an EventThread. This is used internally by the Connection class to act as a proxy to dispatch events to the ConnectionListener. Because of this, only one event per Connection will be active at any one time. Another appropriate name that might describe this would be an EventQueue. But to improve efficency and stability, some events may be reordered or combined. Remember that onReceive means 1 or more packets have been received. If multiple onReceive or onDoneWriting occur, they will probably be combined. Error events will always have first priority, and if the error leads to a disconnect, pending events after that will not be called (except for onDisconnect).

EventThread was created to solve several things:

  • Complexities due to multiple threads calling the ConnectionListener at the same time.
  • Serialized events and all events executing in the same thread is easy to control and eliminates lots of previous ways for syncronization errors to creep in.
  • Events taking a while to execute or block (although this is an error), won't stop the rest of GNE from functioning, but will only stop a single Connection.
  • Multiple event threads take better advantage of multiprocessor machines.

Constructor & Destructor Documentation

GNE::EventThread::EventThread ( const SmartPtr< Connection > &  conn)
protected
See Also
create

Member Function Documentation

EventThread::sptr GNE::EventThread::create ( const SmartPtr< Connection > &  conn)
static

Initializes this class as a event thread for a listener.

The conn pointer is used to call disconnect when an onFailure event is finally processed. This is to assure that disconnect is called from a safe thread that won't lead to deadlock when a failure occurs.

The SmartPtr also keeps the Connection alive if it is still active.

void GNE::EventThread::onDisconnect ( )

For more information about these events, see ConnectionListener.

The processing of an onDisconnect event will be the last, and the thread will essentially stop when onDisconnect completes.

void GNE::EventThread::shutDown ( )
virtual

Overrides Thread::shutDown so that the daemon thread will be woken up since it might be waiting on a ConditionVariable.

Once it shuts down it should not be activated again.

Reimplemented from GNE::Thread.


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