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. 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 () |
Header & | getHeader () |
Get the Header associated with this connection. More... | |
std::string | getRemoteString () |
const TransportPtr & | getTransport () |
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... | |
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.
Enumerator | |
---|---|
TransportDisconnect | |
HeaderError | |
Destructing |
Definition at line 73 of file connection.h.
ros::Connection::Connection | ( | ) |
Definition at line 47 of file connection.cpp.
ros::Connection::~Connection | ( | ) |
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 325 of file connection.cpp.
std::string ros::Connection::getCallerId | ( | ) |
Definition at line 464 of file connection.cpp.
|
inline |
Get the Header associated with this connection.
Definition at line 167 of file connection.h.
std::string ros::Connection::getRemoteString | ( | ) |
Definition at line 475 of file connection.cpp.
|
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 348 of file connection.cpp.
|
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.
|
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.
|
private |
Definition at line 450 of file connection.cpp.
|
private |
Definition at line 386 of file connection.cpp.
|
private |
Definition at line 409 of file connection.cpp.
|
private |
Definition at line 441 of file connection.cpp.
|
private |
Called by the Transport when there is data available to be read.
Definition at line 99 of file connection.cpp.
|
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)
size | The size, in bytes, of data to read |
finished_callback | The function to call when this read is finished |
Definition at line 264 of file connection.cpp.
|
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)
error_message | The error message |
Definition at line 377 of file connection.cpp.
|
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 456 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.
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 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.
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 354 of file connection.cpp.
|
private |
Write data to our transport. Also manages calling the write callback.
Definition at line 192 of file connection.cpp.
|
private |
Synchronizes drop() calls.
Definition at line 262 of file connection.h.
|
private |
Signal raised when this connection is dropped.
Definition at line 259 of file connection.h.
|
private |
Have we dropped?
Definition at line 212 of file connection.h.
|
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.
|
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.
|
private |
Incoming header.
Definition at line 214 of file connection.h.
|
private |
Function that handles the incoming header.
Definition at line 218 of file connection.h.
|
private |
Function to call when the outgoing header has finished writing.
Definition at line 256 of file connection.h.
|
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.
|
private |
Read buffer that ends up being passed to the read callback.
Definition at line 221 of file connection.h.
|
private |
Function to call when the read is finished.
Definition at line 227 of file connection.h.
|
private |
Amount of data currently in the read buffer, in bytes.
Definition at line 223 of file connection.h.
|
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.
|
private |
Size of the read buffer, in bytes.
Definition at line 225 of file connection.h.
|
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.
|
private |
If we're sending a header error we disable most other calls.
Definition at line 265 of file connection.h.
|
private |
Transport associated with us.
Definition at line 216 of file connection.h.
|
private |
Buffer to write from.
Definition at line 238 of file connection.h.
|
private |
Function to call when the current write is finished.
Definition at line 244 of file connection.h.
|
private |
Definition at line 245 of file connection.h.
|
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.
|
private |
Amount of data we've written from the write buffer.
Definition at line 240 of file connection.h.
|
private |
Size of the write buffer.
Definition at line 242 of file connection.h.
|
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.