Panda3D
Classes | Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | Friends

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 DatagramGeneratorNet PStatReader QueuedConnectionReader RecentConnectionReader PStatListener QueuedConnectionListener

List of all members.

Classes

class  ReaderThread
class  SocketInfo

Public Member Functions

 ConnectionReader (ConnectionManager *manager, int num_threads, const string &thread_name=string())
 Creates a new ConnectionReader with the indicated number of threads to handle requests.
bool add_connection (Connection *connection)
 Adds a new socket to the list of sockets the ConnectionReader will monitor.
ConnectionManagerget_manager () const
 Returns a pointer to the ConnectionManager object that serves this ConnectionReader.
int get_num_threads () const
 Returns the number of threads the ConnectionReader has been created with.
bool get_raw_mode () const
 Returns the current setting of the raw mode flag.
int get_tcp_header_size () const
 Returns the current setting of TCP header size.
bool is_connection_ok (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.
bool is_polling () const
 Returns true if the reader is a polling reader, i.e.
void poll ()
 Explicitly polls the available sockets to see if any of them have any noise.
bool remove_connection (Connection *connection)
 Removes a socket from the list of sockets being monitored.
void set_raw_mode (bool mode)
 Sets the ConnectionReader into raw mode (or turns off raw mode).
void set_tcp_header_size (int tcp_header_size)
 Sets the header size of TCP packets.
void shutdown ()
 Terminates all threads cleanly.

Protected Types

typedef pvector< SocketInfo * > Sockets

Protected Member Functions

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 flush_read_connection (Connection *connection)
 Attempts to read all the possible data from the indicated connection, which has just delivered a write error (and has therefore already been closed).
virtual bool process_incoming_data (SocketInfo *sinfo)
 This is run within a thread when the call to select() indicates there is data available on a socket.
virtual bool process_incoming_tcp_data (SocketInfo *sinfo)
virtual bool process_incoming_udp_data (SocketInfo *sinfo)
virtual bool process_raw_incoming_tcp_data (SocketInfo *sinfo)
virtual bool process_raw_incoming_udp_data (SocketInfo *sinfo)
virtual void receive_datagram (const NetDatagram &datagram)=0

Protected Attributes

ConnectionManager_manager
Sockets _removed_sockets
Sockets _sockets
LightMutex _sockets_mutex

Friends

class ConnectionManager
class ReaderThread

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 57 of file connectionReader.h.


Constructor & Destructor Documentation

ConnectionReader::ConnectionReader ( ConnectionManager manager,
int  num_threads,
const string &  thread_name = string() 
)

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 100 of file connectionReader.cxx.

References ConnectionManager::add_reader(), and Thread::is_threading_supported().


Member Function Documentation

bool ConnectionReader::add_connection ( 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 194 of file connectionReader.cxx.

Referenced by PStatServer::listen(), MayaToEggServer::poll(), and PStatReader::set_tcp_connection().

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 469 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 480 of file connectionReader.cxx.

Referenced by ConnectionListener::process_incoming_data().

void ConnectionReader::flush_read_connection ( Connection connection) [protected, virtual]

Attempts to read all the possible data from the indicated connection, which has just delivered a write error (and has therefore already been closed).

If the connection is not monitered by this reader, does nothing.

Definition at line 429 of file connectionReader.cxx.

References Socket_fdset::clear(), process_incoming_data(), and remove_connection().

ConnectionManager * ConnectionReader::get_manager ( ) const

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

Definition at line 328 of file connectionReader.cxx.

int ConnectionReader::get_num_threads ( ) const

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

Definition at line 339 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 366 of file connectionReader.cxx.

int ConnectionReader::get_tcp_header_size ( ) const

Returns the current setting of TCP header size.

See set_tcp_header_size().

Definition at line 391 of file connectionReader.cxx.

bool ConnectionReader::is_connection_ok ( 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 256 of file connectionReader.cxx.

bool ConnectionReader::is_polling ( ) const [inline]

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

it has no threads.

Definition at line 23 of file connectionReader.I.

Referenced by DatagramGeneratorNet::get_datagram(), and ConnectionManager::wait_for_readers().

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 290 of file connectionReader.cxx.

References TrueClock::get_global_ptr(), and process_incoming_data().

Referenced by QueuedConnectionReader::data_available(), DatagramGeneratorNet::get_datagram(), QueuedConnectionListener::new_connection_available(), PStatServer::poll(), and MayaToEggServer::poll().

bool ConnectionReader::process_incoming_data ( SocketInfo sinfo) [protected, virtual]

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

Returns true if the data is read successfully, false on failure (for instance, because the connection is closed).

Reimplemented in ConnectionListener.

Definition at line 498 of file connectionReader.cxx.

Referenced by flush_read_connection(), and poll().

bool ConnectionReader::remove_connection ( 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 225 of file connectionReader.cxx.

Referenced by flush_read_connection().

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.

This is similar to set_tcp_header_size(0), except that it also turns off headers for UDP packets.

Definition at line 355 of file connectionReader.cxx.

void ConnectionReader::set_tcp_header_size ( int  tcp_header_size)

Sets the header size of TCP packets.

At the present, legal values for this are 0, 2, or 4; this specifies the number of bytes to use encode the datagram length at the start of each TCP datagram. Sender and receiver must independently agree on this.

Definition at line 380 of file connectionReader.cxx.

void ConnectionReader::shutdown ( )

Terminates all threads cleanly.

Normally this is only called by the destructor, but it may be called explicitly before destruction.

Definition at line 403 of file connectionReader.cxx.


The documentation for this class was generated from the following files:
 All Classes Functions Variables Enumerations