mavtables  0.2.1
MAVLink router and firewall.
UnixUDPSocket.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 UNIXUDPSOCKET_HPP_
19 #define UNIXUDPSOCKET_HPP_
20 
21 
22 #include <chrono>
23 #include <cstdint>
24 #include <iterator>
25 #include <memory>
26 #include <optional>
27 #include <vector>
28 
29 #include "IPAddress.hpp"
30 #include "UDPSocket.hpp"
31 #include "UnixSyscalls.hpp"
32 
33 
34 /** A unix UDP socket, listening on a port/address combination.
35  */
36 class UnixUDPSocket : public UDPSocket
37 {
38  public:
40  unsigned int port, std::optional<IPAddress> address = {},
41  unsigned long max_bitrate = 0,
42  std::unique_ptr<UnixSyscalls> syscalls =
43  std::make_unique<UnixSyscalls>());
44  virtual ~UnixUDPSocket();
45  virtual void send(
46  const std::vector<uint8_t> &data, const IPAddress &address) final;
47  virtual std::pair<std::vector<uint8_t>, IPAddress> receive(
48  const std::chrono::nanoseconds &timeout =
49  std::chrono::nanoseconds::zero()) final;
50 
51  protected:
52  std::ostream &print_(std::ostream &os) const final;
53 
54  private:
55  // Variables
56  unsigned int port_;
57  std::optional<IPAddress> address_;
58  unsigned long max_bitrate_;
59  std::unique_ptr<UnixSyscalls> syscalls_;
60  int socket_;
61  std::chrono::time_point<std::chrono::steady_clock> next_time_;
62  // Methods
63  void create_socket_();
64  std::pair<std::vector<uint8_t>, IPAddress> receive_();
65 };
66 
67 
68 #endif // UNIXUDPSOCKET_HPP_
UnixUDPSocket(unsigned int port, std::optional< IPAddress > address={}, unsigned long max_bitrate=0, std::unique_ptr< UnixSyscalls > syscalls=std::make_unique< UnixSyscalls >())
virtual std::pair< std::vector< uint8_t >, IPAddress > receive(const std::chrono::nanoseconds &timeout=std::chrono::nanoseconds::zero()) final
virtual ~UnixUDPSocket()
virtual void send(const std::vector< uint8_t > &data, const IPAddress &address) final
std::ostream & print_(std::ostream &os) const final