mavtables  0.2.1
MAVLink router and firewall.
UDPInterface.hpp
Go to the documentation of this file.
1 // MAVLink router and firewall.
2 // Copyright (C) 2018 Michael R. Shannon <mrshannon.aerospace@gmail.com>
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 
18 #ifndef UDPINTERFACE_HPP_
19 #define UDPINTERFACE_HPP_
20 
21 
22 #include <chrono>
23 #include <map>
24 #include <memory>
25 
26 #include "Connection.hpp"
27 #include "ConnectionFactory.hpp"
28 #include "ConnectionPool.hpp"
29 #include "Interface.hpp"
30 #include "IPAddress.hpp"
31 #include "PacketParser.hpp"
32 #include "UDPSocket.hpp"
33 
34 
35 /** A UDP (User Datagram Protocol) interface.
36  *
37  * An interface (for sending and receiving packets) implementing the user
38  * datagram protocol.
39  */
40 class UDPInterface : public Interface
41 {
42  public:
44  std::unique_ptr<UDPSocket> socket,
45  std::shared_ptr<ConnectionPool> connection_pool,
46  std::unique_ptr<ConnectionFactory<>> connection_factory);
47  // LCOV_EXCL_START
48  ~UDPInterface() = default;
49  // LCOV_EXCL_STOP
50  void send_packet(const std::chrono::nanoseconds &timeout) final;
51  void receive_packet(const std::chrono::nanoseconds &timeout) final;
52 
53  protected:
54  std::ostream &print_(std::ostream &os) const final;
55 
56  private:
57  // Variables.
58  std::unique_ptr<UDPSocket> socket_;
59  std::shared_ptr<ConnectionPool> connection_pool_;
60  std::unique_ptr<ConnectionFactory<>> connection_factory_;
61  IPAddress last_ip_address_;
62  std::map<IPAddress, std::shared_ptr<Connection>> connections_;
63  PacketParser parser_;
64  // Methods
65  void update_connections_(
66  const MAVAddress &mav_address, const IPAddress &ip_address);
67 };
68 
69 
70 #endif // UDPINTERFACE_HPP_
void receive_packet(const std::chrono::nanoseconds &timeout) final
std::ostream & print_(std::ostream &os) const final
UDPInterface(std::unique_ptr< UDPSocket > socket, std::shared_ptr< ConnectionPool > connection_pool, std::unique_ptr< ConnectionFactory<>> connection_factory)
void send_packet(const std::chrono::nanoseconds &timeout) final
~UDPInterface()=default