Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "remoting/protocol/fake_datagram_socket.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/single_thread_task_runner.h" | |
| 9 #include "base/thread_task_runner_handle.h" | |
| 10 #include "net/base/address_list.h" | |
| 11 #include "net/base/io_buffer.h" | |
| 12 #include "net/base/net_errors.h" | |
| 13 #include "net/base/net_util.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | |
| 15 | |
| 16 namespace remoting { | |
| 17 namespace protocol { | |
| 18 | |
| 19 FakeDatagramSocket::FakeDatagramSocket() | |
| 20 : read_pending_(false), | |
| 21 input_pos_(0), | |
| 22 task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
| 23 weak_factory_(this) { | |
| 24 } | |
| 25 | |
| 26 FakeDatagramSocket::~FakeDatagramSocket() { | |
| 27 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); | |
| 28 } | |
| 29 | |
| 30 void FakeDatagramSocket::AppendInputPacket(const std::string& data) { | |
| 31 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); | |
| 32 input_packets_.push_back(data); | |
| 33 | |
| 34 // Complete pending read if any. | |
| 35 if (read_pending_) { | |
| 36 read_pending_ = false; | |
|
Wez
2014/09/20 00:25:09
nit: Suggest blank line after this, since next lin
Sergey Ulanov
2014/09/22 19:22:10
Done.
| |
| 37 int result = std::min(static_cast<int>(data.size()), read_buffer_size_); | |
| 38 memcpy(read_buffer_->data(), data.data(), result); | |
| 39 input_pos_ = input_packets_.size(); | |
| 40 read_buffer_ = NULL; | |
|
Wez
2014/09/20 00:25:09
nit: Suggest blank line after this, since the next
Sergey Ulanov
2014/09/22 19:22:10
Done.
| |
| 41 net::CompletionCallback callback = read_callback_; | |
| 42 read_callback_.Reset(); | |
| 43 callback.Run(result); | |
| 44 } | |
| 45 } | |
| 46 | |
| 47 void FakeDatagramSocket::PairWith(FakeDatagramSocket* peer_socket) { | |
| 48 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); | |
| 49 peer_socket_ = peer_socket->weak_factory_.GetWeakPtr(); | |
| 50 peer_socket->peer_socket_ = weak_factory_.GetWeakPtr(); | |
| 51 } | |
| 52 | |
| 53 int FakeDatagramSocket::Read(net::IOBuffer* buf, int buf_len, | |
| 54 const net::CompletionCallback& callback) { | |
| 55 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); | |
| 56 if (input_pos_ < static_cast<int>(input_packets_.size())) { | |
| 57 int result = std::min( | |
| 58 buf_len, static_cast<int>(input_packets_[input_pos_].size())); | |
| 59 memcpy(buf->data(), &(*input_packets_[input_pos_].begin()), result); | |
|
Wez
2014/09/20 00:25:09
nit: You have to do this min+memcpy dance above as
Sergey Ulanov
2014/09/22 19:22:10
Done.
| |
| 60 ++input_pos_; | |
| 61 return result; | |
| 62 } else { | |
| 63 read_pending_ = true; | |
| 64 read_buffer_ = buf; | |
| 65 read_buffer_size_ = buf_len; | |
| 66 read_callback_ = callback; | |
| 67 return net::ERR_IO_PENDING; | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 int FakeDatagramSocket::Write(net::IOBuffer* buf, int buf_len, | |
| 72 const net::CompletionCallback& callback) { | |
| 73 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); | |
| 74 written_packets_.push_back(std::string()); | |
| 75 written_packets_.back().assign(buf->data(), buf->data() + buf_len); | |
| 76 | |
| 77 if (peer_socket_.get()) { | |
| 78 task_runner_->PostTask( | |
| 79 FROM_HERE, | |
| 80 base::Bind(&FakeDatagramSocket::AppendInputPacket, | |
| 81 peer_socket_, | |
| 82 std::string(buf->data(), buf->data() + buf_len))); | |
| 83 } | |
| 84 | |
| 85 return buf_len; | |
| 86 } | |
| 87 | |
| 88 int FakeDatagramSocket::SetReceiveBufferSize(int32 size) { | |
| 89 NOTIMPLEMENTED(); | |
| 90 return net::ERR_NOT_IMPLEMENTED; | |
| 91 } | |
| 92 | |
| 93 int FakeDatagramSocket::SetSendBufferSize(int32 size) { | |
| 94 NOTIMPLEMENTED(); | |
| 95 return net::ERR_NOT_IMPLEMENTED; | |
| 96 } | |
| 97 | |
| 98 FakeDatagramChannelFactory::FakeDatagramChannelFactory() | |
| 99 : task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
| 100 async_creation_(false), | |
| 101 fail_(false), | |
| 102 weak_factory_(this) { | |
| 103 } | |
| 104 | |
| 105 FakeDatagramChannelFactory::~FakeDatagramChannelFactory() {} | |
|
Wez
2014/09/20 00:25:09
EXPECT_TRUE(channels_.empty()) here?
Sergey Ulanov
2014/09/22 19:22:11
Done.
| |
| 106 | |
| 107 void FakeDatagramChannelFactory::PairWith( | |
| 108 FakeDatagramChannelFactory* peer_factory) { | |
| 109 peer_factory_ = peer_factory->weak_factory_.GetWeakPtr(); | |
| 110 peer_factory_->peer_factory_ = weak_factory_.GetWeakPtr(); | |
| 111 } | |
| 112 | |
| 113 FakeDatagramSocket* FakeDatagramChannelFactory::GetChannel( | |
| 114 const std::string& name) { | |
| 115 return channels_[name]; | |
| 116 } | |
| 117 | |
| 118 void FakeDatagramChannelFactory::CreateChannel( | |
| 119 const std::string& name, | |
| 120 const ChannelCreatedCallback& callback) { | |
| 121 scoped_ptr<FakeDatagramSocket> channel; | |
| 122 // If we are in the error state then we put NULL in the channels list, so that | |
| 123 // NotifyChannelCreated() still calls the callback. | |
| 124 if (!fail_) | |
| 125 channel.reset(new FakeDatagramSocket()); | |
|
Wez
2014/09/20 00:25:09
nit: I thought you were generally opting for { } e
Wez
2014/09/20 00:25:09
nit: This would be more readable if you initialize
Sergey Ulanov
2014/09/22 19:22:11
Done.
Sergey Ulanov
2014/09/22 19:22:11
Nah, I don't care about it as long as the code is
| |
| 126 | |
| 127 if (peer_factory_) { | |
| 128 ChannelsMap::iterator peer = peer_factory_->channels_.find(name); | |
| 129 if (peer != peer_factory_->channels_.end()) | |
| 130 channel->PairWith(peer->second); | |
|
Wez
2014/09/20 00:25:09
Won't this crash if |fail_| is true, since |channe
Sergey Ulanov
2014/09/22 19:22:10
Done.
| |
| 131 } | |
| 132 | |
| 133 channels_[name] = channel.release(); | |
| 134 | |
| 135 if (async_creation_) { | |
| 136 task_runner_->PostTask( | |
| 137 FROM_HERE, | |
| 138 base::Bind(&FakeDatagramChannelFactory::NotifyChannelCreated, | |
| 139 weak_factory_.GetWeakPtr(), name, callback)); | |
| 140 } else { | |
| 141 NotifyChannelCreated(name, callback); | |
| 142 } | |
| 143 } | |
| 144 | |
| 145 void FakeDatagramChannelFactory::NotifyChannelCreated( | |
| 146 const std::string& name, | |
| 147 const ChannelCreatedCallback& callback) { | |
| 148 if (channels_.find(name) != channels_.end()) | |
| 149 callback.Run(scoped_ptr<net::Socket>(channels_[name])); | |
|
Wez
2014/09/20 00:25:09
Don't you need to clear the channel from |channels
Sergey Ulanov
2014/09/22 19:22:10
We need it in |channels_| so that the channel can
| |
| 150 } | |
| 151 | |
| 152 void FakeDatagramChannelFactory::CancelChannelCreation( | |
| 153 const std::string& name) { | |
| 154 channels_.erase(name); | |
| 155 } | |
| 156 | |
| 157 } // namespace protocol | |
| 158 } // namespace remoting | |
| OLD | NEW |