Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

QueuedConnectionReader Class Reference

This flavor of ConnectionReader will read from its sockets and queue up all of the datagrams read for later receipt by the client code. More...

#include <queuedConnectionReader.h>

Inheritance diagram for QueuedConnectionReader:

ConnectionReader QueuedReturn< NetDatagram > List of all members.

Public Member Functions

 QueuedConnectionReader (ConnectionManager *manager, int num_threads)
virtual ~QueuedConnectionReader ()
bool data_available ()
 Returns true if a datagram is available on the queue; call get_data() to extract the datagram.

bool get_data (NetDatagram &result)
 If a previous call to data_available() returned true, this function will return the datagram that has become available.

bool get_data (Datagram &result)
 This flavor of QueuedConnectionReader::get_data(), works like the other, except that it only fills a Datagram object, not a NetDatagram object.

void start_delay (double min_delay, double max_delay)
 Enables a simulated network latency.

void stop_delay ()
 Disables the simulated network latency started by a previous call to start_delay().

bool add_connection (const PointerTo< Connection > &connection)
 Adds a new socket to the list of sockets the ConnectionReader will monitor.

bool remove_connection (const PointerTo< Connection > &connection)
 Removes a socket from the list of sockets being monitored.

bool is_connection_ok (const PointerTo< Connection > &connection)
 Returns true if the indicated connection has been added to the ConnectionReader and is being monitored properly, false if it is not known, or if there was some error condition detected on the connection.

void poll ()
 Explicitly polls the available sockets to see if any of them have any noise.

ConnectionManagerget_manager () const
 Returns a pointer to the ConnectionManager object that serves this ConnectionReader.

bool is_polling () const
 Returns true if the reader is a polling reader, i.e.

int get_num_threads () const
 Returns the number of threads the ConnectionReader has been created with.

void set_raw_mode (bool mode)
 Sets the ConnectionReader into raw mode (or turns off raw mode).

bool get_raw_mode () const
 Returns the current setting of the raw mode flag.

void set_max_queue_size (int max_size)
int get_max_queue_size () const
int get_current_queue_size () const

Protected Member Functions

virtual void receive_datagram (const NetDatagram &datagram)
 An internal function called by ConnectionReader() when a new datagram has become available.

void shutdown ()
 Terminates all threads cleanly.

void clear_manager ()
 This should normally only be called when the associated ConnectionManager destructs.

void finish_socket (SocketInfo *sinfo)
 To be called when a socket has been fully read and is ready for polling for additional data.

virtual void process_incoming_data (SocketInfo *sinfo)
 This is run within a thread when the call to PR_Poll() indicates there is data available on a socket.

virtual void process_incoming_udp_data (SocketInfo *sinfo)
virtual void process_incoming_tcp_data (SocketInfo *sinfo)
virtual void process_raw_incoming_udp_data (SocketInfo *sinfo)
virtual void process_raw_incoming_tcp_data (SocketInfo *sinfo)
bool thing_available () const
bool get_thing (NetDatagram &thing)
bool enqueue_thing (const NetDatagram &thing)
bool enqueue_unique_thing (const NetDatagram &thing)

Protected Attributes

ConnectionManager_manager

Private Types

typedef pdeque< DelayedDatagramDelayed

Private Member Functions

void get_delayed ()
 Checks the delayed queue for any now available datagrams, and adds them to the normal queue if they are available.

void delay_datagram (const NetDatagram &datagram)
 Adds the datagram to the delay queue for a random time interval.


Private Attributes

PRLock * _dd_mutex
Delayed _delayed
bool _delay_active
double _min_delay
double _delay_variance

Detailed Description

This flavor of ConnectionReader will read from its sockets and queue up all of the datagrams read for later receipt by the client code.

This class is useful for client code that doesn't want to deal with threading and is willing to poll for datagrams at its convenience.

Definition at line 61 of file queuedConnectionReader.h.


Member Typedef Documentation

typedef pdeque<DelayedDatagram> QueuedConnectionReader::Delayed [private]
 

Definition at line 90 of file queuedConnectionReader.h.


Constructor & Destructor Documentation

QueuedConnectionReader::QueuedConnectionReader ConnectionManager manager,
int  num_threads
 

Definition at line 34 of file queuedConnectionReader.cxx.

QueuedConnectionReader::~QueuedConnectionReader  )  [virtual]
 

Definition at line 53 of file queuedConnectionReader.cxx.


Member Function Documentation

bool ConnectionReader::add_connection const PointerTo< Connection > &  connection  )  [inherited]
 

Adds a new socket to the list of sockets the ConnectionReader will monitor.

A datagram that comes in on any of the monitored sockets will be reported. In the case of a ConnectionListener, this adds a new rendezvous socket; any activity on any of the monitored sockets will cause a connection to be accepted.

The return value is true if the connection was added, false if it was already there.

add_connection() is thread-safe, and may be called at will by any thread.

Definition at line 210 of file connectionReader.cxx.

References ConnectionReader::_reexamine_sockets, ConnectionReader::_removed_sockets, ConnectionReader::_sockets, and ConnectionReader::_sockets_mutex.

void ConnectionReader::clear_manager  )  [protected, inherited]
 

This should normally only be called when the associated ConnectionManager destructs.

It resets the ConnectionManager pointer to NULL so we don't have a floating pointer. This makes the ConnectionReader invalid; presumably it also will be destructed momentarily.

Definition at line 487 of file connectionReader.cxx.

bool QueuedConnectionReader::data_available  ) 
 

Returns true if a datagram is available on the queue; call get_data() to extract the datagram.

Definition at line 74 of file queuedConnectionReader.cxx.

References QueuedReturn< NetDatagram >::get_thing().

void QueuedConnectionReader::delay_datagram const NetDatagram datagram  )  [private]
 

Adds the datagram to the delay queue for a random time interval.

Definition at line 277 of file queuedConnectionReader.cxx.

bool QueuedReturn< NetDatagram >::enqueue_thing const NetDatagram thing  )  [protected, inherited]
 

Adds a new thing to the queue for later retrieval.

Returns true if successful, false if the queue is full (i.e. has reached _max_queue_size).

Referenced by stop_delay().

bool QueuedReturn< NetDatagram >::enqueue_unique_thing const NetDatagram thing  )  [protected, inherited]
 

The same as enqueue_thing(), except the queue is first checked that it doesn't already have something like thing.

The return value is true if the enqueue operation was successful, false if the queue was full or the thing was already on the queue.

void ConnectionReader::finish_socket SocketInfo *  sinfo  )  [protected, inherited]
 

To be called when a socket has been fully read and is ready for polling for additional data.

Definition at line 502 of file connectionReader.cxx.

int QueuedReturn< NetDatagram >::get_current_queue_size  )  const [inherited]
 

Returns the current number of things in the queue.

bool QueuedConnectionReader::get_data Datagram result  ) 
 

This flavor of QueuedConnectionReader::get_data(), works like the other, except that it only fills a Datagram object, not a NetDatagram object.

This means that the Datagram cannot be queried for its source Connection and/or NetAddress, but it is useful in all other respects.

Definition at line 130 of file queuedConnectionReader.cxx.

bool QueuedConnectionReader::get_data NetDatagram result  ) 
 

If a previous call to data_available() returned true, this function will return the datagram that has become available.

The return value is true if a datagram was successfully returned, or false if there was, in fact, no datagram available. (This may happen if there are multiple threads accessing the QueuedConnectionReader).

Definition at line 107 of file queuedConnectionReader.cxx.

Referenced by HTTPClient::read().

void QueuedConnectionReader::get_delayed  )  [private]
 

Checks the delayed queue for any now available datagrams, and adds them to the normal queue if they are available.

Definition at line 246 of file queuedConnectionReader.cxx.

ConnectionManager * ConnectionReader::get_manager  )  const [inherited]
 

Returns a pointer to the ConnectionManager object that serves this ConnectionReader.

Definition at line 364 of file connectionReader.cxx.

References ConnectionReader::_threads.

int QueuedReturn< NetDatagram >::get_max_queue_size  )  const [inherited]
 

Returns the maximum size the queue is allowed to grow to.

See set_max_queue_size().

int ConnectionReader::get_num_threads  )  const [inherited]
 

Returns the number of threads the ConnectionReader has been created with.

Definition at line 394 of file connectionReader.cxx.

bool ConnectionReader::get_raw_mode  )  const [inherited]
 

Returns the current setting of the raw mode flag.

See set_raw_mode().

Definition at line 428 of file connectionReader.cxx.

bool QueuedReturn< NetDatagram >::get_thing NetDatagram thing  )  [protected, inherited]
 

If a previous call to thing_available() returned true, this function will return the thing that has become available.

The return value is true if a thing was successfully returned, or false if there was, in fact, no thing available. (This may happen if there are multiple threads accessing the QueuedReturn).

Referenced by data_available().

bool ConnectionReader::is_connection_ok const PointerTo< Connection > &  connection  )  [inherited]
 

Returns true if the indicated connection has been added to the ConnectionReader and is being monitored properly, false if it is not known, or if there was some error condition detected on the connection.

(If there was an error condition, normally the ConnectionManager would have been informed and closed the connection.)

Definition at line 296 of file connectionReader.cxx.

References ConnectionReader::_manager.

bool ConnectionReader::is_polling  )  const [inherited]
 

Returns true if the reader is a polling reader, i.e.

it has no threads.

Definition at line 379 of file connectionReader.cxx.

void ConnectionReader::poll  )  [inherited]
 

Explicitly polls the available sockets to see if any of them have any noise.

This function does nothing unless this is a polling-type ConnectionReader, i.e. it was created with zero threads (and is_polling() will return true).

It is not necessary to call this explicitly for a QueuedConnectionReader.

Definition at line 342 of file connectionReader.cxx.

References ConnectionReader::_raw_mode.

Referenced by QueuedConnectionListener::~QueuedConnectionListener().

void ConnectionReader::process_incoming_data SocketInfo *  sinfo  )  [protected, virtual, inherited]
 

This is run within a thread when the call to PR_Poll() indicates there is data available on a socket.

Reimplemented in ConnectionListener.

Definition at line 543 of file connectionReader.cxx.

References ConnectionReader::_manager, ConnectionManager::connection_reset(), NULL, and pprerror().

void ConnectionReader::process_incoming_tcp_data SocketInfo *  sinfo  )  [protected, virtual, inherited]
 

Definition at line 640 of file connectionReader.cxx.

void ConnectionReader::process_incoming_udp_data SocketInfo *  sinfo  )  [protected, virtual, inherited]
 

Definition at line 567 of file connectionReader.cxx.

void ConnectionReader::process_raw_incoming_tcp_data SocketInfo *  sinfo  )  [protected, virtual, inherited]
 

Definition at line 839 of file connectionReader.cxx.

References ConnectionReader::_next_index, ConnectionReader::_num_results, ConnectionReader::_poll, ConnectionReader::_polled_sockets, ConnectionReader::_reexamine_sockets, ConnectionReader::_shutdown, and NULL.

void ConnectionReader::process_raw_incoming_udp_data SocketInfo *  sinfo  )  [protected, virtual, inherited]
 

Definition at line 785 of file connectionReader.cxx.

void QueuedConnectionReader::receive_datagram const NetDatagram datagram  )  [protected, virtual]
 

An internal function called by ConnectionReader() when a new datagram has become available.

The QueuedConnectionReader simply queues it up for later retrieval by get_data().

Implements ConnectionReader.

Definition at line 154 of file queuedConnectionReader.cxx.

References _delay_active, and _delay_variance.

bool ConnectionReader::remove_connection const PointerTo< Connection > &  connection  )  [inherited]
 

Removes a socket from the list of sockets being monitored.

Returns true if the socket was correctly removed, false if it was not on the list in the first place.

remove_connection() is thread-safe, and may be called at will by any thread.

Definition at line 253 of file connectionReader.cxx.

void QueuedReturn< NetDatagram >::set_max_queue_size int  max_size  )  [inherited]
 

Sets the maximum size the queue is allowed to grow to.

This is primarily for a sanity check; this is a limit beyond which we can assume something bad has happened.

It's also a crude check against unfortunate seg faults due to the queue filling up and quietly consuming all available memory.

void ConnectionReader::set_raw_mode bool  mode  )  [inherited]
 

Sets the ConnectionReader into raw mode (or turns off raw mode).

In raw mode, datagram headers are not expected; instead, all the data available on the pipe is treated as a single datagram.

Definition at line 413 of file connectionReader.cxx.

Referenced by HTTPClient::HTTPClient().

void ConnectionReader::shutdown  )  [protected, inherited]
 

Terminates all threads cleanly.

Normally this is only called by the destructor.

Definition at line 443 of file connectionReader.cxx.

Referenced by QueuedConnectionListener::QueuedConnectionListener().

void QueuedConnectionReader::start_delay double  min_delay,
double  max_delay
 

Enables a simulated network latency.

All packets received from this point on will be held for a random interval of least min_delay seconds, and no more than max_delay seconds, before being visible to the data_available()/get_data() interface. It is as if packets suddenly took much longer to arrive.

Definition at line 195 of file queuedConnectionReader.cxx.

References _delayed.

void QueuedConnectionReader::stop_delay  ) 
 

Disables the simulated network latency started by a previous call to start_delay().

Packets will once again be visible as soon as they are received.

Definition at line 216 of file queuedConnectionReader.cxx.

References QueuedConnectionReader::DelayedDatagram::_datagram, _dd_mutex, _delay_active, _delay_variance, _delayed, _min_delay, QueuedConnectionReader::DelayedDatagram::_reveal_time, QueuedReturn< NetDatagram >::enqueue_thing(), ClockObject::get_global_clock(), and ClockObject::get_real_time().

bool QueuedReturn< NetDatagram >::thing_available  )  const [inline, protected, inherited]
 

Returns true if a thing is available on the queue; call get_thing() to extract the thing.


Member Data Documentation

PRLock* QueuedConnectionReader::_dd_mutex [private]
 

Definition at line 89 of file queuedConnectionReader.h.

Referenced by stop_delay().

bool QueuedConnectionReader::_delay_active [private]
 

Definition at line 92 of file queuedConnectionReader.h.

Referenced by receive_datagram(), and stop_delay().

double QueuedConnectionReader::_delay_variance [private]
 

Definition at line 93 of file queuedConnectionReader.h.

Referenced by receive_datagram(), and stop_delay().

Delayed QueuedConnectionReader::_delayed [private]
 

Definition at line 91 of file queuedConnectionReader.h.

Referenced by start_delay(), and stop_delay().

ConnectionManager* ConnectionReader::_manager [protected, inherited]
 

Definition at line 146 of file connectionReader.h.

Referenced by ConnectionReader::SocketInfo::get_socket(), ConnectionReader::is_connection_ok(), ConnectionReader::process_incoming_data(), and ConnectionListener::process_incoming_data().

double QueuedConnectionReader::_min_delay [private]
 

Definition at line 93 of file queuedConnectionReader.h.

Referenced by stop_delay().


The documentation for this class was generated from the following files:
Generated on Fri May 2 00:54:33 2003 for Panda by doxygen1.3