23 #ifndef TCP_SESSION_20180131H
24 #define TCP_SESSION_20180131H
30 #include <unordered_map>
34 #include <boost/asio.hpp>
36 #include <google/protobuf/message.h>
38 #include <dccl/binary.h>
40 #include <goby/util/asio_compat.h>
50 virtual void post(
const std::string& pb_name,
const std::string& bytes,
51 const boost::asio::ip::tcp::endpoint& remote) = 0;
57 using CallbackType = std::function<void(
const ProtobufMessage& msg,
58 const boost::asio::ip::tcp::endpoint& remote)>;
61 void post(
const std::string& pb_name,
const std::string& bytes,
62 const boost::asio::ip::tcp::endpoint& remote)
65 msg.ParseFromString(bytes);
76 using CallbackType = std::function<void(
const std::string& pb_name,
const std::string& bytes,
77 const boost::asio::ip::tcp::endpoint& remote)>;
79 void post(
const std::string& pb_name,
const std::string& bytes,
80 const boost::asio::ip::tcp::endpoint& remote)
82 f_(pb_name, bytes, remote);
89 class tcp_session :
public std::enable_shared_from_this<tcp_session>
99 rx_all_.insert(std::move(rx));
103 template <
typename ProtobufMessage>
107 rx_callbacks_.insert(
108 std::make_pair(ProtobufMessage::descriptor()->full_name(), std::move(rx)));
111 void write(
const google::protobuf::Message& message);
118 return socket_->remote_endpoint();
120 return boost::asio::ip::tcp::endpoint();
124 std::unique_ptr<boost::asio::ip::tcp::socket>&
socket() {
return socket_; }
130 void data_read(std::uint32_t size);
133 std::unique_ptr<boost::asio::ip::tcp::socket> socket_;
136 std::shared_ptr<netsim::tcp_session> self_;
139 std::unordered_multimap<std::string, std::unique_ptr<ReceiveBase>> rx_callbacks_;
140 std::set<std::unique_ptr<ReceiveBase>> rx_all_;
142 boost::asio::streambuf buffer_;
144 const char end_of_line_{
'\n'};
145 const char delimiter_{
'|'};
146 const std::string preamble_{
"NETSIM"};