-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.cpp
446 lines (407 loc) · 14.9 KB
/
client.cpp
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/***************************************************************************
* client.cpp - Protobuf stream protocol - client
*
* Created: Thu Jan 31 17:38:04 2013
* Copyright 2013 Tim Niemueller [www.niemueller.de]
****************************************************************************/
/* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* - Neither the name of the authors nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <protobuf_comm/client.h>
#include <boost/lexical_cast.hpp>
using namespace boost::asio;
using namespace boost::system;
namespace protobuf_comm {
/** @class ProtobufStreamClient <protobuf_comm/client.h>
* Stream client for protobuf message transmission.
* The client opens a TCP connection (IPv4) to a specified server and
* send and receives messages to the remote.
* @author Tim Niemueller
*/
/** Constructor. */
ProtobufStreamClient::ProtobufStreamClient()
: resolver_(io_service_), socket_(io_service_), io_service_work_(io_service_)
{
message_register_ = new MessageRegister();
own_message_register_ = true;
connected_ = false;
outbound_active_ = false;
in_data_size_ = 1024;
frame_header_version_ = PB_FRAME_V2;
in_frame_header_size_ = sizeof(frame_header_t);
in_frame_header_ = malloc(in_frame_header_size_);
in_data_ = malloc(in_data_size_);
run_asio();
}
/** Constructor.
* @param proto_path file paths to search for proto files. All message types
* within these files will automatically be registered and available for dynamic
* message creation.
*/
ProtobufStreamClient::ProtobufStreamClient(std::vector<std::string> &proto_path)
: resolver_(io_service_), socket_(io_service_), io_service_work_(io_service_)
{
message_register_ = new MessageRegister(proto_path);
own_message_register_ = true;
connected_ = false;
outbound_active_ = false;
in_data_size_ = 1024;
in_data_ = malloc(in_data_size_);
frame_header_version_ = PB_FRAME_V2;
in_frame_header_size_ = sizeof(frame_header_t);
in_frame_header_ = malloc(in_frame_header_size_);
run_asio();
}
/** Constructor.
* @param mr message register to use to (de)serialize messages
* @param header_version protobuf protocol frame header version to use,
*/
ProtobufStreamClient::ProtobufStreamClient(MessageRegister * mr,
frame_header_version_t header_version)
: resolver_(io_service_),
socket_(io_service_),
io_service_work_(io_service_),
message_register_(mr),
own_message_register_(false),
frame_header_version_(header_version)
{
connected_ = false;
outbound_active_ = false;
in_data_size_ = 1024;
in_data_ = malloc(in_data_size_);
if (frame_header_version_ == PB_FRAME_V1) {
in_frame_header_size_ = sizeof(frame_header_v1_t);
} else {
in_frame_header_size_ = sizeof(frame_header_t);
}
in_frame_header_ = malloc(in_frame_header_size_);
run_asio();
}
/** Destructor. */
ProtobufStreamClient::~ProtobufStreamClient()
{
disconnect_nosig();
io_service_.stop();
asio_thread_.join();
free(in_data_);
free(in_frame_header_);
if (own_message_register_) {
delete message_register_;
}
}
#if defined(__GNUC__) && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 6))
static void
run_asio_thread(boost::asio::io_service &io_service)
{
io_service.run();
}
#endif
void
ProtobufStreamClient::run_asio()
{
#if defined(__GNUC__) && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 6))
asio_thread_ = std::thread(run_asio_thread, std::ref(io_service_));
#else
asio_thread_ = std::thread([this]() { this->io_service_.run(); });
#endif
}
/** Asynchronous connect.
* This triggers connection establishment. The method does not block,
* i.e. it returns immediately and does not wait for the connection to
* be established.
* @param host host to connect to
* @param port TCP port to connect to
*/
void
ProtobufStreamClient::async_connect(const char *host, unsigned short port)
{
ip::tcp::resolver::query query(host, boost::lexical_cast<std::string>(port));
resolver_.async_resolve(query,
boost::bind(&ProtobufStreamClient::handle_resolve,
this,
boost::asio::placeholders::error,
boost::asio::placeholders::iterator));
}
void
ProtobufStreamClient::handle_resolve(const boost::system::error_code &err,
ip::tcp::resolver::iterator endpoint_iterator)
{
if (!err) {
// Attempt a connection to each endpoint in the list until we
// successfully establish a connection.
#if BOOST_ASIO_VERSION > 100409
boost::asio::async_connect(socket_,
endpoint_iterator,
#else
socket_.async_connect(*endpoint_iterator,
#endif
boost::bind(&ProtobufStreamClient::handle_connect,
this,
boost::asio::placeholders::error));
} else if (err != boost::asio::error::operation_aborted) {
disconnect_nosig();
sig_disconnected_(err);
}
}
void
ProtobufStreamClient::handle_connect(const boost::system::error_code &err)
{
if (!err) {
#if BOOST_VERSION >= 105400 && BOOST_VERSION < 105500
// Boost 1.54 has a bug that causes async_connect to report success
// if it cannot connect at all to the other side, cf.
// https://svn.boost.org/trac/boost/ticket/8795
// Work around by explicitly checking for connected status
boost::system::error_code ec;
socket_.remote_endpoint(ec);
if (ec == boost::system::errc::not_connected) {
disconnect_nosig();
sig_disconnected_(ec);
return;
}
#endif
connected_ = true;
start_recv();
sig_connected_();
} else {
disconnect_nosig();
sig_disconnected_(err);
}
}
void
ProtobufStreamClient::disconnect_nosig()
{
boost::system::error_code err;
if (socket_.is_open()) {
socket_.cancel();
socket_.shutdown(ip::tcp::socket::shutdown_both, err);
socket_.close();
}
connected_ = false;
}
/** Disconnect from remote host. */
void
ProtobufStreamClient::disconnect()
{
disconnect_nosig();
sig_disconnected_(boost::system::error_code());
}
void
ProtobufStreamClient::start_recv()
{
boost::asio::async_read(socket_,
boost::asio::buffer(in_frame_header_, in_frame_header_size_),
boost::bind(&ProtobufStreamClient::handle_read_header,
this,
boost::asio::placeholders::error));
}
void
ProtobufStreamClient::handle_read_header(const boost::system::error_code &error)
{
if (!error) {
size_t to_read;
if (frame_header_version_ == PB_FRAME_V1) {
frame_header_v1_t *frame_header = (frame_header_v1_t *)in_frame_header_;
to_read = ntohl(frame_header->payload_size);
} else {
frame_header_t *frame_header = (frame_header_t *)in_frame_header_;
to_read = ntohl(frame_header->payload_size);
}
if (to_read > in_data_size_) {
void *new_data = realloc(in_data_, to_read);
if (new_data) {
in_data_size_ = to_read;
in_data_ = new_data;
} else {
disconnect_nosig();
sig_disconnected_(errc::make_error_code(errc::not_enough_memory));
}
}
// setup new read
boost::asio::async_read(socket_,
boost::asio::buffer(in_data_, to_read),
boost::bind(&ProtobufStreamClient::handle_read_message,
this,
boost::asio::placeholders::error));
} else if (error != boost::asio::error::operation_aborted) {
disconnect_nosig();
sig_disconnected_(error);
}
}
void
ProtobufStreamClient::handle_read_message(const boost::system::error_code &error)
{
if (!error) {
frame_header_t frame_header;
message_header_t message_header;
void * data;
if (frame_header_version_ == PB_FRAME_V1) {
frame_header_v1_t *frame_header_v1 = (frame_header_v1_t *)in_frame_header_;
frame_header.header_version = PB_FRAME_V1;
frame_header.cipher = PB_ENCRYPTION_NONE;
frame_header.payload_size =
htonl(ntohl(frame_header_v1->payload_size) + sizeof(message_header_t));
message_header.component_id = frame_header_v1->component_id;
message_header.msg_type = frame_header_v1->msg_type;
data = in_data_;
} else {
memcpy(&frame_header, in_frame_header_, sizeof(frame_header_t));
message_header_t *msg_header = static_cast<message_header_t *>(in_data_);
message_header.component_id = msg_header->component_id;
message_header.msg_type = msg_header->msg_type;
data = (char *)in_data_ + sizeof(message_header);
}
uint16_t comp_id = ntohs(message_header.component_id);
uint16_t msg_type = ntohs(message_header.msg_type);
try {
std::shared_ptr<google::protobuf::Message> m =
message_register_->deserialize(frame_header, message_header, data);
sig_rcvd_(comp_id, msg_type, m);
} catch (std::runtime_error &e) {
sig_recv_failed_(comp_id, msg_type, e.what());
}
start_recv();
} else if (error != boost::asio::error::operation_aborted) {
disconnect_nosig();
sig_disconnected_(error);
}
}
/** Check whether all outbound messages have been sent.
* @return true if outbound sending is still active, false otherwise
*/
bool
ProtobufStreamClient::outbound_done()
{
return !outbound_active_;
}
void
ProtobufStreamClient::handle_write(const boost::system::error_code &error,
size_t /*bytes_transferred*/,
QueueEntry *entry)
{
delete entry;
if (!error) {
std::lock_guard<std::mutex> lock(outbound_mutex_);
if (!outbound_queue_.empty()) {
QueueEntry *front_entry = outbound_queue_.front();
outbound_queue_.pop();
boost::asio::async_write(socket_,
front_entry->buffers,
boost::bind(&ProtobufStreamClient::handle_write,
this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred,
front_entry));
} else {
outbound_active_ = false;
}
} else if (error != boost::asio::error::operation_aborted) {
disconnect_nosig();
sig_disconnected_(error);
}
}
/** Send a message to the server.
* @param component_id ID of the component to address
* @param msg_type numeric message type
* @param m message to send
*/
void
ProtobufStreamClient::send(uint16_t component_id, uint16_t msg_type, google::protobuf::Message &m)
{
if (!connected_) {
throw std::runtime_error("Cannot send while not connected");
}
QueueEntry *entry = new QueueEntry();
message_register_->serialize(component_id,
msg_type,
m,
entry->frame_header,
entry->message_header,
entry->serialized_message);
if (frame_header_version_ == PB_FRAME_V1) {
entry->frame_header_v1.component_id = entry->message_header.component_id;
entry->frame_header_v1.msg_type = entry->message_header.msg_type;
entry->frame_header_v1.payload_size =
htonl(ntohl(entry->frame_header.payload_size) - sizeof(message_header_t));
entry->buffers[0] = boost::asio::buffer(&entry->frame_header_v1, sizeof(frame_header_v1_t));
entry->buffers[1] = boost::asio::const_buffer();
} else {
entry->buffers[0] = boost::asio::buffer(&entry->frame_header, sizeof(frame_header_t));
entry->buffers[1] = boost::asio::buffer(&entry->message_header, sizeof(message_header_t));
}
entry->buffers[2] = boost::asio::buffer(entry->serialized_message);
std::lock_guard<std::mutex> lock(outbound_mutex_);
if (outbound_active_) {
outbound_queue_.push(entry);
} else {
outbound_active_ = true;
boost::asio::async_write(socket_,
entry->buffers,
boost::bind(&ProtobufStreamClient::handle_write,
this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred,
entry));
}
}
/** Send a message to the server.
* @param m message to send, the message must be of a type with a suitable CompType
* enum indicating component ID and message type.
*/
void
ProtobufStreamClient::send(google::protobuf::Message &m)
{
const google::protobuf::Descriptor * desc = m.GetDescriptor();
const google::protobuf::EnumDescriptor *enumdesc = desc->FindEnumTypeByName("CompType");
if (!enumdesc) {
throw std::logic_error("Message does not have CompType enum");
}
const google::protobuf::EnumValueDescriptor *compdesc = enumdesc->FindValueByName("COMP_ID");
const google::protobuf::EnumValueDescriptor *msgtdesc = enumdesc->FindValueByName("MSG_TYPE");
if (!compdesc || !msgtdesc) {
throw std::logic_error("Message CompType enum hs no COMP_ID or MSG_TYPE value");
}
int comp_id = compdesc->number();
int msg_type = msgtdesc->number();
if (comp_id < 0 || comp_id > std::numeric_limits<uint16_t>::max()) {
throw std::logic_error("Message has invalid COMP_ID");
}
if (msg_type < 0 || msg_type > std::numeric_limits<uint16_t>::max()) {
throw std::logic_error("Message has invalid MSG_TYPE");
}
send(comp_id, msg_type, m);
}
/** Send a message to the server.
* @param m message to send, the message must be of a type with a suitable CompType
* enum indicating component ID and message type.
*/
void
ProtobufStreamClient::send(std::shared_ptr<google::protobuf::Message> m)
{
send(*m);
}
} // end namespace protobuf_comm