Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "net/socket/tcp_socket.h" | 5 #include "net/socket/tcp_socket.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <netinet/tcp.h> | 8 #include <netinet/tcp.h> |
| 9 #include <sys/socket.h> | 9 #include <sys/socket.h> |
| 10 | 10 |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 204 | 204 |
| 205 int TCPSocketLibevent::Read(IOBuffer* buf, | 205 int TCPSocketLibevent::Read(IOBuffer* buf, |
| 206 int buf_len, | 206 int buf_len, |
| 207 const CompletionCallback& callback) { | 207 const CompletionCallback& callback) { |
| 208 DCHECK(socket_); | 208 DCHECK(socket_); |
| 209 DCHECK(!callback.is_null()); | 209 DCHECK(!callback.is_null()); |
| 210 | 210 |
| 211 int rv = socket_->Read( | 211 int rv = socket_->Read( |
| 212 buf, buf_len, | 212 buf, buf_len, |
| 213 base::Bind(&TCPSocketLibevent::ReadCompleted, | 213 base::Bind(&TCPSocketLibevent::ReadCompleted, |
| 214 base::Unretained(this), base::Unretained(buf), callback)); | 214 base::Unretained(this), make_scoped_refptr(buf), callback)); |
| 215 if (rv >= 0) | 215 if (rv >= 0) |
| 216 RecordFastOpenStatus(); | 216 RecordFastOpenStatus(); |
| 217 if (rv != ERR_IO_PENDING) | 217 if (rv != ERR_IO_PENDING) |
| 218 rv = HandleReadCompleted(buf, rv); | 218 rv = HandleReadCompleted(buf, rv); |
| 219 return rv; | 219 return rv; |
| 220 } | 220 } |
| 221 | 221 |
| 222 int TCPSocketLibevent::Write(IOBuffer* buf, | 222 int TCPSocketLibevent::Write(IOBuffer* buf, |
| 223 int buf_len, | 223 int buf_len, |
| 224 const CompletionCallback& callback) { | 224 const CompletionCallback& callback) { |
| 225 DCHECK(socket_); | 225 DCHECK(socket_); |
| 226 DCHECK(!callback.is_null()); | 226 DCHECK(!callback.is_null()); |
| 227 | 227 |
| 228 CompletionCallback write_callback = | 228 CompletionCallback write_callback = |
| 229 base::Bind(&TCPSocketLibevent::WriteCompleted, | 229 base::Bind(&TCPSocketLibevent::WriteCompleted, |
| 230 base::Unretained(this), base::Unretained(buf), callback); | 230 base::Unretained(this), make_scoped_refptr(buf), callback); |
| 231 int rv; | 231 int rv; |
| 232 if (use_tcp_fastopen_ && !tcp_fastopen_connected_) { | 232 if (use_tcp_fastopen_ && !tcp_fastopen_connected_) { |
| 233 rv = TcpFastOpenWrite(buf, buf_len, write_callback); | 233 rv = TcpFastOpenWrite(buf, buf_len, write_callback); |
| 234 } else { | 234 } else { |
| 235 rv = socket_->Write(buf, buf_len, write_callback); | 235 rv = socket_->Write(buf, buf_len, write_callback); |
| 236 } | 236 } |
| 237 | 237 |
| 238 if (rv != ERR_IO_PENDING) | 238 if (rv != ERR_IO_PENDING) |
| 239 rv = HandleWriteCompleted(buf, rv); | 239 rv = HandleWriteCompleted(buf, rv); |
| 240 return rv; | 240 return rv; |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 478 NOTREACHED(); | 478 NOTREACHED(); |
| 479 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_CONNECT, rv); | 479 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_CONNECT, rv); |
| 480 return; | 480 return; |
| 481 } | 481 } |
| 482 | 482 |
| 483 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT, | 483 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT, |
| 484 CreateNetLogSourceAddressCallback(storage.addr, | 484 CreateNetLogSourceAddressCallback(storage.addr, |
| 485 storage.addr_len)); | 485 storage.addr_len)); |
| 486 } | 486 } |
| 487 | 487 |
| 488 void TCPSocketLibevent::ReadCompleted(IOBuffer* buf, | 488 void TCPSocketLibevent::ReadCompleted(scoped_refptr<IOBuffer> buf, |
| 489 const CompletionCallback& callback, | 489 const CompletionCallback& callback, |
| 490 int rv) { | 490 int rv) { |
| 491 DCHECK_NE(ERR_IO_PENDING, rv); | 491 DCHECK_NE(ERR_IO_PENDING, rv); |
| 492 // Records fast open status regardless of error in asynchronous case. | 492 // Records fast open status regardless of error in asynchronous case. |
| 493 // TODO(rdsmith,jri): Change histogram name to indicate it could be called on | 493 // TODO(rdsmith,jri): Change histogram name to indicate it could be called on |
| 494 // error. | 494 // error. |
| 495 RecordFastOpenStatus(); | 495 RecordFastOpenStatus(); |
| 496 callback.Run(HandleReadCompleted(buf, rv)); | 496 callback.Run(HandleReadCompleted(buf, rv)); |
| 497 } | 497 } |
| 498 | 498 |
| 499 int TCPSocketLibevent::HandleReadCompleted(IOBuffer* buf, int rv) { | 499 int TCPSocketLibevent::HandleReadCompleted(IOBuffer* buf, int rv) { |
| 500 if (rv < 0) { | 500 if (rv < 0) { |
| 501 net_log_.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR, | 501 net_log_.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR, |
| 502 CreateNetLogSocketErrorCallback(rv, errno)); | 502 CreateNetLogSocketErrorCallback(rv, errno)); |
| 503 return rv; | 503 return rv; |
| 504 } | 504 } |
| 505 | 505 |
| 506 base::StatsCounter read_bytes("tcp.read_bytes"); | 506 base::StatsCounter read_bytes("tcp.read_bytes"); |
| 507 read_bytes.Add(rv); | 507 read_bytes.Add(rv); |
| 508 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, rv, | 508 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, rv, |
| 509 buf->data()); | 509 buf->data()); |
| 510 return rv; | 510 return rv; |
| 511 } | 511 } |
| 512 | 512 |
| 513 void TCPSocketLibevent::WriteCompleted(IOBuffer* buf, | 513 void TCPSocketLibevent::WriteCompleted(scoped_refptr<IOBuffer> buf, |
| 514 const CompletionCallback& callback, | 514 const CompletionCallback& callback, |
| 515 int rv) const { | 515 int rv) const { |
| 516 DCHECK_NE(ERR_IO_PENDING, rv); | 516 DCHECK_NE(ERR_IO_PENDING, rv); |
| 517 callback.Run(HandleWriteCompleted(buf, rv)); | 517 callback.Run(HandleWriteCompleted(buf, rv)); |
| 518 } | 518 } |
| 519 | 519 |
| 520 int TCPSocketLibevent::HandleWriteCompleted(IOBuffer* buf, int rv) const { | 520 int TCPSocketLibevent::HandleWriteCompleted(IOBuffer* buf, int rv) const { |
| 521 if (rv < 0) { | 521 if (rv < 0) { |
| 522 net_log_.AddEvent(NetLog::TYPE_SOCKET_WRITE_ERROR, | 522 net_log_.AddEvent(NetLog::TYPE_SOCKET_WRITE_ERROR, |
| 523 CreateNetLogSocketErrorCallback(rv, errno)); | 523 CreateNetLogSocketErrorCallback(rv, errno)); |
| 524 return rv; | 524 return rv; |
| 525 } | 525 } |
| 526 | 526 |
| 527 base::StatsCounter write_bytes("tcp.write_bytes"); | 527 base::StatsCounter write_bytes("tcp.write_bytes"); |
| 528 write_bytes.Add(rv); | 528 write_bytes.Add(rv); |
| 529 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT, rv, | 529 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT, rv, |
| 530 buf->data()); | 530 buf->data()); |
|
Ryan Sleevi
2014/07/11 18:13:35
Right, this is the unsafe access point.
byungchul
2014/07/11 18:58:18
Yes.
| |
| 531 return rv; | 531 return rv; |
| 532 } | 532 } |
| 533 | 533 |
| 534 int TCPSocketLibevent::TcpFastOpenWrite( | 534 int TCPSocketLibevent::TcpFastOpenWrite( |
| 535 IOBuffer* buf, | 535 IOBuffer* buf, |
| 536 int buf_len, | 536 int buf_len, |
| 537 const CompletionCallback& callback) { | 537 const CompletionCallback& callback) { |
| 538 SockaddrStorage storage; | 538 SockaddrStorage storage; |
| 539 int rv = socket_->GetPeerAddress(&storage); | 539 int rv = socket_->GetPeerAddress(&storage); |
| 540 if (rv != OK) | 540 if (rv != OK) |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 611 } | 611 } |
| 612 } else { | 612 } else { |
| 613 fast_open_status_ = (fast_open_status_ == FAST_OPEN_FAST_CONNECT_RETURN ? | 613 fast_open_status_ = (fast_open_status_ == FAST_OPEN_FAST_CONNECT_RETURN ? |
| 614 FAST_OPEN_SYN_DATA_FAILED : | 614 FAST_OPEN_SYN_DATA_FAILED : |
| 615 FAST_OPEN_NO_SYN_DATA_FAILED); | 615 FAST_OPEN_NO_SYN_DATA_FAILED); |
| 616 } | 616 } |
| 617 } | 617 } |
| 618 } | 618 } |
| 619 | 619 |
| 620 } // namespace net | 620 } // namespace net |
| OLD | NEW |