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