Encapsulates a connection to a remote host, independent of the transport type. More...
#include <connection.h>
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 () |
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... | |
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... | |
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.
typedef std::function<void(const ConnectionPtr&, DropReason reason)> roswrap::Connection::DropFunc |
Definition at line 149 of file connection.h.
typedef boost::signals2::signal<void(const ConnectionPtr&, DropReason reason)> roswrap::Connection::DropSignal |
Definition at line 148 of file connection.h.
Enumerator | |
---|---|
TransportDisconnect | |
HeaderError | |
Destructing |
Definition at line 74 of file connection.h.
roswrap::Connection::Connection | ( | ) |
roswrap::Connection::~Connection | ( | ) |
Add a callback to be called when this connection has dropped.
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.
std::string roswrap::Connection::getCallerId | ( | ) |
|
inline |
Get the Header associated with this connection.
Definition at line 168 of file connection.h.
std::string roswrap::Connection::getRemoteString | ( | ) |
|
inline |
Get the Transport associated with this connection.
Definition at line 164 of file connection.h.
void roswrap::Connection::initialize | ( | const TransportPtr & | transport, |
bool | is_server, | ||
const HeaderReceivedFunc & | header_func | ||
) |
Initialize this connection.
bool roswrap::Connection::isDropped | ( | ) |
Returns whether or not this connection has been dropped.
|
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.
|
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)
|
private |
|
private |
|
private |
|
private |
|
private |
Called by the Transport when there is data available to be read.
|
private |
Called by the Transport when it is possible to write data.
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)
size | The size, in bytes, of data to read |
finished_callback | The function to call when this read is finished |
|
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.
void roswrap::Connection::removeDropListener | ( | const boost::signals2::connection & | c | ) |
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)
error_message | The error message |
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.
void roswrap::Connection::setHeaderReceivedCallback | ( | const HeaderReceivedFunc & | func | ) |
Set the header receipt callback.
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.
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 |
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.
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 |
|
private |
Write data to our transport. Also manages calling the write callback.
|
private |
Synchronizes drop() calls.
Definition at line 263 of file connection.h.
|
private |
Signal raised when this connection is dropped.
Definition at line 260 of file connection.h.
|
private |
Have we dropped?
Definition at line 213 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 236 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 254 of file connection.h.
|
private |
Incoming header.
Definition at line 215 of file connection.h.
|
private |
Function that handles the incoming header.
Definition at line 219 of file connection.h.
|
private |
Function to call when the outgoing header has finished writing.
Definition at line 257 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 211 of file connection.h.
|
private |
Read buffer that ends up being passed to the read callback.
Definition at line 222 of file connection.h.
|
private |
Function to call when the read is finished.
Definition at line 228 of file connection.h.
|
private |
Amount of data currently in the read buffer, in bytes.
Definition at line 224 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 230 of file connection.h.
|
private |
Size of the read buffer, in bytes.
Definition at line 226 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 232 of file connection.h.
|
private |
If we're sending a header error we disable most other calls.
Definition at line 266 of file connection.h.
|
private |
Transport associated with us.
Definition at line 217 of file connection.h.
|
private |
Buffer to write from.
Definition at line 239 of file connection.h.
|
private |
Function to call when the current write is finished.
Definition at line 245 of file connection.h.
|
private |
Definition at line 246 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 248 of file connection.h.
|
private |
Amount of data we've written from the write buffer.
Definition at line 241 of file connection.h.
|
private |
Size of the write buffer.
Definition at line 243 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 250 of file connection.h.