-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
135 lines (112 loc) · 3.69 KB
/
client.cpp
File metadata and controls
135 lines (112 loc) · 3.69 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "client.hpp"
#define BOOST_JSON_STACK_BUFFER_SIZE 65535
#include <boost/asio/buffer.hpp>
#include <boost/asio/read_until.hpp>
#include <boost/asio/write.hpp>
#include <boost/chrono.hpp>
#include <chrono>
#include <functional>
#include <iostream>
#include <string>
using std::placeholders::_1;
using std::placeholders::_2;
nDPIsrvd_client::nDPIsrvd_client(boost::asio::io_context &io_context)
: m_recvq(), m_socket(io_context), m_deadline_timer(io_context),
m_heartbeat_timer(io_context) {}
void nDPIsrvd_client::start(tcp::resolver::results_type endpoints) {
m_endpoints = endpoints;
start_connect(m_endpoints.begin());
m_deadline_timer.async_wait(
std::bind(&nDPIsrvd_client::check_deadline, this));
}
void nDPIsrvd_client::stop() {
boost::system::error_code ignored_error;
m_socket.close(ignored_error);
m_deadline_timer.cancel();
m_heartbeat_timer.cancel();
m_stopped = true;
}
void nDPIsrvd_client::start_connect(
tcp::resolver::results_type::iterator endpoint_iter) {
if (endpoint_iter != m_endpoints.end()) {
std::cerr << "Trying " << endpoint_iter->endpoint() << "..." << std::endl;
m_deadline_timer.expires_after(std::chrono::seconds(60));
m_socket.async_connect(
endpoint_iter->endpoint(),
std::bind(&nDPIsrvd_client::handle_connect, this, _1, endpoint_iter));
} else {
stop();
}
}
void nDPIsrvd_client::handle_connect(
const boost::system::error_code &error,
tcp::resolver::results_type::iterator endpoint_iter) {
if (m_stopped)
return;
if (!m_socket.is_open()) {
std::cerr << "Connect timed out" << std::endl;
start_connect(++endpoint_iter);
} else if (error) {
std::cerr << "Connect error: " << error.message() << std::endl;
m_socket.close();
start_connect(++endpoint_iter);
} else {
std::cerr << "Connected to " << endpoint_iter->endpoint() << std::endl;
start_read();
}
}
void nDPIsrvd_client::start_read() {
m_deadline_timer.expires_after(std::chrono::seconds(30));
boost::asio::async_read_until(
m_socket,
boost::asio::dynamic_buffer(m_input_buffer, BOOST_JSON_STACK_BUFFER_SIZE),
'\n', std::bind(&nDPIsrvd_client::handle_read, this, _1, _2));
}
void nDPIsrvd_client::handle_read(const boost::system::error_code &error,
std::size_t n) {
if (m_stopped)
return;
if (!error) {
std::string line(m_input_buffer.substr(0, n - 1));
m_input_buffer.erase(0, n);
if (!line.empty()) {
if (!handle_protocol(line))
std::cerr << "Protocol error for received line: " << line << std::endl;
} else {
std::cerr << "Empty line received" << std::endl;
}
start_read();
} else {
std::cout << "Error on receive: " << error.message() << std::endl;
stop();
}
}
void nDPIsrvd_client::check_deadline() {
if (m_stopped)
return;
if (m_deadline_timer.expiry() <= steady_timer::clock_type::now()) {
std::cerr << "No data received until deadline" << std::endl;
m_deadline_timer.expires_after(std::chrono::seconds(30));
}
m_deadline_timer.async_wait(
std::bind(&nDPIsrvd_client::check_deadline, this));
}
bool nDPIsrvd_client::handle_protocol(const std::string &line) {
int json_length;
std::size_t json_index;
const auto line_length = line.length();
if (line_length < 3 /* e.g. "0{}" */)
return false;
try {
json_length = std::stoi(line, &json_index);
} catch (const std::exception &e) {
std::cerr << "Invalid PDU length: " << e.what() << std::endl;
return false;
}
if (line[json_index] != '{' || line[line_length - 1] != '}') {
std::cerr << "Invalid JSON string" << std::endl;
return false;
}
m_recvq.push(&line[json_index]);
return true;
}