Goby3  3.1.5a
2024.05.23
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:
75 
76  std::atomic<bool> tcp_alive_{false};
77  std::unique_ptr<std::thread> tcp_thread_;
78  unsigned port_;
79 
81 
83  std::set<middleware::protobuf::TCPEndPoint> remote_endpoints_;
84 };
85 
86 } // namespace util
87 } // namespace goby
88 
89 #endif
io.pb.h
goby
The global namespace for the Goby project.
Definition: acomms_constants.h:33
goby::util::groups::linebasedcomms_in
constexpr goby::middleware::Group linebasedcomms_in
Definition: interface.h:56
goby::util::TCPServer::~TCPServer
~TCPServer() override
goby::util::TCPServer::remote_endpoints
const std::set< middleware::protobuf::TCPEndPoint > & remote_endpoints()
Definition: tcp_server.h:59
goby::middleware::protobuf::TCPServerConfig
Definition: tcp_config.pb.h:80
interface.h
goby::middleware::protobuf::TCPServerEvent
Definition: io.pb.h:995
goby::util::LineBasedCommsThreadStub
Definition: thread_stub.h:36
goby::middleware::protobuf::TCPEndPoint
Definition: io.pb.h:345
goby::util::TCPServer::local_endpoint
std::string local_endpoint() override
string representation of the local endpoint (e.g. 192.168.1.105:54230)
Definition: tcp_server.h:54
to_string
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
goby::middleware::protobuf::TCPEndPoint::addr
const ::std::string & addr() const
Definition: io.pb.h:1785
goby::util::TCPServer
provides a basic TCP server for line by line text based communications to a one or more remote TCP cl...
Definition: tcp_server.h:43
goby::middleware::io::TCPServerThreadLineBased
Definition: tcp_server.h:105
tcp_server.h
goby::util::LineBasedInterface::delimiter
std::string delimiter() const
Definition: interface.h:107
goby::middleware::protobuf::TCPEndPoint::port
::google::protobuf::uint32 port() const
Definition: io.pb.h:1851
goby::util::groups::linebasedcomms_out
constexpr goby::middleware::Group linebasedcomms_out
Definition: interface.h:57
goby::util::TCPServer::TCPServer
TCPServer(unsigned port, const std::string &delimiter="\r\n")
create a TCP server
goby::util::LineBasedInterface
basic interface class for all the derived serial (and networking mimics) line-based nodes (serial,...
Definition: interface.h:61
goby::middleware::io::PubSubLayer::INTERTHREAD
@ INTERTHREAD