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 } |
| 72 |
59 SockaddrStorage storage; | 73 SockaddrStorage storage; |
60 if (!address.ToSockAddr(storage.addr, &storage.addr_len)) | 74 if (!address.ToSockAddr(storage.addr, &storage.addr_len)) |
61 return ERR_INVALID_ARGUMENT; | 75 return ERR_INVALID_ARGUMENT; |
62 | 76 |
63 int result = bind(socket_, storage.addr, storage.addr_len); | 77 result = bind(socket_, storage.addr, storage.addr_len); |
64 if (result < 0) { | 78 if (result < 0) { |
65 PLOG(ERROR) << "bind() returned an error"; | 79 PLOG(ERROR) << "bind() returned an error"; |
66 result = MapSystemError(WSAGetLastError()); | 80 result = MapSystemError(WSAGetLastError()); |
67 Close(); | 81 Close(); |
68 return result; | 82 return result; |
69 } | 83 } |
70 | 84 |
71 result = listen(socket_, backlog); | 85 result = listen(socket_, backlog); |
72 if (result < 0) { | 86 if (result < 0) { |
73 PLOG(ERROR) << "listen() returned an error"; | 87 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); | 122 WSAEventSelect(socket_, socket_event_, FD_ACCEPT); |
109 accept_watcher_.StartWatching(socket_event_, this); | 123 accept_watcher_.StartWatching(socket_event_, this); |
110 | 124 |
111 accept_socket_ = socket; | 125 accept_socket_ = socket; |
112 accept_callback_ = callback; | 126 accept_callback_ = callback; |
113 } | 127 } |
114 | 128 |
115 return result; | 129 return result; |
116 } | 130 } |
117 | 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 |