23 #include "tcp_server.h" 25 boost::shared_ptr<goby::util::TCPConnection>
26 goby::util::TCPConnection::create(LineBasedInterface* interface)
28 return boost::shared_ptr<TCPConnection>(
new TCPConnection(interface));
31 void goby::util::TCPConnection::socket_write(
const protobuf::Datagram& line)
33 bool write_in_progress = !out().empty();
34 out().push_back(line);
35 if (!write_in_progress)
39 void goby::util::TCPConnection::socket_close(
const boost::system::error_code& error)
42 boost::asio::error::operation_aborted)
49 void goby::util::TCPServer::do_write(
const protobuf::Datagram& line)
53 std::map<Endpoint, boost::shared_ptr<TCPConnection> >::iterator it =
54 connections_.find(line.dest());
55 if (it != connections_.end())
56 (it->second)->write(line);
60 for (std::map<Endpoint, boost::shared_ptr<TCPConnection> >::iterator
61 it = connections_.begin(),
62 end = connections_.end();
64 (it->second)->write(line);
69 void goby::util::TCPServer::do_close(
const boost::system::error_code& error,
70 goby::util::TCPServer::Endpoint endpt )
74 std::map<Endpoint, boost::shared_ptr<TCPConnection> >::iterator it =
75 connections_.find(endpt);
76 if (it != connections_.end())
77 (it->second)->close(error);
81 for (std::map<Endpoint, boost::shared_ptr<TCPConnection> >::iterator
82 it = connections_.begin(),
83 end = connections_.end();
85 (it->second)->close(error);
89 const std::map<goby::util::TCPServer::Endpoint, boost::shared_ptr<goby::util::TCPConnection> >&
90 goby::util::TCPServer::connections()
92 typedef std::map<Endpoint, boost::shared_ptr<TCPConnection> >::iterator It;
93 It it = connections_.begin(), it_end = connections_.end();
96 if (!(it->second)->socket().is_open())
100 connections_.erase(to_delete);
110 void goby::util::TCPServer::start_accept()
112 new_connection_ = TCPConnection::create(
this);
113 acceptor_.async_accept(new_connection_->socket(),
114 boost::bind(&TCPServer::handle_accept,
this, new_connection_,
115 boost::asio::placeholders::error));
118 void goby::util::TCPServer::handle_accept(boost::shared_ptr<TCPConnection> new_connection,
119 const boost::system::error_code& error)
123 new_connection_->start();
124 connections_.insert(std::make_pair(new_connection_->remote_endpoint(), new_connection_));