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

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

#include <connection.h>

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

Public Types

typedef boost::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...
 
boost::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 70 of file connection.h.

Member Typedef Documentation

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

Definition at line 148 of file connection.h.

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

Definition at line 147 of file connection.h.

Member Enumeration Documentation

Enumerator
TransportDisconnect 
HeaderError 
Destructing 

Definition at line 73 of file connection.h.

Constructor & Destructor Documentation

ros::Connection::Connection ( )

Definition at line 47 of file connection.cpp.

ros::Connection::~Connection ( )

Definition at line 62 of file connection.cpp.

Member Function Documentation

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

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

Definition at line 87 of file connection.cpp.

void ros::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.

Definition at line 325 of file connection.cpp.

std::string ros::Connection::getCallerId ( )

Definition at line 461 of file connection.cpp.

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

Get the Header associated with this connection.

Definition at line 167 of file connection.h.

std::string ros::Connection::getRemoteString ( )

Definition at line 472 of file connection.cpp.

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

Get the Transport associated with this connection.

Definition at line 163 of file connection.h.

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

Initialize this connection.

Definition at line 69 of file connection.cpp.

bool ros::Connection::isDropped ( )

Returns whether or not this connection has been dropped.

Definition at line 345 of file connection.cpp.

bool ros::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 101 of file connection.h.

void ros::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)

Definition at line 317 of file connection.cpp.

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

Definition at line 447 of file connection.cpp.

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

Definition at line 383 of file connection.cpp.

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

Definition at line 406 of file connection.cpp.

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

Definition at line 438 of file connection.cpp.

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

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

Definition at line 99 of file connection.cpp.

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

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

Definition at line 256 of file connection.cpp.

void ros::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

Definition at line 264 of file connection.cpp.

void ros::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.

Definition at line 107 of file connection.cpp.

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

Definition at line 93 of file connection.cpp.

void ros::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

Definition at line 374 of file connection.cpp.

void ros::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 173 of file connection.h.

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

Set the header receipt callback.

Definition at line 453 of file connection.cpp.

void ros::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

Definition at line 289 of file connection.cpp.

void ros::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

Definition at line 351 of file connection.cpp.

void ros::Connection::writeTransport ( )
private

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

Definition at line 192 of file connection.cpp.

Member Data Documentation

boost::recursive_mutex ros::Connection::drop_mutex_
private

Synchronizes drop() calls.

Definition at line 262 of file connection.h.

DropSignal ros::Connection::drop_signal_
private

Signal raised when this connection is dropped.

Definition at line 259 of file connection.h.

bool ros::Connection::dropped_
private

Have we dropped?

Definition at line 212 of file connection.h.

volatile uint32_t ros::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 235 of file connection.h.

volatile uint32_t ros::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 253 of file connection.h.

Header ros::Connection::header_
private

Incoming header.

Definition at line 214 of file connection.h.

HeaderReceivedFunc ros::Connection::header_func_
private

Function that handles the incoming header.

Definition at line 218 of file connection.h.

WriteFinishedFunc ros::Connection::header_written_callback_
private

Function to call when the outgoing header has finished writing.

Definition at line 256 of file connection.h.

bool ros::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 210 of file connection.h.

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

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

Definition at line 221 of file connection.h.

ReadFinishedFunc ros::Connection::read_callback_
private

Function to call when the read is finished.

Definition at line 227 of file connection.h.

uint32_t ros::Connection::read_filled_
private

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

Definition at line 223 of file connection.h.

boost::recursive_mutex ros::Connection::read_mutex_
private

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

Definition at line 229 of file connection.h.

uint32_t ros::Connection::read_size_
private

Size of the read buffer, in bytes.

Definition at line 225 of file connection.h.

bool ros::Connection::reading_
private

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

Definition at line 231 of file connection.h.

bool ros::Connection::sending_header_error_
private

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

Definition at line 265 of file connection.h.

TransportPtr ros::Connection::transport_
private

Transport associated with us.

Definition at line 216 of file connection.h.

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

Buffer to write from.

Definition at line 238 of file connection.h.

WriteFinishedFunc ros::Connection::write_callback_
private

Function to call when the current write is finished.

Definition at line 244 of file connection.h.

boost::mutex ros::Connection::write_callback_mutex_
private

Definition at line 245 of file connection.h.

boost::recursive_mutex ros::Connection::write_mutex_
private

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

Definition at line 247 of file connection.h.

uint32_t ros::Connection::write_sent_
private

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

Definition at line 240 of file connection.h.

uint32_t ros::Connection::write_size_
private

Size of the write buffer.

Definition at line 242 of file connection.h.

bool ros::Connection::writing_
private

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

Definition at line 249 of file connection.h.


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


roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim, Dirk Thomas
autogenerated on Mon Nov 2 2020 03:52:27