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 <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
13 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
14 #include "net/base/address_list.h" | 14 #include "net/base/address_list.h" |
15 #include "net/base/io_buffer.h" | 15 #include "net/base/io_buffer.h" |
16 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 | 18 |
19 namespace remoting { | 19 namespace remoting { |
20 namespace protocol { | 20 namespace protocol { |
21 | 21 |
22 FakeStreamSocket::FakeStreamSocket() | 22 FakeStreamSocket::FakeStreamSocket() |
23 : task_runner_(base::ThreadTaskRunnerHandle::Get()), weak_factory_(this) {} | 23 : task_runner_(base::ThreadTaskRunnerHandle::Get()), weak_factory_(this) {} |
24 | 24 |
25 FakeStreamSocket::~FakeStreamSocket() { | 25 FakeStreamSocket::~FakeStreamSocket() { |
26 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); | 26 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); |
27 if (peer_socket_) { | 27 if (peer_socket_) { |
28 task_runner_->PostTask( | 28 task_runner_->PostTask( |
29 FROM_HERE, base::Bind(&FakeStreamSocket::AppendReadError, peer_socket_, | 29 FROM_HERE, base::Bind(&FakeStreamSocket::SetReadError, peer_socket_, |
30 net::ERR_CONNECTION_CLOSED)); | 30 net::ERR_CONNECTION_CLOSED)); |
31 } | 31 } |
32 } | 32 } |
33 | 33 |
34 void FakeStreamSocket::AppendInputData(const std::string& data) { | 34 void FakeStreamSocket::AppendInputData(const std::string& data) { |
35 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); | 35 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); |
36 input_data_.insert(input_data_.end(), data.begin(), data.end()); | 36 input_data_.insert(input_data_.end(), data.begin(), data.end()); |
37 // Complete pending read if any. | 37 // Complete pending read if any. |
38 if (!read_callback_.is_null()) { | 38 if (!read_callback_.is_null()) { |
39 int result = std::min(read_buffer_size_, | 39 int result = std::min(read_buffer_size_, |
40 static_cast<int>(input_data_.size() - input_pos_)); | 40 static_cast<int>(input_data_.size() - input_pos_)); |
41 EXPECT_GT(result, 0); | 41 EXPECT_GT(result, 0); |
42 memcpy(read_buffer_->data(), | 42 memcpy(read_buffer_->data(), |
43 &(*input_data_.begin()) + input_pos_, result); | 43 &(*input_data_.begin()) + input_pos_, result); |
44 input_pos_ += result; | 44 input_pos_ += result; |
45 read_buffer_ = nullptr; | 45 read_buffer_ = nullptr; |
46 | 46 |
47 base::ResetAndReturn(&read_callback_).Run(result); | 47 base::ResetAndReturn(&read_callback_).Run(result); |
48 } | 48 } |
49 } | 49 } |
50 | 50 |
51 void FakeStreamSocket::AppendReadError(int error) { | 51 void FakeStreamSocket::SetReadError(int error) { |
52 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); | 52 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); |
53 // Complete pending read if any. | 53 // Complete pending read if any. |
54 if (!read_callback_.is_null()) { | 54 if (!read_callback_.is_null()) { |
55 base::ResetAndReturn(&read_callback_).Run(error); | 55 base::ResetAndReturn(&read_callback_).Run(error); |
56 } else { | 56 } else { |
57 next_read_error_ = error; | 57 next_read_error_ = error; |
58 } | 58 } |
59 } | 59 } |
60 | 60 |
61 void FakeStreamSocket::PairWith(FakeStreamSocket* peer_socket) { | 61 void FakeStreamSocket::PairWith(FakeStreamSocket* peer_socket) { |
(...skipping 10 matching lines...) Expand all Loading... |
72 int buf_len, | 72 int buf_len, |
73 const net::CompletionCallback& callback) { | 73 const net::CompletionCallback& callback) { |
74 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); | 74 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); |
75 | 75 |
76 if (input_pos_ < static_cast<int>(input_data_.size())) { | 76 if (input_pos_ < static_cast<int>(input_data_.size())) { |
77 int result = std::min(buf_len, | 77 int result = std::min(buf_len, |
78 static_cast<int>(input_data_.size()) - input_pos_); | 78 static_cast<int>(input_data_.size()) - input_pos_); |
79 memcpy(buf->data(), &(*input_data_.begin()) + input_pos_, result); | 79 memcpy(buf->data(), &(*input_data_.begin()) + input_pos_, result); |
80 input_pos_ += result; | 80 input_pos_ += result; |
81 return result; | 81 return result; |
82 } else if (next_read_error_ != net::OK) { | 82 } else if (next_read_error_.has_value()) { |
83 int r = next_read_error_; | 83 int r = next_read_error_.value(); |
84 next_read_error_ = net::OK; | 84 next_read_error_.reset(); |
85 return r; | 85 return r; |
86 } else { | 86 } else { |
87 read_buffer_ = buf; | 87 read_buffer_ = buf; |
88 read_buffer_size_ = buf_len; | 88 read_buffer_size_ = buf_len; |
89 read_callback_ = callback; | 89 read_callback_ = callback; |
90 return net::ERR_IO_PENDING; | 90 return net::ERR_IO_PENDING; |
91 } | 91 } |
92 } | 92 } |
93 | 93 |
94 int FakeStreamSocket::Write(const scoped_refptr<net::IOBuffer>& buf, | 94 int FakeStreamSocket::Write(const scoped_refptr<net::IOBuffer>& buf, |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 if (channels_.find(name) != channels_.end()) | 198 if (channels_.find(name) != channels_.end()) |
199 callback.Run(std::move(owned_channel)); | 199 callback.Run(std::move(owned_channel)); |
200 } | 200 } |
201 | 201 |
202 void FakeStreamChannelFactory::CancelChannelCreation(const std::string& name) { | 202 void FakeStreamChannelFactory::CancelChannelCreation(const std::string& name) { |
203 channels_.erase(name); | 203 channels_.erase(name); |
204 } | 204 } |
205 | 205 |
206 } // namespace protocol | 206 } // namespace protocol |
207 } // namespace remoting | 207 } // namespace remoting |
OLD | NEW |