Goby3  3.1.5
2024.05.14
tcp_server.h
Go to the documentation of this file.
1 // Copyright 2010-2021:
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 // Community contributors (see AUTHORS file)
5 // File authors:
6 // Toby Schneider <toby@gobysoft.org>
7 //
8 //
9 // This file is part of the Goby Underwater Autonomy Project Libraries
10 // ("The Goby Libraries").
11 //
12 // The Goby Libraries are free software: you can redistribute them and/or modify
13 // them under the terms of the GNU Lesser General Public License as published by
14 // the Free Software Foundation, either version 2.1 of the License, or
15 // (at your option) any later version.
16 //
17 // The Goby Libraries are distributed in the hope that they will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU Lesser General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public License
23 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
24 
25 #ifndef GOBY_UTIL_LINEBASEDCOMMS_TCP_SERVER_H
26 #define GOBY_UTIL_LINEBASEDCOMMS_TCP_SERVER_H
27 
28 #include <atomic>
29 #include <memory>
30 #include <string>
31 #include <thread>
32 
35 
36 #include "interface.h"
37 
38 namespace goby
39 {
40 namespace util
41 {
44 {
45  public:
50  TCPServer(unsigned port, const std::string& delimiter = "\r\n");
51  ~TCPServer() override;
52 
54  std::string local_endpoint() override
55  {
56  return local_endpoint_.addr() + ":" + std::to_string(local_endpoint_.port());
57  }
58 
59  const std::set<middleware::protobuf::TCPEndPoint>& remote_endpoints()
60  {
61  return remote_endpoints_;
62  }
63 
64  private:
65  void do_subscribe() override;
66  void do_start() override;
67  void do_close() override;
68 
69  private:
73  goby::middleware::io::PubSubLayer::INTERTHREAD, goby::middleware::protobuf::TCPServerConfig,
75 
76  std::atomic<bool> tcp_alive_{false};
77  std::unique_ptr<std::thread> tcp_thread_;
78  unsigned port_;
79 
80  goby::middleware::protobuf::TCPServerEvent event_;
81 
82  goby::middleware::protobuf::TCPEndPoint local_endpoint_;
83  std::set<middleware::protobuf::TCPEndPoint> remote_endpoints_;
84 };
85 
86 } // namespace util
87 } // namespace goby
88 
89 #endif
basic interface class for all the derived serial (and networking mimics) line-based nodes (serial,...
Definition: interface.h:62
std::string delimiter() const
Definition: interface.h:107
provides a basic TCP server for line by line text based communications to a one or more remote TCP cl...
Definition: tcp_server.h:44
const std::set< middleware::protobuf::TCPEndPoint > & remote_endpoints()
Definition: tcp_server.h:59
TCPServer(unsigned port, const std::string &delimiter="\r\n")
create a TCP server
std::string local_endpoint() override
string representation of the local endpoint (e.g. 192.168.1.105:54230)
Definition: tcp_server.h:54
NLOHMANN_BASIC_JSON_TPL_DECLARATION std::string to_string(const NLOHMANN_BASIC_JSON_TPL &j)
user-defined to_string function for JSON values
Definition: json.hpp:24301
constexpr goby::middleware::Group linebasedcomms_in
Definition: interface.h:56
constexpr goby::middleware::Group linebasedcomms_out
Definition: interface.h:57
The global namespace for the Goby project.