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
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/curvecp/received_block_list.h"
#include "net/base/io_buffer.h"
namespace net {
ReceivedBlockList::ReceivedBlockList()
: current_position_(0),
bytes_received_(0) {
}
ReceivedBlockList::~ReceivedBlockList() {
}
int ReceivedBlockList::bytes_available() const {
int bytes_available = 0;
BlockList::const_iterator it = list_.begin();
while (it != list_.end()) {
if (it->position != current_position_ + bytes_available)
break;
bytes_available += it->data->size();
it++;
}
return bytes_available;
}
void ReceivedBlockList::AddBlock(int64 position, IOBuffer* data, int length) {
if (position < bytes_received_) {
LOG(ERROR) << "Unnecessary retransmit of " << position;
return;
}
BlockList::iterator it = list_.begin();
while (it != list_.end()) {
if (it->position == position) {
// Duplicate block. Make sure they are the same length.
DCHECK_EQ(it->data->size(), length);
return;
}
if (it->position > position) {
// The block must not overlap with the next block.
DCHECK_LE(position + length, it->position);
break;
}
it++;
}
Block new_block;
new_block.position = position;
new_block.data = new DrainableIOBuffer(data, length);
list_.insert(it, new_block);
ComputeBytesReceived();
}
int ReceivedBlockList::ReadBytes(IOBuffer* buffer, int size) {
int bytes_read = 0;
LogBlockList();
while (size > 0 && list_.size()) {
BlockList::iterator it = list_.begin();
if (it->position != current_position_)
break;
int bytes = std::min(size, it->data->BytesRemaining());
memcpy(buffer->data() + bytes_read, it->data->data(), bytes);
it->data->DidConsume(bytes);
size -= bytes;
bytes_read += bytes;
if (!it->data->BytesRemaining()) {
current_position_ += it->data->size();
list_.erase(it);
}
}
return bytes_read;
}
void ReceivedBlockList::ComputeBytesReceived() {
bytes_received_ = current_position_ + bytes_available();
}
void ReceivedBlockList::LogBlockList() const {
LOG(INFO) << "Received Blocks: " << list_.size();
std::string msg;
std::ostringstream stream(msg);
for (size_t index = 0; index < list_.size(); ++index)
stream << "(" << list_[index].position
<< "," << list_[index].data->size() << ")";
LOG(INFO) << stream.str();
}
} // namespace net
|