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 // Grab a reference to |buf| so that ReadCompleted() can still |
| 215 // use it when Read() completes, as otherwise, this transfers |
| 216 // ownership of buf to socket. |
| 217 base::Unretained(this), make_scoped_refptr(buf), callback)); |
215 if (rv >= 0) | 218 if (rv >= 0) |
216 RecordFastOpenStatus(); | 219 RecordFastOpenStatus(); |
217 if (rv != ERR_IO_PENDING) | 220 if (rv != ERR_IO_PENDING) |
218 rv = HandleReadCompleted(buf, rv); | 221 rv = HandleReadCompleted(buf, rv); |
219 return rv; | 222 return rv; |
220 } | 223 } |
221 | 224 |
222 int TCPSocketLibevent::Write(IOBuffer* buf, | 225 int TCPSocketLibevent::Write(IOBuffer* buf, |
223 int buf_len, | 226 int buf_len, |
224 const CompletionCallback& callback) { | 227 const CompletionCallback& callback) { |
225 DCHECK(socket_); | 228 DCHECK(socket_); |
226 DCHECK(!callback.is_null()); | 229 DCHECK(!callback.is_null()); |
227 | 230 |
228 CompletionCallback write_callback = | 231 CompletionCallback write_callback = |
229 base::Bind(&TCPSocketLibevent::WriteCompleted, | 232 base::Bind(&TCPSocketLibevent::WriteCompleted, |
230 base::Unretained(this), base::Unretained(buf), callback); | 233 // Grab a reference to |buf| so that WriteCompleted() can still |
| 234 // use it when Write() completes, as otherwise, this transfers |
| 235 // ownership of buf to socket. |
| 236 base::Unretained(this), make_scoped_refptr(buf), callback); |
231 int rv; | 237 int rv; |
232 if (use_tcp_fastopen_ && !tcp_fastopen_connected_) { | 238 if (use_tcp_fastopen_ && !tcp_fastopen_connected_) { |
233 rv = TcpFastOpenWrite(buf, buf_len, write_callback); | 239 rv = TcpFastOpenWrite(buf, buf_len, write_callback); |
234 } else { | 240 } else { |
235 rv = socket_->Write(buf, buf_len, write_callback); | 241 rv = socket_->Write(buf, buf_len, write_callback); |
236 } | 242 } |
237 | 243 |
238 if (rv != ERR_IO_PENDING) | 244 if (rv != ERR_IO_PENDING) |
239 rv = HandleWriteCompleted(buf, rv); | 245 rv = HandleWriteCompleted(buf, rv); |
240 return rv; | 246 return rv; |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
478 NOTREACHED(); | 484 NOTREACHED(); |
479 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_CONNECT, rv); | 485 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_CONNECT, rv); |
480 return; | 486 return; |
481 } | 487 } |
482 | 488 |
483 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT, | 489 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT, |
484 CreateNetLogSourceAddressCallback(storage.addr, | 490 CreateNetLogSourceAddressCallback(storage.addr, |
485 storage.addr_len)); | 491 storage.addr_len)); |
486 } | 492 } |
487 | 493 |
488 void TCPSocketLibevent::ReadCompleted(IOBuffer* buf, | 494 void TCPSocketLibevent::ReadCompleted(const scoped_refptr<IOBuffer>& buf, |
489 const CompletionCallback& callback, | 495 const CompletionCallback& callback, |
490 int rv) { | 496 int rv) { |
491 DCHECK_NE(ERR_IO_PENDING, rv); | 497 DCHECK_NE(ERR_IO_PENDING, rv); |
492 // Records fast open status regardless of error in asynchronous case. | 498 // Records fast open status regardless of error in asynchronous case. |
493 // TODO(rdsmith,jri): Change histogram name to indicate it could be called on | 499 // TODO(rdsmith,jri): Change histogram name to indicate it could be called on |
494 // error. | 500 // error. |
495 RecordFastOpenStatus(); | 501 RecordFastOpenStatus(); |
496 callback.Run(HandleReadCompleted(buf, rv)); | 502 callback.Run(HandleReadCompleted(buf, rv)); |
497 } | 503 } |
498 | 504 |
499 int TCPSocketLibevent::HandleReadCompleted(IOBuffer* buf, int rv) { | 505 int TCPSocketLibevent::HandleReadCompleted(IOBuffer* buf, int rv) { |
500 if (rv < 0) { | 506 if (rv < 0) { |
501 net_log_.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR, | 507 net_log_.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR, |
502 CreateNetLogSocketErrorCallback(rv, errno)); | 508 CreateNetLogSocketErrorCallback(rv, errno)); |
503 return rv; | 509 return rv; |
504 } | 510 } |
505 | 511 |
506 base::StatsCounter read_bytes("tcp.read_bytes"); | 512 base::StatsCounter read_bytes("tcp.read_bytes"); |
507 read_bytes.Add(rv); | 513 read_bytes.Add(rv); |
508 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, rv, | 514 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, rv, |
509 buf->data()); | 515 buf->data()); |
510 return rv; | 516 return rv; |
511 } | 517 } |
512 | 518 |
513 void TCPSocketLibevent::WriteCompleted(IOBuffer* buf, | 519 void TCPSocketLibevent::WriteCompleted(const scoped_refptr<IOBuffer>& buf, |
514 const CompletionCallback& callback, | 520 const CompletionCallback& callback, |
515 int rv) const { | 521 int rv) const { |
516 DCHECK_NE(ERR_IO_PENDING, rv); | 522 DCHECK_NE(ERR_IO_PENDING, rv); |
517 callback.Run(HandleWriteCompleted(buf, rv)); | 523 callback.Run(HandleWriteCompleted(buf, rv)); |
518 } | 524 } |
519 | 525 |
520 int TCPSocketLibevent::HandleWriteCompleted(IOBuffer* buf, int rv) const { | 526 int TCPSocketLibevent::HandleWriteCompleted(IOBuffer* buf, int rv) const { |
521 if (rv < 0) { | 527 if (rv < 0) { |
522 net_log_.AddEvent(NetLog::TYPE_SOCKET_WRITE_ERROR, | 528 net_log_.AddEvent(NetLog::TYPE_SOCKET_WRITE_ERROR, |
523 CreateNetLogSocketErrorCallback(rv, errno)); | 529 CreateNetLogSocketErrorCallback(rv, errno)); |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
611 } | 617 } |
612 } else { | 618 } else { |
613 fast_open_status_ = (fast_open_status_ == FAST_OPEN_FAST_CONNECT_RETURN ? | 619 fast_open_status_ = (fast_open_status_ == FAST_OPEN_FAST_CONNECT_RETURN ? |
614 FAST_OPEN_SYN_DATA_FAILED : | 620 FAST_OPEN_SYN_DATA_FAILED : |
615 FAST_OPEN_NO_SYN_DATA_FAILED); | 621 FAST_OPEN_NO_SYN_DATA_FAILED); |
616 } | 622 } |
617 } | 623 } |
618 } | 624 } |
619 | 625 |
620 } // namespace net | 626 } // namespace net |
OLD | NEW |