| 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/tcp_client_socket.h" | 15 #include "net/socket/tcp_client_socket.h" |
| 15 | 16 |
| 16 namespace net { | 17 namespace net { |
| 17 | 18 |
| 18 TCPServerSocketWin::TCPServerSocketWin(net::NetLog* net_log, | 19 TCPServerSocketWin::TCPServerSocketWin(net::NetLog* net_log, |
| 19 const net::NetLog::Source& source) | 20 const net::NetLog::Source& source) |
| 20 : socket_(INVALID_SOCKET), | 21 : socket_(INVALID_SOCKET), |
| 21 socket_event_(WSA_INVALID_EVENT), | 22 socket_event_(WSA_INVALID_EVENT), |
| 22 accept_socket_(NULL), | 23 accept_socket_(NULL), |
| 23 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) { | 24 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) { |
| 24 scoped_refptr<NetLog::EventParameters> params; | 25 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, |
| 25 if (source.is_valid()) | 26 source.ToEventParametersCallback()); |
| 26 params = new NetLogSourceParameter("source_dependency", source); | |
| 27 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, params); | |
| 28 EnsureWinsockInit(); | 27 EnsureWinsockInit(); |
| 29 } | 28 } |
| 30 | 29 |
| 31 TCPServerSocketWin::~TCPServerSocketWin() { | 30 TCPServerSocketWin::~TCPServerSocketWin() { |
| 32 Close(); | 31 Close(); |
| 33 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE, NULL); | 32 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE); |
| 34 } | 33 } |
| 35 | 34 |
| 36 int TCPServerSocketWin::Listen(const IPEndPoint& address, int backlog) { | 35 int TCPServerSocketWin::Listen(const IPEndPoint& address, int backlog) { |
| 37 DCHECK(CalledOnValidThread()); | 36 DCHECK(CalledOnValidThread()); |
| 38 DCHECK_GT(backlog, 0); | 37 DCHECK_GT(backlog, 0); |
| 39 DCHECK_EQ(socket_, INVALID_SOCKET); | 38 DCHECK_EQ(socket_, INVALID_SOCKET); |
| 40 DCHECK_EQ(socket_event_, WSA_INVALID_EVENT); | 39 DCHECK_EQ(socket_event_, WSA_INVALID_EVENT); |
| 41 | 40 |
| 42 socket_event_ = WSACreateEvent(); | 41 socket_event_ = WSACreateEvent(); |
| 43 if (socket_event_ == WSA_INVALID_EVENT) { | 42 if (socket_event_ == WSA_INVALID_EVENT) { |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 return OK; | 92 return OK; |
| 94 } | 93 } |
| 95 | 94 |
| 96 int TCPServerSocketWin::Accept( | 95 int TCPServerSocketWin::Accept( |
| 97 scoped_ptr<StreamSocket>* socket, const CompletionCallback& callback) { | 96 scoped_ptr<StreamSocket>* socket, const CompletionCallback& callback) { |
| 98 DCHECK(CalledOnValidThread()); | 97 DCHECK(CalledOnValidThread()); |
| 99 DCHECK(socket); | 98 DCHECK(socket); |
| 100 DCHECK(!callback.is_null()); | 99 DCHECK(!callback.is_null()); |
| 101 DCHECK(accept_callback_.is_null()); | 100 DCHECK(accept_callback_.is_null()); |
| 102 | 101 |
| 103 net_log_.BeginEvent(NetLog::TYPE_TCP_ACCEPT, NULL); | 102 net_log_.BeginEvent(NetLog::TYPE_TCP_ACCEPT); |
| 104 | 103 |
| 105 int result = AcceptInternal(socket); | 104 int result = AcceptInternal(socket); |
| 106 | 105 |
| 107 if (result == ERR_IO_PENDING) { | 106 if (result == ERR_IO_PENDING) { |
| 108 // Start watching | 107 // Start watching |
| 109 WSAEventSelect(socket_, socket_event_, FD_ACCEPT); | 108 WSAEventSelect(socket_, socket_event_, FD_ACCEPT); |
| 110 accept_watcher_.StartWatching(socket_event_, this); | 109 accept_watcher_.StartWatching(socket_event_, this); |
| 111 | 110 |
| 112 accept_socket_ = socket; | 111 accept_socket_ = socket; |
| 113 accept_callback_ = callback; | 112 accept_callback_ = callback; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 139 net_log_.net_log(), net_log_.source())); | 138 net_log_.net_log(), net_log_.source())); |
| 140 int adopt_result = tcp_socket->AdoptSocket(new_socket); | 139 int adopt_result = tcp_socket->AdoptSocket(new_socket); |
| 141 if (adopt_result != OK) { | 140 if (adopt_result != OK) { |
| 142 if (closesocket(new_socket) < 0) | 141 if (closesocket(new_socket) < 0) |
| 143 PLOG(ERROR) << "closesocket"; | 142 PLOG(ERROR) << "closesocket"; |
| 144 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, adopt_result); | 143 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, adopt_result); |
| 145 return adopt_result; | 144 return adopt_result; |
| 146 } | 145 } |
| 147 socket->reset(tcp_socket.release()); | 146 socket->reset(tcp_socket.release()); |
| 148 net_log_.EndEvent(NetLog::TYPE_TCP_ACCEPT, | 147 net_log_.EndEvent(NetLog::TYPE_TCP_ACCEPT, |
| 149 make_scoped_refptr(new NetLogStringParameter( | 148 CreateNetLogIPEndPointCallback(&address)); |
| 150 "address", address.ToString()))); | |
| 151 return OK; | 149 return OK; |
| 152 } | 150 } |
| 153 | 151 |
| 154 void TCPServerSocketWin::Close() { | 152 void TCPServerSocketWin::Close() { |
| 155 if (socket_ != INVALID_SOCKET) { | 153 if (socket_ != INVALID_SOCKET) { |
| 156 if (closesocket(socket_) < 0) | 154 if (closesocket(socket_) < 0) |
| 157 PLOG(ERROR) << "closesocket"; | 155 PLOG(ERROR) << "closesocket"; |
| 158 socket_ = INVALID_SOCKET; | 156 socket_ = INVALID_SOCKET; |
| 159 } | 157 } |
| 160 | 158 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 176 if (result != ERR_IO_PENDING) { | 174 if (result != ERR_IO_PENDING) { |
| 177 accept_socket_ = NULL; | 175 accept_socket_ = NULL; |
| 178 CompletionCallback callback = accept_callback_; | 176 CompletionCallback callback = accept_callback_; |
| 179 accept_callback_.Reset(); | 177 accept_callback_.Reset(); |
| 180 callback.Run(result); | 178 callback.Run(result); |
| 181 } | 179 } |
| 182 } | 180 } |
| 183 } | 181 } |
| 184 | 182 |
| 185 } // namespace net | 183 } // namespace net |
| OLD | NEW |