| 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 "base/message_loop.h" | |
| 16 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
| 17 #include "third_party/libevent/event.h" | 16 #include "third_party/libevent/event.h" |
| 18 #endif | 17 #endif |
| 19 | 18 |
| 20 #include "net/base/net_util.h" | 19 #include "net/base/net_util.h" |
| 21 #include "net/base/listen_socket.h" | 20 #include "net/base/listen_socket.h" |
| 22 | 21 |
| 23 #if defined(OS_WIN) | 22 #if defined(OS_WIN) |
| 24 #define socklen_t int | 23 #define socklen_t int |
| 25 #elif defined(OS_POSIX) | 24 #elif defined(OS_POSIX) |
| 26 const int INVALID_SOCKET = -1; // Used same name as in Windows to avoid #ifdef | 25 const int INVALID_SOCKET = -1; // Used same name as in Windows to avoid #ifdef |
| 27 const int SOCKET_ERROR = -1; | 26 const int SOCKET_ERROR = -1; |
| 28 #endif | 27 #endif |
| 29 | 28 |
| 30 const int kReadBufSize = 200; | 29 const int kReadBufSize = 200; |
| 31 | 30 |
| 32 ListenSocket::ListenSocket(SOCKET s, ListenSocketDelegate *del) | 31 ListenSocket::ListenSocket(SOCKET s, ListenSocketDelegate *del) |
| 33 #if defined(OS_WIN) | |
| 34 : socket_(s), | 32 : socket_(s), |
| 35 #elif defined(OS_POSIX) | |
| 36 : event_(new event), | |
| 37 socket_(s), | |
| 38 #endif | |
| 39 socket_delegate_(del) { | 33 socket_delegate_(del) { |
| 40 #if defined(OS_WIN) | 34 #if defined(OS_WIN) |
| 41 socket_event_ = WSACreateEvent(); | 35 socket_event_ = WSACreateEvent(); |
| 42 // TODO(ibrar): error handling in case of socket_event_ == WSA_INVALID_EVENT | 36 // TODO(ibrar): error handling in case of socket_event_ == WSA_INVALID_EVENT |
| 43 WatchSocket(NOT_WAITING); | 37 WatchSocket(NOT_WAITING); |
| 44 #endif | 38 #endif |
| 45 } | 39 } |
| 46 | 40 |
| 47 ListenSocket::~ListenSocket() { | 41 ListenSocket::~ListenSocket() { |
| 48 #if defined(OS_WIN) | 42 #if defined(OS_WIN) |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 return; | 164 return; |
| 171 wait_state_ = WAITING_CLOSE; | 165 wait_state_ = WAITING_CLOSE; |
| 172 #endif | 166 #endif |
| 173 socket_delegate_->DidClose(this); | 167 socket_delegate_->DidClose(this); |
| 174 } | 168 } |
| 175 | 169 |
| 176 void ListenSocket::UnwatchSocket() { | 170 void ListenSocket::UnwatchSocket() { |
| 177 #if defined(OS_WIN) | 171 #if defined(OS_WIN) |
| 178 watcher_.StopWatching(); | 172 watcher_.StopWatching(); |
| 179 #elif defined(OS_POSIX) | 173 #elif defined(OS_POSIX) |
| 180 MessageLoopForIO::current()->UnwatchSocket(event_.get()); | 174 watcher_.StopWatchingFileDescriptor(); |
| 181 wait_state_ = NOT_WAITING; | |
| 182 #endif | 175 #endif |
| 183 } | 176 } |
| 184 | 177 |
| 185 void ListenSocket::WatchSocket(WaitState state) { | 178 void ListenSocket::WatchSocket(WaitState state) { |
| 186 #if defined(OS_WIN) | 179 #if defined(OS_WIN) |
| 187 WSAEventSelect(socket_, socket_event_, FD_ACCEPT | FD_CLOSE | FD_READ); | 180 WSAEventSelect(socket_, socket_event_, FD_ACCEPT | FD_CLOSE | FD_READ); |
| 188 watcher_.StartWatching(socket_event_, this); | 181 watcher_.StartWatching(socket_event_, this); |
| 189 #elif defined(OS_POSIX) | 182 #elif defined(OS_POSIX) |
| 190 MessageLoopForIO::current()->WatchSocket( | 183 // Implicitly calls StartWatchingFileDescriptor(). |
| 191 socket_, EV_READ|EV_PERSIST, event_.get(),this); | 184 MessageLoopForIO::current()->WatchFileDescriptor( |
| 185 socket_, true, MessageLoopForIO::WATCH_READ, &watcher_, this); |
| 192 wait_state_ = state; | 186 wait_state_ = state; |
| 193 #endif | 187 #endif |
| 194 } | 188 } |
| 195 | 189 |
| 196 void ListenSocket::SendInternal(const char* bytes, int len) { | 190 void ListenSocket::SendInternal(const char* bytes, int len) { |
| 197 int sent = send(socket_, bytes, len, 0); | 191 int sent = send(socket_, bytes, len, 0); |
| 198 if (sent == SOCKET_ERROR) { | 192 if (sent == SOCKET_ERROR) { |
| 199 #if defined(OS_WIN) | 193 #if defined(OS_WIN) |
| 200 int err = WSAGetLastError(); | 194 int err = WSAGetLastError(); |
| 201 if (err == WSAEWOULDBLOCK) { | 195 if (err == WSAEWOULDBLOCK) { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 Accept(); | 237 Accept(); |
| 244 } | 238 } |
| 245 if (ev.lNetworkEvents & FD_READ) { | 239 if (ev.lNetworkEvents & FD_READ) { |
| 246 Read(); | 240 Read(); |
| 247 } | 241 } |
| 248 if (ev.lNetworkEvents & FD_CLOSE) { | 242 if (ev.lNetworkEvents & FD_CLOSE) { |
| 249 Close(); | 243 Close(); |
| 250 } | 244 } |
| 251 } | 245 } |
| 252 #elif defined(OS_POSIX) | 246 #elif defined(OS_POSIX) |
| 253 void ListenSocket::OnSocketReady(short flags) { | 247 void ListenSocket::OnFileCanReadWithoutBlocking(int fd) { |
| 254 if (wait_state_ == WAITING_ACCEPT) { | 248 if (wait_state_ == WAITING_ACCEPT) { |
| 255 Accept(); | 249 Accept(); |
| 256 } | 250 } |
| 257 if (wait_state_ == WAITING_READ) { | 251 if (wait_state_ == WAITING_READ) { |
| 258 Read(); | 252 Read(); |
| 259 } | 253 } |
| 260 if (wait_state_ == WAITING_CLOSE) { | 254 if (wait_state_ == WAITING_CLOSE) { |
| 261 // Close() is called by Read() in the Linux case. | 255 // Close() is called by Read() in the Linux case. |
| 262 // TODO(erikkay): this seems to get hit multiple times after the close | 256 // TODO(erikkay): this seems to get hit multiple times after the close |
| 263 } | 257 } |
| 264 } | 258 } |
| 259 |
| 260 void ListenSocket::OnFileCanWriteWithoutBlocking(int fd) { |
| 261 // MessagePumpLibevent callback, we don't listen for write events |
| 262 // so we shouldn't ever reach here. |
| 263 NOTREACHED(); |
| 264 } |
| 265 |
| 265 #endif | 266 #endif |
| OLD | NEW |