| 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 if (old_read_callback_) | 39 read_callback_.Run(result); |
| 40 old_read_callback_->Run(result); | |
| 41 else | |
| 42 read_callback_.Run(result); | |
| 43 read_buffer_ = NULL; | 40 read_buffer_ = NULL; |
| 44 } | 41 } |
| 45 } | 42 } |
| 46 | 43 |
| 47 int FakeSocket::Read(net::IOBuffer* buf, int buf_len, | 44 int FakeSocket::Read(net::IOBuffer* buf, int buf_len, |
| 48 net::OldCompletionCallback* callback) { | |
| 49 EXPECT_EQ(message_loop_, MessageLoop::current()); | |
| 50 if (input_pos_ < static_cast<int>(input_data_.size())) { | |
| 51 int result = std::min(buf_len, | |
| 52 static_cast<int>(input_data_.size()) - input_pos_); | |
| 53 memcpy(buf->data(), &(*input_data_.begin()) + input_pos_, result); | |
| 54 input_pos_ += result; | |
| 55 return result; | |
| 56 } else { | |
| 57 read_pending_ = true; | |
| 58 read_buffer_ = buf; | |
| 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) { | 45 const net::CompletionCallback& callback) { |
| 66 EXPECT_EQ(message_loop_, MessageLoop::current()); | 46 EXPECT_EQ(message_loop_, MessageLoop::current()); |
| 67 if (input_pos_ < static_cast<int>(input_data_.size())) { | 47 if (input_pos_ < static_cast<int>(input_data_.size())) { |
| 68 int result = std::min(buf_len, | 48 int result = std::min(buf_len, |
| 69 static_cast<int>(input_data_.size()) - input_pos_); | 49 static_cast<int>(input_data_.size()) - input_pos_); |
| 70 memcpy(buf->data(), &(*input_data_.begin()) + input_pos_, result); | 50 memcpy(buf->data(), &(*input_data_.begin()) + input_pos_, result); |
| 71 input_pos_ += result; | 51 input_pos_ += result; |
| 72 return result; | 52 return result; |
| 73 } else { | 53 } else { |
| 74 read_pending_ = true; | 54 read_pending_ = true; |
| 75 read_buffer_ = buf; | 55 read_buffer_ = buf; |
| 76 read_buffer_size_ = buf_len; | 56 read_buffer_size_ = buf_len; |
| 77 read_callback_ = callback; | 57 read_callback_ = callback; |
| 78 return net::ERR_IO_PENDING; | 58 return net::ERR_IO_PENDING; |
| 79 } | 59 } |
| 80 } | 60 } |
| 81 | 61 |
| 82 int FakeSocket::Write(net::IOBuffer* buf, int buf_len, | 62 int FakeSocket::Write(net::IOBuffer* buf, int buf_len, |
| 83 net::OldCompletionCallback* callback) { | 63 const net::CompletionCallback& callback) { |
| 84 EXPECT_EQ(message_loop_, MessageLoop::current()); | 64 EXPECT_EQ(message_loop_, MessageLoop::current()); |
| 85 written_data_.insert(written_data_.end(), | 65 written_data_.insert(written_data_.end(), |
| 86 buf->data(), buf->data() + buf_len); | 66 buf->data(), buf->data() + buf_len); |
| 87 return buf_len; | 67 return buf_len; |
| 88 } | 68 } |
| 89 | 69 |
| 90 bool FakeSocket::SetReceiveBufferSize(int32 size) { | 70 bool FakeSocket::SetReceiveBufferSize(int32 size) { |
| 91 NOTIMPLEMENTED(); | 71 NOTIMPLEMENTED(); |
| 92 return false; | 72 return false; |
| 93 } | 73 } |
| 94 bool FakeSocket::SetSendBufferSize(int32 size) { | 74 bool FakeSocket::SetSendBufferSize(int32 size) { |
| 95 NOTIMPLEMENTED(); | 75 NOTIMPLEMENTED(); |
| 96 return false; | 76 return false; |
| 97 } | 77 } |
| 98 | 78 |
| 99 int FakeSocket::Connect(net::OldCompletionCallback* callback) { | |
| 100 EXPECT_EQ(message_loop_, MessageLoop::current()); | |
| 101 return net::OK; | |
| 102 } | |
| 103 int FakeSocket::Connect(const net::CompletionCallback& callback) { | 79 int FakeSocket::Connect(const net::CompletionCallback& callback) { |
| 104 EXPECT_EQ(message_loop_, MessageLoop::current()); | 80 EXPECT_EQ(message_loop_, MessageLoop::current()); |
| 105 return net::OK; | 81 return net::OK; |
| 106 } | 82 } |
| 107 | 83 |
| 108 void FakeSocket::Disconnect() { | 84 void FakeSocket::Disconnect() { |
| 109 NOTIMPLEMENTED(); | 85 NOTIMPLEMENTED(); |
| 110 } | 86 } |
| 111 | 87 |
| 112 bool FakeSocket::IsConnected() const { | 88 bool FakeSocket::IsConnected() const { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 return 0; | 135 return 0; |
| 160 } | 136 } |
| 161 | 137 |
| 162 base::TimeDelta FakeSocket::GetConnectTimeMicros() const { | 138 base::TimeDelta FakeSocket::GetConnectTimeMicros() const { |
| 163 NOTIMPLEMENTED(); | 139 NOTIMPLEMENTED(); |
| 164 return base::TimeDelta(); | 140 return base::TimeDelta(); |
| 165 } | 141 } |
| 166 | 142 |
| 167 FakeUdpSocket::FakeUdpSocket() | 143 FakeUdpSocket::FakeUdpSocket() |
| 168 : read_pending_(false), | 144 : read_pending_(false), |
| 169 old_read_callback_(NULL), | |
| 170 input_pos_(0), | 145 input_pos_(0), |
| 171 message_loop_(MessageLoop::current()) { | 146 message_loop_(MessageLoop::current()) { |
| 172 } | 147 } |
| 173 | 148 |
| 174 FakeUdpSocket::~FakeUdpSocket() { | 149 FakeUdpSocket::~FakeUdpSocket() { |
| 175 EXPECT_EQ(message_loop_, MessageLoop::current()); | 150 EXPECT_EQ(message_loop_, MessageLoop::current()); |
| 176 } | 151 } |
| 177 | 152 |
| 178 void FakeUdpSocket::AppendInputPacket(const char* data, int data_size) { | 153 void FakeUdpSocket::AppendInputPacket(const char* data, int data_size) { |
| 179 EXPECT_EQ(message_loop_, MessageLoop::current()); | 154 EXPECT_EQ(message_loop_, MessageLoop::current()); |
| 180 input_packets_.push_back(std::string()); | 155 input_packets_.push_back(std::string()); |
| 181 input_packets_.back().assign(data, data + data_size); | 156 input_packets_.back().assign(data, data + data_size); |
| 182 | 157 |
| 183 // Complete pending read if any. | 158 // Complete pending read if any. |
| 184 if (read_pending_) { | 159 if (read_pending_) { |
| 185 read_pending_ = false; | 160 read_pending_ = false; |
| 186 int result = std::min(data_size, read_buffer_size_); | 161 int result = std::min(data_size, read_buffer_size_); |
| 187 memcpy(read_buffer_->data(), data, result); | 162 memcpy(read_buffer_->data(), data, result); |
| 188 input_pos_ = input_packets_.size(); | 163 input_pos_ = input_packets_.size(); |
| 189 if (old_read_callback_) | 164 read_callback_.Run(result); |
| 190 old_read_callback_->Run(result); | |
| 191 else | |
| 192 old_read_callback_->Run(result); | |
| 193 read_buffer_ = NULL; | 165 read_buffer_ = NULL; |
| 194 } | 166 } |
| 195 } | 167 } |
| 196 | 168 |
| 197 int FakeUdpSocket::Read(net::IOBuffer* buf, int buf_len, | 169 int FakeUdpSocket::Read(net::IOBuffer* buf, int buf_len, |
| 198 net::OldCompletionCallback* callback) { | |
| 199 EXPECT_EQ(message_loop_, MessageLoop::current()); | |
| 200 if (input_pos_ < static_cast<int>(input_packets_.size())) { | |
| 201 int result = std::min( | |
| 202 buf_len, static_cast<int>(input_packets_[input_pos_].size())); | |
| 203 memcpy(buf->data(), &(*input_packets_[input_pos_].begin()), result); | |
| 204 ++input_pos_; | |
| 205 return result; | |
| 206 } else { | |
| 207 read_pending_ = true; | |
| 208 read_buffer_ = buf; | |
| 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) { | 170 const net::CompletionCallback& callback) { |
| 216 EXPECT_EQ(message_loop_, MessageLoop::current()); | 171 EXPECT_EQ(message_loop_, MessageLoop::current()); |
| 217 if (input_pos_ < static_cast<int>(input_packets_.size())) { | 172 if (input_pos_ < static_cast<int>(input_packets_.size())) { |
| 218 int result = std::min( | 173 int result = std::min( |
| 219 buf_len, static_cast<int>(input_packets_[input_pos_].size())); | 174 buf_len, static_cast<int>(input_packets_[input_pos_].size())); |
| 220 memcpy(buf->data(), &(*input_packets_[input_pos_].begin()), result); | 175 memcpy(buf->data(), &(*input_packets_[input_pos_].begin()), result); |
| 221 ++input_pos_; | 176 ++input_pos_; |
| 222 return result; | 177 return result; |
| 223 } else { | 178 } else { |
| 224 read_pending_ = true; | 179 read_pending_ = true; |
| 225 read_buffer_ = buf; | 180 read_buffer_ = buf; |
| 226 read_buffer_size_ = buf_len; | 181 read_buffer_size_ = buf_len; |
| 227 read_callback_ = callback; | 182 read_callback_ = callback; |
| 228 return net::ERR_IO_PENDING; | 183 return net::ERR_IO_PENDING; |
| 229 } | 184 } |
| 230 } | 185 } |
| 231 | 186 |
| 232 int FakeUdpSocket::Write(net::IOBuffer* buf, int buf_len, | 187 int FakeUdpSocket::Write(net::IOBuffer* buf, int buf_len, |
| 233 net::OldCompletionCallback* callback) { | 188 const net::CompletionCallback& callback) { |
| 234 EXPECT_EQ(message_loop_, MessageLoop::current()); | 189 EXPECT_EQ(message_loop_, MessageLoop::current()); |
| 235 written_packets_.push_back(std::string()); | 190 written_packets_.push_back(std::string()); |
| 236 written_packets_.back().assign(buf->data(), buf->data() + buf_len); | 191 written_packets_.back().assign(buf->data(), buf->data() + buf_len); |
| 237 return buf_len; | 192 return buf_len; |
| 238 } | 193 } |
| 239 | 194 |
| 240 bool FakeUdpSocket::SetReceiveBufferSize(int32 size) { | 195 bool FakeUdpSocket::SetReceiveBufferSize(int32 size) { |
| 241 NOTIMPLEMENTED(); | 196 NOTIMPLEMENTED(); |
| 242 return false; | 197 return false; |
| 243 } | 198 } |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 void FakeSession::set_config(const SessionConfig& config) { | 260 void FakeSession::set_config(const SessionConfig& config) { |
| 306 config_ = config; | 261 config_ = config; |
| 307 } | 262 } |
| 308 | 263 |
| 309 void FakeSession::Close() { | 264 void FakeSession::Close() { |
| 310 closed_ = true; | 265 closed_ = true; |
| 311 } | 266 } |
| 312 | 267 |
| 313 } // namespace protocol | 268 } // namespace protocol |
| 314 } // namespace remoting | 269 } // namespace remoting |
| OLD | NEW |