| 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/udp/udp_socket_win.h" | 5 #include "net/udp/udp_socket_win.h" |
| 6 | 6 |
| 7 #include <mstcpip.h> | 7 #include <mstcpip.h> |
| 8 | 8 |
| 9 #include "base/callback.h" |
| 9 #include "base/eintr_wrapper.h" | 10 #include "base/eintr_wrapper.h" |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 12 #include "base/metrics/stats_counters.h" | 13 #include "base/metrics/stats_counters.h" |
| 13 #include "base/rand_util.h" | 14 #include "base/rand_util.h" |
| 14 #include "net/base/io_buffer.h" | 15 #include "net/base/io_buffer.h" |
| 15 #include "net/base/ip_endpoint.h" | 16 #include "net/base/ip_endpoint.h" |
| 16 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
| 17 #include "net/base/net_log.h" | 18 #include "net/base/net_log.h" |
| 18 #include "net/base/net_util.h" | 19 #include "net/base/net_util.h" |
| 19 #include "net/base/winsock_init.h" | 20 #include "net/base/winsock_init.h" |
| 20 #include "net/base/winsock_util.h" | 21 #include "net/base/winsock_util.h" |
| 21 #include "net/udp/udp_data_transfer_param.h" | 22 #include "net/udp/udp_net_log_parameters.h" |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 static const int kBindRetries = 10; | 26 static const int kBindRetries = 10; |
| 26 static const int kPortStart = 1024; | 27 static const int kPortStart = 1024; |
| 27 static const int kPortEnd = 65535; | 28 static const int kPortEnd = 65535; |
| 28 | 29 |
| 29 } // namespace net | 30 } // namespace net |
| 30 | 31 |
| 31 namespace net { | 32 namespace net { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 45 net::NetLog* net_log, | 46 net::NetLog* net_log, |
| 46 const net::NetLog::Source& source) | 47 const net::NetLog::Source& source) |
| 47 : socket_(INVALID_SOCKET), | 48 : socket_(INVALID_SOCKET), |
| 48 bind_type_(bind_type), | 49 bind_type_(bind_type), |
| 49 rand_int_cb_(rand_int_cb), | 50 rand_int_cb_(rand_int_cb), |
| 50 ALLOW_THIS_IN_INITIALIZER_LIST(read_delegate_(this)), | 51 ALLOW_THIS_IN_INITIALIZER_LIST(read_delegate_(this)), |
| 51 ALLOW_THIS_IN_INITIALIZER_LIST(write_delegate_(this)), | 52 ALLOW_THIS_IN_INITIALIZER_LIST(write_delegate_(this)), |
| 52 recv_from_address_(NULL), | 53 recv_from_address_(NULL), |
| 53 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_UDP_SOCKET)) { | 54 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_UDP_SOCKET)) { |
| 54 EnsureWinsockInit(); | 55 EnsureWinsockInit(); |
| 55 scoped_refptr<NetLog::EventParameters> params; | 56 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, |
| 56 if (source.is_valid()) | 57 source.ToEventParametersCallback()); |
| 57 params = new NetLogSourceParameter("source_dependency", source); | |
| 58 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, params); | |
| 59 memset(&read_overlapped_, 0, sizeof(read_overlapped_)); | 58 memset(&read_overlapped_, 0, sizeof(read_overlapped_)); |
| 60 read_overlapped_.hEvent = WSACreateEvent(); | 59 read_overlapped_.hEvent = WSACreateEvent(); |
| 61 memset(&write_overlapped_, 0, sizeof(write_overlapped_)); | 60 memset(&write_overlapped_, 0, sizeof(write_overlapped_)); |
| 62 write_overlapped_.hEvent = WSACreateEvent(); | 61 write_overlapped_.hEvent = WSACreateEvent(); |
| 63 if (bind_type == DatagramSocket::RANDOM_BIND) | 62 if (bind_type == DatagramSocket::RANDOM_BIND) |
| 64 DCHECK(!rand_int_cb.is_null()); | 63 DCHECK(!rand_int_cb.is_null()); |
| 65 } | 64 } |
| 66 | 65 |
| 67 UDPSocketWin::~UDPSocketWin() { | 66 UDPSocketWin::~UDPSocketWin() { |
| 68 Close(); | 67 Close(); |
| 69 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE, NULL); | 68 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE); |
| 70 } | 69 } |
| 71 | 70 |
| 72 void UDPSocketWin::Close() { | 71 void UDPSocketWin::Close() { |
| 73 DCHECK(CalledOnValidThread()); | 72 DCHECK(CalledOnValidThread()); |
| 74 | 73 |
| 75 if (!is_connected()) | 74 if (!is_connected()) |
| 76 return; | 75 return; |
| 77 | 76 |
| 78 // Zero out any pending read/write callback state. | 77 // Zero out any pending read/write callback state. |
| 79 read_callback_.Reset(); | 78 read_callback_.Reset(); |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 return nwrite; | 184 return nwrite; |
| 186 | 185 |
| 187 if (address) | 186 if (address) |
| 188 send_to_address_.reset(new IPEndPoint(*address)); | 187 send_to_address_.reset(new IPEndPoint(*address)); |
| 189 write_iobuffer_ = buf; | 188 write_iobuffer_ = buf; |
| 190 write_callback_ = callback; | 189 write_callback_ = callback; |
| 191 return ERR_IO_PENDING; | 190 return ERR_IO_PENDING; |
| 192 } | 191 } |
| 193 | 192 |
| 194 int UDPSocketWin::Connect(const IPEndPoint& address) { | 193 int UDPSocketWin::Connect(const IPEndPoint& address) { |
| 195 net_log_.BeginEvent( | 194 net_log_.BeginEvent(NetLog::TYPE_UDP_CONNECT, |
| 196 NetLog::TYPE_UDP_CONNECT, | 195 CreateNetLogUDPConnectCallback(&address)); |
| 197 make_scoped_refptr(new NetLogStringParameter("address", | |
| 198 address.ToString()))); | |
| 199 int rv = InternalConnect(address); | 196 int rv = InternalConnect(address); |
| 200 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_UDP_CONNECT, rv); | 197 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_UDP_CONNECT, rv); |
| 201 return rv; | 198 return rv; |
| 202 } | 199 } |
| 203 | 200 |
| 204 int UDPSocketWin::InternalConnect(const IPEndPoint& address) { | 201 int UDPSocketWin::InternalConnect(const IPEndPoint& address) { |
| 205 DCHECK(!is_connected()); | 202 DCHECK(!is_connected()); |
| 206 DCHECK(!remote_address_.get()); | 203 DCHECK(!remote_address_.get()); |
| 207 int rv = CreateSocket(address); | 204 int rv = CreateSocket(address); |
| 208 if (rv < 0) | 205 if (rv < 0) |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 net_log_.AddEventWithNetErrorCode(NetLog::TYPE_UDP_RECEIVE_ERROR, result); | 302 net_log_.AddEventWithNetErrorCode(NetLog::TYPE_UDP_RECEIVE_ERROR, result); |
| 306 return; | 303 return; |
| 307 } | 304 } |
| 308 | 305 |
| 309 if (net_log_.IsLoggingAllEvents()) { | 306 if (net_log_.IsLoggingAllEvents()) { |
| 310 // Get address for logging, if |address| is NULL. | 307 // Get address for logging, if |address| is NULL. |
| 311 IPEndPoint address; | 308 IPEndPoint address; |
| 312 bool is_address_valid = ReceiveAddressToIPEndpoint(&address); | 309 bool is_address_valid = ReceiveAddressToIPEndpoint(&address); |
| 313 net_log_.AddEvent( | 310 net_log_.AddEvent( |
| 314 NetLog::TYPE_UDP_BYTES_RECEIVED, | 311 NetLog::TYPE_UDP_BYTES_RECEIVED, |
| 315 make_scoped_refptr( | 312 CreateNetLogUDPDataTranferCallback( |
| 316 new UDPDataTransferNetLogParam( | 313 result, bytes, |
| 317 result, bytes, net_log_.IsLoggingBytes(), | 314 is_address_valid ? &address : NULL)); |
| 318 is_address_valid ? &address : NULL))); | |
| 319 } | 315 } |
| 320 | 316 |
| 321 base::StatsCounter read_bytes("udp.read_bytes"); | 317 base::StatsCounter read_bytes("udp.read_bytes"); |
| 322 read_bytes.Add(result); | 318 read_bytes.Add(result); |
| 323 } | 319 } |
| 324 | 320 |
| 325 void UDPSocketWin::DidCompleteWrite() { | 321 void UDPSocketWin::DidCompleteWrite() { |
| 326 DWORD num_bytes, flags; | 322 DWORD num_bytes, flags; |
| 327 BOOL ok = WSAGetOverlappedResult(socket_, &write_overlapped_, | 323 BOOL ok = WSAGetOverlappedResult(socket_, &write_overlapped_, |
| 328 &num_bytes, FALSE, &flags); | 324 &num_bytes, FALSE, &flags); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 339 const char* bytes, | 335 const char* bytes, |
| 340 const IPEndPoint* address) const { | 336 const IPEndPoint* address) const { |
| 341 if (result < 0) { | 337 if (result < 0) { |
| 342 net_log_.AddEventWithNetErrorCode(NetLog::TYPE_UDP_SEND_ERROR, result); | 338 net_log_.AddEventWithNetErrorCode(NetLog::TYPE_UDP_SEND_ERROR, result); |
| 343 return; | 339 return; |
| 344 } | 340 } |
| 345 | 341 |
| 346 if (net_log_.IsLoggingAllEvents()) { | 342 if (net_log_.IsLoggingAllEvents()) { |
| 347 net_log_.AddEvent( | 343 net_log_.AddEvent( |
| 348 NetLog::TYPE_UDP_BYTES_SENT, | 344 NetLog::TYPE_UDP_BYTES_SENT, |
| 349 make_scoped_refptr( | 345 CreateNetLogUDPDataTranferCallback(result, bytes, address)); |
| 350 new UDPDataTransferNetLogParam(result, bytes, | |
| 351 net_log_.IsLoggingBytes(), | |
| 352 address))); | |
| 353 } | 346 } |
| 354 | 347 |
| 355 base::StatsCounter write_bytes("udp.write_bytes"); | 348 base::StatsCounter write_bytes("udp.write_bytes"); |
| 356 write_bytes.Add(result); | 349 write_bytes.Add(result); |
| 357 } | 350 } |
| 358 | 351 |
| 359 int UDPSocketWin::InternalRecvFrom(IOBuffer* buf, int buf_len, | 352 int UDPSocketWin::InternalRecvFrom(IOBuffer* buf, int buf_len, |
| 360 IPEndPoint* address) { | 353 IPEndPoint* address) { |
| 361 recv_addr_len_ = sizeof(recv_addr_storage_); | 354 recv_addr_len_ = sizeof(recv_addr_storage_); |
| 362 struct sockaddr* addr = | 355 struct sockaddr* addr = |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 460 return DoBind(IPEndPoint(ip, 0)); | 453 return DoBind(IPEndPoint(ip, 0)); |
| 461 } | 454 } |
| 462 | 455 |
| 463 bool UDPSocketWin::ReceiveAddressToIPEndpoint(IPEndPoint* address) const { | 456 bool UDPSocketWin::ReceiveAddressToIPEndpoint(IPEndPoint* address) const { |
| 464 const struct sockaddr* addr = | 457 const struct sockaddr* addr = |
| 465 reinterpret_cast<const struct sockaddr*>(&recv_addr_storage_); | 458 reinterpret_cast<const struct sockaddr*>(&recv_addr_storage_); |
| 466 return address->FromSockAddr(addr, recv_addr_len_); | 459 return address->FromSockAddr(addr, recv_addr_len_); |
| 467 } | 460 } |
| 468 | 461 |
| 469 } // namespace net | 462 } // namespace net |
| OLD | NEW |