Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 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" |
| 12 #include "net/base/winsock_init.h" | 12 #include "net/base/winsock_init.h" |
| 13 #include "net/base/winsock_util.h" | 13 #include "net/base/winsock_util.h" |
| 14 #include "net/socket/socket_net_log_params.h" | 14 #include "net/socket/socket_net_log_params.h" |
| 15 #include "net/socket/tcp_client_socket.h" | 15 #include "net/socket/tcp_client_socket.h" |
| 16 | 16 |
| 17 namespace net { | 17 namespace net { |
| 18 | 18 |
| 19 TCPServerSocketWin::TCPServerSocketWin(net::NetLog* net_log, | 19 TCPServerSocketWin::TCPServerSocketWin(net::NetLog* net_log, |
| 20 const net::NetLog::Source& source) | 20 const net::NetLog::Source& source) |
| 21 : socket_(INVALID_SOCKET), | 21 : socket_(INVALID_SOCKET), |
| 22 socket_event_(WSA_INVALID_EVENT), | 22 socket_event_(WSA_INVALID_EVENT), |
| 23 accept_socket_(NULL), | 23 accept_socket_(NULL), |
| 24 reuse_address_(false), | |
| 24 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) { | 25 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) { |
| 25 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, | 26 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, |
| 26 source.ToEventParametersCallback()); | 27 source.ToEventParametersCallback()); |
| 27 EnsureWinsockInit(); | 28 EnsureWinsockInit(); |
| 28 } | 29 } |
| 29 | 30 |
| 30 TCPServerSocketWin::~TCPServerSocketWin() { | 31 TCPServerSocketWin::~TCPServerSocketWin() { |
| 31 Close(); | 32 Close(); |
| 32 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE); | 33 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE); |
| 33 } | 34 } |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 49 PLOG(ERROR) << "socket() returned an error"; | 50 PLOG(ERROR) << "socket() returned an error"; |
| 50 return MapSystemError(WSAGetLastError()); | 51 return MapSystemError(WSAGetLastError()); |
| 51 } | 52 } |
| 52 | 53 |
| 53 if (SetNonBlocking(socket_)) { | 54 if (SetNonBlocking(socket_)) { |
| 54 int result = MapSystemError(WSAGetLastError()); | 55 int result = MapSystemError(WSAGetLastError()); |
| 55 Close(); | 56 Close(); |
| 56 return result; | 57 return result; |
| 57 } | 58 } |
| 58 | 59 |
| 60 int result = SetSocketOptions(); | |
| 61 if (result < 0) { | |
|
wtc
2012/09/13 18:32:56
Nit: this should also be changed to result != OK.
justinlin
2012/09/14 03:54:40
Done.
| |
| 62 return result; | |
| 63 } | |
| 64 | |
| 59 SockaddrStorage storage; | 65 SockaddrStorage storage; |
| 60 if (!address.ToSockAddr(storage.addr, &storage.addr_len)) | 66 if (!address.ToSockAddr(storage.addr, &storage.addr_len)) |
| 61 return ERR_INVALID_ARGUMENT; | 67 return ERR_INVALID_ARGUMENT; |
| 62 | 68 |
| 63 int result = bind(socket_, storage.addr, storage.addr_len); | 69 result = bind(socket_, storage.addr, storage.addr_len); |
| 64 if (result < 0) { | 70 if (result < 0) { |
| 65 PLOG(ERROR) << "bind() returned an error"; | 71 PLOG(ERROR) << "bind() returned an error"; |
| 66 result = MapSystemError(WSAGetLastError()); | 72 result = MapSystemError(WSAGetLastError()); |
| 67 Close(); | 73 Close(); |
| 68 return result; | 74 return result; |
| 69 } | 75 } |
| 70 | 76 |
| 71 result = listen(socket_, backlog); | 77 result = listen(socket_, backlog); |
| 72 if (result < 0) { | 78 if (result < 0) { |
| 73 PLOG(ERROR) << "listen() returned an error"; | 79 PLOG(ERROR) << "listen() returned an error"; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 108 WSAEventSelect(socket_, socket_event_, FD_ACCEPT); | 114 WSAEventSelect(socket_, socket_event_, FD_ACCEPT); |
| 109 accept_watcher_.StartWatching(socket_event_, this); | 115 accept_watcher_.StartWatching(socket_event_, this); |
| 110 | 116 |
| 111 accept_socket_ = socket; | 117 accept_socket_ = socket; |
| 112 accept_callback_ = callback; | 118 accept_callback_ = callback; |
| 113 } | 119 } |
| 114 | 120 |
| 115 return result; | 121 return result; |
| 116 } | 122 } |
| 117 | 123 |
| 124 void TCPServerSocketWin::AllowAddressReuse() { | |
| 125 DCHECK(CalledOnValidThread()); | |
| 126 DCHECK_EQ(socket_, INVALID_SOCKET); | |
| 127 DCHECK_EQ(socket_event_, WSA_INVALID_EVENT); | |
| 128 | |
| 129 reuse_address_ = true; | |
| 130 } | |
| 131 | |
| 132 int TCPServerSocketWin::SetSocketOptions() { | |
| 133 BOOL true_value = 1; | |
| 134 if (reuse_address_) { | |
| 135 int rv = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, | |
| 136 reinterpret_cast<const char*>(&true_value), | |
| 137 sizeof(true_value)); | |
| 138 if (rv < 0) | |
| 139 return MapSystemError(errno); | |
| 140 } | |
| 141 return OK; | |
| 142 } | |
| 143 | |
| 118 int TCPServerSocketWin::AcceptInternal(scoped_ptr<StreamSocket>* socket) { | 144 int TCPServerSocketWin::AcceptInternal(scoped_ptr<StreamSocket>* socket) { |
| 119 SockaddrStorage storage; | 145 SockaddrStorage storage; |
| 120 int new_socket = accept(socket_, storage.addr, &storage.addr_len); | 146 int new_socket = accept(socket_, storage.addr, &storage.addr_len); |
| 121 if (new_socket < 0) { | 147 if (new_socket < 0) { |
| 122 int net_error = MapSystemError(WSAGetLastError()); | 148 int net_error = MapSystemError(WSAGetLastError()); |
| 123 if (net_error != ERR_IO_PENDING) | 149 if (net_error != ERR_IO_PENDING) |
| 124 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, net_error); | 150 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, net_error); |
| 125 return net_error; | 151 return net_error; |
| 126 } | 152 } |
| 127 | 153 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 174 if (result != ERR_IO_PENDING) { | 200 if (result != ERR_IO_PENDING) { |
| 175 accept_socket_ = NULL; | 201 accept_socket_ = NULL; |
| 176 CompletionCallback callback = accept_callback_; | 202 CompletionCallback callback = accept_callback_; |
| 177 accept_callback_.Reset(); | 203 accept_callback_.Reset(); |
| 178 callback.Run(result); | 204 callback.Run(result); |
| 179 } | 205 } |
| 180 } | 206 } |
| 181 } | 207 } |
| 182 | 208 |
| 183 } // namespace net | 209 } // namespace net |
| OLD | NEW |