mavtables  0.2.1
MAVLink router and firewall.
UDPSocket.cpp
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 #include <algorithm>
19 #include <chrono>
20 #include <cstdint>
21 #include <iterator>
22 #include <utility>
23 #include <vector>
24 
25 #include "IPAddress.hpp"
26 #include "UDPSocket.hpp"
27 
28 
29 // Placed here to avoid weak-vtables error.
30 // LCOV_EXCL_START
32 {
33 }
34 // LCOV_EXCL_STOP
35 
36 
37 /** Send data to the given address using the socket.
38  *
39  * \param data The bytes to send.
40  * \param address The IP address (with port number) to send the bytes to, using
41  * UDP.
42  */
43 void UDPSocket::send(const std::vector<uint8_t> &data, const IPAddress &address)
44 {
45  send(data.begin(), data.end(), address);
46 }
47 
48 
49 /** Send data to the given address using the socket.
50  *
51  * \param first Iterator to first byte in range to send.
52  * \param last Iterator to one past the last byte to send.
53  * \param address The IP address (with port number) to send the bytes to, using
54  * UDP.
55  */
57  std::vector<uint8_t>::const_iterator first,
58  std::vector<uint8_t>::const_iterator last,
59  const IPAddress &address)
60 {
61  std::vector<uint8_t> vec;
62  std::copy(first, last, std::back_inserter(vec));
63  send(vec, address);
64 }
65 
66 
67 /** Receive data on the socket.
68  *
69  * \note The \p timeout is not guaranteed to be up to nanosecond precision, the
70  * actual precision is up to the operating system's implementation but is
71  * guaranteed to have at least millisecond precision.
72  *
73  * \param timeout How long to wait for data to arrive on the socket. The
74  * default is to not wait.
75  * \returns The data read from the socket and the IP address it was sent from.
76  */
77 std::pair<std::vector<uint8_t>, IPAddress> UDPSocket::receive(
78  const std::chrono::nanoseconds &timeout)
79 {
80  std::vector<uint8_t> vec;
81  return {vec, receive(std::back_inserter(vec), timeout)};
82 }
83 
84 
85 /** Receive data on the socket.
86  *
87  * \note The \p timeout is not guaranteed to be up to nanosecond precision, the
88  * actual precision is up to the operating system's implementation but is
89  * guaranteed to have at least millisecond precision.
90  *
91  * \param it A back insert iterator to read bytes into.
92  * \param timeout How long to wait for data to arrive on the socket.
93  * The default is to not wait.
94  * \returns The IP address the data was sent from, this is where a
95  * reply should be sent to.
96  */
98  std::back_insert_iterator<std::vector<uint8_t>> it,
99  const std::chrono::nanoseconds &timeout)
100 {
101  auto [vec, address] = receive(timeout);
102  std::copy(vec.begin(), vec.end(), it);
103  return address;
104 }
105 
106 
107 /** Print the UDP socket to the given output stream.
108  *
109  * \param os The output stream to print to.
110  * \returns The output stream.
111  */
112 std::ostream &UDPSocket::print_(std::ostream &os) const
113 {
114  os << "unknown UDP socket";
115  return os;
116 }
117 
118 
119 /** Print the given UDP socket to the given output stream.
120  *
121  * An example:
122  * ```
123  * udp {
124  * port 14555;
125  * address 127.0.0.1;
126  * max_bitrate 262144;
127  * }
128  * ```
129  *
130  * The base \ref UDPSocket class will print:
131  * ```
132  * unknown UDP socket
133  * ```
134  *
135  * \relates UDPSocket
136  * \param os The output stream to print to.
137  * \param udp_socket The UDP socket (or any child of \ref UDPSocket) to print.
138  * \returns The output stream.
139  */
140 std::ostream &operator<<(std::ostream &os, const UDPSocket &udp_socket)
141 {
142  return udp_socket.print_(os);
143 }
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
virtual std::ostream & print_(std::ostream &os) const
Definition: UDPSocket.cpp:112
virtual ~UDPSocket()
Definition: UDPSocket.cpp:31