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 "chrome/browser/net/network_stats.h" | 5 #include "chrome/browser/net/network_stats.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback_old.h" | 8 #include "base/callback_old.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 // This specifies the starting position of the <encoded_payload> and length of | 75 // This specifies the starting position of the <encoded_payload> and length of |
76 // the <encoded_payload> in "echo response". | 76 // the <encoded_payload> in "echo response". |
77 static const uint32 kEncodedPayloadStart = kKeyEnd; | 77 static const uint32 kEncodedPayloadStart = kKeyEnd; |
78 | 78 |
79 // NetworkStats methods and members. | 79 // NetworkStats methods and members. |
80 NetworkStats::NetworkStats() | 80 NetworkStats::NetworkStats() |
81 : load_size_(0), | 81 : load_size_(0), |
82 bytes_to_read_(0), | 82 bytes_to_read_(0), |
83 bytes_to_send_(0), | 83 bytes_to_send_(0), |
84 encoded_message_(""), | 84 encoded_message_(""), |
85 ALLOW_THIS_IN_INITIALIZER_LIST( | |
86 read_callback_(this, &NetworkStats::OnReadComplete)), | |
87 ALLOW_THIS_IN_INITIALIZER_LIST( | |
88 write_callback_(this, &NetworkStats::OnWriteComplete)), | |
89 finished_callback_(NULL), | 85 finished_callback_(NULL), |
90 start_time_(base::TimeTicks::Now()), | 86 start_time_(base::TimeTicks::Now()), |
91 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 87 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
92 } | 88 } |
93 | 89 |
94 NetworkStats::~NetworkStats() { | 90 NetworkStats::~NetworkStats() { |
95 socket_.reset(); | 91 socket_.reset(); |
96 } | 92 } |
97 | 93 |
98 bool NetworkStats::Start(net::HostResolver* host_resolver, | 94 bool NetworkStats::Start(net::HostResolver* host_resolver, |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 int rv; | 235 int rv; |
240 do { | 236 do { |
241 if (!socket_.get()) | 237 if (!socket_.get()) |
242 return; | 238 return; |
243 | 239 |
244 DCHECK(!read_buffer_.get()); | 240 DCHECK(!read_buffer_.get()); |
245 | 241 |
246 // We release the read_buffer_ in the destructor if there is an error. | 242 // We release the read_buffer_ in the destructor if there is an error. |
247 read_buffer_ = new net::IOBuffer(kMaxMessage); | 243 read_buffer_ = new net::IOBuffer(kMaxMessage); |
248 | 244 |
249 rv = socket_->Read(read_buffer_, kMaxMessage, &read_callback_); | 245 rv = socket_->Read(read_buffer_, kMaxMessage, |
| 246 base::Bind(&NetworkStats::OnReadComplete, |
| 247 base::Unretained(this))); |
250 if (rv == net::ERR_IO_PENDING) | 248 if (rv == net::ERR_IO_PENDING) |
251 return; | 249 return; |
252 // If we have read all the data then return. | 250 // If we have read all the data then return. |
253 if (ReadComplete(rv)) | 251 if (ReadComplete(rv)) |
254 return; | 252 return; |
255 } while (rv > 0); | 253 } while (rv > 0); |
256 } | 254 } |
257 | 255 |
258 int NetworkStats::SendData() { | 256 int NetworkStats::SendData() { |
259 DCHECK(bytes_to_send_); // We should have data to send. | 257 DCHECK(bytes_to_send_); // We should have data to send. |
260 do { | 258 do { |
261 if (!write_buffer_.get()) { | 259 if (!write_buffer_.get()) { |
262 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(bytes_to_send_)); | 260 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(bytes_to_send_)); |
263 GetEchoRequest(buffer); | 261 GetEchoRequest(buffer); |
264 write_buffer_ = new net::DrainableIOBuffer(buffer, bytes_to_send_); | 262 write_buffer_ = new net::DrainableIOBuffer(buffer, bytes_to_send_); |
265 } | 263 } |
266 | 264 |
267 if (!socket_.get()) | 265 if (!socket_.get()) |
268 return net::ERR_UNEXPECTED; | 266 return net::ERR_UNEXPECTED; |
269 int rv = socket_->Write(write_buffer_, | 267 int rv = socket_->Write(write_buffer_, |
270 write_buffer_->BytesRemaining(), | 268 write_buffer_->BytesRemaining(), |
271 &write_callback_); | 269 base::Bind(&NetworkStats::OnWriteComplete, |
| 270 base::Unretained(this))); |
272 if (rv < 0) | 271 if (rv < 0) |
273 return rv; | 272 return rv; |
274 write_buffer_->DidConsume(rv); | 273 write_buffer_->DidConsume(rv); |
275 bytes_to_send_ -= rv; | 274 bytes_to_send_ -= rv; |
276 if (!write_buffer_->BytesRemaining()) | 275 if (!write_buffer_->BytesRemaining()) |
277 write_buffer_ = NULL; | 276 write_buffer_ = NULL; |
278 } while (bytes_to_send_); | 277 } while (bytes_to_send_); |
279 return net::OK; | 278 return net::OK; |
280 } | 279 } |
281 | 280 |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 // Close the socket so that there are no more IO operations. | 435 // Close the socket so that there are no more IO operations. |
437 net::UDPClientSocket* udp_socket = | 436 net::UDPClientSocket* udp_socket = |
438 static_cast<net::UDPClientSocket*>(socket()); | 437 static_cast<net::UDPClientSocket*>(socket()); |
439 if (udp_socket) | 438 if (udp_socket) |
440 udp_socket->Close(); | 439 udp_socket->Close(); |
441 | 440 |
442 delete this; | 441 delete this; |
443 } | 442 } |
444 | 443 |
445 // TCPStatsClient methods and members. | 444 // TCPStatsClient methods and members. |
446 TCPStatsClient::TCPStatsClient() | 445 TCPStatsClient::TCPStatsClient() { |
447 : NetworkStats(), | |
448 ALLOW_THIS_IN_INITIALIZER_LIST( | |
449 connect_callback_(this, &TCPStatsClient::OnConnectComplete)) { | |
450 } | 446 } |
451 | 447 |
452 TCPStatsClient::~TCPStatsClient() { | 448 TCPStatsClient::~TCPStatsClient() { |
453 } | 449 } |
454 | 450 |
455 bool TCPStatsClient::DoConnect(int result) { | 451 bool TCPStatsClient::DoConnect(int result) { |
456 if (result != net::OK) { | 452 if (result != net::OK) { |
457 Finish(RESOLVE_FAILED, result); | 453 Finish(RESOLVE_FAILED, result); |
458 return false; | 454 return false; |
459 } | 455 } |
460 | 456 |
461 net::TCPClientSocket* tcp_socket = | 457 net::TCPClientSocket* tcp_socket = |
462 new net::TCPClientSocket(GetAddressList(), NULL, net::NetLog::Source()); | 458 new net::TCPClientSocket(GetAddressList(), NULL, net::NetLog::Source()); |
463 if (!tcp_socket) { | 459 if (!tcp_socket) { |
464 Finish(SOCKET_CREATE_FAILED, net::ERR_INVALID_ARGUMENT); | 460 Finish(SOCKET_CREATE_FAILED, net::ERR_INVALID_ARGUMENT); |
465 return false; | 461 return false; |
466 } | 462 } |
467 set_socket(tcp_socket); | 463 set_socket(tcp_socket); |
468 | 464 |
469 int rv = tcp_socket->Connect(&connect_callback_); | 465 int rv = tcp_socket->Connect(base::Bind(&TCPStatsClient::OnConnectComplete, |
| 466 base::Unretained(this))); |
470 if (rv == net::ERR_IO_PENDING) | 467 if (rv == net::ERR_IO_PENDING) |
471 return true; | 468 return true; |
472 | 469 |
473 return NetworkStats::ConnectComplete(rv); | 470 return NetworkStats::ConnectComplete(rv); |
474 } | 471 } |
475 | 472 |
476 void TCPStatsClient::OnConnectComplete(int result) { | 473 void TCPStatsClient::OnConnectComplete(int result) { |
477 NetworkStats::ConnectComplete(result); | 474 NetworkStats::ConnectComplete(result); |
478 } | 475 } |
479 | 476 |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
597 TCPStatsClient* small_tcp_client = new TCPStatsClient(); | 594 TCPStatsClient* small_tcp_client = new TCPStatsClient(); |
598 small_tcp_client->Start( | 595 small_tcp_client->Start( |
599 host_resolver, tcp_server_address, kSmallTestBytesToSend, NULL); | 596 host_resolver, tcp_server_address, kSmallTestBytesToSend, NULL); |
600 | 597 |
601 TCPStatsClient* large_tcp_client = new TCPStatsClient(); | 598 TCPStatsClient* large_tcp_client = new TCPStatsClient(); |
602 large_tcp_client->Start( | 599 large_tcp_client->Start( |
603 host_resolver, tcp_server_address, kLargeTestBytesToSend, NULL); | 600 host_resolver, tcp_server_address, kLargeTestBytesToSend, NULL); |
604 } | 601 } |
605 | 602 |
606 } // namespace chrome_browser_net | 603 } // namespace chrome_browser_net |
OLD | NEW |