| 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> |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 case EADDRNOTAVAIL: | 60 case EADDRNOTAVAIL: |
| 61 return ERR_ADDRESS_INVALID; | 61 return ERR_ADDRESS_INVALID; |
| 62 case 0: | 62 case 0: |
| 63 return OK; | 63 return OK; |
| 64 default: | 64 default: |
| 65 LOG(WARNING) << "Unknown error " << err << " mapped to net::ERR_FAILED"; | 65 LOG(WARNING) << "Unknown error " << err << " mapped to net::ERR_FAILED"; |
| 66 return ERR_FAILED; | 66 return ERR_FAILED; |
| 67 } | 67 } |
| 68 } | 68 } |
| 69 | 69 |
| 70 int MapConnectError(int err) { |
| 71 switch (err) { |
| 72 case ETIMEDOUT: |
| 73 return ERR_CONNECTION_TIMED_OUT; |
| 74 default: |
| 75 return MapPosixError(err); |
| 76 } |
| 77 } |
| 78 |
| 70 } // namespace | 79 } // namespace |
| 71 | 80 |
| 72 //----------------------------------------------------------------------------- | 81 //----------------------------------------------------------------------------- |
| 73 | 82 |
| 74 TCPClientSocketLibevent::TCPClientSocketLibevent(const AddressList& addresses) | 83 TCPClientSocketLibevent::TCPClientSocketLibevent(const AddressList& addresses) |
| 75 : socket_(kInvalidSocket), | 84 : socket_(kInvalidSocket), |
| 76 addresses_(addresses), | 85 addresses_(addresses), |
| 77 current_ai_(addresses_.head()), | 86 current_ai_(addresses_.head()), |
| 78 waiting_connect_(false), | 87 waiting_connect_(false), |
| 79 read_watcher_(this), | 88 read_watcher_(this), |
| (...skipping 28 matching lines...) Expand all Loading... |
| 108 return OK; | 117 return OK; |
| 109 } | 118 } |
| 110 | 119 |
| 111 // Synchronous operation not supported | 120 // Synchronous operation not supported |
| 112 DCHECK(callback); | 121 DCHECK(callback); |
| 113 | 122 |
| 114 if (errno != EINPROGRESS) { | 123 if (errno != EINPROGRESS) { |
| 115 DLOG(INFO) << "connect failed: " << errno; | 124 DLOG(INFO) << "connect failed: " << errno; |
| 116 close(socket_); | 125 close(socket_); |
| 117 socket_ = kInvalidSocket; | 126 socket_ = kInvalidSocket; |
| 118 return MapPosixError(errno); | 127 return MapConnectError(errno); |
| 119 } | 128 } |
| 120 | 129 |
| 121 // Initialize write_socket_watcher_ and link it to our MessagePump. | 130 // Initialize write_socket_watcher_ and link it to our MessagePump. |
| 122 // POLLOUT is set if the connection is established. | 131 // POLLOUT is set if the connection is established. |
| 123 // POLLIN is set if the connection fails. | 132 // POLLIN is set if the connection fails. |
| 124 if (!MessageLoopForIO::current()->WatchFileDescriptor( | 133 if (!MessageLoopForIO::current()->WatchFileDescriptor( |
| 125 socket_, true, MessageLoopForIO::WATCH_WRITE, &write_socket_watcher_, | 134 socket_, true, MessageLoopForIO::WATCH_WRITE, &write_socket_watcher_, |
| 126 &write_watcher_)) { | 135 &write_watcher_)) { |
| 127 DLOG(INFO) << "WatchFileDescriptor failed: " << errno; | 136 DLOG(INFO) << "WatchFileDescriptor failed: " << errno; |
| 128 close(socket_); | 137 close(socket_); |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 error_code == ECONNREFUSED || | 311 error_code == ECONNREFUSED || |
| 303 error_code == ENETUNREACH || | 312 error_code == ENETUNREACH || |
| 304 error_code == EHOSTUNREACH || | 313 error_code == EHOSTUNREACH || |
| 305 error_code == ETIMEDOUT)) { | 314 error_code == ETIMEDOUT)) { |
| 306 // This address failed, try next one in list. | 315 // This address failed, try next one in list. |
| 307 const addrinfo* next = current_ai_->ai_next; | 316 const addrinfo* next = current_ai_->ai_next; |
| 308 Disconnect(); | 317 Disconnect(); |
| 309 current_ai_ = next; | 318 current_ai_ = next; |
| 310 result = Connect(write_callback_); | 319 result = Connect(write_callback_); |
| 311 } else { | 320 } else { |
| 312 result = MapPosixError(error_code); | 321 result = MapConnectError(error_code); |
| 313 bool ok = write_socket_watcher_.StopWatchingFileDescriptor(); | 322 bool ok = write_socket_watcher_.StopWatchingFileDescriptor(); |
| 314 DCHECK(ok); | 323 DCHECK(ok); |
| 315 waiting_connect_ = false; | 324 waiting_connect_ = false; |
| 316 } | 325 } |
| 317 | 326 |
| 318 if (result != ERR_IO_PENDING) { | 327 if (result != ERR_IO_PENDING) { |
| 319 DoWriteCallback(result); | 328 DoWriteCallback(result); |
| 320 } | 329 } |
| 321 } | 330 } |
| 322 | 331 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 DoWriteCallback(result); | 373 DoWriteCallback(result); |
| 365 } | 374 } |
| 366 } | 375 } |
| 367 | 376 |
| 368 int TCPClientSocketLibevent::GetPeerName(struct sockaddr *name, | 377 int TCPClientSocketLibevent::GetPeerName(struct sockaddr *name, |
| 369 socklen_t *namelen) { | 378 socklen_t *namelen) { |
| 370 return ::getpeername(socket_, name, namelen); | 379 return ::getpeername(socket_, name, namelen); |
| 371 } | 380 } |
| 372 | 381 |
| 373 } // namespace net | 382 } // namespace net |
| OLD | NEW |