| 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 "build/build_config.h" | 5 #include "build/build_config.h" |
| 6 | 6 |
| 7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
| 8 // winsock2.h must be included first in order to ensure it is included before | 8 // winsock2.h must be included first in order to ensure it is included before |
| 9 // windows.h. | 9 // windows.h. |
| 10 #include <winsock2.h> | 10 #include <winsock2.h> |
| 11 #elif defined(OS_POSIX) | 11 #elif defined(OS_POSIX) |
| 12 #include <errno.h> | 12 #include <errno.h> |
| 13 #include <sys/socket.h> | 13 #include <sys/socket.h> |
| 14 #include <arpa/inet.h> | 14 #include <arpa/inet.h> |
| 15 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
| 16 #include "third_party/libevent/event.h" | 16 #include "third_party/libevent/event.h" |
| 17 #endif | 17 #endif |
| 18 | 18 |
| 19 #include "base/eintr_wrappers.h" |
| 19 #include "net/base/net_util.h" | 20 #include "net/base/net_util.h" |
| 20 #include "net/base/listen_socket.h" | 21 #include "net/base/listen_socket.h" |
| 21 | 22 |
| 22 #if defined(OS_WIN) | 23 #if defined(OS_WIN) |
| 23 #define socklen_t int | 24 #define socklen_t int |
| 24 #elif defined(OS_POSIX) | 25 #elif defined(OS_POSIX) |
| 25 const int INVALID_SOCKET = -1; // Used same name as in Windows to avoid #ifdef | 26 const int INVALID_SOCKET = -1; // Used same name as in Windows to avoid #ifdef |
| 26 const int SOCKET_ERROR = -1; | 27 const int SOCKET_ERROR = -1; |
| 27 #endif | 28 #endif |
| 28 | 29 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 listen(socket_, backlog); | 87 listen(socket_, backlog); |
| 87 // TODO(erikkay): error handling | 88 // TODO(erikkay): error handling |
| 88 #if defined(OS_POSIX) | 89 #if defined(OS_POSIX) |
| 89 WatchSocket(WAITING_ACCEPT); | 90 WatchSocket(WAITING_ACCEPT); |
| 90 #endif | 91 #endif |
| 91 } | 92 } |
| 92 | 93 |
| 93 SOCKET ListenSocket::Accept(SOCKET s) { | 94 SOCKET ListenSocket::Accept(SOCKET s) { |
| 94 sockaddr_in from; | 95 sockaddr_in from; |
| 95 socklen_t from_len = sizeof(from); | 96 socklen_t from_len = sizeof(from); |
| 96 SOCKET conn = accept(s, reinterpret_cast<sockaddr*>(&from), &from_len); | 97 SOCKET conn = |
| 98 HANDLE_EINTR(accept(s, reinterpret_cast<sockaddr*>(&from), &from_len)); |
| 97 if (conn != INVALID_SOCKET) { | 99 if (conn != INVALID_SOCKET) { |
| 98 net::SetNonBlocking(conn); | 100 net::SetNonBlocking(conn); |
| 99 } | 101 } |
| 100 return conn; | 102 return conn; |
| 101 } | 103 } |
| 102 | 104 |
| 103 void ListenSocket::Accept() { | 105 void ListenSocket::Accept() { |
| 104 SOCKET conn = Accept(socket_); | 106 SOCKET conn = Accept(socket_); |
| 105 if (conn != INVALID_SOCKET) { | 107 if (conn != INVALID_SOCKET) { |
| 106 scoped_refptr<ListenSocket> sock = | 108 scoped_refptr<ListenSocket> sock = |
| 107 new ListenSocket(conn, socket_delegate_); | 109 new ListenSocket(conn, socket_delegate_); |
| 108 // it's up to the delegate to AddRef if it wants to keep it around | 110 // it's up to the delegate to AddRef if it wants to keep it around |
| 109 #if defined(OS_POSIX) | 111 #if defined(OS_POSIX) |
| 110 sock->WatchSocket(WAITING_READ); | 112 sock->WatchSocket(WAITING_READ); |
| 111 #endif | 113 #endif |
| 112 socket_delegate_->DidAccept(this, sock); | 114 socket_delegate_->DidAccept(this, sock); |
| 113 } else { | 115 } else { |
| 114 // TODO(ibrar): some error handling required here | 116 // TODO(ibrar): some error handling required here |
| 115 } | 117 } |
| 116 } | 118 } |
| 117 | 119 |
| 118 void ListenSocket::Read() { | 120 void ListenSocket::Read() { |
| 119 char buf[kReadBufSize + 1]; // +1 for null termination | 121 char buf[kReadBufSize + 1]; // +1 for null termination |
| 120 int len; | 122 int len; |
| 121 do { | 123 do { |
| 122 len = recv(socket_, buf, kReadBufSize, 0); | 124 len = HANDLE_EINTR(recv(socket_, buf, kReadBufSize, 0)); |
| 123 if (len == SOCKET_ERROR) { | 125 if (len == SOCKET_ERROR) { |
| 124 #if defined(OS_WIN) | 126 #if defined(OS_WIN) |
| 125 int err = WSAGetLastError(); | 127 int err = WSAGetLastError(); |
| 126 if (err == WSAEWOULDBLOCK) { | 128 if (err == WSAEWOULDBLOCK) { |
| 127 #elif defined(OS_POSIX) | 129 #elif defined(OS_POSIX) |
| 128 if (errno == EWOULDBLOCK || errno == EAGAIN) { | 130 if (errno == EWOULDBLOCK || errno == EAGAIN) { |
| 129 #endif | 131 #endif |
| 130 break; | 132 break; |
| 131 } else { | 133 } else { |
| 132 // TODO(ibrar): some error handling required here | 134 // TODO(ibrar): some error handling required here |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 watcher_.StartWatching(socket_event_, this); | 183 watcher_.StartWatching(socket_event_, this); |
| 182 #elif defined(OS_POSIX) | 184 #elif defined(OS_POSIX) |
| 183 // Implicitly calls StartWatchingFileDescriptor(). | 185 // Implicitly calls StartWatchingFileDescriptor(). |
| 184 MessageLoopForIO::current()->WatchFileDescriptor( | 186 MessageLoopForIO::current()->WatchFileDescriptor( |
| 185 socket_, true, MessageLoopForIO::WATCH_READ, &watcher_, this); | 187 socket_, true, MessageLoopForIO::WATCH_READ, &watcher_, this); |
| 186 wait_state_ = state; | 188 wait_state_ = state; |
| 187 #endif | 189 #endif |
| 188 } | 190 } |
| 189 | 191 |
| 190 void ListenSocket::SendInternal(const char* bytes, int len) { | 192 void ListenSocket::SendInternal(const char* bytes, int len) { |
| 191 int sent = send(socket_, bytes, len, 0); | 193 int sent = HANDLE_EINTR(send(socket_, bytes, len, 0)); |
| 192 if (sent == SOCKET_ERROR) { | 194 if (sent == SOCKET_ERROR) { |
| 193 #if defined(OS_WIN) | 195 #if defined(OS_WIN) |
| 194 int err = WSAGetLastError(); | 196 int err = WSAGetLastError(); |
| 195 if (err == WSAEWOULDBLOCK) { | 197 if (err == WSAEWOULDBLOCK) { |
| 196 #elif defined(OS_POSIX) | 198 #elif defined(OS_POSIX) |
| 197 if (errno == EWOULDBLOCK || errno == EAGAIN) { | 199 if (errno == EWOULDBLOCK || errno == EAGAIN) { |
| 198 #endif | 200 #endif |
| 199 // TODO (ibrar): there should be logic here to handle this because | 201 // TODO (ibrar): there should be logic here to handle this because |
| 200 // it is not an error | 202 // it is not an error |
| 201 } | 203 } |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 } | 259 } |
| 258 } | 260 } |
| 259 | 261 |
| 260 void ListenSocket::OnFileCanWriteWithoutBlocking(int fd) { | 262 void ListenSocket::OnFileCanWriteWithoutBlocking(int fd) { |
| 261 // MessagePumpLibevent callback, we don't listen for write events | 263 // MessagePumpLibevent callback, we don't listen for write events |
| 262 // so we shouldn't ever reach here. | 264 // so we shouldn't ever reach here. |
| 263 NOTREACHED(); | 265 NOTREACHED(); |
| 264 } | 266 } |
| 265 | 267 |
| 266 #endif | 268 #endif |
| OLD | NEW |