Goby3  3.1.5a
2024.05.23
tcp_client.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_CLIENT_H
26 #define GOBY_UTIL_LINEBASEDCOMMS_TCP_CLIENT_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:
52  TCPClient(std::string server, unsigned port, const std::string& delimiter = "\r\n",
53  int retry_interval = 10);
54  ~TCPClient() override;
55 
57  std::string local_endpoint() override
58  {
59  return local_endpoint_.addr() + ":" + std::to_string(local_endpoint_.port());
60  }
62  std::string remote_endpoint() override
63  {
64  return remote_endpoint_.addr() + ":" + std::to_string(remote_endpoint_.port());
65  }
66 
67  private:
68  void do_subscribe() override;
69  void do_start() override;
70  void do_close() override;
71 
72  private:
73  std::string server_;
74  unsigned port_;
75 
81 
82  std::atomic<bool> tcp_alive_{false};
83  std::unique_ptr<std::thread> tcp_thread_;
84 
86 
89 };
90 } // namespace util
91 } // namespace goby
92 
93 #endif
goby::util::TCPClient::remote_endpoint
std::string remote_endpoint() override
string representation of the remote endpoint, (e.g. 192.168.1.106:50000)
Definition: tcp_client.h:62
io.pb.h
goby
The global namespace for the Goby project.
Definition: acomms_constants.h:33
goby::middleware::protobuf::TCPClientConfig
Definition: tcp_config.pb.h:235
goby::middleware::protobuf::TCPClientEvent
Definition: io.pb.h:1187
goby::util::groups::linebasedcomms_in
constexpr goby::middleware::Group linebasedcomms_in
Definition: interface.h:56
tcp_client.h
interface.h
goby::util::LineBasedCommsThreadStub
Definition: thread_stub.h:36
goby::middleware::protobuf::TCPEndPoint
Definition: io.pb.h:345
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::middleware::io::TCPClientThreadLineBased
Reads/Writes strings from/to a TCP connection using a line-based (typically ASCII) protocol with a de...
Definition: tcp_client.h:74
goby::util::TCPClient::~TCPClient
~TCPClient() override
goby::util::TCPClient
provides a basic TCP client for line by line text based communications to a remote TCP server
Definition: tcp_client.h:43
goby::util::TCPClient::TCPClient
TCPClient(std::string server, unsigned port, const std::string &delimiter="\r\n", int retry_interval=10)
create a TCPClient
goby::util::LineBasedInterface::delimiter
std::string delimiter() const
Definition: interface.h:107
goby::util::TCPClient::local_endpoint
std::string local_endpoint() override
string representation of the local endpoint (e.g. 192.168.1.105:54230)
Definition: tcp_client.h:57
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::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