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.value() == boost::asio::error::eof)
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.value() == boost::asio::error::eof)
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); }
329 public:
void ProcessWriteQueue(
bool _blocking =
false);
333 public:
unsigned int GetId()
const;
338 public:
static bool ValidateIP(
const std::string &_ip);
343 public: std::string GetIPWhiteList()
const;
347 private:
void PostWrite();
352 private:
void OnWrite(
const boost::system::error_code &_e);
356 private:
void OnAccept(
const boost::system::error_code &_e);
360 private: std::size_t ParseHeader(
const std::string &_header);
363 private:
void ReadLoop(
const ReadCallback &_cb);
367 private:
static boost::asio::ip::tcp::endpoint GetLocalEndpoint();
371 private: boost::asio::ip::tcp::endpoint GetRemoteEndpoint()
const;
375 private:
static std::string GetHostname(
376 boost::asio::ip::tcp::endpoint _ep);
381 private:
void OnConnect(
const boost::system::error_code &_error,
382 boost::asio::ip::tcp::resolver::iterator _endPointIter);
385 private: boost::asio::ip::tcp::socket *socket;
388 private: boost::asio::ip::tcp::acceptor *acceptor;
391 private: std::deque<std::string> writeQueue;
395 private: std::deque< std::vector<
396 std::pair<boost::function<void(uint32_t)>, uint32_t> > >
400 private: boost::mutex connectMutex;
403 private: boost::recursive_mutex writeMutex;
406 private: boost::recursive_mutex readMutex;
409 private:
mutable boost::mutex socketMutex;
412 private: boost::condition_variable connectCondition;
415 private: AcceptCallback acceptCB;
418 private: std::vector<char> inboundHeader;
421 private: std::vector<char> inboundData;
424 private:
bool readQuit;
427 private:
unsigned int id;
430 private:
static unsigned int idCounter;
442 private:
unsigned int writeCount;
445 private: std::string localURI;
448 private: std::string localAddress;
451 private: std::string remoteURI;
454 private: std::string remoteAddress;
457 private:
bool connectError;
460 private: std::string ipWhiteList;
463 private:
bool dropMsgLogged;
466 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
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
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