Goby3 3.5.1
2026.06.04
Loading...
Searching...
No Matches
tcp_client_interface.h
Go to the documentation of this file.
1// Copyright 2020-2026:
2// GobySoft, LLC (2013-)
3// Community contributors (see AUTHORS file)
4// File authors:
5// Toby Schneider <toby@gobysoft.org>
6// Copilot <198982749+Copilot@users.noreply.github.com>
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_MIDDLEWARE_IO_DETAIL_TCP_CLIENT_INTERFACE_H
26#define GOBY_MIDDLEWARE_IO_DETAIL_TCP_CLIENT_INTERFACE_H
27
28#include <memory> // for shared_ptr, __sh...
29#include <string> // for string, to_string
30
31#include "goby/middleware/io/detail/io_interface.h" // for PubSubLayer, IOT...
32#include "goby/middleware/protobuf/io.pb.h" // for IOData, TCPEndPo...
33#include "goby/middleware/protobuf/tcp_config.pb.h" // for TCPClientConfig
34#include <boost/asio/ip/tcp.hpp> // for tcp, tcp::endpoint
35
36namespace goby
37{
38namespace middleware
39{
40class Group;
41}
42} // namespace goby
43
44namespace goby
45{
46namespace middleware
47{
48namespace io
49{
50namespace detail
51{
52template <const goby::middleware::Group& line_in_group,
53 const goby::middleware::Group& line_out_group, PubSubLayer publish_layer,
54 PubSubLayer subscribe_layer, typename Config, template <class> class ThreadType,
55 bool use_indexed_groups = false>
57 : public IOThread<line_in_group, line_out_group, publish_layer, subscribe_layer, Config,
58 boost::asio::ip::tcp::socket, ThreadType, use_indexed_groups>
59{
60 using Base = IOThread<line_in_group, line_out_group, publish_layer, subscribe_layer, Config,
61 boost::asio::ip::tcp::socket, ThreadType, use_indexed_groups>;
62
63 using ConfigType = Config;
64
65 public:
68 TCPClientThread(const Config& config, int index = -1)
69 : Base(config, index,
70 std::string("tcp: ") + config.remote_address() + ":" +
71 std::to_string(config.remote_port()))
72 {
73 boost::asio::ip::tcp::resolver resolver(this->mutable_io());
74 remote_endpoint_ =
75 resolver
76 .resolve(this->cfg().remote_address(), std::to_string(this->cfg().remote_port()),
77 boost::asio::ip::resolver_base::numeric_service)
78 .begin()
79 ->endpoint();
80
82 this->interthread().template publish<line_in_group>(ready);
83 }
84
86 {
87 auto event = std::make_shared<goby::middleware::protobuf::TCPClientEvent>();
88 if (this->index() != -1)
89 event->set_index(this->index());
91 *event->mutable_local_endpoint() = endpoint_convert<protobuf::TCPEndPoint>(local_endpoint_);
92 *event->mutable_remote_endpoint() =
93 endpoint_convert<protobuf::TCPEndPoint>(remote_endpoint_);
95 << "Event: " << event->ShortDebugString() << std::endl;
96 this->publish_in(event);
97 }
98
99 protected:
100 void insert_endpoints(std::shared_ptr<goby::middleware::protobuf::IOData>& io_msg)
101 {
102 *io_msg->mutable_tcp_src() = endpoint_convert<protobuf::TCPEndPoint>(remote_endpoint_);
103 *io_msg->mutable_tcp_dest() = endpoint_convert<protobuf::TCPEndPoint>(local_endpoint_);
104 }
105
106 private:
107 void async_write(std::shared_ptr<const goby::middleware::protobuf::IOData> io_msg) override
108 {
109 basic_async_write(this, io_msg);
110 }
111
113 void open_socket() override
114 {
115 this->mutable_socket().connect(remote_endpoint_);
116
117 auto event = std::make_shared<goby::middleware::protobuf::TCPClientEvent>();
118 if (this->index() != -1)
119 event->set_index(this->index());
121 *event->mutable_local_endpoint() = endpoint_convert<protobuf::TCPEndPoint>(local_endpoint_);
122 *event->mutable_remote_endpoint() =
123 endpoint_convert<protobuf::TCPEndPoint>(remote_endpoint_);
125 << "Event: " << event->ShortDebugString() << std::endl;
126 this->publish_in(event);
127
128 local_endpoint_ = this->mutable_socket().local_endpoint();
129 }
130
131 private:
132 boost::asio::ip::tcp::endpoint remote_endpoint_;
133 boost::asio::ip::tcp::endpoint local_endpoint_;
134};
135
136} // namespace detail
137} // namespace io
138} // namespace middleware
139} // namespace goby
140
141#endif
Class for grouping publications in the Goby middleware. Analogous to "topics" in ROS,...
Definition group.h:61
SocketType & mutable_socket()
Access the (mutable) socket (or serial_port) object.
friend void basic_async_write(IOThreadImplementation *this_thread, std::shared_ptr< const goby::middleware::protobuf::IOData > io_msg)
boost::asio::io_context & mutable_io()
TCPClientThread(const Config &config, int index=-1)
Constructs the thread.
void insert_endpoints(std::shared_ptr< goby::middleware::protobuf::IOData > &io_msg)
static constexpr Event EVENT_CONNECT
Definition io.pb.h:1688
static constexpr Event EVENT_DISCONNECT
Definition io.pb.h:1690
goby::util::logger::GroupSetter group(std::string n)
detail namespace with internal helper functions
Definition json.hpp:263
std::string to_string(goby::middleware::protobuf::Layer layer)
Definition common.h:44
The global namespace for the Goby project.
util::FlexOstream glog
Access the Goby logger through this object.
STL namespace.