NETSIM  1.0.0
Hardware-in-the-loopacousticnetworksimulator
tcp_server.h
Go to the documentation of this file.
1 // Copyright 2020:
2 // GobySoft, LLC (2017-)
3 // Massachusetts Institute of Technology (2017-)
4 // File authors:
5 // Toby Schneider <toby@gobysoft.org>
6 //
7 //
8 // This file is part of the NETSIM Libraries.
9 //
10 // The NETSIM Libraries are free software: you can redistribute them and/or modify
11 // them under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 2.1 of the License, or
13 // (at your option) any later version.
14 //
15 // The NETSIM Libraries are distributed in the hope that they will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with NETSIM. If not, see <http://www.gnu.org/licenses/>.
22 
23 #ifndef TCP_SERVER_20180131H
24 #define TCP_SERVER_20180131H
25 
26 #include <list>
27 
28 #include "tcp_session.h"
29 
30 namespace netsim
31 {
33 {
34  public:
35  tcp_server(boost::asio::io_service& io_service, short port);
36  void write(const google::protobuf::Message& message, const boost::asio::ip::tcp::endpoint& ep);
37 
38  template <typename ProtobufMessage>
40  {
41  std::unique_ptr<ReceiveBase> rx(new Receive<ProtobufMessage>(f));
42  rx_callbacks_.insert(
43  std::make_pair(ProtobufMessage::descriptor()->full_name(), std::move(rx)));
44  }
45 
46  private:
47  void accept();
48  std::map<boost::asio::ip::tcp::endpoint, std::weak_ptr<tcp_session>> sessions_;
49  boost::asio::ip::tcp::acceptor acceptor_;
50  boost::asio::ip::tcp::socket socket_;
51 
52  std::unordered_multimap<std::string, std::unique_ptr<ReceiveBase>> rx_callbacks_;
53 };
54 } // namespace netsim
55 
56 #endif
netsim::tcp_server::tcp_server
tcp_server(boost::asio::io_service &io_service, short port)
netsim::tcp_server
Definition: tcp_server.h:32
netsim::Receive
Definition: tcp_session.h:54
netsim
Definition: environment.h:30
netsim::tcp_server::read_callback
void read_callback(typename Receive< ProtobufMessage >::CallbackType f)
Definition: tcp_server.h:39
netsim::Receive::CallbackType
std::function< void(const ProtobufMessage &msg, const boost::asio::ip::tcp::endpoint &remote)> CallbackType
Definition: tcp_session.h:58
tcp_session.h
netsim::tcp_server::write
void write(const google::protobuf::Message &message, const boost::asio::ip::tcp::endpoint &ep)