| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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_client_socket_libevent.h" | 5 #include "net/socket/tcp_client_socket_libevent.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <netdb.h> | 9 #include <netdb.h> |
| 10 #include <sys/socket.h> | 10 #include <sys/socket.h> |
| 11 #include <netinet/tcp.h> | 11 #include <netinet/tcp.h> |
| 12 | 12 |
| 13 #include "base/eintr_wrapper.h" | 13 #include "base/eintr_wrapper.h" |
| 14 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
| 15 #include "base/string_util.h" | 15 #include "base/string_util.h" |
| 16 #include "base/trace_event.h" | 16 #include "base/trace_event.h" |
| 17 #include "net/base/io_buffer.h" | 17 #include "net/base/io_buffer.h" |
| 18 #include "net/base/load_log.h" | |
| 19 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
| 19 #include "net/base/net_log.h" |
| 20 #if defined(USE_SYSTEM_LIBEVENT) | 20 #if defined(USE_SYSTEM_LIBEVENT) |
| 21 #include <event.h> | 21 #include <event.h> |
| 22 #else | 22 #else |
| 23 #include "third_party/libevent/event.h" | 23 #include "third_party/libevent/event.h" |
| 24 #endif | 24 #endif |
| 25 | 25 |
| 26 namespace net { | 26 namespace net { |
| 27 | 27 |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 write_watcher_(this), | 131 write_watcher_(this), |
| 132 read_callback_(NULL), | 132 read_callback_(NULL), |
| 133 write_callback_(NULL) { | 133 write_callback_(NULL) { |
| 134 } | 134 } |
| 135 | 135 |
| 136 TCPClientSocketLibevent::~TCPClientSocketLibevent() { | 136 TCPClientSocketLibevent::~TCPClientSocketLibevent() { |
| 137 Disconnect(); | 137 Disconnect(); |
| 138 } | 138 } |
| 139 | 139 |
| 140 int TCPClientSocketLibevent::Connect(CompletionCallback* callback, | 140 int TCPClientSocketLibevent::Connect(CompletionCallback* callback, |
| 141 LoadLog* load_log) { | 141 const BoundNetLog& net_log) { |
| 142 // If already connected, then just return OK. | 142 // If already connected, then just return OK. |
| 143 if (socket_ != kInvalidSocket) | 143 if (socket_ != kInvalidSocket) |
| 144 return OK; | 144 return OK; |
| 145 | 145 |
| 146 DCHECK(!waiting_connect_); | 146 DCHECK(!waiting_connect_); |
| 147 DCHECK(!load_log_); | 147 DCHECK(!net_log_.net_log()); |
| 148 | 148 |
| 149 TRACE_EVENT_BEGIN("socket.connect", this, ""); | 149 TRACE_EVENT_BEGIN("socket.connect", this, ""); |
| 150 | 150 |
| 151 LoadLog::BeginEvent(load_log, LoadLog::TYPE_TCP_CONNECT); | 151 net_log.BeginEvent(NetLog::TYPE_TCP_CONNECT); |
| 152 | 152 |
| 153 int rv = DoConnect(); | 153 int rv = DoConnect(); |
| 154 | 154 |
| 155 if (rv == ERR_IO_PENDING) { | 155 if (rv == ERR_IO_PENDING) { |
| 156 // Synchronous operation not supported. | 156 // Synchronous operation not supported. |
| 157 DCHECK(callback); | 157 DCHECK(callback); |
| 158 | 158 |
| 159 load_log_ = load_log; | 159 net_log_ = net_log; |
| 160 waiting_connect_ = true; | 160 waiting_connect_ = true; |
| 161 write_callback_ = callback; | 161 write_callback_ = callback; |
| 162 } else { | 162 } else { |
| 163 TRACE_EVENT_END("socket.connect", this, ""); | 163 TRACE_EVENT_END("socket.connect", this, ""); |
| 164 LoadLog::EndEvent(load_log, LoadLog::TYPE_TCP_CONNECT); | 164 net_log.EndEvent(NetLog::TYPE_TCP_CONNECT); |
| 165 } | 165 } |
| 166 | 166 |
| 167 return rv; | 167 return rv; |
| 168 } | 168 } |
| 169 | 169 |
| 170 int TCPClientSocketLibevent::DoConnect() { | 170 int TCPClientSocketLibevent::DoConnect() { |
| 171 while (true) { | 171 while (true) { |
| 172 DCHECK(current_ai_); | 172 DCHECK(current_ai_); |
| 173 | 173 |
| 174 int rv = CreateSocket(current_ai_); | 174 int rv = CreateSocket(current_ai_); |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 396 os_error = errno; | 396 os_error = errno; |
| 397 | 397 |
| 398 if (os_error == EINPROGRESS || os_error == EALREADY) { | 398 if (os_error == EINPROGRESS || os_error == EALREADY) { |
| 399 NOTREACHED(); // This indicates a bug in libevent or our code. | 399 NOTREACHED(); // This indicates a bug in libevent or our code. |
| 400 result = ERR_IO_PENDING; | 400 result = ERR_IO_PENDING; |
| 401 } else if (current_ai_->ai_next && ShouldTryNextAddress(os_error)) { | 401 } else if (current_ai_->ai_next && ShouldTryNextAddress(os_error)) { |
| 402 // This address failed, try next one in list. | 402 // This address failed, try next one in list. |
| 403 const addrinfo* next = current_ai_->ai_next; | 403 const addrinfo* next = current_ai_->ai_next; |
| 404 Disconnect(); | 404 Disconnect(); |
| 405 current_ai_ = next; | 405 current_ai_ = next; |
| 406 scoped_refptr<LoadLog> load_log; | 406 BoundNetLog net_log = net_log_; |
| 407 load_log.swap(load_log_); | 407 net_log_ = BoundNetLog(); |
| 408 TRACE_EVENT_END("socket.connect", this, ""); | 408 TRACE_EVENT_END("socket.connect", this, ""); |
| 409 LoadLog::EndEvent(load_log, LoadLog::TYPE_TCP_CONNECT); | 409 net_log.EndEvent(NetLog::TYPE_TCP_CONNECT); |
| 410 result = Connect(write_callback_, load_log); | 410 result = Connect(write_callback_, net_log); |
| 411 } else { | 411 } else { |
| 412 result = MapConnectError(os_error); | 412 result = MapConnectError(os_error); |
| 413 bool ok = write_socket_watcher_.StopWatchingFileDescriptor(); | 413 bool ok = write_socket_watcher_.StopWatchingFileDescriptor(); |
| 414 DCHECK(ok); | 414 DCHECK(ok); |
| 415 waiting_connect_ = false; | 415 waiting_connect_ = false; |
| 416 TRACE_EVENT_END("socket.connect", this, ""); | 416 TRACE_EVENT_END("socket.connect", this, ""); |
| 417 LoadLog::EndEvent(load_log_, LoadLog::TYPE_TCP_CONNECT); | 417 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT); |
| 418 load_log_ = NULL; | 418 net_log_ = BoundNetLog(); |
| 419 } | 419 } |
| 420 | 420 |
| 421 if (result != ERR_IO_PENDING) { | 421 if (result != ERR_IO_PENDING) { |
| 422 DoWriteCallback(result); | 422 DoWriteCallback(result); |
| 423 } | 423 } |
| 424 } | 424 } |
| 425 | 425 |
| 426 void TCPClientSocketLibevent::DidCompleteRead() { | 426 void TCPClientSocketLibevent::DidCompleteRead() { |
| 427 int bytes_transferred; | 427 int bytes_transferred; |
| 428 bytes_transferred = HANDLE_EINTR(read(socket_, read_buf_->data(), | 428 bytes_transferred = HANDLE_EINTR(read(socket_, read_buf_->data(), |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 470 | 470 |
| 471 int TCPClientSocketLibevent::GetPeerAddress(AddressList* address) const { | 471 int TCPClientSocketLibevent::GetPeerAddress(AddressList* address) const { |
| 472 DCHECK(address); | 472 DCHECK(address); |
| 473 if (!current_ai_) | 473 if (!current_ai_) |
| 474 return ERR_UNEXPECTED; | 474 return ERR_UNEXPECTED; |
| 475 address->Copy(current_ai_, false); | 475 address->Copy(current_ai_, false); |
| 476 return OK; | 476 return OK; |
| 477 } | 477 } |
| 478 | 478 |
| 479 } // namespace net | 479 } // namespace net |
| OLD | NEW |