Index: net/socket/tcp_server_socket_win.cc |
diff --git a/net/socket/tcp_server_socket_win.cc b/net/socket/tcp_server_socket_win.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d43128aaf8d18252920fccef0e8f989e57f88c1a |
--- /dev/null |
+++ b/net/socket/tcp_server_socket_win.cc |
@@ -0,0 +1,175 @@ |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "net/socket/tcp_server_socket_win.h" |
+ |
+#include <mstcpip.h> |
+ |
+#include "base/eintr_wrapper.h" |
willchan no longer on Chromium
2011/04/13 16:08:51
This should not be needed.
Sergey Ulanov
2011/04/13 21:20:19
Done.
|
+#include "net/base/ip_endpoint.h" |
+#include "net/base/net_errors.h" |
+#include "net/base/net_util.h" |
+#include "net/base/winsock_init.h" |
+#include "net/base/winsock_util.h" |
+ |
+namespace net { |
+ |
+TCPServerSocketWin::TCPServerSocketWin(net::NetLog* net_log, |
+ const net::NetLog::Source& source) |
+ : socket_(INVALID_SOCKET), |
+ socket_event_(WSA_INVALID_EVENT), |
+ accept_socket_(NULL), |
+ accept_address_(NULL), |
+ accept_callback_(NULL), |
+ net_log_(net_log), |
+ net_log_source_(source) { |
+ EnsureWinsockInit(); |
+} |
+ |
+TCPServerSocketWin::~TCPServerSocketWin() { |
+ Close(); |
+} |
+ |
+int TCPServerSocketWin::Listen(const IPEndPoint& address, int backlog) { |
+ DCHECK(CalledOnValidThread()); |
+ DCHECK_GT(backlog, 0); |
+ DCHECK_EQ(socket_, INVALID_SOCKET); |
+ DCHECK_EQ(socket_event_, WSA_INVALID_EVENT); |
+ |
+ socket_event_ = WSACreateEvent(); |
+ if (socket_event_ == WSA_INVALID_EVENT) { |
+ PLOG(ERROR) << "WSACreateEvent()"; |
+ return ERR_FAILED; |
+ } |
+ |
+ socket_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
+ if (socket_ < 0) { |
+ PLOG(ERROR) << "socket() returned an error"; |
+ return MapSystemError(WSAGetLastError()); |
+ } |
+ |
+ if (SetNonBlocking(socket_)) { |
+ int result = MapSystemError(WSAGetLastError()); |
+ Close(); |
+ return result; |
+ } |
+ |
+ struct sockaddr_storage addr_storage; |
+ size_t addr_len = sizeof(addr_storage); |
+ struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage); |
+ if (!address.ToSockAddr(addr, &addr_len)) |
+ return ERR_INVALID_ARGUMENT; |
+ |
+ int result = bind(socket_, addr, addr_len); |
+ if (result < 0) { |
+ PLOG(ERROR) << "bind() returned an error"; |
+ result = MapSystemError(WSAGetLastError()); |
+ Close(); |
+ return result; |
+ } |
+ |
+ result = listen(socket_, backlog); |
+ if (result < 0) { |
+ PLOG(ERROR) << "listen() returned an error"; |
+ result = MapSystemError(WSAGetLastError()); |
+ Close(); |
+ return result; |
+ } |
+ |
+ return OK; |
+} |
+ |
+int TCPServerSocketWin::GetLocalAddress(IPEndPoint* address) const { |
+ DCHECK(CalledOnValidThread()); |
+ DCHECK(address); |
+ |
+ struct sockaddr_storage addr_storage; |
+ socklen_t addr_len = sizeof(addr_storage); |
+ struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage); |
+ if (getsockname(socket_, addr, &addr_len)) |
+ return MapSystemError(WSAGetLastError()); |
+ if (!address->FromSockAddr(addr, addr_len)) |
+ return ERR_FAILED; |
+ |
+ return OK; |
+} |
+ |
+int TCPServerSocketWin::Accept( |
+ TCPClientSocket** socket, net::IPEndPoint* address, |
+ CompletionCallback* callback) { |
+ DCHECK(CalledOnValidThread()); |
+ DCHECK(socket); |
+ DCHECK(address); |
+ DCHECK(callback); |
+ DCHECK(!accept_callback_); |
+ |
+ int result = AcceptInternal(socket, address); |
+ |
+ if (result == ERR_IO_PENDING) { |
+ // Start watching |
+ WSAEventSelect(socket_, socket_event_, FD_ACCEPT); |
+ accept_watcher_.StartWatching(socket_event_, this); |
+ |
+ accept_socket_ = socket; |
+ accept_address_ = address; |
+ accept_callback_ = callback; |
+ } |
+ |
+ return result; |
+} |
+ |
+int TCPServerSocketWin::AcceptInternal( |
+ TCPClientSocket** socket, net::IPEndPoint* address) { |
+ struct sockaddr_storage addr_storage; |
+ socklen_t addr_len = sizeof(addr_storage); |
+ struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage); |
+ |
+ int result = accept(socket_, addr, &addr_len); |
+ if (result < 0) |
+ return MapSystemError(WSAGetLastError()); |
+ |
+ if (!address->FromSockAddr(addr, addr_len)) { |
+ NOTREACHED(); |
+ HANDLE_EINTR(closesocket(result)); |
+ return ERR_FAILED; |
+ } |
+ *socket = new TCPClientSocket(AddressList(), net_log_, net_log_source_); |
willchan no longer on Chromium
2011/04/13 16:08:51
You should fix AddressList() here too.
Sergey Ulanov
2011/04/13 21:20:19
Done.
|
+ (*socket)->AdoptSocket(result); |
+ return OK; |
+} |
+ |
+void TCPServerSocketWin::Close() { |
+ if (socket_ != INVALID_SOCKET) { |
+ HANDLE_EINTR(closesocket(socket_)); |
willchan no longer on Chromium
2011/04/13 16:08:51
HANDLE_EINTR isn't necessary, it's just for POSIX.
Sergey Ulanov
2011/04/13 21:20:19
Done.
|
+ socket_ = INVALID_SOCKET; |
+ } |
+ |
+ if (socket_event_) { |
+ WSACloseEvent(socket_event_); |
+ socket_event_ = WSA_INVALID_EVENT; |
+ } |
+} |
+ |
+void TCPServerSocketWin::OnObjectSignaled(HANDLE object) { |
+ WSANETWORKEVENTS ev; |
+ if (WSAEnumNetworkEvents(socket_, socket_event_, &ev) == SOCKET_ERROR) { |
+ PLOG(ERROR) << "WSAEnumNetworkEvents()"; |
+ return; |
+ } |
+ |
+ if (ev.lNetworkEvents & FD_ACCEPT) { |
+ int result = AcceptInternal(accept_socket_, accept_address_); |
+ if (result == 0) { |
+ CompletionCallback* c = accept_callback_; |
+ accept_callback_ = NULL; |
+ accept_socket_ = NULL; |
+ accept_address_ = NULL; |
+ c->Run(0); |
+ } else { |
+ PLOG(ERROR) << "accept() returned error after accept event was signalled"; |
+ } |
+ } |
+} |
+ |
+} // namespace net |