-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUdpServer.cpp
More file actions
53 lines (46 loc) · 1.25 KB
/
UdpServer.cpp
File metadata and controls
53 lines (46 loc) · 1.25 KB
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
/**
* (C) 2016, Richard G. Roman <richard.roman@gmx.de>
* Created on 2016-01-23 16:27:28
*
*/
#include <boost/bind.hpp>
#include <cassert>
#include <cstdint>
#include <iostream>
#include "Logger.h"
#include "UdpServer.h"
using namespace boost::asio::ip;
UdpServer::UdpServer(boost::asio::io_service* io_service,
uint32_t portNumber,
ReceiveCallback* receiveCallback)
: socket_(*io_service, udp::endpoint(udp::v4(), portNumber))
, receiveCallback_(receiveCallback)
{
assert(receiveCallback);
startReceive();
}
void
UdpServer::startReceive()
{
using namespace boost::asio;
socket_.async_receive_from(
buffer(receiveBuffer_),
peer_,
std::bind(&UdpServer::handleReceive,
this,
std::placeholders::_1 /* error_code */,
std::placeholders::_2 /* bytes_transferred */));
}
void
UdpServer::handleReceive(const boost::system::error_code& error,
std::size_t bytes_transferred)
{
using namespace boost::asio;
if (error) {
L("Receive error: " << error.message());
} else {
receiveCallback_->udpReceived(
std::string(receiveBuffer_.data(), bytes_transferred));
startReceive();
}
}