Goby3 3.4.0
2026.04.13
Loading...
Searching...
No Matches
udp_point_to_point.h
Go to the documentation of this file.
1// Copyright 2019-2026:
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_UDP_POINT_TO_POINT_H
25#define GOBY_MIDDLEWARE_IO_UDP_POINT_TO_POINT_H
26
27#include <iosfwd> // for size_t
28#include <memory> // for shared_ptr, __sh...
29#include <string> // for to_string
30
31#include <boost/asio/buffer.hpp> // for buffer
32#include <boost/asio/ip/udp.hpp> // for udp, udp::endpoint
33#include <boost/system/error_code.hpp> // for error_code
34
35#include "goby/middleware/io/detail/io_interface.h" // for PubSubLayer, Pub...
36#include "goby/middleware/protobuf/io.pb.h" // for IOData
38
39#include "udp_one_to_many.h"
40
41namespace goby
42{
43namespace middleware
44{
45class Group;
46}
47} // namespace goby
48namespace goby
49{
50namespace middleware
51{
52namespace protobuf
53{
54class UDPPointToPointConfig;
55}
56} // namespace middleware
57} // namespace goby
58
59namespace goby
60{
61namespace middleware
62{
63namespace io
64{
65template <const goby::middleware::Group& line_in_group,
66 const goby::middleware::Group& line_out_group,
67 // by default publish all incoming traffic to interprocess for logging
69 // but only subscribe on interthread for outgoing traffic
70 PubSubLayer subscribe_layer = PubSubLayer::INTERTHREAD,
71 template <class> class ThreadType = goby::zeromq::SimpleThread,
72 bool use_indexed_groups = false>
74 : public UDPOneToManyThread<line_in_group, line_out_group, publish_layer, subscribe_layer,
75 goby::middleware::protobuf::UDPPointToPointConfig, ThreadType,
76 use_indexed_groups>
77{
78 using Base = UDPOneToManyThread<line_in_group, line_out_group, publish_layer, subscribe_layer,
80 use_indexed_groups>;
81
82 public:
86 int index = -1)
87 : Base(config, index, false)
88 {
89 boost::asio::ip::udp::resolver resolver(this->mutable_io());
90 remote_endpoint_ = *resolver.resolve(
91 {this->cfg().remote_address(), std::to_string(this->cfg().remote_port()),
92 boost::asio::ip::resolver_query_base::numeric_service});
93
95 this->interthread().template publish<line_in_group>(ready);
96 }
97
99
100 private:
102 void async_write(std::shared_ptr<const goby::middleware::protobuf::IOData> io_msg) override;
103
104 private:
105 boost::asio::ip::udp::endpoint remote_endpoint_;
106};
107} // namespace io
108} // namespace middleware
109} // namespace goby
110
111template <const goby::middleware::Group& line_in_group,
112 const goby::middleware::Group& line_out_group,
114 goby::middleware::io::PubSubLayer subscribe_layer, template <class> class ThreadType,
115 bool use_indexed_groups>
116void goby::middleware::io::UDPPointToPointThread<line_in_group, line_out_group, publish_layer,
117 subscribe_layer, ThreadType, use_indexed_groups>::
118 async_write(std::shared_ptr<const goby::middleware::protobuf::IOData> io_msg)
119{
120 this->mutable_socket().async_send_to(
121 boost::asio::buffer(io_msg->data()), remote_endpoint_,
122 [this, io_msg](const boost::system::error_code& ec, std::size_t bytes_transferred)
123 {
124 if (!ec && bytes_transferred > 0)
125 {
126 this->handle_write_success(bytes_transferred);
127 }
128 else
129 {
130 this->handle_write_error(ec);
131 }
132 });
133}
134
135#endif
Class for grouping publications in the Goby middleware. Analogous to "topics" in ROS,...
Definition group.h:60
UDPPointToPointThread(const goby::middleware::protobuf::UDPPointToPointConfig &config, int index=-1)
Constructs the thread.
boost::asio::io_context & mutable_io()
middleware::SimpleThread< Config, detail::InterProcessTag > SimpleThread
Zeromq-backed SimpleThread. Derives from middleware::SimpleThread using InterProcessTag.
The global namespace for the Goby project.