Goby3 3.2.3
2025.05.13
Loading...
Searching...
No Matches
tcp_client_interface.h
Go to the documentation of this file.
1// Copyright 2020-2023:
2// GobySoft, LLC (2013-)
3// Community contributors (see AUTHORS file)
4// File authors:
5// Toby Schneider <toby@gobysoft.org>
6//
7//
8// This file is part of the Goby Underwater Autonomy Project Libraries
9// ("The Goby Libraries").
10//
11// The Goby Libraries are free software: you can redistribute them and/or modify
12// them under the terms of the GNU Lesser General Public License as published by
13// the Free Software Foundation, either version 2.1 of the License, or
14// (at your option) any later version.
15//
16// The Goby Libraries are distributed in the hope that they will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU Lesser General Public License for more details.
20//
21// You should have received a copy of the GNU Lesser General Public License
22// along with Goby. If not, see <http://www.gnu.org/licenses/>.
23
24#ifndef GOBY_MIDDLEWARE_IO_DETAIL_TCP_CLIENT_INTERFACE_H
25#define GOBY_MIDDLEWARE_IO_DETAIL_TCP_CLIENT_INTERFACE_H
26
27#include <memory> // for shared_ptr, __sh...
28#include <string> // for string, to_string
29
30#include "goby/middleware/io/detail/io_interface.h" // for PubSubLayer, IOT...
31#include "goby/middleware/protobuf/io.pb.h" // for IOData, TCPEndPo...
32#include "goby/middleware/protobuf/tcp_config.pb.h" // for TCPClientConfig
33#include <boost/asio/ip/tcp.hpp> // for tcp, tcp::endpoint
34
35namespace goby
36{
37namespace middleware
38{
39class Group;
40}
41} // namespace goby
42
43namespace goby
44{
45namespace middleware
46{
47namespace io
48{
49namespace detail
50{
51template <const goby::middleware::Group& line_in_group,
52 const goby::middleware::Group& line_out_group, PubSubLayer publish_layer,
53 PubSubLayer subscribe_layer, typename Config, template <class> class ThreadType,
54 bool use_indexed_groups = false>
56 : public IOThread<line_in_group, line_out_group, publish_layer, subscribe_layer, Config,
57 boost::asio::ip::tcp::socket, ThreadType, use_indexed_groups>
58{
59 using Base = IOThread<line_in_group, line_out_group, publish_layer, subscribe_layer, Config,
60 boost::asio::ip::tcp::socket, ThreadType, use_indexed_groups>;
61
62 using ConfigType = Config;
63
64 public:
67 TCPClientThread(const Config& config, int index = -1)
68 : Base(config, index,
69 std::string("tcp: ") + config.remote_address() + ":" +
70 std::to_string(config.remote_port()))
71 {
72 boost::asio::ip::tcp::resolver resolver(this->mutable_io());
73 remote_endpoint_ = *resolver.resolve(
74 {this->cfg().remote_address(), std::to_string(this->cfg().remote_port()),
75 boost::asio::ip::resolver_query_base::numeric_service});
76
78 this->interthread().template publish<line_in_group>(ready);
79 }
80
82 {
83 auto event = std::make_shared<goby::middleware::protobuf::TCPClientEvent>();
84 if (this->index() != -1)
85 event->set_index(this->index());
87 *event->mutable_local_endpoint() = endpoint_convert<protobuf::TCPEndPoint>(local_endpoint_);
88 *event->mutable_remote_endpoint() =
89 endpoint_convert<protobuf::TCPEndPoint>(remote_endpoint_);
91 << "Event: " << event->ShortDebugString() << std::endl;
92 this->publish_in(event);
93 }
94
95 protected:
96 void insert_endpoints(std::shared_ptr<goby::middleware::protobuf::IOData>& io_msg)
97 {
98 *io_msg->mutable_tcp_src() = endpoint_convert<protobuf::TCPEndPoint>(remote_endpoint_);
99 *io_msg->mutable_tcp_dest() = endpoint_convert<protobuf::TCPEndPoint>(local_endpoint_);
100 }
101
102 private:
103 void async_write(std::shared_ptr<const goby::middleware::protobuf::IOData> io_msg) override
104 {
105 basic_async_write(this, io_msg);
106 }
107
109 void open_socket() override
110 {
111 this->mutable_socket().connect(remote_endpoint_);
112
113 auto event = std::make_shared<goby::middleware::protobuf::TCPClientEvent>();
114 if (this->index() != -1)
115 event->set_index(this->index());
117 *event->mutable_local_endpoint() = endpoint_convert<protobuf::TCPEndPoint>(local_endpoint_);
118 *event->mutable_remote_endpoint() =
119 endpoint_convert<protobuf::TCPEndPoint>(remote_endpoint_);
121 << "Event: " << event->ShortDebugString() << std::endl;
122 this->publish_in(event);
123
124 local_endpoint_ = this->mutable_socket().local_endpoint();
125 }
126
127 private:
128 boost::asio::ip::tcp::endpoint remote_endpoint_;
129 boost::asio::ip::tcp::endpoint local_endpoint_;
130};
131
132} // namespace detail
133} // namespace io
134} // namespace middleware
135} // namespace goby
136
137#endif
Class for grouping publications in the Goby middleware. Analogous to "topics" in ROS,...
Definition group.h:60
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:247
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.