17 #ifndef _CONNECTION_HH_
18 #define _CONNECTION_HH_
21 #include <google/protobuf/message.h>
24 #ifndef BOOST_BIND_GLOBAL_PLACEHOLDERS
25 #define BOOST_BIND_GLOBAL_PLACEHOLDERS
27 #include <boost/asio.hpp>
28 #include <boost/bind.hpp>
29 #include <boost/function.hpp>
30 #include <boost/thread.hpp>
31 #include <boost/tuple/tuple.hpp>
46 #define HEADER_LENGTH 8
61 class GZ_TRANSPORT_VISIBLE ConnectionReadTask :
public tbb::task
67 public: ConnectionReadTask(
68 boost::function<
void (
const std::string &)> _func,
69 const std::string &_data) :
77 public: tbb::task *execute()
79 this->func(this->data);
84 private: boost::function<void (
const std::string &)> func;
87 private: std::string data;
106 public boost::enable_shared_from_this<Connection>
118 public:
bool Connect(
const std::string &_host,
unsigned int _port);
148 private:
void Close();
156 public:
bool Read(std::string &_data);
166 boost::function<
void(uint32_t)> _cb, uint32_t _id,
167 bool _force =
false);
173 public:
void EnqueueMsg(
const std::string &_buffer,
bool _force =
false);
209 public:
template<
typename Handler>
212 boost::mutex::scoped_lock lock(this->socketMutex);
215 gzerr <<
"AsyncRead on a closed socket\n";
219 void (
Connection::*f)(
const boost::system::error_code &,
220 boost::tuple<Handler>) = &Connection::OnReadHeader<Handler>;
223 boost::asio::async_read(*this->socket,
224 boost::asio::buffer(this->inboundHeader),
226 boost::asio::placeholders::error,
227 boost::make_tuple(_handler)));
237 private:
template<
typename Handler>
238 void OnReadHeader(
const boost::system::error_code &_e,
239 boost::tuple<Handler> _handler)
243 if (_e.value() == boost::asio::error::eof)
244 this->isOpen =
false;
248 std::size_t inboundData_size = 0;
249 std::string header(&this->inboundHeader[0],
250 this->inboundHeader.size());
251 this->inboundHeader.clear();
253 inboundData_size = this->ParseHeader(header);
255 if (inboundData_size > 0)
258 this->inboundData.resize(inboundData_size);
260 void (Connection::*f)(
const boost::system::error_code &e,
261 boost::tuple<Handler>) =
262 &Connection::OnReadData<Handler>;
264 boost::asio::async_read(*this->socket,
265 boost::asio::buffer(this->inboundData),
267 boost::asio::placeholders::error,
272 gzerr <<
"Header is empty\n";
273 boost::get<0>(_handler)(
"");
297 private:
template<
typename Handler>
298 void OnReadData(
const boost::system::error_code &_e,
299 boost::tuple<Handler> _handler)
303 if (_e.value() == boost::asio::error::eof)
304 this->isOpen =
false;
308 std::string data(&this->inboundData[0],
309 this->inboundData.size());
310 this->inboundData.clear();
313 gzerr <<
"OnReadData got empty data!!!\n";
317 ConnectionReadTask *task =
new(tbb::task::allocate_root())
318 ConnectionReadTask(boost::get<0>(_handler), data);
319 tbb::task::enqueue(*task);
331 {
return this->
shutdown.Connect(_subscriber); }
352 private:
void PostWrite();
357 private:
void OnWrite(
const boost::system::error_code &_e);
361 private:
void OnAccept(
const boost::system::error_code &_e);
365 private: std::size_t ParseHeader(
const std::string &_header);
372 private:
static boost::asio::ip::tcp::endpoint GetLocalEndpoint();
376 private: boost::asio::ip::tcp::endpoint GetRemoteEndpoint()
const;
380 private:
static std::string GetHostname(
381 boost::asio::ip::tcp::endpoint _ep);
386 private:
void OnConnect(
const boost::system::error_code &_error,
387 boost::asio::ip::tcp::resolver::iterator _endPointIter);
390 private: boost::asio::ip::tcp::socket *socket;
393 private: boost::asio::ip::tcp::acceptor *acceptor;
396 private: std::deque<std::string> writeQueue;
400 private: std::deque< std::vector<
401 std::pair<boost::function<void(uint32_t)>, uint32_t> > >
405 private: boost::mutex connectMutex;
408 private: boost::recursive_mutex writeMutex;
411 private: boost::recursive_mutex readMutex;
414 private:
mutable boost::mutex socketMutex;
417 private: boost::condition_variable connectCondition;
423 private: std::vector<char> inboundHeader;
426 private: std::vector<char> inboundData;
429 private:
bool readQuit;
432 private:
unsigned int id;
435 private:
static unsigned int idCounter;
447 private:
unsigned int writeCount;
450 private: std::string localURI;
453 private: std::string localAddress;
456 private: std::string remoteURI;
459 private: std::string remoteAddress;
462 private:
bool connectError;
465 private: std::string ipWhiteList;
468 private:
bool dropMsgLogged;
471 private:
bool isOpen;
#define NULL
Definition: CommonTypes.hh:31
transport
Definition: ConnectionManager.hh:35
#define HEADER_LENGTH
Definition: Connection.hh:46
A class for event processing.
Definition: Event.hh:100
Single TCP/IP connection manager.
Definition: Connection.hh:107
bool IsOpen() const
Is the connection open?
void ProcessWriteQueue(bool _blocking=false)
Handle on-write callbacks.
bool Connect(const std::string &_host, unsigned int _port)
Connect to a remote host.
void EnqueueMsg(const std::string &_buffer, boost::function< void(uint32_t)> _cb, uint32_t _id, bool _force=false)
Write data to the socket.
std::string GetLocalURI() const
Get the local URI.
unsigned int GetId() const
Get the ID of the connection.
void Listen(unsigned int _port, const AcceptCallback &_acceptCB)
Start a server that listens on a port.
void StartRead(const ReadCallback &_cb)
Start a thread that reads from the connection and passes new message to the ReadCallback.
std::string GetRemoteURI() const
Get the remote URI.
std::string GetRemoteHostname() const
Get the remote hostname.
std::string GetIPWhiteList() const
Get the IP white list, from GAZEBO_IP_WHITE_LIST environment variable.
static std::string GetLocalHostname()
Get the local hostname.
unsigned int GetLocalPort() const
Get the port of this connection.
void Cancel()
Cancel all async operations on an open socket.
std::string GetRemoteAddress() const
Get the remote address.
void EnqueueMsg(const std::string &_buffer, bool _force=false)
Write data to the socket.
unsigned int GetRemotePort() const
Get the remote port number.
event::ConnectionPtr ConnectToShutdown(boost::function< void()> _subscriber)
Register a function to be called when the connection is shut down.
Definition: Connection.hh:329
void Shutdown()
Shutdown the socket.
void StopRead()
Stop the read loop.
bool Read(std::string &_data)
Read data from the socket.
std::string GetLocalAddress() const
Get the local address of this connection.
virtual ~Connection()
Destructor.
static bool ValidateIP(const std::string &_ip)
Return true if the _ip is a valid.
void AsyncRead(Handler _handler)
Peform an asyncronous read param[in] _handler Callback to invoke on received data.
Definition: Connection.hh:210
boost::function< void(const std::string &_data)> ReadCallback
The signature of a connection read callback.
Definition: Connection.hh:130
boost::function< void(const ConnectionPtr &)> AcceptCallback
The signature of a connection accept callback.
Definition: Connection.hh:121
Manages boost::asio IO.
Definition: IOManager.hh:36
auto weakBind(Func _func, boost::shared_ptr< T > _ptr, Args... _args) -> decltype(details::makeWeakBinder(boost::bind(_func, _ptr.get(), _args...), boost::weak_ptr< T >(_ptr)))
Definition: WeakBind.hh:114
#define gzerr
Output an error message.
Definition: Console.hh:50
bool is_stopped()
Is the transport system stopped?
boost::shared_ptr< Connection > ConnectionPtr
Definition: CommonTypes.hh:134
boost::shared_ptr< Connection > ConnectionPtr
Definition: Connection.hh:55
Forward declarations for the common classes.
Definition: Animation.hh:27
GAZEBO_VISIBLE bool shutdown()
Stop and cleanup simulation.