OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/protocol/fake_stream_socket.h" | 5 #include "remoting/protocol/fake_stream_socket.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback_helpers.h" | |
8 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
9 #include "base/thread_task_runner_handle.h" | 10 #include "base/thread_task_runner_handle.h" |
10 #include "net/base/address_list.h" | 11 #include "net/base/address_list.h" |
11 #include "net/base/io_buffer.h" | 12 #include "net/base/io_buffer.h" |
12 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
13 #include "net/base/net_util.h" | 14 #include "net/base/net_util.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 | 16 |
16 namespace remoting { | 17 namespace remoting { |
17 namespace protocol { | 18 namespace protocol { |
(...skipping 20 matching lines...) Expand all Loading... | |
38 // Complete pending read if any. | 39 // Complete pending read if any. |
39 if (!read_callback_.is_null()) { | 40 if (!read_callback_.is_null()) { |
40 int result = std::min(read_buffer_size_, | 41 int result = std::min(read_buffer_size_, |
41 static_cast<int>(input_data_.size() - input_pos_)); | 42 static_cast<int>(input_data_.size() - input_pos_)); |
42 EXPECT_GT(result, 0); | 43 EXPECT_GT(result, 0); |
43 memcpy(read_buffer_->data(), | 44 memcpy(read_buffer_->data(), |
44 &(*input_data_.begin()) + input_pos_, result); | 45 &(*input_data_.begin()) + input_pos_, result); |
45 input_pos_ += result; | 46 input_pos_ += result; |
46 read_buffer_ = nullptr; | 47 read_buffer_ = nullptr; |
47 | 48 |
48 net::CompletionCallback callback = read_callback_; | 49 base::ResetAndReturn(&read_callback_).Run(result); |
49 read_callback_.Reset(); | |
50 callback.Run(result); | |
51 } | 50 } |
52 } | 51 } |
53 | 52 |
53 void FakeStreamSocket::AppendReadError(int error) { | |
54 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); | |
55 // Complete pending ead if any. | |
Sergey Ulanov
2015/04/24 00:34:45
typo: ead
davidben
2015/04/24 20:52:44
Done.
| |
56 if (!read_callback_.is_null()) { | |
57 base::ResetAndReturn(&read_callback_).Run(error); | |
58 } else { | |
59 next_read_error_ = error; | |
60 } | |
61 } | |
62 | |
54 void FakeStreamSocket::PairWith(FakeStreamSocket* peer_socket) { | 63 void FakeStreamSocket::PairWith(FakeStreamSocket* peer_socket) { |
55 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); | 64 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); |
56 peer_socket_ = peer_socket->GetWeakPtr(); | 65 peer_socket_ = peer_socket->GetWeakPtr(); |
57 peer_socket->peer_socket_ = GetWeakPtr(); | 66 peer_socket->peer_socket_ = GetWeakPtr(); |
58 } | 67 } |
59 | 68 |
60 base::WeakPtr<FakeStreamSocket> FakeStreamSocket::GetWeakPtr() { | 69 base::WeakPtr<FakeStreamSocket> FakeStreamSocket::GetWeakPtr() { |
61 return weak_factory_.GetWeakPtr(); | 70 return weak_factory_.GetWeakPtr(); |
62 } | 71 } |
63 | 72 |
64 int FakeStreamSocket::Read(net::IOBuffer* buf, int buf_len, | 73 int FakeStreamSocket::Read(net::IOBuffer* buf, int buf_len, |
65 const net::CompletionCallback& callback) { | 74 const net::CompletionCallback& callback) { |
66 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); | 75 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); |
67 | 76 |
68 if (next_read_error_ != net::OK) { | |
69 int r = next_read_error_; | |
70 next_read_error_ = net::OK; | |
71 return r; | |
72 } | |
73 | |
74 if (input_pos_ < static_cast<int>(input_data_.size())) { | 77 if (input_pos_ < static_cast<int>(input_data_.size())) { |
75 int result = std::min(buf_len, | 78 int result = std::min(buf_len, |
76 static_cast<int>(input_data_.size()) - input_pos_); | 79 static_cast<int>(input_data_.size()) - input_pos_); |
77 memcpy(buf->data(), &(*input_data_.begin()) + input_pos_, result); | 80 memcpy(buf->data(), &(*input_data_.begin()) + input_pos_, result); |
78 input_pos_ += result; | 81 input_pos_ += result; |
79 return result; | 82 return result; |
83 } else if (next_read_error_ != net::OK) { | |
84 int r = next_read_error_; | |
85 next_read_error_ = net::OK; | |
86 return r; | |
80 } else { | 87 } else { |
81 read_buffer_ = buf; | 88 read_buffer_ = buf; |
82 read_buffer_size_ = buf_len; | 89 read_buffer_size_ = buf_len; |
83 read_callback_ = callback; | 90 read_callback_ = callback; |
84 return net::ERR_IO_PENDING; | 91 return net::ERR_IO_PENDING; |
85 } | 92 } |
86 } | 93 } |
87 | 94 |
88 int FakeStreamSocket::Write(net::IOBuffer* buf, int buf_len, | 95 int FakeStreamSocket::Write(net::IOBuffer* buf, int buf_len, |
89 const net::CompletionCallback& callback) { | 96 const net::CompletionCallback& callback) { |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
152 return net::ERR_NOT_IMPLEMENTED; | 159 return net::ERR_NOT_IMPLEMENTED; |
153 } | 160 } |
154 | 161 |
155 int FakeStreamSocket::Connect(const net::CompletionCallback& callback) { | 162 int FakeStreamSocket::Connect(const net::CompletionCallback& callback) { |
156 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); | 163 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); |
157 return net::OK; | 164 return net::OK; |
158 } | 165 } |
159 | 166 |
160 void FakeStreamSocket::Disconnect() { | 167 void FakeStreamSocket::Disconnect() { |
161 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); | 168 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); |
169 | |
170 if (peer_socket_.get()) { | |
171 task_runner_->PostTask( | |
172 FROM_HERE, | |
173 base::Bind(&FakeStreamSocket::AppendReadError, | |
174 peer_socket_, | |
175 net::ERR_CONNECTION_CLOSED)); | |
176 } | |
162 peer_socket_.reset(); | 177 peer_socket_.reset(); |
163 } | 178 } |
164 | 179 |
165 bool FakeStreamSocket::IsConnected() const { | 180 bool FakeStreamSocket::IsConnected() const { |
166 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); | 181 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); |
167 return true; | 182 return true; |
168 } | 183 } |
169 | 184 |
170 bool FakeStreamSocket::IsConnectedAndIdle() const { | 185 bool FakeStreamSocket::IsConnectedAndIdle() const { |
171 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); | 186 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
268 if (channels_.find(name) != channels_.end()) | 283 if (channels_.find(name) != channels_.end()) |
269 callback.Run(owned_channel.Pass()); | 284 callback.Run(owned_channel.Pass()); |
270 } | 285 } |
271 | 286 |
272 void FakeStreamChannelFactory::CancelChannelCreation(const std::string& name) { | 287 void FakeStreamChannelFactory::CancelChannelCreation(const std::string& name) { |
273 channels_.erase(name); | 288 channels_.erase(name); |
274 } | 289 } |
275 | 290 |
276 } // namespace protocol | 291 } // namespace protocol |
277 } // namespace remoting | 292 } // namespace remoting |
OLD | NEW |