-
Notifications
You must be signed in to change notification settings - Fork 0
/
measurement_server.h
58 lines (43 loc) · 1.49 KB
/
measurement_server.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
#ifndef MEASUREMENT_SERVER_H
#define MEASUREMENT_SERVER_H
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/array.hpp>
#include <endian.h>
#include "common.h"
#include "get_time_usec.h"
using boost::asio::ip::udp;
/* Serwer do pomiarów opóźnień przez UDP - taki jak 'czekamnaudp' w zadaniu 1. */
class MeasurementServer {
public:
MeasurementServer(boost::asio::io_service& io_service) :
socket(io_service, udp::v4()) {
boost::system::error_code error;
socket.bind(udp::endpoint(udp::v4(), UDP_PORT_DEFAULT), error);
if (error)
std::cerr << "Failed to start Measurement Server!\n";
else
start_receive();
}
private:
void start_receive() {
socket.async_receive_from(boost::asio::buffer(time_buffer), remote_endpoint,
boost::bind(&MeasurementServer::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 && bytes_transferred >= sizeof(uint64_t)) { // else ignorujemy
time_buffer[1] = htobe64(get_time_usec());
socket.async_send_to(boost::asio::buffer(time_buffer), remote_endpoint,
boost::bind(&MeasurementServer::handle_send, this));
}
start_receive();
}
void handle_send() {}
boost::array<uint64_t, 2> time_buffer;
udp::socket socket;
udp::endpoint remote_endpoint;
};
#endif // MEASUREMENT_SERVER_H