| 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/callback.h" |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
| 15 #include "base/metrics/sparse_histogram.h" | 15 #include "base/metrics/sparse_histogram.h" |
| 16 #include "base/rand_util.h" | 16 #include "base/rand_util.h" |
| 17 #include "net/base/io_buffer.h" | 17 #include "net/base/io_buffer.h" |
| 18 #include "net/base/ip_address.h" | 18 #include "net/base/ip_address.h" |
| 19 #include "net/base/ip_endpoint.h" | 19 #include "net/base/ip_endpoint.h" |
| 20 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
| 21 #include "net/base/network_activity_monitor.h" | 21 #include "net/base/network_activity_monitor.h" |
| 22 #include "net/base/network_change_notifier.h" | 22 #include "net/base/network_change_notifier.h" |
| 23 #include "net/base/sockaddr_storage.h" | 23 #include "net/base/sockaddr_storage.h" |
| 24 #include "net/base/winsock_init.h" | 24 #include "net/base/winsock_init.h" |
| 25 #include "net/base/winsock_util.h" | 25 #include "net/base/winsock_util.h" |
| 26 #include "net/log/net_log.h" | 26 #include "net/log/net_log.h" |
| 27 #include "net/log/net_log_event_type.h" |
| 28 #include "net/log/net_log_source_type.h" |
| 27 #include "net/socket/socket_descriptor.h" | 29 #include "net/socket/socket_descriptor.h" |
| 28 #include "net/udp/udp_net_log_parameters.h" | 30 #include "net/udp/udp_net_log_parameters.h" |
| 29 | 31 |
| 30 namespace { | 32 namespace { |
| 31 | 33 |
| 32 const int kBindRetries = 10; | 34 const int kBindRetries = 10; |
| 33 const int kPortStart = 1024; | 35 const int kPortStart = 1024; |
| 34 const int kPortEnd = 65535; | 36 const int kPortEnd = 65535; |
| 35 | 37 |
| 36 } // namespace | 38 } // namespace |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 is_connected_(false), | 250 is_connected_(false), |
| 249 socket_options_(SOCKET_OPTION_MULTICAST_LOOP), | 251 socket_options_(SOCKET_OPTION_MULTICAST_LOOP), |
| 250 multicast_interface_(0), | 252 multicast_interface_(0), |
| 251 multicast_time_to_live_(1), | 253 multicast_time_to_live_(1), |
| 252 bind_type_(bind_type), | 254 bind_type_(bind_type), |
| 253 rand_int_cb_(rand_int_cb), | 255 rand_int_cb_(rand_int_cb), |
| 254 use_non_blocking_io_(false), | 256 use_non_blocking_io_(false), |
| 255 read_iobuffer_len_(0), | 257 read_iobuffer_len_(0), |
| 256 write_iobuffer_len_(0), | 258 write_iobuffer_len_(0), |
| 257 recv_from_address_(NULL), | 259 recv_from_address_(NULL), |
| 258 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_UDP_SOCKET)), | 260 net_log_(BoundNetLog::Make(net_log, NetLogSourceType::UDP_SOCKET)), |
| 259 qos_handle_(NULL), | 261 qos_handle_(NULL), |
| 260 qos_flow_id_(0) { | 262 qos_flow_id_(0) { |
| 261 EnsureWinsockInit(); | 263 EnsureWinsockInit(); |
| 262 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, | 264 net_log_.BeginEvent(NetLogEventType::SOCKET_ALIVE, |
| 263 source.ToEventParametersCallback()); | 265 source.ToEventParametersCallback()); |
| 264 if (bind_type == DatagramSocket::RANDOM_BIND) | 266 if (bind_type == DatagramSocket::RANDOM_BIND) |
| 265 DCHECK(!rand_int_cb.is_null()); | 267 DCHECK(!rand_int_cb.is_null()); |
| 266 } | 268 } |
| 267 | 269 |
| 268 UDPSocketWin::~UDPSocketWin() { | 270 UDPSocketWin::~UDPSocketWin() { |
| 269 Close(); | 271 Close(); |
| 270 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE); | 272 net_log_.EndEvent(NetLogEventType::SOCKET_ALIVE); |
| 271 } | 273 } |
| 272 | 274 |
| 273 int UDPSocketWin::Open(AddressFamily address_family) { | 275 int UDPSocketWin::Open(AddressFamily address_family) { |
| 274 DCHECK(CalledOnValidThread()); | 276 DCHECK(CalledOnValidThread()); |
| 275 DCHECK_EQ(socket_, INVALID_SOCKET); | 277 DCHECK_EQ(socket_, INVALID_SOCKET); |
| 276 | 278 |
| 277 addr_family_ = ConvertAddressFamily(address_family); | 279 addr_family_ = ConvertAddressFamily(address_family); |
| 278 socket_ = CreatePlatformSocket(addr_family_, SOCK_DGRAM, IPPROTO_UDP); | 280 socket_ = CreatePlatformSocket(addr_family_, SOCK_DGRAM, IPPROTO_UDP); |
| 279 if (socket_ == INVALID_SOCKET) | 281 if (socket_ == INVALID_SOCKET) |
| 280 return MapSystemError(WSAGetLastError()); | 282 return MapSystemError(WSAGetLastError()); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 | 350 |
| 349 // TODO(szym): Simplify. http://crbug.com/126152 | 351 // TODO(szym): Simplify. http://crbug.com/126152 |
| 350 if (!local_address_.get()) { | 352 if (!local_address_.get()) { |
| 351 SockaddrStorage storage; | 353 SockaddrStorage storage; |
| 352 if (getsockname(socket_, storage.addr, &storage.addr_len)) | 354 if (getsockname(socket_, storage.addr, &storage.addr_len)) |
| 353 return MapSystemError(WSAGetLastError()); | 355 return MapSystemError(WSAGetLastError()); |
| 354 std::unique_ptr<IPEndPoint> local_address(new IPEndPoint()); | 356 std::unique_ptr<IPEndPoint> local_address(new IPEndPoint()); |
| 355 if (!local_address->FromSockAddr(storage.addr, storage.addr_len)) | 357 if (!local_address->FromSockAddr(storage.addr, storage.addr_len)) |
| 356 return ERR_ADDRESS_INVALID; | 358 return ERR_ADDRESS_INVALID; |
| 357 local_address_.reset(local_address.release()); | 359 local_address_.reset(local_address.release()); |
| 358 net_log_.AddEvent(NetLog::TYPE_UDP_LOCAL_ADDRESS, | 360 net_log_.AddEvent(NetLogEventType::UDP_LOCAL_ADDRESS, |
| 359 CreateNetLogUDPConnectCallback( | 361 CreateNetLogUDPConnectCallback( |
| 360 local_address_.get(), | 362 local_address_.get(), |
| 361 NetworkChangeNotifier::kInvalidNetworkHandle)); | 363 NetworkChangeNotifier::kInvalidNetworkHandle)); |
| 362 } | 364 } |
| 363 | 365 |
| 364 *address = *local_address_; | 366 *address = *local_address_; |
| 365 return OK; | 367 return OK; |
| 366 } | 368 } |
| 367 | 369 |
| 368 int UDPSocketWin::Read(IOBuffer* buf, | 370 int UDPSocketWin::Read(IOBuffer* buf, |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 | 425 |
| 424 if (address) | 426 if (address) |
| 425 send_to_address_.reset(new IPEndPoint(*address)); | 427 send_to_address_.reset(new IPEndPoint(*address)); |
| 426 write_callback_ = callback; | 428 write_callback_ = callback; |
| 427 return ERR_IO_PENDING; | 429 return ERR_IO_PENDING; |
| 428 } | 430 } |
| 429 | 431 |
| 430 int UDPSocketWin::Connect(const IPEndPoint& address) { | 432 int UDPSocketWin::Connect(const IPEndPoint& address) { |
| 431 DCHECK_NE(socket_, INVALID_SOCKET); | 433 DCHECK_NE(socket_, INVALID_SOCKET); |
| 432 net_log_.BeginEvent( | 434 net_log_.BeginEvent( |
| 433 NetLog::TYPE_UDP_CONNECT, | 435 NetLogEventType::UDP_CONNECT, |
| 434 CreateNetLogUDPConnectCallback( | 436 CreateNetLogUDPConnectCallback( |
| 435 &address, NetworkChangeNotifier::kInvalidNetworkHandle)); | 437 &address, NetworkChangeNotifier::kInvalidNetworkHandle)); |
| 436 int rv = InternalConnect(address); | 438 int rv = InternalConnect(address); |
| 437 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_UDP_CONNECT, rv); | 439 net_log_.EndEventWithNetErrorCode(NetLogEventType::UDP_CONNECT, rv); |
| 438 is_connected_ = (rv == OK); | 440 is_connected_ = (rv == OK); |
| 439 return rv; | 441 return rv; |
| 440 } | 442 } |
| 441 | 443 |
| 442 int UDPSocketWin::InternalConnect(const IPEndPoint& address) { | 444 int UDPSocketWin::InternalConnect(const IPEndPoint& address) { |
| 443 DCHECK(!is_connected()); | 445 DCHECK(!is_connected()); |
| 444 DCHECK(!remote_address_.get()); | 446 DCHECK(!remote_address_.get()); |
| 445 | 447 |
| 446 int rv = 0; | 448 int rv = 0; |
| 447 if (bind_type_ == DatagramSocket::RANDOM_BIND) { | 449 if (bind_type_ == DatagramSocket::RANDOM_BIND) { |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 694 return; | 696 return; |
| 695 bool watched = | 697 bool watched = |
| 696 read_write_watcher_.StartWatchingOnce(read_write_event_.Get(), this); | 698 read_write_watcher_.StartWatchingOnce(read_write_event_.Get(), this); |
| 697 DCHECK(watched); | 699 DCHECK(watched); |
| 698 } | 700 } |
| 699 | 701 |
| 700 void UDPSocketWin::LogRead(int result, | 702 void UDPSocketWin::LogRead(int result, |
| 701 const char* bytes, | 703 const char* bytes, |
| 702 const IPEndPoint* address) const { | 704 const IPEndPoint* address) const { |
| 703 if (result < 0) { | 705 if (result < 0) { |
| 704 net_log_.AddEventWithNetErrorCode(NetLog::TYPE_UDP_RECEIVE_ERROR, result); | 706 net_log_.AddEventWithNetErrorCode(NetLogEventType::UDP_RECEIVE_ERROR, |
| 707 result); |
| 705 return; | 708 return; |
| 706 } | 709 } |
| 707 | 710 |
| 708 if (net_log_.IsCapturing()) { | 711 if (net_log_.IsCapturing()) { |
| 709 net_log_.AddEvent( | 712 net_log_.AddEvent( |
| 710 NetLog::TYPE_UDP_BYTES_RECEIVED, | 713 NetLogEventType::UDP_BYTES_RECEIVED, |
| 711 CreateNetLogUDPDataTranferCallback(result, bytes, address)); | 714 CreateNetLogUDPDataTranferCallback(result, bytes, address)); |
| 712 } | 715 } |
| 713 | 716 |
| 714 NetworkActivityMonitor::GetInstance()->IncrementBytesReceived(result); | 717 NetworkActivityMonitor::GetInstance()->IncrementBytesReceived(result); |
| 715 } | 718 } |
| 716 | 719 |
| 717 void UDPSocketWin::LogWrite(int result, | 720 void UDPSocketWin::LogWrite(int result, |
| 718 const char* bytes, | 721 const char* bytes, |
| 719 const IPEndPoint* address) const { | 722 const IPEndPoint* address) const { |
| 720 if (result < 0) { | 723 if (result < 0) { |
| 721 net_log_.AddEventWithNetErrorCode(NetLog::TYPE_UDP_SEND_ERROR, result); | 724 net_log_.AddEventWithNetErrorCode(NetLogEventType::UDP_SEND_ERROR, result); |
| 722 return; | 725 return; |
| 723 } | 726 } |
| 724 | 727 |
| 725 if (net_log_.IsCapturing()) { | 728 if (net_log_.IsCapturing()) { |
| 726 net_log_.AddEvent( | 729 net_log_.AddEvent( |
| 727 NetLog::TYPE_UDP_BYTES_SENT, | 730 NetLogEventType::UDP_BYTES_SENT, |
| 728 CreateNetLogUDPDataTranferCallback(result, bytes, address)); | 731 CreateNetLogUDPDataTranferCallback(result, bytes, address)); |
| 729 } | 732 } |
| 730 | 733 |
| 731 NetworkActivityMonitor::GetInstance()->IncrementBytesSent(result); | 734 NetworkActivityMonitor::GetInstance()->IncrementBytesSent(result); |
| 732 } | 735 } |
| 733 | 736 |
| 734 int UDPSocketWin::InternalRecvFromOverlapped(IOBuffer* buf, | 737 int UDPSocketWin::InternalRecvFromOverlapped(IOBuffer* buf, |
| 735 int buf_len, | 738 int buf_len, |
| 736 IPEndPoint* address) { | 739 IPEndPoint* address) { |
| 737 DCHECK(!core_->read_iobuffer_.get()); | 740 DCHECK(!core_->read_iobuffer_.get()); |
| (...skipping 450 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1188 void UDPSocketWin::DetachFromThread() { | 1191 void UDPSocketWin::DetachFromThread() { |
| 1189 base::NonThreadSafe::DetachFromThread(); | 1192 base::NonThreadSafe::DetachFromThread(); |
| 1190 } | 1193 } |
| 1191 | 1194 |
| 1192 void UDPSocketWin::UseNonBlockingIO() { | 1195 void UDPSocketWin::UseNonBlockingIO() { |
| 1193 DCHECK(!core_); | 1196 DCHECK(!core_); |
| 1194 use_non_blocking_io_ = true; | 1197 use_non_blocking_io_ = true; |
| 1195 } | 1198 } |
| 1196 | 1199 |
| 1197 } // namespace net | 1200 } // namespace net |
| OLD | NEW |