Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(582)

Side by Side Diff: remoting/protocol/connection_tester.cc

Issue 1197853003: Add P2PDatagramSocket and P2PStreamSocket interfaces. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « remoting/protocol/connection_tester.h ('k') | remoting/protocol/datagram_channel_factory.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/connection_tester.h" 5 #include "remoting/protocol/connection_tester.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
9 #include "net/base/io_buffer.h" 9 #include "net/base/io_buffer.h"
10 #include "net/base/net_errors.h" 10 #include "net/base/net_errors.h"
11 #include "net/socket/stream_socket.h" 11 #include "remoting/protocol/p2p_datagram_socket.h"
12 #include "remoting/protocol/p2p_stream_socket.h"
12 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
13 14
14 namespace remoting { 15 namespace remoting {
15 namespace protocol { 16 namespace protocol {
16 17
17 StreamConnectionTester::StreamConnectionTester(net::StreamSocket* client_socket, 18 StreamConnectionTester::StreamConnectionTester(P2PStreamSocket* client_socket,
18 net::StreamSocket* host_socket, 19 P2PStreamSocket* host_socket,
19 int message_size, 20 int message_size,
20 int message_count) 21 int message_count)
21 : message_loop_(base::MessageLoop::current()), 22 : message_loop_(base::MessageLoop::current()),
22 host_socket_(host_socket), 23 host_socket_(host_socket),
23 client_socket_(client_socket), 24 client_socket_(client_socket),
24 message_size_(message_size), 25 message_size_(message_size),
25 test_data_size_(message_size * message_count), 26 test_data_size_(message_size * message_count),
26 done_(false), 27 done_(false),
27 write_errors_(0), 28 write_errors_(0),
28 read_errors_(0) { 29 read_errors_(0) {
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 read_errors_++; 121 read_errors_++;
121 Done(); 122 Done();
122 } else if (result > 0) { 123 } else if (result > 0) {
123 // Allocate memory for the next read. 124 // Allocate memory for the next read.
124 input_buffer_->set_offset(input_buffer_->offset() + result); 125 input_buffer_->set_offset(input_buffer_->offset() + result);
125 if (input_buffer_->offset() == test_data_size_) 126 if (input_buffer_->offset() == test_data_size_)
126 Done(); 127 Done();
127 } 128 }
128 } 129 }
129 130
130 DatagramConnectionTester::DatagramConnectionTester(net::Socket* client_socket, 131 DatagramConnectionTester::DatagramConnectionTester(
131 net::Socket* host_socket, 132 P2PDatagramSocket* client_socket,
132 int message_size, 133 P2PDatagramSocket* host_socket,
133 int message_count, 134 int message_size,
134 int delay_ms) 135 int message_count,
136 int delay_ms)
135 : message_loop_(base::MessageLoop::current()), 137 : message_loop_(base::MessageLoop::current()),
136 host_socket_(host_socket), 138 host_socket_(host_socket),
137 client_socket_(client_socket), 139 client_socket_(client_socket),
138 message_size_(message_size), 140 message_size_(message_size),
139 message_count_(message_count), 141 message_count_(message_count),
140 delay_ms_(delay_ms), 142 delay_ms_(delay_ms),
141 done_(false), 143 done_(false),
142 write_errors_(0), 144 write_errors_(0),
143 read_errors_(0), 145 read_errors_(0),
144 packets_sent_(0), 146 packets_sent_(0),
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 } 181 }
180 182
181 scoped_refptr<net::IOBuffer> packet(new net::IOBuffer(message_size_)); 183 scoped_refptr<net::IOBuffer> packet(new net::IOBuffer(message_size_));
182 for (int i = 0; i < message_size_; ++i) { 184 for (int i = 0; i < message_size_; ++i) {
183 packet->data()[i] = static_cast<char>(i); 185 packet->data()[i] = static_cast<char>(i);
184 } 186 }
185 sent_packets_[packets_sent_] = packet; 187 sent_packets_[packets_sent_] = packet;
186 // Put index of this packet in the beginning of the packet body. 188 // Put index of this packet in the beginning of the packet body.
187 memcpy(packet->data(), &packets_sent_, sizeof(packets_sent_)); 189 memcpy(packet->data(), &packets_sent_, sizeof(packets_sent_));
188 190
189 int result = client_socket_->Write( 191 int result = client_socket_->Send(
190 packet.get(), 192 packet.get(), message_size_,
191 message_size_,
192 base::Bind(&DatagramConnectionTester::OnWritten, base::Unretained(this))); 193 base::Bind(&DatagramConnectionTester::OnWritten, base::Unretained(this)));
193 HandleWriteResult(result); 194 HandleWriteResult(result);
194 } 195 }
195 196
196 void DatagramConnectionTester::OnWritten(int result) { 197 void DatagramConnectionTester::OnWritten(int result) {
197 HandleWriteResult(result); 198 HandleWriteResult(result);
198 } 199 }
199 200
200 void DatagramConnectionTester::HandleWriteResult(int result) { 201 void DatagramConnectionTester::HandleWriteResult(int result) {
201 if (result <= 0 && result != net::ERR_IO_PENDING) { 202 if (result <= 0 && result != net::ERR_IO_PENDING) {
202 LOG(ERROR) << "Received error " << result << " when trying to write"; 203 LOG(ERROR) << "Received error " << result << " when trying to write";
203 write_errors_++; 204 write_errors_++;
204 Done(); 205 Done();
205 } else if (result > 0) { 206 } else if (result > 0) {
206 EXPECT_EQ(message_size_, result); 207 EXPECT_EQ(message_size_, result);
207 packets_sent_++; 208 packets_sent_++;
208 message_loop_->PostDelayedTask( 209 message_loop_->PostDelayedTask(
209 FROM_HERE, 210 FROM_HERE,
210 base::Bind(&DatagramConnectionTester::DoWrite, base::Unretained(this)), 211 base::Bind(&DatagramConnectionTester::DoWrite, base::Unretained(this)),
211 base::TimeDelta::FromMilliseconds(delay_ms_)); 212 base::TimeDelta::FromMilliseconds(delay_ms_));
212 } 213 }
213 } 214 }
214 215
215 void DatagramConnectionTester::DoRead() { 216 void DatagramConnectionTester::DoRead() {
216 int result = 1; 217 int result = 1;
217 while (result > 0) { 218 while (result > 0) {
218 int kReadSize = message_size_ * 2; 219 int kReadSize = message_size_ * 2;
219 read_buffer_ = new net::IOBuffer(kReadSize); 220 read_buffer_ = new net::IOBuffer(kReadSize);
220 221
221 result = host_socket_->Read( 222 result = host_socket_->Recv(
222 read_buffer_.get(), 223 read_buffer_.get(), kReadSize,
223 kReadSize,
224 base::Bind(&DatagramConnectionTester::OnRead, base::Unretained(this))); 224 base::Bind(&DatagramConnectionTester::OnRead, base::Unretained(this)));
225 HandleReadResult(result); 225 HandleReadResult(result);
226 }; 226 };
227 } 227 }
228 228
229 void DatagramConnectionTester::OnRead(int result) { 229 void DatagramConnectionTester::OnRead(int result) {
230 HandleReadResult(result); 230 HandleReadResult(result);
231 DoRead(); 231 DoRead();
232 } 232 }
233 233
(...skipping 18 matching lines...) Expand all
252 if (memcmp(read_buffer_->data(), sent_packets_[packet_id]->data(), 252 if (memcmp(read_buffer_->data(), sent_packets_[packet_id]->data(),
253 message_size_) != 0) 253 message_size_) != 0)
254 bad_packets_received_++; 254 bad_packets_received_++;
255 } 255 }
256 } 256 }
257 } 257 }
258 } 258 }
259 259
260 } // namespace protocol 260 } // namespace protocol
261 } // namespace remoting 261 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/protocol/connection_tester.h ('k') | remoting/protocol/datagram_channel_factory.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698