Encapsulates a connection to a remote host, independent of the transport type. More...
#include <connection.h>
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. | |
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. | |
std::string | getCallerId () |
Header & | getHeader () |
Get the Header associated with this connection. | |
std::string | getRemoteString () |
const TransportPtr & | getTransport () |
Get the Transport associated with this connection. | |
void | initialize (const TransportPtr &transport, bool is_server, const HeaderReceivedFunc &header_func) |
Initialize this connection. | |
bool | isDropped () |
Returns whether or not this connection has been dropped. | |
bool | isSendingHeaderError () |
Returns true if we're currently sending a header error (and will be automatically dropped when it's finished) | |
void | read (uint32_t size, const ReadFinishedFunc &finished_callback) |
Read a number of bytes, calling a callback when finished. | |
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) | |
void | setHeader (const Header &header) |
Set the Header associated with this connection (used with UDPROS, which receives the connection during XMLRPC negotiation). | |
void | setHeaderReceivedCallback (const HeaderReceivedFunc &func) |
Set the header receipt callback. | |
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. | |
void | writeHeader (const M_string &key_vals, const WriteFinishedFunc &finished_callback) |
Send a list of string key/value pairs as a header message. | |
~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) | |
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. | |
void | onWriteable (const TransportPtr &transport) |
Called by the Transport when it is possible to write data. | |
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. | |
void | writeTransport () |
Write data to our transport. Also manages calling the write callback. | |
Private Attributes | |
boost::recursive_mutex | drop_mutex_ |
Synchronizes drop() calls. | |
DropSignal | drop_signal_ |
Signal raised when this connection is dropped. | |
bool | dropped_ |
Have we dropped? | |
volatile uint32_t | has_read_callback_ |
volatile uint32_t | has_write_callback_ |
Header | header_ |
Incoming header. | |
HeaderReceivedFunc | header_func_ |
Function that handles the incoming header. | |
WriteFinishedFunc | header_written_callback_ |
Function to call when the outgoing header has finished writing. | |
bool | is_server_ |
Are we a server? Servers wait for clients to send a header and then send a header in response. | |
boost::shared_array< uint8_t > | read_buffer_ |
Read buffer that ends up being passed to the read callback. | |
ReadFinishedFunc | read_callback_ |
Function to call when the read is finished. | |
uint32_t | read_filled_ |
Amount of data currently in the read buffer, in bytes. | |
boost::recursive_mutex | read_mutex_ |
Mutex used for protecting reading. Recursive because a read can immediately cause another read through the callback. | |
uint32_t | read_size_ |
Size of the read buffer, in bytes. | |
bool | reading_ |
Flag telling us if we're in the middle of a read (mostly to avoid recursive deadlocking) | |
bool | sending_header_error_ |
If we're sending a header error we disable most other calls. | |
TransportPtr | transport_ |
Transport associated with us. | |
boost::shared_array< uint8_t > | write_buffer_ |
Buffer to write from. | |
WriteFinishedFunc | write_callback_ |
Function to call when the current write is finished. | |
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. | |
uint32_t | write_sent_ |
Amount of data we've written from the write buffer. | |
uint32_t | write_size_ |
Size of the write buffer. | |
bool | writing_ |
Flag telling us if we're in the middle of a write (mostly used to avoid recursive deadlocking) |
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.
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.
Definition at line 73 of file connection.h.
Definition at line 47 of file connection.cpp.
Definition at line 62 of file connection.cpp.
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 322 of file connection.cpp.
std::string ros::Connection::getCallerId | ( | ) |
Definition at line 457 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 468 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 342 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 315 of file connection.cpp.
void ros::Connection::onErrorHeaderWritten | ( | const ConnectionPtr & | conn | ) | [private] |
Definition at line 443 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 380 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 402 of file connection.cpp.
void ros::Connection::onHeaderWritten | ( | const ConnectionPtr & | conn | ) | [private] |
Definition at line 434 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 255 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)
size | The size, in bytes, of data to read |
finished_callback | The function to call when this read is finished |
Definition at line 262 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 106 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)
error_message | The error message |
Definition at line 371 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 449 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&)
buffer | The buffer of data to write |
size | The size of the buffer, in bytes |
finished_callback | The function to call when the write has finished |
immediate | Whether 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 287 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.
key_vals | The values to send. Neither keys nor values can have any newlines in them |
finished_callback | The function to call when the header has finished writing |
Definition at line 348 of file connection.cpp.
void ros::Connection::writeTransport | ( | ) | [private] |
Write data to our transport. Also manages calling the write callback.
Definition at line 191 of file connection.cpp.
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.