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

ConnectionReader Class Reference

This is an abstract base class for a family of classes that listen for activity on a socket and respond to it, for instance by reading a datagram and serving it (or queueing it up for later service). More...

#include <connectionReader.h>

Inheritance diagram for ConnectionReader:

ConnectionListener QueuedConnectionReader RecentConnectionReader QueuedConnectionListener List of all members.

Public Member Functions

 ConnectionReader (ConnectionManager *manager, int num_threads)
 Creates a new ConnectionReader with the indicated number of threads to handle requests.

virtual ~ConnectionReader ()
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.


Protected Member Functions

virtual void receive_datagram (const NetDatagram &datagram)=0
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)

Protected Attributes

ConnectionManager_manager

Private Types

typedef pvector< PRThread * > Threads
typedef pvector< PRPollDesc > Poll
typedef pvector< SocketInfo * > Sockets

Private Member Functions

void thread_run ()
 This is the actual executing function for each thread.

SocketInfoget_next_available_socket (PRIntervalTime timeout, PRInt32 current_thread_index)
 Polls the known connections for activity and returns the next one known to have activity, or NULL if no activity is detected within the timeout interval.

void rebuild_poll_list ()
 Rebuilds the _poll and _polled_sockets arrays based on the sockets that are currently available for polling.


Static Private Member Functions

void thread_start (void *data)
 The static wrapper around the thread's executing function.


Private Attributes

bool _raw_mode
bool _shutdown
Threads _threads
PRLock * _startup_mutex
bool _polling
Poll _poll
Sockets _polled_sockets
int _next_index
int _num_results
PRLock * _select_mutex
PRInt32 _currently_polling_thread
Sockets _sockets
Sockets _removed_sockets
bool _reexamine_sockets
PRLock * _sockets_mutex

Friends

class ConnectionManager

Detailed Description

This is an abstract base class for a family of classes that listen for activity on a socket and respond to it, for instance by reading a datagram and serving it (or queueing it up for later service).

A ConnectionReader may define an arbitrary number of threads (at least one) to process datagrams coming in from an arbitrary number of sockets that it is monitoring. The number of threads is specified at construction time and cannot be changed, but the set of sockets that is to be monitored may be constantly modified at will.

This is an abstract class because it doesn't define how to process each received datagram. See QueuedConnectionReader. Also note that ConnectionListener derives from this class, extending it to accept connections on a rendezvous socket rather than read datagrams.

Definition at line 83 of file connectionReader.h.


Member Typedef Documentation

typedef pvector<PRPollDesc> ConnectionReader::Poll [private]
 

Definition at line 159 of file connectionReader.h.

typedef pvector<SocketInfo *> ConnectionReader::Sockets [private]
 

Definition at line 160 of file connectionReader.h.

typedef pvector<PRThread *> ConnectionReader::Threads [private]
 

Definition at line 152 of file connectionReader.h.


Constructor & Destructor Documentation

ConnectionReader::ConnectionReader ConnectionManager manager,
int  num_threads
 

Creates a new ConnectionReader with the indicated number of threads to handle requests.

If num_threads is 0, the sockets will only be read by polling, during an explicit poll() call. (QueuedConnectionReader will do this automatically.)

Definition at line 97 of file connectionReader.cxx.

ConnectionReader::~ConnectionReader  )  [virtual]
 

Definition at line 146 of file connectionReader.cxx.


Member Function Documentation

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

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 _reexamine_sockets, _removed_sockets, _sockets, and _sockets_mutex.

void ConnectionReader::clear_manager  )  [protected]
 

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.

void ConnectionReader::finish_socket SocketInfo sinfo  )  [protected]
 

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.

ConnectionManager * ConnectionReader::get_manager  )  const
 

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

Definition at line 364 of file connectionReader.cxx.

References _threads.

ConnectionReader::SocketInfo * ConnectionReader::get_next_available_socket PRIntervalTime  timeout,
PRInt32  current_thread_index
[private]
 

Polls the known connections for activity and returns the next one known to have activity, or NULL if no activity is detected within the timeout interval.

This function may block indefinitely if it is being called by multiple threads; if there are no other threads, it may block only if timeout != PR_INTERVAL_NO_WAIT.

Definition at line 967 of file connectionReader.cxx.

References _polled_sockets.

int ConnectionReader::get_num_threads  )  const
 

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
 

Returns the current setting of the raw mode flag.

See set_raw_mode().

Definition at line 428 of file connectionReader.cxx.

bool ConnectionReader::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.

(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 _manager.

bool ConnectionReader::is_polling  )  const
 

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  ) 
 

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 _raw_mode.

Referenced by QueuedConnectionListener::~QueuedConnectionListener().

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

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 _manager, ConnectionManager::connection_reset(), NULL, and pprerror().

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

Definition at line 640 of file connectionReader.cxx.

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

Definition at line 567 of file connectionReader.cxx.

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

Definition at line 839 of file connectionReader.cxx.

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

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

Definition at line 785 of file connectionReader.cxx.

void ConnectionReader::rebuild_poll_list  )  [private]
 

Rebuilds the _poll and _polled_sockets arrays based on the sockets that are currently available for polling.

Definition at line 1089 of file connectionReader.cxx.

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

Implemented in ConnectionListener, QueuedConnectionReader, and RecentConnectionReader.

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

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 ConnectionReader::set_raw_mode bool  mode  ) 
 

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]
 

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 ConnectionReader::thread_run  )  [private]
 

This is the actual executing function for each thread.

Definition at line 919 of file connectionReader.cxx.

void ConnectionReader::thread_start void *  data  )  [static, private]
 

The static wrapper around the thread's executing function.

This must be a static member function because it is passed through the C interface to PR_CreateThread().

Definition at line 904 of file connectionReader.cxx.


Friends And Related Function Documentation

friend class ConnectionManager [friend]
 

Definition at line 187 of file connectionReader.h.


Member Data Documentation

PRInt32 ConnectionReader::_currently_polling_thread [private]
 

Definition at line 172 of file connectionReader.h.

ConnectionManager* ConnectionReader::_manager [protected]
 

Definition at line 146 of file connectionReader.h.

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

int ConnectionReader::_next_index [private]
 

Definition at line 163 of file connectionReader.h.

Referenced by process_raw_incoming_tcp_data().

int ConnectionReader::_num_results [private]
 

Definition at line 164 of file connectionReader.h.

Referenced by process_raw_incoming_tcp_data().

Poll ConnectionReader::_poll [private]
 

Definition at line 161 of file connectionReader.h.

Referenced by process_raw_incoming_tcp_data().

Sockets ConnectionReader::_polled_sockets [private]
 

Definition at line 162 of file connectionReader.h.

Referenced by get_next_available_socket(), and process_raw_incoming_tcp_data().

bool ConnectionReader::_polling [private]
 

Definition at line 155 of file connectionReader.h.

bool ConnectionReader::_raw_mode [private]
 

Definition at line 149 of file connectionReader.h.

Referenced by poll().

bool ConnectionReader::_reexamine_sockets [private]
 

Definition at line 182 of file connectionReader.h.

Referenced by add_connection(), and process_raw_incoming_tcp_data().

Sockets ConnectionReader::_removed_sockets [private]
 

Definition at line 179 of file connectionReader.h.

Referenced by add_connection().

PRLock* ConnectionReader::_select_mutex [private]
 

Definition at line 167 of file connectionReader.h.

bool ConnectionReader::_shutdown [private]
 

Definition at line 150 of file connectionReader.h.

Referenced by process_raw_incoming_tcp_data().

Sockets ConnectionReader::_sockets [private]
 

Definition at line 176 of file connectionReader.h.

Referenced by add_connection().

PRLock* ConnectionReader::_sockets_mutex [private]
 

Definition at line 184 of file connectionReader.h.

Referenced by add_connection().

PRLock* ConnectionReader::_startup_mutex [private]
 

Definition at line 154 of file connectionReader.h.

Threads ConnectionReader::_threads [private]
 

Definition at line 153 of file connectionReader.h.

Referenced by ConnectionWriter::ConnectionWriter(), and get_manager().


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