-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudp_receiver.h
103 lines (83 loc) · 2.74 KB
/
udp_receiver.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#ifndef UDP_RECEIVER_H
#define UDP_RECEIVER_H
#include "udp_config.h"
#include <iostream>
#include <string>
#include <memory>
#include <boost/array.hpp>
#include <boost/bind/bind.hpp>
#include <boost/asio.hpp>
#include "udp_consumer.h"
/*
// Example use
#include <iostream>
#include <string>
#include <chrono>
#include <thread>
#include "udp_receiver.h.h"
#include "udp_consumer.h"
int main()
{
// receive messages on local port 10253
udp_receiver udp(10253);
// receive multiple messages
udp.receive(std::make_shared<udp_consumer>());
}
*/
// udp_receiver receives asynchronous UDP messages on a local port in an eternal loop
// and forwards the data to an udp_consumer
class udp_receiver {
public:
udp_receiver(int udp_port = 10253)
: m_udp_port(udp_port)
, m_socket(m_io_context, boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), udp_port))
{ }
void receive(std::shared_ptr<udp_consumer> consumer = nullptr)
{
m_consumer = consumer;
// prepare for first async read
start_receive();
// run forever, handle_receive will be called as data come in
m_io_context.run();
}
virtual ~udp_receiver()
{
m_socket.close();
}
int udp_port() const { return m_udp_port; }
private:
void start_receive()
{
// schedule the next asynchronous read
m_socket.async_receive_from(
boost::asio::buffer(m_recv_buffer), m_sender_endpoint,
boost::bind(&udp_receiver::handle_receive, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
void handle_receive(const boost::system::error_code& error, std::size_t bytes_transferred)
{
if (!error) {
if(m_consumer.get()) {
// forward message data to consumer
m_consumer->consume(&m_recv_buffer[0],bytes_transferred);
}
else {
std::cout << "Received: " << std::string(m_recv_buffer.begin(), m_recv_buffer.begin()+bytes_transferred) << std::endl;
}
// prepare for next async read
start_receive();
}
else {
std::cout << "Receive failed: " << error.message() << std::endl;
}
}
private:
boost::asio::io_context m_io_context;
boost::asio::ip::udp::socket m_socket; // socket used for communication
int m_udp_port; // local UDP port (on receiving end)
boost::asio::ip::udp::endpoint m_sender_endpoint; // sender endpoint can be anything
boost::array<unsigned char, 1024> m_recv_buffer; // receive buffer of max 1024 bytes
std::shared_ptr<udp_consumer> m_consumer; // messages are delegated to the consumer
};
#endif // UDP_RECEIVER_H