Thunderbots Project
Loading...
Searching...
No Matches
udp_listener.h
1#pragma once
2
3#include <boost/asio.hpp>
4
5typedef std::function<void(const char*, const size_t&)> ReceiveCallback;
6
11{
12 public:
28 UdpListener(boost::asio::io_service& io_service, const std::string& ip_address,
29 unsigned short port, const std::string& interface, bool multicast,
30 ReceiveCallback receive_callback);
31
44 UdpListener(boost::asio::io_service& io_service, const unsigned short port,
45 ReceiveCallback receive_callback);
46
50 virtual ~UdpListener();
51
55 void close();
56
57 private:
62 void handleDataReception(const boost::system::error_code& error,
63 std::size_t bytes_transferred);
64
68 void startListen();
69
78 void setupMulticast(const boost::asio::ip::address& ip_address,
79 const std::string& listen_interface);
80
81 // The maximum buffer length for the raw data received from the network
82 static constexpr unsigned int MAX_BUFFER_LENGTH = 9000;
83
84 // Whether this listener should continue running
85 bool running_;
86
87 // The raw data received from the network
88 std::array<char, MAX_BUFFER_LENGTH> raw_received_data_;
89
90 // A UDP socket to receive data on
91 boost::asio::ip::udp::socket socket_;
92
93 // The endpoint for the sender
94 boost::asio::ip::udp::endpoint sender_endpoint_;
95
96 // Callback once a new message is received
97 ReceiveCallback receive_callback_;
98};
Definition udp_listener.h:11
void close()
Definition udp_listener.cpp:95
virtual ~UdpListener()
Definition udp_listener.cpp:93