mavtables  0.2.1
MAVLink router and firewall.
UDPSocket.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 UDPSOCKET_HPP_
19 #define UDPSOCKET_HPP_
20 
21 
22 #include <chrono>
23 #include <cstdint>
24 #include <iterator>
25 #include <optional>
26 #include <utility>
27 #include <vector>
28 
29 #include "IPAddress.hpp"
30 
31 
32 /** A UDP socket, listening on a port/address combination.
33  *
34  * \warning This class should be treated as pure virtual and should never be
35  * instantiated.
36  *
37  * \warning Either \ref send(const std::vector<uint8_t> &data, const IPAddress &) or
38  * send(std::vector<uint8_t>::const_iterator,std::vector<uint8_t>::const_iterator, const IPAddress &)
39  * must be overridden in child classes to avoid infinite recursion.
40  *
41  * \warning Either \ref receive(const std::chrono::nanoseconds &) or
42  * receive(std::back_insert_iterator<std::vector<uint8_t>>,const std::chrono::nanoseconds &)
43  * must be overridden in child classes to avoid infinite recursion.
44  *
45  */
46 class UDPSocket
47 {
48  public:
49  virtual ~UDPSocket();
50  virtual void send(
51  const std::vector<uint8_t> &data, const IPAddress &address);
52  virtual void send(
53  std::vector<uint8_t>::const_iterator first,
54  std::vector<uint8_t>::const_iterator last,
55  const IPAddress &address);
56  virtual std::pair<std::vector<uint8_t>, IPAddress> receive(
57  const std::chrono::nanoseconds &timeout =
58  std::chrono::nanoseconds::zero());
59  virtual IPAddress receive(
60  std::back_insert_iterator<std::vector<uint8_t>> it,
61  const std::chrono::nanoseconds &timeout =
62  std::chrono::nanoseconds::zero());
63 
64  friend std::ostream &operator<<(
65  std::ostream &os, const UDPSocket &udp_socket);
66 
67  protected:
68  /** Print the UDP socket to the given output stream.
69  *
70  * \param os The output stream to print to.
71  * \returns The output stream.
72  */
73  virtual std::ostream &print_(std::ostream &os) const;
74 };
75 
76 
77 std::ostream &operator<<(std::ostream &os, const UDPSocket &udp_socket);
78 
79 
80 #endif // UDPSOCKET_HPP_
virtual std::pair< std::vector< uint8_t >, IPAddress > receive(const std::chrono::nanoseconds &timeout=std::chrono::nanoseconds::zero())
Definition: UDPSocket.cpp:77
virtual void send(const std::vector< uint8_t > &data, const IPAddress &address)
Definition: UDPSocket.cpp:43
std::ostream & operator<<(std::ostream &os, const Action &action)
Definition: Action.cpp:188
friend std::ostream & operator<<(std::ostream &os, const UDPSocket &udp_socket)
Definition: UDPSocket.cpp:140
virtual std::ostream & print_(std::ostream &os) const
Definition: UDPSocket.cpp:112
virtual ~UDPSocket()
Definition: UDPSocket.cpp:31