-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnection.cpp
executable file
·721 lines (612 loc) · 25.3 KB
/
connection.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
// anynet
// Copyright (C) 2009 Steven Siloti
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// In addition, as a special exception, the copyright holders give
// permission to link the code of portions of this program with the
// OpenSSL library under certain conditions as described in each
// individual source file, and distribute linked combinations
// including the two.
//
// You must obey the GNU General Public License in all respects
// for all of the code used other than OpenSSL. If you modify
// file(s) with this exception, you may extend this exception to your
// version of the file(s), but you are not obligated to do so. If you
// do not wish to do so, delete this exception statement from your
// version. If you delete this exception statement from all source
// files in the program, then also delete it here.
//
// Contact: Steven Siloti <[email protected]>
#include "payload_failure.hpp"
#include "connection.hpp"
#include "node.hpp"
#include "config.hpp"
#include <boost/asio/read.hpp>
#include <boost/bind/protect.hpp>
#include <boost/make_shared.hpp>
#include <algorithm>
#include <cstring>
#ifdef SIMULATION
#include "simulator.hpp"
#endif
const boost::posix_time::millisec connection::target_latency(100);
namespace
{
template <typename Adr>
struct link_handshake
{
boost::uint8_t sig[6];
boost::uint8_t protocol;
boost::uint8_t type;
boost::uint8_t remote_ip[Adr::bytes_type::static_size];
boost::uint8_t rsvd[2];
boost::uint8_t incoming_port[2];
boost::uint8_t supported_protocol_count;
boost::uint8_t supported_protocols[1][2];
};
const char* handshake_signature = "ANYNET";
struct oob_threshold_frame
{
boost::uint8_t type;
boost::uint8_t rsvd[2];
boost::uint8_t ack;
boost::uint8_t oob_threshold[4];
};
template <typename Adr>
struct successor_frame
{
boost::uint8_t type;
boost::uint8_t rsvd;
boost::uint8_t sucessor_port[2];
boost::uint8_t sucessor_ip[Adr::bytes_type::static_size];
};
}
connection::connection(boost::shared_ptr<local_node> node, routing_type rtype)
: node_(node)
, link_(node->io_service(), node->context)
, routing_type_(rtype)
, incoming_port_(0)
, lifecycle_(connecting)
, oob_threshold_(min_oob_threshold)
, local_oob_threshold_(min_oob_threshold)
, receive_outstanding_(false)
, outstanding_non_packet_frames_(0)
, ack_sends_needed_(0)
#ifdef SIMULATION
, send_delay_(node->io_service(), boost::posix_time::ptime(boost::date_time::not_a_date_time))
#endif
{
}
ip::tcp::endpoint connection::remote_endpoint() const
{
boost::system::error_code error;
return ip::tcp::endpoint(link_.socket.lowest_layer().remote_endpoint(error).address(), incoming_port_);
}
void connection::starting_connection()
{
node_->connection_in_progress(shared_from_this());
}
void connection::stillborn()
{
DLOG(INFO) << "Error while establishing connection";
close();
node_->register_connection(shared_from_this());
}
void connection::send_failure()
{
DLOG(INFO) << std::string(node_->id()) << " Failed sending";
if (lifecycle_ != cleanup)
lifecycle_ = disconnecting;
boost::system::error_code error;
link_.socket.lowest_layer().shutdown(ip::tcp::socket::shutdown_send, error);
node_->send_failure(shared_from_this());
redispatch_send_queue();
}
connection::ptr_t connection::connect(boost::shared_ptr<local_node> node, ip::tcp::endpoint peer, routing_type rtype)
{
DLOG(INFO) << "Connecting: " << node->public_endpoint().port() << ", " << peer.address() << ':' << peer.port();
ptr_t con(new connection(node, rtype));
con->starting_connection();
con->incoming_port_ = peer.port();
con->link_.socket.lowest_layer().async_connect(peer, boost::bind(&connection::ssl_handshake,
con,
boost::asio::ssl::stream_base::client,
placeholders::error));
return con;
}
void connection::accept(boost::shared_ptr<local_node> node, ip::tcp::acceptor& incoming)
{
ptr_t con(new connection(node, ib));
con->starting_connection();
incoming.async_accept(con->link_.socket.lowest_layer(),
boost::bind(&connection::connection_accepted,
con,
placeholders::error,
boost::ref(incoming)) );
}
void connection::connection_accepted(const boost::system::error_code& error, ip::tcp::acceptor& incoming)
{
if (error) {
stillborn();
return;
}
accept(node_, incoming);
ssl_handshake(boost::asio::ssl::stream_base::server, boost::system::error_code());
}
void connection::ssl_handshake(boost::asio::ssl::stream_base::handshake_type type, const boost::system::error_code& error)
{
if (error) {
stillborn();
return;
}
link_.socket.async_handshake(type,
boost::bind(&connection::write_handshake,
shared_from_this(),
boost::asio::placeholders::error));
}
void connection::write_handshake(const boost::system::error_code& error)
{
if (error) {
stillborn();
return;
}
boost::asio::async_write(link_.socket,
const_buffers_1(generate_handshake()),
boost::bind(&connection::read_handshake,
shared_from_this(),
placeholders::error,
placeholders::bytes_transferred));
}
void connection::read_handshake(const boost::system::error_code& error, std::size_t bytes_transferred)
{
if (error) {
stillborn();
return;
}
std::size_t handshake_min_size;
if (link_.socket.lowest_layer().remote_endpoint().address().is_v4())
handshake_min_size = sizeof(link_handshake<ip::address_v4>);
else
handshake_min_size = sizeof(link_handshake<ip::address_v6>);
boost::asio::async_read(link_.socket,
mutable_buffers_1(link_.receive_buffer()),
boost::asio::transfer_at_least(handshake_min_size),
boost::bind(&connection::handshake_received,
shared_from_this(),
placeholders::error,
placeholders::bytes_transferred));
}
template <typename Adr>
const_buffer connection::do_generate_handshake()
{
const std::vector<protocol_id>& protocols = node_->supported_protocols();
link_handshake<Adr>* handshake = buffer_cast<link_handshake<Adr>*>( link_.send_buffer(sizeof(link_handshake<Adr>)
+ (protocols.size() - 1) * 2) );
std::memcpy(handshake->sig, handshake_signature, sizeof(handshake->sig));
handshake->protocol = link_.protocol_version;
handshake->type = routing_type_;
typename Adr::bytes_type peer_ip = to<Adr>(link_.socket.lowest_layer().remote_endpoint().address()).to_bytes();
std::memcpy(handshake->remote_ip, peer_ip.data(), peer_ip.size());
u16(handshake->incoming_port, node_->config().listen_port());
handshake->supported_protocol_count = protocols.size();
for (std::vector<protocol_id>::const_iterator protocol = protocols.begin(); protocol != protocols.end(); ++protocol) {
u16(handshake->supported_protocols[protocol - protocols.begin()], *protocol);
}
return link_.sendable_buffer();
}
const_buffer connection::generate_handshake()
{
// DLOG(INFO) << "Generating handshake";
if (link_.socket.lowest_layer().remote_endpoint().address().is_v4())
return do_generate_handshake<ip::address_v4>();
else
return do_generate_handshake<ip::address_v6>();
}
template <typename Adr>
bool connection::do_parse_handshake()
{
// DLOG(INFO) << "Parsing handshake";
const link_handshake<Adr>* handshake = buffer_cast<const link_handshake<Adr>*>(link_.received_buffer());
if (std::memcmp(handshake->sig, handshake_signature, sizeof(handshake->sig)) || handshake->protocol != link_.protocol_version)
return false;
routing_type_ = std::min(routing_type_, connection::routing_type(handshake->type & 0x03));
incoming_port_ = u16(handshake->incoming_port);
typename Adr::bytes_type ip_bytes;
std::memcpy(ip_bytes.data(), handshake->remote_ip, ip_bytes.size());
reported_peer_address_ = Adr(ip_bytes);
supported_protocols_.resize(handshake->supported_protocol_count);
link_.consume_receive_buffer(sizeof(link_handshake<Adr>) - sizeof(handshake->supported_protocols));
return true;
}
void connection::handshake_received(const boost::system::error_code& error, std::size_t bytes_transferred)
{
if (error) {
stillborn();
return;
}
link_.received(bytes_transferred);
bool handshake_valid;
if (link_.socket.lowest_layer().remote_endpoint().address().is_v4())
handshake_valid = do_parse_handshake<ip::address_v4>();
else
handshake_valid = do_parse_handshake<ip::address_v6>();
if (!handshake_valid) {
link_.socket.lowest_layer().close();
DLOG(INFO) << "Error parsing handshake";
return;
}
remote_identity_ = network_key(::SSL_get_peer_certificate(link_.socket.impl()->ssl));
if (supported_protocols_.size() * 2 > link_.valid_received_bytes())
boost::asio::async_read(link_.socket,
mutable_buffers_1(link_.receive_buffer()),
boost::asio::transfer_at_least(supported_protocols_.size() - link_.valid_received_bytes()),
boost::bind(&connection::complete_connection,
shared_from_this(),
placeholders::error,
placeholders::bytes_transferred));
else
complete_connection(error, 0);
}
void connection::complete_connection(const boost::system::error_code& error, std::size_t bytes_transferred)
{
if (error) {
stillborn();
return;
}
link_.received(bytes_transferred);
const boost::uint8_t (*supported_protocols)[2] = buffer_cast<const boost::uint8_t(*)[2]>(link_.received_buffer());
for (std::vector<protocol_id>::iterator protocol = supported_protocols_.begin(); protocol != supported_protocols_.end(); ++protocol) {
*protocol = u16(supported_protocols[protocol - supported_protocols_.begin()]);
}
link_.consume_receive_buffer(supported_protocols_.size() * 2);
lifecycle_ = connected;
established_ = boost::posix_time::second_clock::universal_time();
DLOG(INFO) << "Connected with cipher: " << ::SSL_CIPHER_get_name(::SSL_get_current_cipher(link_.socket.impl()->ssl)); //<< " and compression: " << ::SSL_COMP_get_name(::SSL_get_current_compression(link_.socket.impl()->ssl));
node_->register_connection(shared_from_this());
if (accepts_ib_traffic())
outstanding_non_packet_frames_ |= frame_bit_successor_request;
send_next_frame();
receive_next_frame();
}
void connection::send(packet::ptr_t pkt, std::size_t oob_threshold_override)
{
packet_queue_.push_back(queued_packet(pkt, oob_threshold_override));
if (!outstanding_non_packet_frames_ && packet_queue_.size() == 1 && lifecycle_ == connected) {
send_next_frame();
}
else
DLOG(INFO) << "Queued packet from " << std::string(node_->id()) << " to " << std::string(remote_id());
}
void connection::receive_next_frame()
{
if (link_.valid_received_bytes()) {
receive_outstanding_ = true;
frame_head_received(boost::system::error_code(), 0);
}
else {
receive_outstanding_ = false;
boost::asio::async_read(link_.socket,
mutable_buffers_1(link_.receive_buffer()),
boost::asio::transfer_at_least(4),
boost::bind(&connection::frame_head_received,
shared_from_this(),
placeholders::error,
placeholders::bytes_transferred));
}
}
void connection::frame_head_received(const boost::system::error_code& error, std::size_t bytes_transfered)
{
if (error) {
DLOG(INFO) << "Error receiving frame";
node_->receive_failure(shared_from_this());
return;
}
link_.received(bytes_transfered);
DLOG(INFO) << std::string(node_->id()) << " Recieved " << bytes_transfered << " bytes, total buffered: " << link_.valid_received_bytes();
while (link_.valid_received_bytes())
{
int frame_type(buffer_cast<const boost::uint8_t*>(link_.received_buffer())[0]);
switch (frame_type)
{
case frame_network_packet:
{
// DLOG(INFO) << "Receiving network packet frame";
packet::ptr_t pkt(boost::make_shared<packet>());
pkt->receive(link_, boost::protect(boost::bind(&connection::incoming_packet, shared_from_this(), _1, _2)));
return;
}
case frame_oob_threshold_update:
if (link_.valid_received_bytes() >= oob_threshold_size()) {
parse_oob_threshold();
break;
}
boost::asio::async_read(link_.socket,
mutable_buffers_1(link_.receive_buffer()),
boost::asio::transfer_at_least(oob_threshold_size() - link_.valid_received_bytes()),
boost::bind(&connection::frame_head_received,
shared_from_this(),
placeholders::error,
placeholders::bytes_transferred));
return;
case frame_successor_request:
{
if (link_.valid_received_bytes() >= 4) {
DLOG(INFO) << std::string(node_->id()) << "Sending successor";
send_next_frame(frame_bit_successor);
link_.consume_receive_buffer(4);
break;
}
boost::asio::async_read(link_.socket,
mutable_buffers_1(link_.receive_buffer()),
boost::asio::transfer_at_least(4 - link_.valid_received_bytes()),
boost::bind(&connection::frame_head_received,
shared_from_this(),
placeholders::error,
placeholders::bytes_transferred));
return;
}
case frame_successor:
if (link_.valid_received_bytes() >= successor_size()) {
parse_successor();
break;
}
boost::asio::async_read(link_.socket,
mutable_buffers_1(link_.receive_buffer()),
boost::asio::transfer_at_least(oob_threshold_size() - link_.valid_received_bytes()),
boost::bind(&connection::frame_head_received,
shared_from_this(),
placeholders::error,
placeholders::bytes_transferred));
return;
default:
{
DLOG(INFO) << "Unknown frame type: " << buffer_cast<const boost::uint8_t*>(link_.received_buffer())[0];
google::FlushLogFiles(google::INFO);
node_->receive_failure(shared_from_this());
return;
}
}
}
receive_next_frame();
}
void connection::incoming_packet(packet::ptr_t pkt, std::size_t payload_size)
{
node_->incoming_packet(shared_from_this(), pkt, payload_size);
if (!payload_size) {
receive_next_frame();
}
}
std::size_t connection::oob_threshold_size()
{
return sizeof(oob_threshold_frame);
}
void connection::parse_oob_threshold()
{
const oob_threshold_frame* frame = buffer_cast<const oob_threshold_frame*>(link_.received_buffer());
// OOB threshold updates double as an ACK for network packets
// This is strictly for timing purposes. Idealy we could use the TCP ack for this but operating systems
// don't provide that capability.
for (unsigned acks_remaining = frame->ack; acks_remaining != 0; --acks_remaining) {
if (!ack_queue_.empty()) {
update_local_threshold(boost::posix_time::microsec_clock::universal_time() - ack_queue_.front().entered,
ack_queue_.front().bytes_transfered );
ack_queue_.pop_front();
}
}
remote_oob_threshold_ = u32(frame->oob_threshold);
// update_oob_threshold();
link_.consume_receive_buffer(sizeof(oob_threshold_frame));
}
void connection::update_local_threshold(boost::posix_time::time_duration duration, std::size_t bytes_sent)
{
// Move the oob threshold up or down in proportion to the difference between the target latency and
// how long this operation took
// We cap any increase at the amount of data sent for this operation. This is to prevent
// the threshold from overshooting too much on lightly loaded short-thin links.
local_oob_threshold_ += std::min(double((target_latency.total_milliseconds() - duration.total_milliseconds()))
/ double(target_latency.total_milliseconds()) * local_oob_threshold_,
double(bytes_sent));
local_oob_threshold_ = std::max(local_oob_threshold_, 1.0);
}
void connection::update_oob_threshold()
{
#ifdef FORCE_OOB_THRESHOLD
oob_threshold_ = FORCE_OOB_THRESHOLD;
#else
oob_threshold_ = std::min(remote_oob_threshold_, boost::uint32_t(local_oob_threshold_));
if (oob_threshold_ < min_oob_threshold)
oob_threshold_ = min_oob_threshold;
#endif
node_->update_threshold_stats();
send_next_frame(frame_bit_oob_threshold_update);
}
std::size_t connection::successor_size()
{
if (link_.socket.lowest_layer().remote_endpoint().address().is_v4())
return sizeof(successor_frame<ip::address_v4>);
else
return sizeof(successor_frame<ip::address_v6>);
}
template <typename Addr>
std::size_t do_parse_successor(const_buffer buf, ip::tcp::endpoint& ep)
{
const successor_frame<Addr>* frame = buffer_cast<const successor_frame<Addr>*>(buf);
typename Addr::bytes_type ip_bytes;
std::memcpy(ip_bytes.data(), frame->sucessor_ip, ip_bytes.size());
ep.address(Addr(ip_bytes));
ep.port(u16(frame->sucessor_port));
return sizeof(successor_frame<Addr>);
}
void connection::parse_successor()
{
ip::tcp::endpoint successor;
std::size_t consumed;
if (link_.socket.lowest_layer().remote_endpoint().address().is_v4())
consumed = do_parse_successor<ip::address_v4>(link_.received_buffer(), successor);
else
consumed = do_parse_successor<ip::address_v6>(link_.received_buffer(), successor);
link_.consume_receive_buffer(consumed);
DLOG(INFO) << incoming_port_ << ": Got Successor: " << successor.address() << ':' << successor.port();
#if 0
if (successor.port() == remote_endpoint().port())
sim.verify_reverse_successor(node_->id(), remote_id());
#endif
node_->make_connection(successor);
}
template <typename Addr>
std::size_t do_generate_successor_frame(net_link& link, const Addr& sucessor_address, boost::uint16_t sucessor_port)
{
successor_frame<Addr>* frame = buffer_cast<successor_frame<Addr>*>(link.send_buffer(sizeof(successor_frame<Addr>)));
frame->type = connection::frame_successor;
typename Addr::bytes_type peer_ip = sucessor_address.to_bytes();
std::memcpy(frame->sucessor_ip, peer_ip.data(), peer_ip.size());
u16(frame->sucessor_port, sucessor_port);
return sizeof(successor_frame<Addr>);
}
void connection::send_next_frame(int send_non_packet_frame)
{
if (!outstanding_non_packet_frames_ && packet_queue_.empty() && lifecycle_ == connected) {
outstanding_non_packet_frames_ |= send_non_packet_frame;
send_next_frame();
}
else {
outstanding_non_packet_frames_ |= send_non_packet_frame;
}
}
void connection::send_next_frame()
{
link_.clear_send_buffer();
if (outstanding_non_packet_frames_ & frame_bit_oob_threshold_update) {
oob_threshold_frame* frame = buffer_cast<oob_threshold_frame*>(link_.send_buffer(sizeof(oob_threshold_frame)));
frame->type = frame_oob_threshold_update;
unsigned ack_count = ack_sends_needed_ & 0xFF;
ack_sends_needed_ -= ack_count;
frame->ack = ack_count;
frame->rsvd[0] = frame->rsvd[1] = 0;
u32(frame->oob_threshold, boost::uint32_t(local_oob_threshold_));
boost::asio::async_write(link_.socket,
const_buffers_1(link_.sendable_buffer()),
boost::bind(&connection::frame_sent,
shared_from_this(),
frame_bit_oob_threshold_update,
placeholders::error,
placeholders::bytes_transferred));
}
else if (outstanding_non_packet_frames_ & frame_bit_successor_request) {
boost::uint8_t* frame = buffer_cast<boost::uint8_t*>(link_.send_buffer(4));
*frame = frame_successor_request;
boost::asio::async_write(link_.socket,
const_buffers_1(link_.sendable_buffer()),
boost::bind(&connection::frame_sent,
shared_from_this(),
frame_bit_successor_request,
placeholders::error,
placeholders::bytes_transferred));
DLOG(INFO) << std::string(node_->id()) << " Wrote " << 4 << " bytes to " << std::string(remote_id()).substr(0, 4);
}
else if (outstanding_non_packet_frames_ & frame_bit_successor) {
ip::tcp::endpoint successor = node_->successor_endpoint(remote_identity_);
if (successor.address().is_v4())
do_generate_successor_frame(link_, successor.address().to_v4(), successor.port());
else
do_generate_successor_frame(link_, successor.address().to_v6(), successor.port());
boost::asio::async_write(link_.socket,
const_buffers_1(link_.sendable_buffer()),
boost::bind(&connection::frame_sent,
shared_from_this(),
frame_bit_successor,
placeholders::error,
placeholders::bytes_transferred));
DLOG(INFO) << std::string(node_->id()) << " Wrote " << buffer_size(link_.sendable_buffer()) << " bytes to " << std::string(remote_id()).substr(0, 4);
}
else if (!packet_queue_.empty()) {
DLOG(INFO) << "Sending packet from " << std::string(node_->id()) << " to " << std::string(remote_id());
std::size_t threshold
= packet_queue_.front().oob_threshold_override == std::numeric_limits<std::size_t>::max()
? oob_threshold() : packet_queue_.front().oob_threshold_override;
const std::vector<const_buffer>& send_buffers = packet_queue_.front().pkt->serialize(threshold, link_.send_buffer());
std::size_t bytes_transfered = 0;
for (std::vector<const_buffer>::const_iterator buf = send_buffers.begin(); buf != send_buffers.end(); ++buf)
bytes_transfered += buffer_size(*buf);
#ifdef SIMULATION
if (send_delay_.expires_at() == boost::date_time::not_a_date_time) {
send_delay_.expires_from_now(boost::posix_time::milliseconds(bytes_transfered / 2));
send_delay_.async_wait(boost::bind(&connection::send_delayed_frame, shared_from_this(), placeholders::error));
return;
}
send_delay_.expires_at(boost::date_time::not_a_date_time);
#endif
ack_queue_.push_back(pending_ack(packet_queue_.front().entered, bytes_transfered));
assert(buffer_cast<const boost::uint8_t*>(send_buffers[0])[0] == 0);
DLOG(INFO) << "Sending " << bytes_transfered << " packet bytes";
boost::asio::async_write(link_.socket,
send_buffers,
boost::bind(&connection::packet_sent,
shared_from_this(),
placeholders::error,
placeholders::bytes_transferred));
}
}
void connection::packet_sent(const boost::system::error_code& error, std::size_t bytes_transfered)
{
if (error) {
send_failure();
return;
}
DLOG(INFO) << std::string(node_->id()) << " Sent " << bytes_transfered << " bytes of packet content";
node_->sent_content(remote_id(), bytes_transfered);
packet_queue_.pop_front();
send_next_frame();
}
void connection::frame_sent(frame_bits frame_bit, const boost::system::error_code& error, std::size_t bytes_transfered)
{
// DLOG(INFO) << "Sent " << bytes_transfered << " bytes for frame type " << frame_bit;
if (!(frame_bit & frame_bit_oob_threshold_update && ack_sends_needed_))
outstanding_non_packet_frames_ &= ~frame_bit;
if (error) {
send_failure();
return;
}
send_next_frame();
}
void connection::close()
{
if (link_.socket.lowest_layer().is_open()) {
// link_.socket.async_shutdown(boost::bind(&connection::link_shutdown, shared_from_this()));
boost::system::error_code error;
link_.socket.lowest_layer().shutdown(ip::tcp::socket::shutdown_both, error);
link_.socket.lowest_layer().close();
}
redispatch_send_queue();
lifecycle_ = cleanup;
}
void connection::redispatch_send_queue()
{
for (std::deque<queued_packet>::iterator it = packet_queue_.begin(); it != packet_queue_.end(); ++it) {
if (it->pkt->is_direct())
// this was a direct request, turn it into an error so the requester gets notified
it->pkt->to_reply(packet::content_failure, boost::make_shared<payload_failure>(it->pkt->payload()->content_size()));
node_->dispatch(it->pkt);
}
packet_queue_.clear();
}
#ifdef SIMULATION
void connection::send_delayed_frame(const boost::system::error_code& error)
{
if (!error)
send_next_frame();
}
#endif