17 #ifndef _CONNECTION_HH_
18 #define _CONNECTION_HH_
21 #include <google/protobuf/message.h>
23 #include <boost/asio.hpp>
24 #include <boost/bind.hpp>
25 #include <boost/function.hpp>
26 #include <boost/thread.hpp>
27 #include <boost/tuple/tuple.hpp>
42 #define HEADER_LENGTH 8
57 class GZ_TRANSPORT_VISIBLE ConnectionReadTask :
public tbb::task
63 public: ConnectionReadTask(
64 boost::function<
void (
const std::string &)> _func,
65 const std::string &_data) :
73 public: tbb::task *execute()
75 this->func(this->data);
80 private: boost::function<void (
const std::string &)> func;
83 private: std::string data;
102 public boost::enable_shared_from_this<Connection>
114 public:
bool Connect(
const std::string &_host,
unsigned int _port);
144 private:
void Close();
152 public:
bool Read(std::string &_data);
162 boost::function<
void(uint32_t)> _cb, uint32_t _id,
163 bool _force =
false);
169 public:
void EnqueueMsg(
const std::string &_buffer,
bool _force =
false);
205 public:
template<
typename Handler>
208 boost::mutex::scoped_lock lock(this->socketMutex);
211 gzerr <<
"AsyncRead on a closed socket\n";
215 void (
Connection::*f)(
const boost::system::error_code &,
216 boost::tuple<Handler>) = &Connection::OnReadHeader<Handler>;
219 boost::asio::async_read(*this->socket,
220 boost::asio::buffer(this->inboundHeader),
222 boost::asio::placeholders::error,
223 boost::make_tuple(_handler)));
233 private:
template<
typename Handler>
234 void OnReadHeader(
const boost::system::error_code &_e,
235 boost::tuple<Handler> _handler)
239 if (_e.value() == boost::asio::error::eof)
240 this->isOpen =
false;
244 std::size_t inboundData_size = 0;
245 std::string header(&this->inboundHeader[0],
246 this->inboundHeader.size());
247 this->inboundHeader.clear();
249 inboundData_size = this->ParseHeader(header);
251 if (inboundData_size > 0)
254 this->inboundData.resize(inboundData_size);
256 void (Connection::*f)(
const boost::system::error_code &e,
257 boost::tuple<Handler>) =
258 &Connection::OnReadData<Handler>;
260 boost::asio::async_read(*this->socket,
261 boost::asio::buffer(this->inboundData),
263 boost::asio::placeholders::error,
268 gzerr <<
"Header is empty\n";
269 boost::get<0>(_handler)(
"");
293 private:
template<
typename Handler>
294 void OnReadData(
const boost::system::error_code &_e,
295 boost::tuple<Handler> _handler)
299 if (_e.value() == boost::asio::error::eof)
300 this->isOpen =
false;
304 std::string data(&this->inboundData[0],
305 this->inboundData.size());
306 this->inboundData.clear();
309 gzerr <<
"OnReadData got empty data!!!\n";
313 ConnectionReadTask *task =
new(tbb::task::allocate_root())
314 ConnectionReadTask(boost::get<0>(_handler), data);
315 tbb::task::enqueue(*task);
327 {
return this->
shutdown.Connect(_subscriber); }
348 private:
void PostWrite();
353 private:
void OnWrite(
const boost::system::error_code &_e);
357 private:
void OnAccept(
const boost::system::error_code &_e);
361 private: std::size_t ParseHeader(
const std::string &_header);
368 private:
static boost::asio::ip::tcp::endpoint GetLocalEndpoint();
372 private: boost::asio::ip::tcp::endpoint GetRemoteEndpoint()
const;
376 private:
static std::string GetHostname(
377 boost::asio::ip::tcp::endpoint _ep);
382 private:
void OnConnect(
const boost::system::error_code &_error,
383 boost::asio::ip::tcp::resolver::iterator _endPointIter);
386 private: boost::asio::ip::tcp::socket *socket;
389 private: boost::asio::ip::tcp::acceptor *acceptor;
392 private: std::deque<std::string> writeQueue;
396 private: std::deque< std::vector<
397 std::pair<boost::function<void(uint32_t)>, uint32_t> > >
401 private: boost::mutex connectMutex;
404 private: boost::recursive_mutex writeMutex;
407 private: boost::recursive_mutex readMutex;
410 private:
mutable boost::mutex socketMutex;
413 private: boost::condition_variable connectCondition;
419 private: std::vector<char> inboundHeader;
422 private: std::vector<char> inboundData;
425 private:
bool readQuit;
428 private:
unsigned int id;
431 private:
static unsigned int idCounter;
443 private:
unsigned int writeCount;
446 private: std::string localURI;
449 private: std::string localAddress;
452 private: std::string remoteURI;
455 private: std::string remoteAddress;
458 private:
bool connectError;
461 private: std::string ipWhiteList;
464 private:
bool dropMsgLogged;
467 private:
bool isOpen;
#define NULL
Definition: CommonTypes.hh:31
transport
Definition: ConnectionManager.hh:35
#define HEADER_LENGTH
Definition: Connection.hh:42
A class for event processing.
Definition: Event.hh:100
Single TCP/IP connection manager.
Definition: Connection.hh:103
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:325
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:206
boost::function< void(const std::string &_data)> ReadCallback
The signature of a connection read callback.
Definition: Connection.hh:126
boost::function< void(const ConnectionPtr &)> AcceptCallback
The signature of a connection accept callback.
Definition: Connection.hh:117
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:110
#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:51
Forward declarations for the common classes.
Definition: Animation.hh:27
GAZEBO_VISIBLE bool shutdown()
Stop and cleanup simulation.