| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_server_socket_win.h" | 5 #include "net/socket/tcp_server_socket_win.h" |
| 6 | 6 |
| 7 #include <mstcpip.h> | 7 #include <mstcpip.h> |
| 8 | 8 |
| 9 #include "net/base/ip_endpoint.h" | 9 #include "net/base/ip_endpoint.h" |
| 10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| 11 #include "net/base/net_util.h" | 11 #include "net/base/net_util.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 PLOG(ERROR) << "socket() returned an error"; | 50 PLOG(ERROR) << "socket() returned an error"; |
| 51 return MapSystemError(WSAGetLastError()); | 51 return MapSystemError(WSAGetLastError()); |
| 52 } | 52 } |
| 53 | 53 |
| 54 if (SetNonBlocking(socket_)) { | 54 if (SetNonBlocking(socket_)) { |
| 55 int result = MapSystemError(WSAGetLastError()); | 55 int result = MapSystemError(WSAGetLastError()); |
| 56 Close(); | 56 Close(); |
| 57 return result; | 57 return result; |
| 58 } | 58 } |
| 59 | 59 |
| 60 struct sockaddr_storage addr_storage; | 60 SockaddrStorage storage; |
| 61 size_t addr_len = sizeof(addr_storage); | 61 if (!address.ToSockAddr(storage.addr, &storage.addr_len)) |
| 62 struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage); | |
| 63 if (!address.ToSockAddr(addr, &addr_len)) | |
| 64 return ERR_INVALID_ARGUMENT; | 62 return ERR_INVALID_ARGUMENT; |
| 65 | 63 |
| 66 int result = bind(socket_, addr, addr_len); | 64 int result = bind(socket_, storage.addr, storage.addr_len); |
| 67 if (result < 0) { | 65 if (result < 0) { |
| 68 PLOG(ERROR) << "bind() returned an error"; | 66 PLOG(ERROR) << "bind() returned an error"; |
| 69 result = MapSystemError(WSAGetLastError()); | 67 result = MapSystemError(WSAGetLastError()); |
| 70 Close(); | 68 Close(); |
| 71 return result; | 69 return result; |
| 72 } | 70 } |
| 73 | 71 |
| 74 result = listen(socket_, backlog); | 72 result = listen(socket_, backlog); |
| 75 if (result < 0) { | 73 if (result < 0) { |
| 76 PLOG(ERROR) << "listen() returned an error"; | 74 PLOG(ERROR) << "listen() returned an error"; |
| 77 result = MapSystemError(WSAGetLastError()); | 75 result = MapSystemError(WSAGetLastError()); |
| 78 Close(); | 76 Close(); |
| 79 return result; | 77 return result; |
| 80 } | 78 } |
| 81 | 79 |
| 82 return OK; | 80 return OK; |
| 83 } | 81 } |
| 84 | 82 |
| 85 int TCPServerSocketWin::GetLocalAddress(IPEndPoint* address) const { | 83 int TCPServerSocketWin::GetLocalAddress(IPEndPoint* address) const { |
| 86 DCHECK(CalledOnValidThread()); | 84 DCHECK(CalledOnValidThread()); |
| 87 DCHECK(address); | 85 DCHECK(address); |
| 88 | 86 |
| 89 struct sockaddr_storage addr_storage; | 87 SockaddrStorage storage; |
| 90 socklen_t addr_len = sizeof(addr_storage); | 88 if (getsockname(socket_, storage.addr, &storage.addr_len)) |
| 91 struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage); | |
| 92 if (getsockname(socket_, addr, &addr_len)) | |
| 93 return MapSystemError(WSAGetLastError()); | 89 return MapSystemError(WSAGetLastError()); |
| 94 if (!address->FromSockAddr(addr, addr_len)) | 90 if (!address->FromSockAddr(storage.addr, storage.addr_len)) |
| 95 return ERR_FAILED; | 91 return ERR_FAILED; |
| 96 | 92 |
| 97 return OK; | 93 return OK; |
| 98 } | 94 } |
| 99 | 95 |
| 100 int TCPServerSocketWin::Accept( | 96 int TCPServerSocketWin::Accept( |
| 101 scoped_ptr<StreamSocket>* socket, const CompletionCallback& callback) { | 97 scoped_ptr<StreamSocket>* socket, const CompletionCallback& callback) { |
| 102 DCHECK(CalledOnValidThread()); | 98 DCHECK(CalledOnValidThread()); |
| 103 DCHECK(socket); | 99 DCHECK(socket); |
| 104 DCHECK(!callback.is_null()); | 100 DCHECK(!callback.is_null()); |
| 105 DCHECK(accept_callback_.is_null()); | 101 DCHECK(accept_callback_.is_null()); |
| 106 | 102 |
| 107 net_log_.BeginEvent(NetLog::TYPE_TCP_ACCEPT, NULL); | 103 net_log_.BeginEvent(NetLog::TYPE_TCP_ACCEPT, NULL); |
| 108 | 104 |
| 109 int result = AcceptInternal(socket); | 105 int result = AcceptInternal(socket); |
| 110 | 106 |
| 111 if (result == ERR_IO_PENDING) { | 107 if (result == ERR_IO_PENDING) { |
| 112 // Start watching | 108 // Start watching |
| 113 WSAEventSelect(socket_, socket_event_, FD_ACCEPT); | 109 WSAEventSelect(socket_, socket_event_, FD_ACCEPT); |
| 114 accept_watcher_.StartWatching(socket_event_, this); | 110 accept_watcher_.StartWatching(socket_event_, this); |
| 115 | 111 |
| 116 accept_socket_ = socket; | 112 accept_socket_ = socket; |
| 117 accept_callback_ = callback; | 113 accept_callback_ = callback; |
| 118 } | 114 } |
| 119 | 115 |
| 120 return result; | 116 return result; |
| 121 } | 117 } |
| 122 | 118 |
| 123 int TCPServerSocketWin::AcceptInternal(scoped_ptr<StreamSocket>* socket) { | 119 int TCPServerSocketWin::AcceptInternal(scoped_ptr<StreamSocket>* socket) { |
| 124 struct sockaddr_storage addr_storage; | 120 SockaddrStorage storage; |
| 125 socklen_t addr_len = sizeof(addr_storage); | 121 int new_socket = accept(socket_, storage.addr, &storage.addr_len); |
| 126 struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage); | |
| 127 | |
| 128 int new_socket = accept(socket_, addr, &addr_len); | |
| 129 if (new_socket < 0) { | 122 if (new_socket < 0) { |
| 130 int net_error = MapSystemError(WSAGetLastError()); | 123 int net_error = MapSystemError(WSAGetLastError()); |
| 131 if (net_error != ERR_IO_PENDING) | 124 if (net_error != ERR_IO_PENDING) |
| 132 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, net_error); | 125 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, net_error); |
| 133 return net_error; | 126 return net_error; |
| 134 } | 127 } |
| 135 | 128 |
| 136 IPEndPoint address; | 129 IPEndPoint address; |
| 137 if (!address.FromSockAddr(addr, addr_len)) { | 130 if (!address.FromSockAddr(storage.addr, storage.addr_len)) { |
| 138 NOTREACHED(); | 131 NOTREACHED(); |
| 139 if (closesocket(new_socket) < 0) | 132 if (closesocket(new_socket) < 0) |
| 140 PLOG(ERROR) << "closesocket"; | 133 PLOG(ERROR) << "closesocket"; |
| 141 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, ERR_FAILED); | 134 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, ERR_FAILED); |
| 142 return ERR_FAILED; | 135 return ERR_FAILED; |
| 143 } | 136 } |
| 144 scoped_ptr<TCPClientSocket> tcp_socket(new TCPClientSocket( | 137 scoped_ptr<TCPClientSocket> tcp_socket(new TCPClientSocket( |
| 145 AddressList::CreateFromIPAddress(address.address(), address.port()), | 138 AddressList(address), |
| 146 net_log_.net_log(), net_log_.source())); | 139 net_log_.net_log(), net_log_.source())); |
| 147 int adopt_result = tcp_socket->AdoptSocket(new_socket); | 140 int adopt_result = tcp_socket->AdoptSocket(new_socket); |
| 148 if (adopt_result != OK) { | 141 if (adopt_result != OK) { |
| 149 if (closesocket(new_socket) < 0) | 142 if (closesocket(new_socket) < 0) |
| 150 PLOG(ERROR) << "closesocket"; | 143 PLOG(ERROR) << "closesocket"; |
| 151 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, adopt_result); | 144 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, adopt_result); |
| 152 return adopt_result; | 145 return adopt_result; |
| 153 } | 146 } |
| 154 socket->reset(tcp_socket.release()); | 147 socket->reset(tcp_socket.release()); |
| 155 net_log_.EndEvent(NetLog::TYPE_TCP_ACCEPT, | 148 net_log_.EndEvent(NetLog::TYPE_TCP_ACCEPT, |
| (...skipping 27 matching lines...) Expand all Loading... |
| 183 if (result != ERR_IO_PENDING) { | 176 if (result != ERR_IO_PENDING) { |
| 184 accept_socket_ = NULL; | 177 accept_socket_ = NULL; |
| 185 CompletionCallback callback = accept_callback_; | 178 CompletionCallback callback = accept_callback_; |
| 186 accept_callback_.Reset(); | 179 accept_callback_.Reset(); |
| 187 callback.Run(result); | 180 callback.Run(result); |
| 188 } | 181 } |
| 189 } | 182 } |
| 190 } | 183 } |
| 191 | 184 |
| 192 } // namespace net | 185 } // namespace net |
| OLD | NEW |