Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
roswrap::Connection Class Reference

Encapsulates a connection to a remote host, independent of the transport type. More...

#include <connection.h>

Inheritance diagram for roswrap::Connection:
Inheritance graph
[legend]

Public Types

typedef std::function< void(const ConnectionPtr &, DropReason reason)> DropFunc
 
enum  DropReason { TransportDisconnect, HeaderError, Destructing }
 
typedef boost::signals2::signal< void(const ConnectionPtr &, DropReason reason)> DropSignal
 

Public Member Functions

boost::signals2::connection addDropListener (const DropFunc &slot)
 Add a callback to be called when this connection has dropped. More...
 
 Connection ()
 
void drop (DropReason reason)
 Drop this connection. Anything added as a drop listener through addDropListener will get called back when this connection has been dropped. More...
 
std::string getCallerId ()
 
HeadergetHeader ()
 Get the Header associated with this connection. More...
 
std::string getRemoteString ()
 
const TransportPtrgetTransport ()
 Get the Transport associated with this connection. More...
 
void initialize (const TransportPtr &transport, bool is_server, const HeaderReceivedFunc &header_func)
 Initialize this connection. More...
 
bool isDropped ()
 Returns whether or not this connection has been dropped. More...
 
bool isSendingHeaderError ()
 Returns true if we're currently sending a header error (and will be automatically dropped when it's finished) More...
 
void read (uint32_t size, const ReadFinishedFunc &finished_callback)
 Read a number of bytes, calling a callback when finished. More...
 
void removeDropListener (const boost::signals2::connection &c)
 
void sendHeaderError (const std::string &error_message)
 Send a header error message, of the form "error=<message>". Drops the connection once the data has written successfully (or fails to write) More...
 
void setHeader (const Header &header)
 Set the Header associated with this connection (used with UDPROS, which receives the connection during XMLRPC negotiation). More...
 
void setHeaderReceivedCallback (const HeaderReceivedFunc &func)
 Set the header receipt callback. More...
 
void write (const boost::shared_array< uint8_t > &buffer, uint32_t size, const WriteFinishedFunc &finished_callback, bool immedate=true)
 Write a buffer of bytes, calling a callback when finished. More...
 
void writeHeader (const M_string &key_vals, const WriteFinishedFunc &finished_callback)
 Send a list of string key/value pairs as a header message. More...
 
 ~Connection ()
 

Private Member Functions

void onDisconnect (const TransportPtr &transport)
 Called by the Transport when it has been disconnected, either through a call to close() or through an error in the connection (such as a remote disconnect) More...
 
void onErrorHeaderWritten (const ConnectionPtr &conn)
 
void onHeaderLengthRead (const ConnectionPtr &conn, const boost::shared_array< uint8_t > &buffer, uint32_t size, bool success)
 
void onHeaderRead (const ConnectionPtr &conn, const boost::shared_array< uint8_t > &buffer, uint32_t size, bool success)
 
void onHeaderWritten (const ConnectionPtr &conn)
 
void onReadable (const TransportPtr &transport)
 Called by the Transport when there is data available to be read. More...
 
void onWriteable (const TransportPtr &transport)
 Called by the Transport when it is possible to write data. More...
 
void readTransport ()
 Read data off our transport. Also manages calling the read callback. If there is any data to be read, read() will read it until the fixed read buffer is filled. More...
 
void writeTransport ()
 Write data to our transport. Also manages calling the write callback. More...
 

Private Attributes

boost::recursive_mutex drop_mutex_
 Synchronizes drop() calls. More...
 
DropSignal drop_signal_
 Signal raised when this connection is dropped. More...
 
bool dropped_
 Have we dropped? More...
 
volatile uint32_t has_read_callback_
 
volatile uint32_t has_write_callback_
 
Header header_
 Incoming header. More...
 
HeaderReceivedFunc header_func_
 Function that handles the incoming header. More...
 
WriteFinishedFunc header_written_callback_
 Function to call when the outgoing header has finished writing. More...
 
bool is_server_
 Are we a server? Servers wait for clients to send a header and then send a header in response. More...
 
boost::shared_array< uint8_t > read_buffer_
 Read buffer that ends up being passed to the read callback. More...
 
ReadFinishedFunc read_callback_
 Function to call when the read is finished. More...
 
uint32_t read_filled_
 Amount of data currently in the read buffer, in bytes. More...
 
boost::recursive_mutex read_mutex_
 Mutex used for protecting reading. Recursive because a read can immediately cause another read through the callback. More...
 
uint32_t read_size_
 Size of the read buffer, in bytes. More...
 
bool reading_
 Flag telling us if we're in the middle of a read (mostly to avoid recursive deadlocking) More...
 
bool sending_header_error_
 If we're sending a header error we disable most other calls. More...
 
TransportPtr transport_
 Transport associated with us. More...
 
boost::shared_array< uint8_t > write_buffer_
 Buffer to write from. More...
 
WriteFinishedFunc write_callback_
 Function to call when the current write is finished. More...
 
std::mutex write_callback_mutex_
 
boost::recursive_mutex write_mutex_
 Mutex used for protecting writing. Recursive because a write can immediately cause another write through the callback. More...
 
uint32_t write_sent_
 Amount of data we've written from the write buffer. More...
 
uint32_t write_size_
 Size of the write buffer. More...
 
bool writing_
 Flag telling us if we're in the middle of a write (mostly used to avoid recursive deadlocking) More...
 

Detailed Description

Encapsulates a connection to a remote host, independent of the transport type.

Connection provides automatic header negotiation, as well as easy ways of reading and writing arbitrary amounts of data without having to set up your own state machines.

Definition at line 71 of file connection.h.

Member Typedef Documentation

◆ DropFunc

typedef std::function<void(const ConnectionPtr&, DropReason reason)> roswrap::Connection::DropFunc

Definition at line 149 of file connection.h.

◆ DropSignal

typedef boost::signals2::signal<void(const ConnectionPtr&, DropReason reason)> roswrap::Connection::DropSignal

Definition at line 148 of file connection.h.

Member Enumeration Documentation

◆ DropReason

Enumerator
TransportDisconnect 
HeaderError 
Destructing 

Definition at line 74 of file connection.h.

Constructor & Destructor Documentation

◆ Connection()

roswrap::Connection::Connection ( )

◆ ~Connection()

roswrap::Connection::~Connection ( )

Member Function Documentation

◆ addDropListener()

boost::signals2::connection roswrap::Connection::addDropListener ( const DropFunc slot)

Add a callback to be called when this connection has dropped.

◆ drop()

void roswrap::Connection::drop ( DropReason  reason)

Drop this connection. Anything added as a drop listener through addDropListener will get called back when this connection has been dropped.

◆ getCallerId()

std::string roswrap::Connection::getCallerId ( )

◆ getHeader()

Header& roswrap::Connection::getHeader ( )
inline

Get the Header associated with this connection.

Definition at line 168 of file connection.h.

◆ getRemoteString()

std::string roswrap::Connection::getRemoteString ( )

◆ getTransport()

const TransportPtr& roswrap::Connection::getTransport ( )
inline

Get the Transport associated with this connection.

Definition at line 164 of file connection.h.

◆ initialize()

void roswrap::Connection::initialize ( const TransportPtr transport,
bool  is_server,
const HeaderReceivedFunc header_func 
)

Initialize this connection.

◆ isDropped()

bool roswrap::Connection::isDropped ( )

Returns whether or not this connection has been dropped.

◆ isSendingHeaderError()

bool roswrap::Connection::isSendingHeaderError ( )
inline

Returns true if we're currently sending a header error (and will be automatically dropped when it's finished)

Definition at line 102 of file connection.h.

◆ onDisconnect()

void roswrap::Connection::onDisconnect ( const TransportPtr transport)
private

Called by the Transport when it has been disconnected, either through a call to close() or through an error in the connection (such as a remote disconnect)

◆ onErrorHeaderWritten()

void roswrap::Connection::onErrorHeaderWritten ( const ConnectionPtr conn)
private

◆ onHeaderLengthRead()

void roswrap::Connection::onHeaderLengthRead ( const ConnectionPtr conn,
const boost::shared_array< uint8_t > &  buffer,
uint32_t  size,
bool  success 
)
private

◆ onHeaderRead()

void roswrap::Connection::onHeaderRead ( const ConnectionPtr conn,
const boost::shared_array< uint8_t > &  buffer,
uint32_t  size,
bool  success 
)
private

◆ onHeaderWritten()

void roswrap::Connection::onHeaderWritten ( const ConnectionPtr conn)
private

◆ onReadable()

void roswrap::Connection::onReadable ( const TransportPtr transport)
private

Called by the Transport when there is data available to be read.

◆ onWriteable()

void roswrap::Connection::onWriteable ( const TransportPtr transport)
private

Called by the Transport when it is possible to write data.

◆ read()

void roswrap::Connection::read ( uint32_t  size,
const ReadFinishedFunc finished_callback 
)

Read a number of bytes, calling a callback when finished.

read() will not queue up multiple reads. Once read() has been called, it is not valid to call it again until the finished callback has been called. It is valid to call read() from within the finished callback.

The finished callback is of the form void(const ConnectionPtr&, const boost::shared_array<uint8_t>&, uint32_t)

Note
The finished callback may be called from within this call to read() if the data has already arrived
Parameters
sizeThe size, in bytes, of data to read
finished_callbackThe function to call when this read is finished

◆ readTransport()

void roswrap::Connection::readTransport ( )
private

Read data off our transport. Also manages calling the read callback. If there is any data to be read, read() will read it until the fixed read buffer is filled.

◆ removeDropListener()

void roswrap::Connection::removeDropListener ( const boost::signals2::connection &  c)

◆ sendHeaderError()

void roswrap::Connection::sendHeaderError ( const std::string &  error_message)

Send a header error message, of the form "error=<message>". Drops the connection once the data has written successfully (or fails to write)

Parameters
error_messageThe error message

◆ setHeader()

void roswrap::Connection::setHeader ( const Header header)
inline

Set the Header associated with this connection (used with UDPROS, which receives the connection during XMLRPC negotiation).

Definition at line 174 of file connection.h.

◆ setHeaderReceivedCallback()

void roswrap::Connection::setHeaderReceivedCallback ( const HeaderReceivedFunc func)

Set the header receipt callback.

◆ write()

void roswrap::Connection::write ( const boost::shared_array< uint8_t > &  buffer,
uint32_t  size,
const WriteFinishedFunc finished_callback,
bool  immedate = true 
)

Write a buffer of bytes, calling a callback when finished.

write() will not queue up multiple writes. Once write() has been called, it is not valid to call it again until the finished callback has been called. It is valid to call write() from within the finished callback.

  • The finished callback is of the form void(const ConnectionPtr&)
Note
The finished callback may be called from within this call to write() if the data can be written immediately
Parameters
bufferThe buffer of data to write
sizeThe size of the buffer, in bytes
finished_callbackThe function to call when the write has finished
immediateWhether to immediately try to write as much data as possible to the socket or to pass the data off to the server thread

◆ writeHeader()

void roswrap::Connection::writeHeader ( const M_string key_vals,
const WriteFinishedFunc finished_callback 
)

Send a list of string key/value pairs as a header message.

Parameters
key_valsThe values to send. Neither keys nor values can have any newlines in them
finished_callbackThe function to call when the header has finished writing

◆ writeTransport()

void roswrap::Connection::writeTransport ( )
private

Write data to our transport. Also manages calling the write callback.

Member Data Documentation

◆ drop_mutex_

boost::recursive_mutex roswrap::Connection::drop_mutex_
private

Synchronizes drop() calls.

Definition at line 263 of file connection.h.

◆ drop_signal_

DropSignal roswrap::Connection::drop_signal_
private

Signal raised when this connection is dropped.

Definition at line 260 of file connection.h.

◆ dropped_

bool roswrap::Connection::dropped_
private

Have we dropped?

Definition at line 213 of file connection.h.

◆ has_read_callback_

volatile uint32_t roswrap::Connection::has_read_callback_
private

flag telling us if there is a read callback 32-bit loads and stores are atomic on x86 and PPC... TODO: use a cross-platform atomic operations library to ensure this is done atomically

Definition at line 236 of file connection.h.

◆ has_write_callback_

volatile uint32_t roswrap::Connection::has_write_callback_
private

flag telling us if there is a write callback 32-bit loads and stores are atomic on x86 and PPC... TODO: use a cross-platform atomic operations library to ensure this is done atomically

Definition at line 254 of file connection.h.

◆ header_

Header roswrap::Connection::header_
private

Incoming header.

Definition at line 215 of file connection.h.

◆ header_func_

HeaderReceivedFunc roswrap::Connection::header_func_
private

Function that handles the incoming header.

Definition at line 219 of file connection.h.

◆ header_written_callback_

WriteFinishedFunc roswrap::Connection::header_written_callback_
private

Function to call when the outgoing header has finished writing.

Definition at line 257 of file connection.h.

◆ is_server_

bool roswrap::Connection::is_server_
private

Are we a server? Servers wait for clients to send a header and then send a header in response.

Definition at line 211 of file connection.h.

◆ read_buffer_

boost::shared_array<uint8_t> roswrap::Connection::read_buffer_
private

Read buffer that ends up being passed to the read callback.

Definition at line 222 of file connection.h.

◆ read_callback_

ReadFinishedFunc roswrap::Connection::read_callback_
private

Function to call when the read is finished.

Definition at line 228 of file connection.h.

◆ read_filled_

uint32_t roswrap::Connection::read_filled_
private

Amount of data currently in the read buffer, in bytes.

Definition at line 224 of file connection.h.

◆ read_mutex_

boost::recursive_mutex roswrap::Connection::read_mutex_
private

Mutex used for protecting reading. Recursive because a read can immediately cause another read through the callback.

Definition at line 230 of file connection.h.

◆ read_size_

uint32_t roswrap::Connection::read_size_
private

Size of the read buffer, in bytes.

Definition at line 226 of file connection.h.

◆ reading_

bool roswrap::Connection::reading_
private

Flag telling us if we're in the middle of a read (mostly to avoid recursive deadlocking)

Definition at line 232 of file connection.h.

◆ sending_header_error_

bool roswrap::Connection::sending_header_error_
private

If we're sending a header error we disable most other calls.

Definition at line 266 of file connection.h.

◆ transport_

TransportPtr roswrap::Connection::transport_
private

Transport associated with us.

Definition at line 217 of file connection.h.

◆ write_buffer_

boost::shared_array<uint8_t> roswrap::Connection::write_buffer_
private

Buffer to write from.

Definition at line 239 of file connection.h.

◆ write_callback_

WriteFinishedFunc roswrap::Connection::write_callback_
private

Function to call when the current write is finished.

Definition at line 245 of file connection.h.

◆ write_callback_mutex_

std::mutex roswrap::Connection::write_callback_mutex_
private

Definition at line 246 of file connection.h.

◆ write_mutex_

boost::recursive_mutex roswrap::Connection::write_mutex_
private

Mutex used for protecting writing. Recursive because a write can immediately cause another write through the callback.

Definition at line 248 of file connection.h.

◆ write_sent_

uint32_t roswrap::Connection::write_sent_
private

Amount of data we've written from the write buffer.

Definition at line 241 of file connection.h.

◆ write_size_

uint32_t roswrap::Connection::write_size_
private

Size of the write buffer.

Definition at line 243 of file connection.h.

◆ writing_

bool roswrap::Connection::writing_
private

Flag telling us if we're in the middle of a write (mostly used to avoid recursive deadlocking)

Definition at line 250 of file connection.h.


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


sick_scan_xd
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Fri Oct 25 2024 02:47:15