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 } |
34 | 35 |
| 36 void TCPServerSocketWin::AllowAddressReuse() { |
| 37 DCHECK(CalledOnValidThread()); |
| 38 DCHECK_EQ(socket_, INVALID_SOCKET); |
| 39 DCHECK_EQ(socket_event_, WSA_INVALID_EVENT); |
| 40 |
| 41 reuse_address_ = true; |
| 42 } |
| 43 |
35 int TCPServerSocketWin::Listen(const IPEndPoint& address, int backlog) { | 44 int TCPServerSocketWin::Listen(const IPEndPoint& address, int backlog) { |
36 DCHECK(CalledOnValidThread()); | 45 DCHECK(CalledOnValidThread()); |
37 DCHECK_GT(backlog, 0); | 46 DCHECK_GT(backlog, 0); |
38 DCHECK_EQ(socket_, INVALID_SOCKET); | 47 DCHECK_EQ(socket_, INVALID_SOCKET); |
39 DCHECK_EQ(socket_event_, WSA_INVALID_EVENT); | 48 DCHECK_EQ(socket_event_, WSA_INVALID_EVENT); |
40 | 49 |
41 socket_event_ = WSACreateEvent(); | 50 socket_event_ = WSACreateEvent(); |
42 if (socket_event_ == WSA_INVALID_EVENT) { | 51 if (socket_event_ == WSA_INVALID_EVENT) { |
43 PLOG(ERROR) << "WSACreateEvent()"; | 52 PLOG(ERROR) << "WSACreateEvent()"; |
44 return ERR_FAILED; | 53 return ERR_FAILED; |
45 } | 54 } |
46 | 55 |
47 socket_ = socket(address.GetFamily(), SOCK_STREAM, IPPROTO_TCP); | 56 socket_ = socket(address.GetFamily(), SOCK_STREAM, IPPROTO_TCP); |
48 if (socket_ < 0) { | 57 if (socket_ < 0) { |
49 PLOG(ERROR) << "socket() returned an error"; | 58 PLOG(ERROR) << "socket() returned an error"; |
50 return MapSystemError(WSAGetLastError()); | 59 return MapSystemError(WSAGetLastError()); |
51 } | 60 } |
52 | 61 |
53 if (SetNonBlocking(socket_)) { | 62 if (SetNonBlocking(socket_)) { |
54 int result = MapSystemError(WSAGetLastError()); | 63 int result = MapSystemError(WSAGetLastError()); |
55 Close(); | 64 Close(); |
56 return result; | 65 return result; |
57 } | 66 } |
58 | 67 |
| 68 int result = SetSocketOptions(); |
| 69 if (result != OK) |
| 70 return result; |
| 71 |
59 SockaddrStorage storage; | 72 SockaddrStorage storage; |
60 if (!address.ToSockAddr(storage.addr, &storage.addr_len)) | 73 if (!address.ToSockAddr(storage.addr, &storage.addr_len)) |
61 return ERR_INVALID_ARGUMENT; | 74 return ERR_INVALID_ARGUMENT; |
62 | 75 |
63 int result = bind(socket_, storage.addr, storage.addr_len); | 76 result = bind(socket_, storage.addr, storage.addr_len); |
64 if (result < 0) { | 77 if (result < 0) { |
65 PLOG(ERROR) << "bind() returned an error"; | 78 PLOG(ERROR) << "bind() returned an error"; |
66 result = MapSystemError(WSAGetLastError()); | 79 result = MapSystemError(WSAGetLastError()); |
67 Close(); | 80 Close(); |
68 return result; | 81 return result; |
69 } | 82 } |
70 | 83 |
71 result = listen(socket_, backlog); | 84 result = listen(socket_, backlog); |
72 if (result < 0) { | 85 if (result < 0) { |
73 PLOG(ERROR) << "listen() returned an error"; | 86 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); | 121 WSAEventSelect(socket_, socket_event_, FD_ACCEPT); |
109 accept_watcher_.StartWatching(socket_event_, this); | 122 accept_watcher_.StartWatching(socket_event_, this); |
110 | 123 |
111 accept_socket_ = socket; | 124 accept_socket_ = socket; |
112 accept_callback_ = callback; | 125 accept_callback_ = callback; |
113 } | 126 } |
114 | 127 |
115 return result; | 128 return result; |
116 } | 129 } |
117 | 130 |
| 131 int TCPServerSocketWin::SetSocketOptions() { |
| 132 BOOL true_value = 1; |
| 133 if (reuse_address_) { |
| 134 int rv = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, |
| 135 reinterpret_cast<const char*>(&true_value), |
| 136 sizeof(true_value)); |
| 137 if (rv < 0) |
| 138 return MapSystemError(errno); |
| 139 } |
| 140 return OK; |
| 141 } |
| 142 |
118 int TCPServerSocketWin::AcceptInternal(scoped_ptr<StreamSocket>* socket) { | 143 int TCPServerSocketWin::AcceptInternal(scoped_ptr<StreamSocket>* socket) { |
119 SockaddrStorage storage; | 144 SockaddrStorage storage; |
120 int new_socket = accept(socket_, storage.addr, &storage.addr_len); | 145 int new_socket = accept(socket_, storage.addr, &storage.addr_len); |
121 if (new_socket < 0) { | 146 if (new_socket < 0) { |
122 int net_error = MapSystemError(WSAGetLastError()); | 147 int net_error = MapSystemError(WSAGetLastError()); |
123 if (net_error != ERR_IO_PENDING) | 148 if (net_error != ERR_IO_PENDING) |
124 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, net_error); | 149 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, net_error); |
125 return net_error; | 150 return net_error; |
126 } | 151 } |
127 | 152 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 if (result != ERR_IO_PENDING) { | 199 if (result != ERR_IO_PENDING) { |
175 accept_socket_ = NULL; | 200 accept_socket_ = NULL; |
176 CompletionCallback callback = accept_callback_; | 201 CompletionCallback callback = accept_callback_; |
177 accept_callback_.Reset(); | 202 accept_callback_.Reset(); |
178 callback.Run(result); | 203 callback.Run(result); |
179 } | 204 } |
180 } | 205 } |
181 } | 206 } |
182 | 207 |
183 } // namespace net | 208 } // namespace net |
OLD | NEW |