Goby3 3.5.1
2026.06.04
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// 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_UDP_POINT_TO_POINT_H
26#define GOBY_MIDDLEWARE_IO_UDP_POINT_TO_POINT_H
27
28#include <iosfwd> // for size_t
29#include <memory> // for shared_ptr, __sh...
30#include <string> // for to_string
31
32#include <boost/asio/buffer.hpp> // for buffer
33#include <boost/asio/ip/udp.hpp> // for udp, udp::endpoint
34#include <boost/system/error_code.hpp> // for error_code
35
36#include "goby/middleware/io/detail/io_interface.h" // for PubSubLayer, Pub...
37#include "goby/middleware/protobuf/io.pb.h" // for IOData
39
40#include "udp_one_to_many.h"
41
42namespace goby
43{
44namespace middleware
45{
46class Group;
47}
48} // namespace goby
49namespace goby
50{
51namespace middleware
52{
53namespace protobuf
54{
55class UDPPointToPointConfig;
56}
57} // namespace middleware
58} // namespace goby
59
60namespace goby
61{
62namespace middleware
63{
64namespace io
65{
66template <const goby::middleware::Group& line_in_group,
67 const goby::middleware::Group& line_out_group,
68 // by default publish all incoming traffic to interprocess for logging
70 // but only subscribe on interthread for outgoing traffic
71 PubSubLayer subscribe_layer = PubSubLayer::INTERTHREAD,
72 template <class> class ThreadType = goby::zeromq::SimpleThread,
73 bool use_indexed_groups = false>
75 : public UDPOneToManyThread<line_in_group, line_out_group, publish_layer, subscribe_layer,
76 goby::middleware::protobuf::UDPPointToPointConfig, ThreadType,
77 use_indexed_groups>
78{
79 using Base = UDPOneToManyThread<line_in_group, line_out_group, publish_layer, subscribe_layer,
81 use_indexed_groups>;
82
83 public:
87 int index = -1)
88 : Base(config, index, false)
89 {
90 boost::asio::ip::udp::resolver resolver(this->mutable_io());
91 auto endpoints = resolver.resolve(this->cfg().remote_address(),
92 std::to_string(this->cfg().remote_port()),
93 boost::asio::ip::resolver_base::numeric_service);
94 if (endpoints.begin() == endpoints.end())
95 throw(goby::Exception("Failed to resolve UDP remote endpoint: " +
96 this->cfg().remote_address() + ":" +
97 std::to_string(this->cfg().remote_port())));
98 remote_endpoint_ = endpoints.begin()->endpoint();
99
101 this->interthread().template publish<line_in_group>(ready);
102 }
103
105
106 private:
108 void async_write(std::shared_ptr<const goby::middleware::protobuf::IOData> io_msg) override;
109
110 private:
111 boost::asio::ip::udp::endpoint remote_endpoint_;
112};
113} // namespace io
114} // namespace middleware
115} // namespace goby
116
117template <const goby::middleware::Group& line_in_group,
118 const goby::middleware::Group& line_out_group,
120 goby::middleware::io::PubSubLayer subscribe_layer, template <class> class ThreadType,
121 bool use_indexed_groups>
122void goby::middleware::io::UDPPointToPointThread<line_in_group, line_out_group, publish_layer,
123 subscribe_layer, ThreadType, use_indexed_groups>::
124 async_write(std::shared_ptr<const goby::middleware::protobuf::IOData> io_msg)
125{
126 this->mutable_socket().async_send_to(
127 boost::asio::buffer(io_msg->data()), remote_endpoint_,
128 [this, io_msg](const boost::system::error_code& ec, std::size_t bytes_transferred)
129 {
130 if (!ec && bytes_transferred > 0)
131 {
132 this->handle_write_success(bytes_transferred);
133 }
134 else
135 {
136 this->handle_write_error(ec);
137 }
138 });
139}
140
141#endif
simple exception class for goby applications
Definition exception.h:35
Class for grouping publications in the Goby middleware. Analogous to "topics" in ROS,...
Definition group.h:61
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.