conexus logo

Conexus::Endpoint Class Reference
[Conexus I/O Endpoints]

This class is the top level object for all subclasses performing I/O (Input/Output) operations. More...

#include <endpoint.h>

Inheritance diagram for Conexus::Endpoint:

Inheritance graph
[legend]

List of all members.

Public Types

typedef ConexusPointer< Endpointpointer
 Class scope smart pointer typedef.

Public Member Functions

virtual ~Endpoint ()
 Destructor does nothing.
virtual void start ()
 Start the endpoint.
virtual void start (bool use_dispatcher)
 Starts the endpoint, and forces it to use a specific dispatcher value possibly overriding the global default.
virtual void stop ()
 Stops any running threads.
virtual bool is_running ()
 True if the endpoint is running with threaded reads.
virtual bool is_timestamping_received_data ()
 True if received data is timestamped.
virtual void set_timestamp_received_data (bool t=true)
 Set to true to timestamp received data.
sigc::signal< void, const Datasignal_data ()
 Signal emitted when data is read.
sigc::signal< void, size_t > signal_data_received_size ()
 Signal emitted when data is received notifying the callback of the number of bytes read.
sigc::signal< void, size_t > signal_data_transmitted_size ()
 Signal emitted when data is transmitted, notifying the callback of the number of bytes transmitted.
sigc::signal< void > signal_opened ()
 Signal emitted when this endpoint is opened.
sigc::signal< void > signal_closed ()
 Signal emitted when this endpoint is closed.
sigc::signal< void, StartStopsignal_start_stop ()
 Signal emitted when this endpoint is started.
sigc::signal< void > signal_eof ()
 Signal emitted when an EOF (End Of File) condition is observed.
sigc::signal< void > signal_disconnected ()
 Signal emitted when a disconnect condition is observed.
const Timeoutdefault_read_timeout () const
 The default timeout used when read(size_t) is called.
void set_default_read_timeout (Timeout t)
 Set the default read timeout A null timeout causes a blocking read to occur, while a non-null timeout causes a non-blocking read to occur with the timeout parameter as specified.
size_t fallback_read_size ()
 When a read operation occurs, the read_data() method relies upon the endpoint's input_available() method.
void set_fallback_read_size (size_t s)
 Sets the default read size used when input_available() returns 0.
const Timeoutdefault_write_timeout () const
 The default timeout used when a write(uint8_t*,size_t) or write(Data) is called.
void set_default_write_timeout (Timeout t)
 Set the default write timeout A null timeout causes a blocking write to occur, while a non-null timeout causes a non-blocking write to occur with the timeout parameter as specified.
bool read_terminate_immediate ()
void set_read_terminate_immediate (bool i=true)
virtual void open ()=0 throw (open_exception)
 Will attempt to open the object without any additional information.
virtual void close (bool force=false)=0 throw (close_exception)
ssize_t write (const void *data, size_t size) throw (write_exception)
 Write data to the endpoint using the default write timeout parameter.
ssize_t write (const void *data, size_t size, Timeout timeout) throw (write_exception)
 Write data to the endpoint using the specified write timeout.
ssize_t write (Endpoint::pointer source, size_t block_size=65535, bool keep_open=true)
 Reads source until a zero-length read, timeout or EOF occurs and writes into this endpoint.
ssize_t write (const Data data) throw (write_exception)
 Write a Conexus::Data object to the endpoint using the default write timeout parameter.
ssize_t write (const Data data, Timeout timeout) throw (write_exception)
 Write a Conexus::Data object to the endpoint using the specified write timeout.
Data read (size_t size=0) throw (read_exception)
 Read into a dynamically created buffer.
Data read (size_t size, Timeout timeout) throw (read_exception)
 Read into a dynamically created buffer.
Data read (Timeout timeout) throw (read_exception)
 Read into a dynamically created buffer.
virtual size_t input_available () throw ()
 Returns the number of bytes available for immediate reading, 0 if no bytes are available, or -1 if the method is invalid for a derived class.
virtual void change_state (long new_state) throw (state_exception)
 It is intended that children will provide their own implementation of this method to take into account the actions necessary for any additional states they may introduce.
long state ()
 Return this endpoint's current numeric state.
void set_close_on_destruct (bool value)
 If set, calls this endpoint's virtual close() method on destruction.
bool close_on_destruct () const
 True if this endpoint's virtual close() method will be called on destruction.
void close_and_reopen (long state=ENDPOINT_UNCHANGED)
 Close the I/O point and reopen to a new (or same state).
bool is_open ()
 True if this endpoint is in an open state.
bool is_closed ()
 True if this endpoint is in a closed state.

Protected Member Functions

 Endpoint (bool close_on_destruct=true)
 Default constructor setting state to 0 and file descriptor to -1 (which should be an invalid fd on just about every POSIX system.
virtual void read_thread_main ()
void read_thread_main_proxy ()
virtual void read_delivery_thread_main ()
void queue_received_data (const Data d)
void emit_received_data ()
virtual void set_state_opened ()
virtual void set_state_closed ()
virtual size_t write_data (const Data data, Timeout timeout)=0 throw (write_exception)
virtual Data read_data (size_t size, Timeout timeout)=0 throw (read_exception)
 This is the primary mechanism of reading from an endpoint and must be reimplemented by children.

Protected Attributes

bool m_use_blocking_reads
bool m_use_blocking_writes
Glib::Thread * m_read_thread
Glib::Thread * m_read_delivery_thread
bool m_read_terminate
bool m_read_terminate_immediate
Timeout m_default_read_timeout
size_t m_fallback_read_size
Timeout m_default_write_timeout
bool m_timestamp
Glib::Mutex m_read_queue_lock
Glib::Cond m_read_delivery_conditional
Glib::Dispatcher * m_dispatcher
bool m_use_dispatcher
sigc::signal< void, const Datam_signal_data
sigc::signal< void, size_t > m_signal_data_received_size
sigc::signal< void, size_t > m_signal_data_transmitted_size
std::queue< Datam_read_queue
bool m_close_on_destruct
long m_state
bool m_readable
bool m_writable
sigc::signal< void > m_signal_opened
sigc::signal< void > m_signal_closed
sigc::signal< void, StartStopm_signal_start_stop
sigc::signal< void > m_signal_eof
sigc::signal< void > m_signal_disconnected

Related Functions

(Note that these are not member functions.)

enum  EndpointState {
  ENDPOINT_NOSTATE = 0, ENDPOINT_CLOSED = 1<<0, ENDPOINT_OPENED = 1<<1, ENDPOINT_LAST_STATE = ENDPOINT_OPENED,
  ENDPOINT_UNCHANGED = ~0
}
enum  StartStop { STOP = 0, START = 1 }


Detailed Description

This class is the top level object for all subclasses performing I/O (Input/Output) operations.

read/write methods are pure virtual methods in this base class and it is the responsibility of all children to provide proper implementations that may go beyond the traditional read(2) and write(2) functions standard in Linux/Unix.

Children are also responsible for maintenance of the file descriptor member m_fd and the integer state value m_state.

Author:
Rick L Vinyard Jr
Examples:

ipv4_tcp_server.cpp, and ipv6_tcp_server.cpp.


Member Typedef Documentation

typedef ConexusPointer<Endpoint> Conexus::Endpoint::pointer


Constructor & Destructor Documentation

Conexus::Endpoint::Endpoint ( bool  close_on_destruct = true  )  [protected]

Default constructor setting state to 0 and file descriptor to -1 (which should be an invalid fd on just about every POSIX system.

This constructor simply sets the values according to the parameters and performs no real actions with the file descriptor.

Conexus::Endpoint::~Endpoint (  )  [virtual]

Destructor does nothing.

It will not call close() upon the file descriptor or perform any other cleanup; these are the responsibility of the children.

References is_running(), and stop().


Member Function Documentation

void Conexus::Endpoint::change_state ( long  new_state  )  throw (state_exception) [virtual]

It is intended that children will provide their own implementation of this method to take into account the actions necessary for any additional states they may introduce.

This method as implemented recognizes the OPEN, CLOSE and UNCHANGED states and calls the virtual open, close (or no call) as is appropriate.

This method does not actually modify the m_state member. It is the responsibility of the called virtual methods open() and close() to actually perform the modification.

Returns:
true if all requested state changes occurred, false otherwise

Reimplemented in Conexus::Socket, and Conexus::NSPR::Socket.

References Conexus::ENDPOINT_CLOSED, Conexus::ENDPOINT_OPENED, and Conexus::ENDPOINT_UNCHANGED.

Referenced by Conexus::NSPR::Socket::change_state(), Conexus::Socket::change_state(), and close_and_reopen().

virtual void Conexus::Endpoint::close ( bool  force = false  )  throw (close_exception) [pure virtual]

void Conexus::Endpoint::close_and_reopen ( long  state = ENDPOINT_UNCHANGED  ) 

bool Conexus::Endpoint::close_on_destruct (  )  const

True if this endpoint's virtual close() method will be called on destruction.

References m_close_on_destruct.

const Timeout & Conexus::Endpoint::default_read_timeout (  )  const

The default timeout used when read(size_t) is called.

References m_default_read_timeout.

const Timeout & Conexus::Endpoint::default_write_timeout (  )  const

The default timeout used when a write(uint8_t*,size_t) or write(Data) is called.

References m_default_write_timeout.

void Conexus::Endpoint::emit_received_data (  )  [protected]

size_t Conexus::Endpoint::fallback_read_size (  ) 

When a read operation occurs, the read_data() method relies upon the endpoint's input_available() method.

When this method returns 0, the default read_size is used for the read operation.

Default setting is 65535 (2^16 - 1 or 0xFFFF)

References m_fallback_read_size.

size_t Conexus::Endpoint::input_available (  )  throw () [virtual]

Returns the number of bytes available for immediate reading, 0 if no bytes are available, or -1 if the method is invalid for a derived class.

Reimplemented in Conexus::IPv4::TCP, Conexus::IPv4::UDP, Conexus::IPv6::TCP, Conexus::IPv6::UDP, Conexus::LL::Packet, Conexus::Pipe, Conexus::TTY, Conexus::NSPR::FileDescriptor, and Conexus::NSS::SSLSocket.

bool Conexus::Endpoint::is_closed (  ) 

bool Conexus::Endpoint::is_open (  ) 

bool Conexus::Endpoint::is_running (  )  [virtual]

True if the endpoint is running with threaded reads.

References m_read_thread.

Referenced by ~Endpoint().

bool Conexus::Endpoint::is_timestamping_received_data (  )  [virtual]

True if received data is timestamped.

References m_timestamp.

virtual void Conexus::Endpoint::open (  )  throw (open_exception) [pure virtual]

void Conexus::Endpoint::queue_received_data ( const Data  d  )  [protected]

Data Conexus::Endpoint::read ( Timeout  timeout  )  throw (read_exception)

Read into a dynamically created buffer.

Read all available data from the endpoint using the provided read timeout parameter

Returns:
the data read from the endpoint
Note:
The returned data object stores the data as a smart pointer. Therefore, if you want to keep the data from being automatically deallocated, keep the returned data object (or a copy of the returned data object) around as long as you need it. Call by value is sufficient to keep the object around, but extracting the pointer to the actual data via the dereference operator or through the smart pointer's get() method is not sufficient to keep the data block from being deallocated (again, keeping a copy around is easier).

Data Conexus::Endpoint::read ( size_t  size,
Timeout  timeout 
) throw (read_exception)

Read into a dynamically created buffer.

Read up to size bytes from the endpoint or all available data if s=0 using the provided read timeout parameter

Returns:
the data read from the endpoint
Note:
The returned data object stores the data as a smart pointer. Therefore, if you want to keep the data from being automatically deallocated, keep the returned data object (or a copy of the returned data object) around as long as you need it. Call by value is sufficient to keep the object around, but extracting the pointer to the actual data via the dereference operator or through the smart pointer's get() method is not sufficient to keep the data block from being deallocated (again, keeping a copy around is easier).

Data Conexus::Endpoint::read ( size_t  size = 0  )  throw (read_exception)

Read into a dynamically created buffer.

Read up to size bytes from the endpoint or all available data if s=0 using the default read timeout parameter

Returns:
the data read from the endpoint
Note:
The returned data object stores the data as a smart pointer. Therefore, if you want to keep the data from being automatically deallocated, keep the returned data object (or a copy of the returned data object) around as long as you need it. Call by value is sufficient to keep the object around, but extracting the pointer to the actual data via the dereference operator or through the smart pointer's get() method is not sufficient to keep the data block from being deallocated (again, keeping a copy around is easier).

Referenced by operator>>().

virtual Data Conexus::Endpoint::read_data ( size_t  size,
Timeout  timeout 
) throw (read_exception) [protected, pure virtual]

This is the primary mechanism of reading from an endpoint and must be reimplemented by children.

Returns:
The data object read. If no data was available for reading an instance may still be returned, but it will have a zero data size.
Parameters:
timeout If > 0, an upper bound on the amount of time (in microseconds) elapsed before read_data() returns. If = 0, then the read will be attempted immediately. If < 0, read will block without timeout.
s The upper bound on the number of bytes to read. If s = 0, all available data will be read.
Exceptions:
Conexus::exception::read::timeout Thrown when a read timeout occurs
Conexus::exception::read::eof Thrown when a read encounters an EOF (End Of File) condition
Conexus::exception::read::disconnect Thrown when a read encounters a disconnect condition
Conexus::read_exception Thrown (or a descendant is thrown) when a read encounters an exceptional condition

Implemented in Conexus::File, Conexus::FileDescriptor, Conexus::MessageQueue, Conexus::Pipe, Conexus::Socket, Conexus::NSPR::FileDescriptor, Conexus::NSPR::Socket, Conexus::SSL::IPv4::TCP, Conexus::SSL::IPv4::UDP, and Conexus::SSL::IPv6::TCP.

Referenced by read_thread_main().

void Conexus::Endpoint::read_delivery_thread_main (  )  [protected, virtual]

bool Conexus::Endpoint::read_terminate_immediate (  ) 

void Conexus::Endpoint::read_thread_main (  )  [protected, virtual]

void Conexus::Endpoint::read_thread_main_proxy (  )  [protected]

References read_thread_main().

Referenced by start().

void Conexus::Endpoint::set_close_on_destruct ( bool  value  ) 

If set, calls this endpoint's virtual close() method on destruction.

References m_close_on_destruct.

void Conexus::Endpoint::set_default_read_timeout ( Timeout  t  ) 

Set the default read timeout A null timeout causes a blocking read to occur, while a non-null timeout causes a non-blocking read to occur with the timeout parameter as specified.

References Conexus::Timeout::is_null(), and m_default_read_timeout.

void Conexus::Endpoint::set_default_write_timeout ( Timeout  t  ) 

Set the default write timeout A null timeout causes a blocking write to occur, while a non-null timeout causes a non-blocking write to occur with the timeout parameter as specified.

References Conexus::Timeout::is_null(), and m_default_write_timeout.

void Conexus::Endpoint::set_fallback_read_size ( size_t  s  ) 

Sets the default read size used when input_available() returns 0.

References m_fallback_read_size.

void Conexus::Endpoint::set_read_terminate_immediate ( bool  i = true  ) 

void Conexus::Endpoint::set_state_closed (  )  [protected, virtual]

void Conexus::Endpoint::set_state_opened (  )  [protected, virtual]

void Conexus::Endpoint::set_timestamp_received_data ( bool  t = true  )  [virtual]

Set to true to timestamp received data.

References m_timestamp.

sigc::signal< void > Conexus::Endpoint::signal_closed (  ) 

Signal emitted when this endpoint is closed.

References m_signal_closed.

sigc::signal< void, const Data > Conexus::Endpoint::signal_data (  ) 

Signal emitted when data is read.

Connect to this signal if you want to receive all data read by the endpoint. The data object received is unique for each callback point, allowing a callback to modify the data set without effect subsequent callbacks.

References m_signal_data.

sigc::signal< void, size_t > Conexus::Endpoint::signal_data_received_size (  ) 

Signal emitted when data is received notifying the callback of the number of bytes read.

Connect to this signal if you need to know when data is received, and how much, but don't want the data contents.

References m_signal_data_received_size.

sigc::signal< void, size_t > Conexus::Endpoint::signal_data_transmitted_size (  ) 

Signal emitted when data is transmitted, notifying the callback of the number of bytes transmitted.

Connect to this signal if you need to know when data is transmitted, and how much, but don't want the transmitted data contents.

References m_signal_data_transmitted_size.

sigc::signal< void > Conexus::Endpoint::signal_disconnected (  ) 

Signal emitted when a disconnect condition is observed.

References m_signal_disconnected.

sigc::signal< void > Conexus::Endpoint::signal_eof (  ) 

Signal emitted when an EOF (End Of File) condition is observed.

References m_signal_eof.

sigc::signal< void > Conexus::Endpoint::signal_opened (  ) 

Signal emitted when this endpoint is opened.

References m_signal_opened.

sigc::signal< void, StartStop > Conexus::Endpoint::signal_start_stop (  ) 

Signal emitted when this endpoint is started.

The first signal parameter indicates the state

References m_signal_start_stop.

void Conexus::Endpoint::start ( bool  use_dispatcher  )  [virtual]

Starts the endpoint, and forces it to use a specific dispatcher value possibly overriding the global default.

References emit_received_data(), is_closed(), m_dispatcher, m_read_delivery_thread, m_read_terminate, m_read_thread, m_signal_start_stop, m_use_dispatcher, open(), read_delivery_thread_main(), read_thread_main_proxy(), and Conexus::START.

void Conexus::Endpoint::start (  )  [virtual]

Start the endpoint.

If the endpoint is configured for multi-threaded operation this will create and start the read thread.

Whether a dispatcher is used or not is dependent upon the global setting which defaults to false, but may be set with Conexus::init() or Conexus::set_use_dispatcher_default().

If you want to handle it manually, endpoints are capable of reading/writing without ever calling start/stop.

References Conexus::use_dispatcher_default().

long Conexus::Endpoint::state (  ) 

Return this endpoint's current numeric state.

The meaning of this value is specific to the endpoint.

References m_state.

Referenced by close_and_reopen().

void Conexus::Endpoint::stop (  )  [virtual]

Stops any running threads.

Stopping is not immediate. If a read operation is being performed the current read will complete before the thread is stopped.

References m_dispatcher, m_read_delivery_conditional, m_read_delivery_thread, m_read_terminate, m_read_thread, m_signal_start_stop, and Conexus::STOP.

Referenced by ~Endpoint().

ssize_t Conexus::Endpoint::write ( const Data  data,
Timeout  timeout 
) throw (write_exception)

Write a Conexus::Data object to the endpoint using the specified write timeout.

Returns:
The number of bytes actually written or accepted by the write queue
Parameters:
data The Conexus::Data object to be written
timeout If timeout is null a blocking write will occur. Otherwise a non-blocking write will occur using the timeout parameter.

ssize_t Conexus::Endpoint::write ( const Data  data  )  throw (write_exception)

Write a Conexus::Data object to the endpoint using the default write timeout parameter.

If the default write timeout parameter is null (sec<0 or nsec<0) then a blocking write will occur. Otherwise a non-blocking write will occur using the default write timeout value.

Returns:
The number of bytes actually written
Parameters:
data The Conexus::Data object to be written

ssize_t Conexus::Endpoint::write ( Endpoint::pointer  source,
size_t  block_size = 65535,
bool  keep_open = true 
)

Reads source until a zero-length read, timeout or EOF occurs and writes into this endpoint.

Inspired by the NSPR PR_TransmitFile() function

References Conexus::Data::size(), and write().

ssize_t Conexus::Endpoint::write ( const void *  data,
size_t  size,
Timeout  timeout 
) throw (write_exception)

Write data to the endpoint using the specified write timeout.

Returns:
The number of bytes actually written or accepted by the write queue
Parameters:
data Pointer to the raw data block to be written.
size Size in bytes of the raw data block to write.
timeout If timeout is null a blocking write will occur. Otherwise a non-blocking write will occur using the timeout parameter.

References Conexus::UNMANAGED.

ssize_t Conexus::Endpoint::write ( const void *  data,
size_t  size 
) throw (write_exception)

Write data to the endpoint using the default write timeout parameter.

If the default write timeout parameter is null (sec<0 or nsec<0) then a blocking write will occur. Otherwise a non-blocking write will occur using the default write timeout value.

Returns:
The number of bytes actually written
Parameters:
data Pointer to the raw data block to be written.
size Size in bytes of the raw data block to write.

References Conexus::UNMANAGED.

Referenced by operator<<(), and write().

virtual size_t Conexus::Endpoint::write_data ( const Data  data,
Timeout  timeout 
) throw (write_exception) [protected, pure virtual]


Friends And Related Function Documentation

enum EndpointState [related]

These enumerations are used in the socket class methods, and use is also encouraged in children.

Enumerator:
ENDPOINT_NOSTATE 
ENDPOINT_CLOSED  The Endpoint object is in a closed state.
ENDPOINT_OPENED  The Endpoint object is in an opened state.
ENDPOINT_LAST_STATE 
ENDPOINT_UNCHANGED  setting this mode will keep the mode unchanged

enum StartStop [related]

Enumerates start/stop states

STOP was selected as 0, so that a variable of this enum can boolean test START as true and STOP as false.

Enumerator:
STOP 
START 


Member Data Documentation

Glib::Dispatcher* Conexus::Endpoint::m_dispatcher [protected]

Referenced by queue_received_data(), start(), and stop().

Glib::Thread* Conexus::Endpoint::m_read_delivery_thread [protected]

Referenced by read_thread_main(), start(), and stop().

std::queue<Data> Conexus::Endpoint::m_read_queue [protected]

Glib::Mutex Conexus::Endpoint::m_read_queue_lock [protected]

Glib::Thread* Conexus::Endpoint::m_read_thread [protected]

sigc::signal<void> Conexus::Endpoint::m_signal_closed [protected]

Referenced by set_state_closed(), and signal_closed().

sigc::signal<void, const Data> Conexus::Endpoint::m_signal_data [protected]

sigc::signal<void, size_t> Conexus::Endpoint::m_signal_data_received_size [protected]

sigc::signal<void, size_t> Conexus::Endpoint::m_signal_data_transmitted_size [protected]

sigc::signal<void> Conexus::Endpoint::m_signal_disconnected [protected]

sigc::signal<void> Conexus::Endpoint::m_signal_eof [protected]

Referenced by read_thread_main(), and signal_eof().

sigc::signal<void> Conexus::Endpoint::m_signal_opened [protected]

Referenced by set_state_opened(), and signal_opened().

sigc::signal<void,StartStop> Conexus::Endpoint::m_signal_start_stop [protected]

Referenced by signal_start_stop(), start(), and stop().

long Conexus::Endpoint::m_state [protected]

Referenced by queue_received_data(), and start().


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

Generated on Wed Jul 8 15:51:18 2009 for conexus by doxygen 1.5.8