OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_session.h" | 5 #include "remoting/protocol/fake_session.h" |
6 | 6 |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "net/base/io_buffer.h" | 8 #include "net/base/io_buffer.h" |
9 #include "net/base/net_errors.h" | 9 #include "net/base/net_errors.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 input_data_.insert(input_data_.end(), data, data + data_size); | 29 input_data_.insert(input_data_.end(), data, data + data_size); |
30 // Complete pending read if any. | 30 // Complete pending read if any. |
31 if (read_pending_) { | 31 if (read_pending_) { |
32 read_pending_ = false; | 32 read_pending_ = false; |
33 int result = std::min(read_buffer_size_, | 33 int result = std::min(read_buffer_size_, |
34 static_cast<int>(input_data_.size() - input_pos_)); | 34 static_cast<int>(input_data_.size() - input_pos_)); |
35 CHECK(result > 0); | 35 CHECK(result > 0); |
36 memcpy(read_buffer_->data(), | 36 memcpy(read_buffer_->data(), |
37 &(*input_data_.begin()) + input_pos_, result); | 37 &(*input_data_.begin()) + input_pos_, result); |
38 input_pos_ += result; | 38 input_pos_ += result; |
39 read_callback_->Run(result); | 39 if (old_read_callback_) |
| 40 old_read_callback_->Run(result); |
| 41 else |
| 42 read_callback_.Run(result); |
40 read_buffer_ = NULL; | 43 read_buffer_ = NULL; |
41 } | 44 } |
42 } | 45 } |
43 | 46 |
44 int FakeSocket::Read(net::IOBuffer* buf, int buf_len, | 47 int FakeSocket::Read(net::IOBuffer* buf, int buf_len, |
45 net::OldCompletionCallback* callback) { | 48 net::OldCompletionCallback* callback) { |
46 EXPECT_EQ(message_loop_, MessageLoop::current()); | 49 EXPECT_EQ(message_loop_, MessageLoop::current()); |
47 if (input_pos_ < static_cast<int>(input_data_.size())) { | 50 if (input_pos_ < static_cast<int>(input_data_.size())) { |
48 int result = std::min(buf_len, | 51 int result = std::min(buf_len, |
49 static_cast<int>(input_data_.size()) - input_pos_); | 52 static_cast<int>(input_data_.size()) - input_pos_); |
50 memcpy(buf->data(), &(*input_data_.begin()) + input_pos_, result); | 53 memcpy(buf->data(), &(*input_data_.begin()) + input_pos_, result); |
51 input_pos_ += result; | 54 input_pos_ += result; |
52 return result; | 55 return result; |
53 } else { | 56 } else { |
54 read_pending_ = true; | 57 read_pending_ = true; |
55 read_buffer_ = buf; | 58 read_buffer_ = buf; |
56 read_buffer_size_ = buf_len; | 59 read_buffer_size_ = buf_len; |
| 60 old_read_callback_ = callback; |
| 61 return net::ERR_IO_PENDING; |
| 62 } |
| 63 } |
| 64 int FakeSocket::Read(net::IOBuffer* buf, int buf_len, |
| 65 const net::CompletionCallback& callback) { |
| 66 EXPECT_EQ(message_loop_, MessageLoop::current()); |
| 67 if (input_pos_ < static_cast<int>(input_data_.size())) { |
| 68 int result = std::min(buf_len, |
| 69 static_cast<int>(input_data_.size()) - input_pos_); |
| 70 memcpy(buf->data(), &(*input_data_.begin()) + input_pos_, result); |
| 71 input_pos_ += result; |
| 72 return result; |
| 73 } else { |
| 74 read_pending_ = true; |
| 75 read_buffer_ = buf; |
| 76 read_buffer_size_ = buf_len; |
57 read_callback_ = callback; | 77 read_callback_ = callback; |
58 return net::ERR_IO_PENDING; | 78 return net::ERR_IO_PENDING; |
59 } | 79 } |
60 } | 80 } |
61 | 81 |
62 int FakeSocket::Write(net::IOBuffer* buf, int buf_len, | 82 int FakeSocket::Write(net::IOBuffer* buf, int buf_len, |
63 net::OldCompletionCallback* callback) { | 83 net::OldCompletionCallback* callback) { |
64 EXPECT_EQ(message_loop_, MessageLoop::current()); | 84 EXPECT_EQ(message_loop_, MessageLoop::current()); |
65 written_data_.insert(written_data_.end(), | 85 written_data_.insert(written_data_.end(), |
66 buf->data(), buf->data() + buf_len); | 86 buf->data(), buf->data() + buf_len); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 return 0; | 159 return 0; |
140 } | 160 } |
141 | 161 |
142 base::TimeDelta FakeSocket::GetConnectTimeMicros() const { | 162 base::TimeDelta FakeSocket::GetConnectTimeMicros() const { |
143 NOTIMPLEMENTED(); | 163 NOTIMPLEMENTED(); |
144 return base::TimeDelta(); | 164 return base::TimeDelta(); |
145 } | 165 } |
146 | 166 |
147 FakeUdpSocket::FakeUdpSocket() | 167 FakeUdpSocket::FakeUdpSocket() |
148 : read_pending_(false), | 168 : read_pending_(false), |
| 169 old_read_callback_(NULL), |
149 input_pos_(0), | 170 input_pos_(0), |
150 message_loop_(MessageLoop::current()) { | 171 message_loop_(MessageLoop::current()) { |
151 } | 172 } |
152 | 173 |
153 FakeUdpSocket::~FakeUdpSocket() { | 174 FakeUdpSocket::~FakeUdpSocket() { |
154 EXPECT_EQ(message_loop_, MessageLoop::current()); | 175 EXPECT_EQ(message_loop_, MessageLoop::current()); |
155 } | 176 } |
156 | 177 |
157 void FakeUdpSocket::AppendInputPacket(const char* data, int data_size) { | 178 void FakeUdpSocket::AppendInputPacket(const char* data, int data_size) { |
158 EXPECT_EQ(message_loop_, MessageLoop::current()); | 179 EXPECT_EQ(message_loop_, MessageLoop::current()); |
159 input_packets_.push_back(std::string()); | 180 input_packets_.push_back(std::string()); |
160 input_packets_.back().assign(data, data + data_size); | 181 input_packets_.back().assign(data, data + data_size); |
161 | 182 |
162 // Complete pending read if any. | 183 // Complete pending read if any. |
163 if (read_pending_) { | 184 if (read_pending_) { |
164 read_pending_ = false; | 185 read_pending_ = false; |
165 int result = std::min(data_size, read_buffer_size_); | 186 int result = std::min(data_size, read_buffer_size_); |
166 memcpy(read_buffer_->data(), data, result); | 187 memcpy(read_buffer_->data(), data, result); |
167 input_pos_ = input_packets_.size(); | 188 input_pos_ = input_packets_.size(); |
168 read_callback_->Run(result); | 189 if (old_read_callback_) |
| 190 old_read_callback_->Run(result); |
| 191 else |
| 192 old_read_callback_->Run(result); |
169 read_buffer_ = NULL; | 193 read_buffer_ = NULL; |
170 } | 194 } |
171 } | 195 } |
172 | 196 |
173 int FakeUdpSocket::Read(net::IOBuffer* buf, int buf_len, | 197 int FakeUdpSocket::Read(net::IOBuffer* buf, int buf_len, |
174 net::OldCompletionCallback* callback) { | 198 net::OldCompletionCallback* callback) { |
175 EXPECT_EQ(message_loop_, MessageLoop::current()); | 199 EXPECT_EQ(message_loop_, MessageLoop::current()); |
176 if (input_pos_ < static_cast<int>(input_packets_.size())) { | 200 if (input_pos_ < static_cast<int>(input_packets_.size())) { |
177 int result = std::min( | 201 int result = std::min( |
178 buf_len, static_cast<int>(input_packets_[input_pos_].size())); | 202 buf_len, static_cast<int>(input_packets_[input_pos_].size())); |
179 memcpy(buf->data(), &(*input_packets_[input_pos_].begin()), result); | 203 memcpy(buf->data(), &(*input_packets_[input_pos_].begin()), result); |
180 ++input_pos_; | 204 ++input_pos_; |
181 return result; | 205 return result; |
182 } else { | 206 } else { |
183 read_pending_ = true; | 207 read_pending_ = true; |
184 read_buffer_ = buf; | 208 read_buffer_ = buf; |
185 read_buffer_size_ = buf_len; | 209 read_buffer_size_ = buf_len; |
| 210 old_read_callback_ = callback; |
| 211 return net::ERR_IO_PENDING; |
| 212 } |
| 213 } |
| 214 int FakeUdpSocket::Read(net::IOBuffer* buf, int buf_len, |
| 215 const net::CompletionCallback& callback) { |
| 216 EXPECT_EQ(message_loop_, MessageLoop::current()); |
| 217 if (input_pos_ < static_cast<int>(input_packets_.size())) { |
| 218 int result = std::min( |
| 219 buf_len, static_cast<int>(input_packets_[input_pos_].size())); |
| 220 memcpy(buf->data(), &(*input_packets_[input_pos_].begin()), result); |
| 221 ++input_pos_; |
| 222 return result; |
| 223 } else { |
| 224 read_pending_ = true; |
| 225 read_buffer_ = buf; |
| 226 read_buffer_size_ = buf_len; |
186 read_callback_ = callback; | 227 read_callback_ = callback; |
187 return net::ERR_IO_PENDING; | 228 return net::ERR_IO_PENDING; |
188 } | 229 } |
189 } | 230 } |
190 | 231 |
191 int FakeUdpSocket::Write(net::IOBuffer* buf, int buf_len, | 232 int FakeUdpSocket::Write(net::IOBuffer* buf, int buf_len, |
192 net::OldCompletionCallback* callback) { | 233 net::OldCompletionCallback* callback) { |
193 EXPECT_EQ(message_loop_, MessageLoop::current()); | 234 EXPECT_EQ(message_loop_, MessageLoop::current()); |
194 written_packets_.push_back(std::string()); | 235 written_packets_.push_back(std::string()); |
195 written_packets_.back().assign(buf->data(), buf->data() + buf_len); | 236 written_packets_.back().assign(buf->data(), buf->data() + buf_len); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 void FakeSession::set_config(const SessionConfig& config) { | 305 void FakeSession::set_config(const SessionConfig& config) { |
265 config_ = config; | 306 config_ = config; |
266 } | 307 } |
267 | 308 |
268 void FakeSession::Close() { | 309 void FakeSession::Close() { |
269 closed_ = true; | 310 closed_ = true; |
270 } | 311 } |
271 | 312 |
272 } // namespace protocol | 313 } // namespace protocol |
273 } // namespace remoting | 314 } // namespace remoting |
OLD | NEW |