35 #ifndef ROSCPP_CONNECTION_H 36 #define ROSCPP_CONNECTION_H 41 #include <boost/signals2.hpp> 43 #include <boost/function.hpp> 44 #include <boost/shared_ptr.hpp> 45 #include <boost/shared_array.hpp> 46 #include <boost/enable_shared_from_this.hpp> 47 #include <boost/thread/mutex.hpp> 48 #include <boost/thread/recursive_mutex.hpp> 50 #define READ_BUFFER_SIZE (1024*64) 59 typedef boost::function<void(const ConnectionPtr&, const boost::shared_array<uint8_t>&, uint32_t, bool)>
ReadFinishedFunc;
70 class ROSCPP_DECL
Connection :
public boost::enable_shared_from_this<Connection>
86 void initialize(
const TransportPtr& transport,
bool is_server,
const HeaderReceivedFunc& header_func);
107 void sendHeaderError(
const std::string& error_message);
113 void writeHeader(
const M_string& key_vals,
const WriteFinishedFunc& finished_callback);
145 void write(
const boost::shared_array<uint8_t>& buffer, uint32_t size,
const WriteFinishedFunc& finished_callback,
bool immedate =
true);
147 typedef boost::signals2::signal<void(const ConnectionPtr&, DropReason reason)>
DropSignal;
148 typedef boost::function<void(const ConnectionPtr&, DropReason reason)>
DropFunc;
152 boost::signals2::connection addDropListener(
const DropFunc& slot);
153 void removeDropListener(
const boost::signals2::connection& c);
158 void setHeaderReceivedCallback(
const HeaderReceivedFunc& func);
175 std::string getCallerId();
176 std::string getRemoteString();
182 void onReadable(
const TransportPtr& transport);
186 void onWriteable(
const TransportPtr& transport);
191 void onDisconnect(
const TransportPtr& transport);
194 void onHeaderWritten(
const ConnectionPtr& conn);
195 void onErrorHeaderWritten(
const ConnectionPtr& conn);
203 void readTransport();
207 void writeTransport();
271 #endif // ROSCPP_CONNECTION_H const TransportPtr & getTransport()
Get the Transport associated with this connection.
Abstract base class that allows abstraction of the transport type, eg. TCP, shared memory...
bool is_server_
Are we a server? Servers wait for clients to send a header and then send a header in response...
volatile uint32_t has_write_callback_
ROSCONSOLE_DECL void initialize()
uint32_t write_sent_
Amount of data we've written from the write buffer.
bool sending_header_error_
If we're sending a header error we disable most other calls.
bool reading_
Flag telling us if we're in the middle of a read (mostly to avoid recursive deadlocking) ...
boost::function< void(const ConnectionPtr &, DropReason reason)> DropFunc
boost::shared_ptr< Transport > TransportPtr
boost::shared_ptr< Connection > ConnectionPtr
ReadFinishedFunc read_callback_
Function to call when the read is finished.
Encapsulates a connection to a remote host, independent of the transport type.
void setHeader(const Header &header)
Set the Header associated with this connection (used with UDPROS, which receives the connection durin...
boost::signals2::signal< void(const ConnectionPtr &, DropReason reason)> DropSignal
DropSignal drop_signal_
Signal raised when this connection is dropped.
boost::mutex write_callback_mutex_
boost::function< bool(const ConnectionPtr &, const Header &)> HeaderReceivedFunc
boost::recursive_mutex drop_mutex_
Synchronizes drop() calls.
std::map< std::string, std::string > M_string
boost::recursive_mutex read_mutex_
Mutex used for protecting reading. Recursive because a read can immediately cause another read throug...
uint32_t read_filled_
Amount of data currently in the read buffer, in bytes.
uint32_t write_size_
Size of the write buffer.
bool dropped_
Have we dropped?
boost::function< void(const ConnectionPtr &, const boost::shared_array< uint8_t > &, uint32_t, bool)> ReadFinishedFunc
Header & getHeader()
Get the Header associated with this connection.
bool writing_
Flag telling us if we're in the middle of a write (mostly used to avoid recursive deadlocking) ...
bool isSendingHeaderError()
Returns true if we're currently sending a header error (and will be automatically dropped when it's f...
WriteFinishedFunc write_callback_
Function to call when the current write is finished.
WriteFinishedFunc header_written_callback_
Function to call when the outgoing header has finished writing.
Header header_
Incoming header.
boost::shared_array< uint8_t > write_buffer_
Buffer to write from.
boost::recursive_mutex write_mutex_
Mutex used for protecting writing. Recursive because a write can immediately cause another write thro...
HeaderReceivedFunc header_func_
Function that handles the incoming header.
boost::function< void(const ConnectionPtr &)> WriteFinishedFunc
TransportPtr transport_
Transport associated with us.
uint32_t read_size_
Size of the read buffer, in bytes.
volatile uint32_t has_read_callback_
boost::shared_array< uint8_t > read_buffer_
Read buffer that ends up being passed to the read callback.