Go to the documentation of this file.
41 #include <boost/shared_array.hpp>
42 #include <boost/bind/bind.hpp>
53 , has_read_callback_(0)
57 , has_write_callback_(0)
58 , sending_header_error_(false)
89 boost::recursive_mutex::scoped_lock lock(
drop_mutex_);
95 boost::recursive_mutex::scoped_lock lock(
drop_mutex_);
109 boost::recursive_mutex::scoped_try_lock lock(
read_mutex_);
130 else if (bytes_read < 0)
144 callback(shared_from_this(),
read_buffer_, size,
false);
176 callback(shared_from_this(), buffer, size,
true);
194 boost::recursive_mutex::scoped_try_lock lock(
write_mutex_);
202 bool can_write_more =
true;
207 ROS_DEBUG_NAMED(
"superdebug",
"Connection writing %d bytes", to_write);
209 ROS_DEBUG_NAMED(
"superdebug",
"Connection wrote %d bytes", bytes_sent);
221 can_write_more =
false;
241 callback(shared_from_this());
272 boost::recursive_mutex::scoped_lock lock(
read_mutex_);
328 bool did_drop =
false;
330 boost::recursive_mutex::scoped_lock lock(
drop_mutex_);
342 boost::recursive_mutex::scoped_lock lock(
drop_mutex_);
350 boost::recursive_mutex::scoped_lock lock(
drop_mutex_);
369 uint32_t msg_len = len + 4;
371 memcpy(full_msg.get() + 4, buffer.get(), len);
372 *((uint32_t*)full_msg.get()) = len;
380 m[
"error"] = error_msg;
395 uint32_t len = *((uint32_t*)buffer.get());
397 if (len > 1000000000)
399 ROS_ERROR(
"a header of over a gigabyte was " \
400 "predicted in tcpros. that seems highly " \
401 "unlikely, so I'll assume protocol " \
402 "synchronization is lost.");
406 read(len, boost::bind(&
Connection::onHeaderRead,
this, boost::placeholders::_1, boost::placeholders::_2, boost::placeholders::_3, boost::placeholders::_4));
416 std::string error_msg;
423 std::string error_val;
426 ROSCPP_LOG_DEBUG(
"Received error message in header for connection to [%s]: [%s]",
transport_->getTransportInfo().c_str(), error_val.c_str());
466 std::string callerid;
472 return std::string(
"unknown");
477 std::stringstream ss;
void writeHeader(const M_string &key_vals, const WriteFinishedFunc &finished_callback)
Send a list of string key/value pairs as a header message.
void onErrorHeaderWritten(const ConnectionPtr &conn)
uint32_t read_size_
Size of the read buffer, in bytes.
boost::signals2::connection addDropListener(const DropFunc &slot)
Add a callback to be called when this connection has dropped.
boost::function< void(const ConnectionPtr &)> WriteFinishedFunc
boost::shared_array< uint8_t > read_buffer_
Read buffer that ends up being passed to the read callback.
volatile uint32_t has_write_callback_
boost::function< void(const ConnectionPtr &, const boost::shared_array< uint8_t > &, uint32_t, bool)> ReadFinishedFunc
bool reading_
Flag telling us if we're in the middle of a read (mostly to avoid recursive deadlocking)
DropSignal drop_signal_
Signal raised when this connection is dropped.
boost::mutex write_callback_mutex_
void writeTransport()
Write data to our transport. Also manages calling the write callback.
void readTransport()
Read data off our transport. Also manages calling the read callback. If there is any data to be read,...
void read(uint32_t size, const ReadFinishedFunc &finished_callback)
Read a number of bytes, calling a callback when finished.
uint32_t write_sent_
Amount of data we've written from the write buffer.
bool is_server_
Are we a server? Servers wait for clients to send a header and then send a header in response.
void setHeaderReceivedCallback(const HeaderReceivedFunc &func)
Set the header receipt 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::function< void(const ConnectionPtr &, DropReason reason)> DropFunc
void initialize(const TransportPtr &transport, bool is_server, const HeaderReceivedFunc &header_func)
Initialize this connection.
uint32_t write_size_
Size of the write buffer.
bool sending_header_error_
If we're sending a header error we disable most other calls.
void sendHeaderError(const std::string &error_message)
Send a header error message, of the form "error=<message>". Drops the connection once the data has wr...
void onHeaderWritten(const ConnectionPtr &conn)
std::string getCallerId()
bool writing_
Flag telling us if we're in the middle of a write (mostly used to avoid recursive deadlocking)
#define ROS_DEBUG_NAMED(name,...)
bool dropped_
Have we dropped?
void onHeaderLengthRead(const ConnectionPtr &conn, const boost::shared_array< uint8_t > &buffer, uint32_t size, bool success)
boost::recursive_mutex drop_mutex_
Synchronizes drop() calls.
#define ROS_ASSERT_MSG(cond,...)
void onDisconnect(const TransportPtr &transport)
Called by the Transport when it has been disconnected, either through a call to close() or through an...
HeaderReceivedFunc header_func_
Function that handles the incoming header.
boost::recursive_mutex read_mutex_
Mutex used for protecting reading. Recursive because a read can immediately cause another read throug...
boost::shared_array< uint8_t > write_buffer_
Buffer to write from.
volatile uint32_t has_read_callback_
WriteFinishedFunc write_callback_
Function to call when the current write is finished.
TransportPtr transport_
Transport associated with us.
WriteFinishedFunc header_written_callback_
Function to call when the outgoing header has finished writing.
std::string getRemoteString()
boost::function< bool(const ConnectionPtr &, const Header &)> HeaderReceivedFunc
void onHeaderRead(const ConnectionPtr &conn, const boost::shared_array< uint8_t > &buffer, uint32_t size, bool success)
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 drop(DropReason reason)
Drop this connection. Anything added as a drop listener through addDropListener will get called back ...
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.
Header header_
Incoming header.
boost::recursive_mutex write_mutex_
Mutex used for protecting writing. Recursive because a write can immediately cause another write thro...
void removeDropListener(const boost::signals2::connection &c)
#define ROSCPP_LOG_DEBUG(...)
bool isDropped()
Returns whether or not this connection has been dropped.
std::map< std::string, std::string > M_string
roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim, Dirk Thomas
, Jacob Perron
autogenerated on Sat Sep 14 2024 02:59:35