Goby3  3.1.5
2024.05.14
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 
79  goby::middleware::io::PubSubLayer::INTERTHREAD, goby::middleware::protobuf::TCPClientConfig,
81 
82  std::atomic<bool> tcp_alive_{false};
83  std::unique_ptr<std::thread> tcp_thread_;
84 
85  goby::middleware::protobuf::TCPClientEvent event_;
86 
87  goby::middleware::protobuf::TCPEndPoint remote_endpoint_;
88  goby::middleware::protobuf::TCPEndPoint local_endpoint_;
89 };
90 } // namespace util
91 } // namespace goby
92 
93 #endif
Reads/Writes strings from/to a TCP connection using a line-based (typically ASCII) protocol with a de...
Definition: tcp_client.h:77
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 client for line by line text based communications to a remote TCP server
Definition: tcp_client.h:44
TCPClient(std::string server, unsigned port, const std::string &delimiter="\r\n", int retry_interval=10)
create a TCPClient
std::string remote_endpoint() override
string representation of the remote endpoint, (e.g. 192.168.1.106:50000)
Definition: tcp_client.h:62
std::string local_endpoint() override
string representation of the local endpoint (e.g. 192.168.1.105:54230)
Definition: tcp_client.h:57
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.