Goby3 3.6.1
2026.09.15
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 auto endpoints = resolver.resolve(this->cfg().remote_address(),
91 std::to_string(this->cfg().remote_port()),
92 boost::asio::ip::resolver_base::numeric_service);
93 if (endpoints.begin() == endpoints.end())
94 throw(goby::Exception("Failed to resolve UDP remote endpoint: " +
95 this->cfg().remote_address() + ":" +
96 std::to_string(this->cfg().remote_port())));
97 remote_endpoint_ = endpoints.begin()->endpoint();
98
100 this->interthread().template publish<line_in_group>(ready);
101 }
102
104
105 private:
107 void async_write(std::shared_ptr<const goby::middleware::protobuf::IOData> io_msg) override;
108
109 private:
110 boost::asio::ip::udp::endpoint remote_endpoint_;
111};
112} // namespace io
113} // namespace middleware
114} // namespace goby
115
116template <const goby::middleware::Group& line_in_group,
117 const goby::middleware::Group& line_out_group,
119 goby::middleware::io::PubSubLayer subscribe_layer, template <class> class ThreadType,
120 bool use_indexed_groups>
121void goby::middleware::io::UDPPointToPointThread<line_in_group, line_out_group, publish_layer,
122 subscribe_layer, ThreadType, use_indexed_groups>::
123 async_write(std::shared_ptr<const goby::middleware::protobuf::IOData> io_msg)
124{
125 this->mutable_socket().async_send_to(
126 boost::asio::buffer(io_msg->data()), remote_endpoint_,
127 [this, io_msg](const boost::system::error_code& ec, std::size_t bytes_transferred)
128 {
129 if (!ec && bytes_transferred > 0)
130 {
131 this->handle_write_success(bytes_transferred);
132 }
133 else
134 {
135 this->handle_write_error(ec);
136 }
137 });
138}
139
140#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: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.