forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfake_stream_socket.cc
228 lines (188 loc) · 6.22 KB
/
fake_stream_socket.cc
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
// Copyright 2016 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 "chromecast/net/fake_stream_socket.h"
#include <algorithm>
#include <cstring>
#include <vector>
#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/check_op.h"
#include "base/location.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/socket/next_proto.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
namespace chromecast {
// Buffer used for communication between two FakeStreamSockets.
class SocketBuffer {
public:
SocketBuffer() : pending_read_data_(nullptr), pending_read_len_(0) {}
~SocketBuffer() {}
// Reads |len| bytes from the buffer and writes it to |data|. Returns the
// number of bytes written to |data| if the read is synchronous, or
// ERR_IO_PENDING if the read is asynchronous. If the read is asynchronous,
// |callback| is called with the number of bytes written to |data| once the
// data has been written.
int Read(char* data, size_t len, net::CompletionOnceCallback callback) {
DCHECK(data);
DCHECK_GT(len, 0u);
DCHECK(callback);
if (data_.empty()) {
if (eos_) {
return 0;
}
pending_read_data_ = data;
pending_read_len_ = len;
pending_read_callback_ = std::move(callback);
return net::ERR_IO_PENDING;
}
return ReadInternal(data, len);
}
// Writes |len| bytes from |data| to the buffer. The write is always completed
// synchronously and all bytes are guaranteed to be written.
void Write(const char* data, size_t len) {
DCHECK(data);
DCHECK_GT(len, 0u);
data_.insert(data_.end(), data, data + len);
if (!pending_read_callback_.is_null()) {
int result = ReadInternal(pending_read_data_, pending_read_len_);
pending_read_data_ = nullptr;
pending_read_len_ = 0;
PostReadCallback(std::move(pending_read_callback_), result);
}
}
// Called when the remote end of the fake connection disconnects.
void ReceiveEOS() {
eos_ = true;
if (pending_read_callback_ && data_.empty()) {
PostReadCallback(std::move(pending_read_callback_), 0);
}
}
private:
int ReadInternal(char* data, size_t len) {
DCHECK(data);
DCHECK_GT(len, 0u);
len = std::min(len, data_.size());
std::memcpy(data, data_.data(), len);
data_.erase(data_.begin(), data_.begin() + len);
return len;
}
void PostReadCallback(net::CompletionOnceCallback callback, int result) {
base::SequencedTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(&SocketBuffer::CallReadCallback,
weak_factory_.GetWeakPtr(),
std::move(callback), result));
}
// Need a member function to asynchronously call the read callback, so we
// can use weak ptr.
void CallReadCallback(net::CompletionOnceCallback callback, int result) {
std::move(callback).Run(result);
}
std::vector<char> data_;
char* pending_read_data_;
size_t pending_read_len_;
net::CompletionOnceCallback pending_read_callback_;
bool eos_ = false;
base::WeakPtrFactory<SocketBuffer> weak_factory_{this};
DISALLOW_COPY_AND_ASSIGN(SocketBuffer);
};
FakeStreamSocket::FakeStreamSocket() : FakeStreamSocket(net::IPEndPoint()) {}
FakeStreamSocket::FakeStreamSocket(const net::IPEndPoint& local_address)
: local_address_(local_address),
buffer_(std::make_unique<SocketBuffer>()),
peer_(nullptr) {}
FakeStreamSocket::~FakeStreamSocket() {
if (peer_) {
peer_->RemoteDisconnected();
}
}
void FakeStreamSocket::SetPeer(FakeStreamSocket* peer) {
DCHECK(peer);
peer_ = peer;
}
void FakeStreamSocket::RemoteDisconnected() {
peer_ = nullptr;
buffer_->ReceiveEOS();
}
void FakeStreamSocket::SetBadSenderMode(bool bad_sender) {
bad_sender_mode_ = bad_sender;
}
int FakeStreamSocket::Read(net::IOBuffer* buf,
int buf_len,
net::CompletionOnceCallback callback) {
DCHECK(buf);
return buffer_->Read(buf->data(), buf_len, std::move(callback));
}
int FakeStreamSocket::Write(
net::IOBuffer* buf,
int buf_len,
net::CompletionOnceCallback /* callback */,
const net::NetworkTrafficAnnotationTag& /*traffic_annotation*/) {
DCHECK(buf);
if (!peer_) {
return net::ERR_SOCKET_NOT_CONNECTED;
}
int amount_to_send = buf_len;
if (bad_sender_mode_) {
amount_to_send = std::min(buf_len, buf_len / 2 + 1);
}
peer_->buffer_->Write(buf->data(), amount_to_send);
return amount_to_send;
}
int FakeStreamSocket::SetReceiveBufferSize(int32_t /* size */) {
return net::OK;
}
int FakeStreamSocket::SetSendBufferSize(int32_t /* size */) {
return net::OK;
}
int FakeStreamSocket::Connect(net::CompletionOnceCallback /* callback */) {
return net::OK;
}
void FakeStreamSocket::Disconnect() {}
bool FakeStreamSocket::IsConnected() const {
return true;
}
bool FakeStreamSocket::IsConnectedAndIdle() const {
return false;
}
int FakeStreamSocket::GetPeerAddress(net::IPEndPoint* address) const {
DCHECK(address);
if (!peer_) {
return net::ERR_SOCKET_NOT_CONNECTED;
}
*address = peer_->local_address_;
return net::OK;
}
int FakeStreamSocket::GetLocalAddress(net::IPEndPoint* address) const {
DCHECK(address);
*address = local_address_;
return net::OK;
}
const net::NetLogWithSource& FakeStreamSocket::NetLog() const {
return net_log_;
}
bool FakeStreamSocket::WasEverUsed() const {
return false;
}
bool FakeStreamSocket::WasAlpnNegotiated() const {
return false;
}
net::NextProto FakeStreamSocket::GetNegotiatedProtocol() const {
return net::kProtoUnknown;
}
bool FakeStreamSocket::GetSSLInfo(net::SSLInfo* /* ssl_info */) {
return false;
}
void FakeStreamSocket::GetConnectionAttempts(
net::ConnectionAttempts* /* out */) const {}
void FakeStreamSocket::ClearConnectionAttempts() {}
void FakeStreamSocket::AddConnectionAttempts(
const net::ConnectionAttempts& /* attempts */) {}
int64_t FakeStreamSocket::GetTotalReceivedBytes() const {
return 0;
}
void FakeStreamSocket::ApplySocketTag(const net::SocketTag& tag) {}
} // namespace chromecast