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>
41 #define HEADER_LENGTH 8
56 class GZ_TRANSPORT_VISIBLE ConnectionReadTask :
public tbb::task
62 public: ConnectionReadTask(
63 boost::function<
void (
const std::string &)> _func,
64 const std::string &_data) :
72 public: tbb::task *execute()
74 this->func(this->data);
79 private: boost::function<void (const std::string &)> func;
82 private: std::string data;
101 public boost::enable_shared_from_this<Connection>
113 public:
bool Connect(
const std::string &_host,
unsigned int _port);
122 public:
void Listen(
unsigned int _port,
const AcceptCallback &_acceptCB);
133 public:
void StopRead();
136 public:
void Shutdown();
140 public:
bool IsOpen()
const;
143 private:
void Close();
146 public:
void Cancel();
151 public:
bool Read(std::string &_data);
160 public:
void EnqueueMsg(
const std::string &_buffer,
161 boost::function<
void(uint32_t)> _cb, uint32_t _id,
162 bool _force =
false);
168 public:
void EnqueueMsg(
const std::string &_buffer,
bool _force =
false);
172 public: std::string GetLocalURI()
const;
176 public: std::string GetRemoteURI()
const;
180 public: std::string GetLocalAddress()
const;
184 public:
unsigned int GetLocalPort()
const;
188 public: std::string GetRemoteAddress()
const;
192 public:
unsigned int GetRemotePort()
const;
196 public: std::string GetRemoteHostname()
const;
200 public:
static std::string GetLocalHostname();
204 public:
template<
typename Handler>
207 boost::mutex::scoped_lock lock(this->socketMutex);
210 gzerr <<
"AsyncRead on a closed socket\n";
214 void (
Connection::*f)(
const boost::system::error_code &,
215 boost::tuple<Handler>) = &Connection::OnReadHeader<Handler>;
218 boost::asio::async_read(*this->socket,
219 boost::asio::buffer(this->inboundHeader),
221 boost::asio::placeholders::error,
222 boost::make_tuple(_handler)));
232 private:
template<
typename Handler>
233 void OnReadHeader(
const boost::system::error_code &_e,
234 boost::tuple<Handler> _handler)
238 if (_e.message() ==
"End of file")
239 this->isOpen =
false;
243 std::size_t inboundData_size = 0;
244 std::string header(&this->inboundHeader[0],
245 this->inboundHeader.size());
246 this->inboundHeader.clear();
248 inboundData_size = this->ParseHeader(header);
250 if (inboundData_size > 0)
253 this->inboundData.resize(inboundData_size);
255 void (Connection::*f)(
const boost::system::error_code &e,
256 boost::tuple<Handler>) =
257 &Connection::OnReadData<Handler>;
259 boost::asio::async_read(*this->socket,
260 boost::asio::buffer(this->inboundData),
262 boost::asio::placeholders::error,
267 gzerr <<
"Header is empty\n";
268 boost::get<0>(_handler)(
"");
292 private:
template<
typename Handler>
293 void OnReadData(
const boost::system::error_code &_e,
294 boost::tuple<Handler> _handler)
298 if (_e.message() ==
"End of file")
299 this->isOpen =
false;
303 std::string data(&this->inboundData[0],
304 this->inboundData.size());
305 this->inboundData.clear();
308 gzerr <<
"OnReadData got empty data!!!\n";
312 ConnectionReadTask *task =
new(tbb::task::allocate_root())
313 ConnectionReadTask(boost::get<0>(_handler), data);
314 tbb::task::enqueue(*task);
326 {
return this->
shutdown.Connect(_subscriber); }
335 {this->
shutdown.Disconnect(_subscriber->Id());}
338 public:
void ProcessWriteQueue(
bool _blocking =
false);
342 public:
unsigned int GetId()
const;
347 public:
static bool ValidateIP(
const std::string &_ip);
352 public: std::string GetIPWhiteList()
const;
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);
368 private:
void ReadLoop(
const ReadCallback &_cb);
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;
401 std::pair<boost::function<void(uint32_t)>, uint32_t> > callbacks;
404 private: boost::mutex connectMutex;
407 private: boost::recursive_mutex writeMutex;
410 private: boost::recursive_mutex readMutex;
413 private:
mutable boost::mutex socketMutex;
416 private: boost::condition_variable connectCondition;
419 private: AcceptCallback acceptCB;
422 private: std::vector<char> inboundHeader;
425 private: std::vector<char> inboundData;
428 private:
bool readQuit;
431 private:
unsigned int id;
434 private:
static unsigned int idCounter;
446 private:
unsigned int writeCount;
449 private: std::string localURI;
452 private: std::string localAddress;
455 private: std::string remoteURI;
458 private: std::string remoteAddress;
461 private:
bool connectError;
464 private: std::string ipWhiteList;
467 private:
char *headerBuffer;
470 private:
bool dropMsgLogged;
474 private:
unsigned int callbackIndex;
477 private:
bool isOpen;
boost::function< void(const ConnectionPtr &)> AcceptCallback
The signature of a connection accept callback.
Definition: Connection.hh:116
boost::function< void(const std::string &_data)> ReadCallback
The signature of a connection read callback.
Definition: Connection.hh:125
#define HEADER_LENGTH
Definition: Connection.hh:41
#define gzerr
Output an error message.
Definition: Console.hh:50
Manages boost::asio IO.
Definition: IOManager.hh:33
void DisconnectShutdown(event::ConnectionPtr _subscriber) GAZEBO_DEPRECATED(8.0)
Unregister a function to be called when the connection is shut down.
Definition: Connection.hh:333
event::ConnectionPtr ConnectToShutdown(boost::function< void()> _subscriber)
Register a function to be called when the connection is shut down.
Definition: Connection.hh:324
boost::shared_ptr< Connection > ConnectionPtr
Definition: Connection.hh:50
GAZEBO_VISIBLE bool shutdown()
Stop and cleanup simulation.
boost::shared_ptr< Connection > ConnectionPtr
Definition: CommonTypes.hh:134
#define NULL
Definition: CommonTypes.hh:31
#define GAZEBO_DEPRECATED(version)
Definition: system.hh:302
void AsyncRead(Handler _handler)
Peform an asyncronous read param[in] _handler Callback to invoke on received data.
Definition: Connection.hh:205
bool is_stopped()
Is the transport system stopped?
Single TCP/IP connection manager.
Definition: Connection.hh:100