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_addr(false), | |
|
Sergey Ulanov
2012/09/12 21:52:35
typo: reuse_addr_
And rename it to reuse_address_.
| |
| 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) { | |
| 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 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 173 int result = AcceptInternal(accept_socket_); | 179 int result = AcceptInternal(accept_socket_); |
| 174 if (result != ERR_IO_PENDING) { | 180 if (result != ERR_IO_PENDING) { |
| 175 accept_socket_ = NULL; | 181 accept_socket_ = NULL; |
| 176 CompletionCallback callback = accept_callback_; | 182 CompletionCallback callback = accept_callback_; |
| 177 accept_callback_.Reset(); | 183 accept_callback_.Reset(); |
| 178 callback.Run(result); | 184 callback.Run(result); |
| 179 } | 185 } |
| 180 } | 186 } |
| 181 } | 187 } |
| 182 | 188 |
| 189 void TCPServerSocketWin::AllowAddressReuse() { | |
| 190 DCHECK(CalledOnValidThread()); | |
| 191 DCHECK_EQ(socket_, INVALID_SOCKET); | |
| 192 DCHECK_EQ(socket_event_, WSA_INVALID_EVENT); | |
| 193 | |
| 194 reuse_addr_ = true; | |
| 195 } | |
| 196 | |
| 197 int TCPServerSocketWin::SetSocketOptions() { | |
| 198 int true_value = 1; | |
| 199 if (reuse_addr_) { | |
| 200 int rv = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &true_value, | |
| 201 sizeof(true_value)); | |
| 202 if (rv < 0) | |
| 203 return MapSystemError(errno); | |
| 204 } | |
| 205 return OK; | |
| 206 } | |
| 207 | |
| 183 } // namespace net | 208 } // namespace net |
| OLD | NEW |