23 #ifndef TCPServer20100719H 24 #define TCPServer20100719H 31 #include <boost/asio.hpp> 32 #include <boost/bind.hpp> 33 #include <boost/enable_shared_from_this.hpp> 34 #include <boost/foreach.hpp> 35 #include <boost/shared_ptr.hpp> 37 #include "goby/util/as.h" 39 #include "connection.h" 40 #include "interface.h" 56 TCPServer(
unsigned port,
const std::string& delimiter =
"\r\n")
58 acceptor_(io_service(),
boost::asio::ip::tcp::endpoint(
boost::asio::ip::tcp::v4(), port))
63 typedef std::string Endpoint;
64 void close(
const Endpoint& endpoint)
67 boost::bind(&TCPServer::do_close,
this, boost::system::error_code(), endpoint));
71 std::string
local_endpoint() {
return goby::util::as<std::string>(acceptor_.local_endpoint()); }
73 const std::map<Endpoint, boost::shared_ptr<TCPConnection> >& connections();
86 void do_close(
const boost::system::error_code& error) { do_close(error,
""); }
87 void do_close(
const boost::system::error_code& error, Endpoint endpt);
91 void handle_accept(boost::shared_ptr<TCPConnection> new_connection,
92 const boost::system::error_code& error);
96 boost::asio::ip::tcp::acceptor acceptor_;
97 boost::shared_ptr<TCPConnection> new_connection_;
98 std::map<Endpoint, boost::shared_ptr<TCPConnection> > connections_;
107 boost::asio::ip::tcp::socket& socket() {
return socket_; }
109 void start() { socket_.get_io_service().post(boost::bind(&TCPConnection::read_start,
this)); }
113 socket_.get_io_service().post(boost::bind(&TCPConnection::socket_write,
this, msg));
116 void close(
const boost::system::error_code& error)
118 socket_.get_io_service().post(boost::bind(&TCPConnection::socket_close,
this, error));
121 std::string
local_endpoint() {
return goby::util::as<std::string>(socket_.local_endpoint()); }
122 std::string remote_endpoint() {
return goby::util::as<std::string>(socket_.remote_endpoint()); }
126 void socket_close(
const boost::system::error_code& error);
130 socket_(interface->io_service())
135 boost::asio::ip::tcp::socket socket_;
TCPServer(unsigned port, const std::string &delimiter="\r\n")
create a TCP server
provides a basic TCP server for line by line text based communications to a one or more remote TCP cl...
The global namespace for the Goby project.
std::string local_endpoint()
string representation of the local endpoint (e.g. 192.168.1.105:54230
basic interface class for all the derived serial (and networking mimics) line-based nodes (serial...