Goby3  3.1.5a
2024.05.23
udp_driver.h
Go to the documentation of this file.
1 // Copyright 2011-2023:
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 // Community contributors (see AUTHORS file)
5 // File authors:
6 // Toby Schneider <toby@gobysoft.org>
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_ACOMMS_MODEMDRIVER_UDP_DRIVER_H
26 #define GOBY_ACOMMS_MODEMDRIVER_UDP_DRIVER_H
27 
28 #include <array> // for array
29 #include <cstddef> // for size_t
30 #include <cstdint> // for uint32_t
31 #include <map> // for multimap
32 #include <memory> // for unique_ptr
33 #include <set> // for set
34 
35 #include <boost/asio/ip/udp.hpp> // for udp, udp::endpoint
36 
37 #include "goby/acomms/modemdriver/driver_base.h" // for ModemDriverBase
38 #include "goby/acomms/protobuf/driver_base.pb.h" // for DriverConfig
39 #include "goby/util/asio_compat.h" // for io_context
40 
41 namespace boost
42 {
43 namespace system
44 {
45 class error_code;
46 } // namespace system
47 } // namespace boost
48 
49 namespace goby
50 {
51 namespace acomms
52 {
53 namespace protobuf
54 {
55 class ModemTransmission;
56 } // namespace protobuf
57 
58 class UDPDriver : public ModemDriverBase
59 {
60  public:
61  UDPDriver();
62  ~UDPDriver() override;
63 
64  void startup(const protobuf::DriverConfig& cfg) override;
65  void shutdown() override;
66  void do_work() override;
68 
69  void report(protobuf::ModemReport* report) override;
70 
71  private:
72  void start_send(const protobuf::ModemTransmission& msg);
73  void send_complete(const boost::system::error_code& error, std::size_t bytes_transferred);
74  void start_receive();
75  void receive_complete(const boost::system::error_code& error, std::size_t bytes_transferred);
76  void receive_message(const protobuf::ModemTransmission& m);
77 
78  private:
79  protobuf::DriverConfig driver_cfg_;
80  boost::asio::io_context io_context_;
81  std::unique_ptr<boost::asio::ip::udp::socket> socket_;
82  // modem id to endpoint
83  std::multimap<int, boost::asio::ip::udp::endpoint> receivers_;
84  boost::asio::ip::udp::endpoint sender_;
85 
86  // (16 bit length = 65535 - 8 byte UDP header - 20 byte IP
87  static constexpr size_t UDP_MAX_PACKET_SIZE = 65507;
88 
89  std::array<char, UDP_MAX_PACKET_SIZE> receive_buffer_;
90 
91  // ids we are providing acks for, normally just our modem_id()
92  std::set<unsigned> application_ack_ids_;
93 
94  std::uint32_t next_frame_{0};
95 };
96 } // namespace acomms
97 } // namespace goby
98 #endif
goby::acomms::protobuf::DriverConfig
Definition: driver_base.pb.h:123
goby::acomms::protobuf::ModemTransmission
Definition: modem_message.pb.h:166
goby::util::units::rpm::system
boost::units::make_system< boost::units::angle::revolution_base_unit, boost::units::metric::minute_base_unit >::type system
Definition: system.hpp:45
goby::acomms::UDPDriver::do_work
void do_work() override
Allows the modem driver to do its work.
goby
The global namespace for the Goby project.
Definition: acomms_constants.h:33
goby::acomms::UDPDriver::startup
void startup(const protobuf::DriverConfig &cfg) override
Starts the modem driver. Must be called before poll().
goby::acomms::protobuf::ModemReport
Definition: modem_message.pb.h:673
goby::acomms::UDPDriver::UDPDriver
UDPDriver()
boost
Definition: udp_driver.h:41
driver_base.h
io_service
driver_base.pb.h
goby::acomms::UDPDriver::report
void report(protobuf::ModemReport *report) override
Returns report including modem availability and signal quality (if known)
goby::msg
extern ::google::protobuf::internal::ExtensionIdentifier< ::google::protobuf::MessageOptions, ::google::protobuf::internal::MessageTypeTraits< ::goby::GobyMessageOptions >, 11, false > msg
Definition: option_extensions.pb.h:1327
asio_compat.h
goby::acomms::UDPDriver
Definition: udp_driver.h:58
goby::acomms::ModemDriverBase
provides an abstract base class for acoustic modem drivers. This is subclassed by the various drivers...
Definition: driver_base.h:58
goby::acomms::UDPDriver::~UDPDriver
~UDPDriver() override
goby::acomms::UDPDriver::shutdown
void shutdown() override
Shuts down the modem driver.
goby::acomms::UDPDriver::handle_initiate_transmission
void handle_initiate_transmission(const protobuf::ModemTransmission &m) override
Virtual initiate_transmission method. Typically connected to MACManager::signal_initiate_transmission...