| 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_libevent.h" | 5 #include "net/udp/udp_socket_libevent.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <netdb.h> | 9 #include <netdb.h> |
| 10 #include <sys/socket.h> | 10 #include <sys/socket.h> |
| 11 | 11 |
| 12 #include "base/callback.h" |
| 12 #include "base/eintr_wrapper.h" | 13 #include "base/eintr_wrapper.h" |
| 13 #include "base/logging.h" | 14 #include "base/logging.h" |
| 14 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
| 15 #include "base/metrics/stats_counters.h" | 16 #include "base/metrics/stats_counters.h" |
| 16 #include "base/rand_util.h" | 17 #include "base/rand_util.h" |
| 17 #include "net/base/io_buffer.h" | 18 #include "net/base/io_buffer.h" |
| 18 #include "net/base/ip_endpoint.h" | 19 #include "net/base/ip_endpoint.h" |
| 19 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
| 20 #include "net/base/net_log.h" | 21 #include "net/base/net_log.h" |
| 21 #include "net/base/net_util.h" | 22 #include "net/base/net_util.h" |
| 22 #include "net/udp/udp_data_transfer_param.h" | 23 #include "net/udp/udp_net_log_parameters.h" |
| 23 #if defined(OS_POSIX) | 24 #if defined(OS_POSIX) |
| 24 #include <netinet/in.h> | 25 #include <netinet/in.h> |
| 25 #endif | 26 #endif |
| 26 | 27 |
| 27 namespace { | 28 namespace { |
| 28 | 29 |
| 29 static const int kBindRetries = 10; | 30 static const int kBindRetries = 10; |
| 30 static const int kPortStart = 1024; | 31 static const int kPortStart = 1024; |
| 31 static const int kPortEnd = 65535; | 32 static const int kPortEnd = 65535; |
| 32 | 33 |
| 33 } // namespace net | 34 } // namespace net |
| 34 | 35 |
| 35 namespace net { | 36 namespace net { |
| 36 | 37 |
| 37 UDPSocketLibevent::UDPSocketLibevent( | 38 UDPSocketLibevent::UDPSocketLibevent( |
| 38 DatagramSocket::BindType bind_type, | 39 DatagramSocket::BindType bind_type, |
| 39 const RandIntCallback& rand_int_cb, | 40 const RandIntCallback& rand_int_cb, |
| 40 net::NetLog* net_log, | 41 net::NetLog* net_log, |
| 41 const net::NetLog::Source& source) | 42 const net::NetLog::Source& source) |
| 42 : socket_(kInvalidSocket), | 43 : socket_(kInvalidSocket), |
| 43 bind_type_(bind_type), | 44 bind_type_(bind_type), |
| 44 rand_int_cb_(rand_int_cb), | 45 rand_int_cb_(rand_int_cb), |
| 45 read_watcher_(this), | 46 read_watcher_(this), |
| 46 write_watcher_(this), | 47 write_watcher_(this), |
| 47 read_buf_len_(0), | 48 read_buf_len_(0), |
| 48 recv_from_address_(NULL), | 49 recv_from_address_(NULL), |
| 49 write_buf_len_(0), | 50 write_buf_len_(0), |
| 50 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_UDP_SOCKET)) { | 51 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_UDP_SOCKET)) { |
| 51 scoped_refptr<NetLog::EventParameters> params; | 52 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, |
| 52 if (source.is_valid()) | 53 source.ToEventParametersCallback()); |
| 53 params = new NetLogSourceParameter("source_dependency", source); | |
| 54 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, params); | |
| 55 if (bind_type == DatagramSocket::RANDOM_BIND) | 54 if (bind_type == DatagramSocket::RANDOM_BIND) |
| 56 DCHECK(!rand_int_cb.is_null()); | 55 DCHECK(!rand_int_cb.is_null()); |
| 57 } | 56 } |
| 58 | 57 |
| 59 UDPSocketLibevent::~UDPSocketLibevent() { | 58 UDPSocketLibevent::~UDPSocketLibevent() { |
| 60 Close(); | 59 Close(); |
| 61 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE, NULL); | 60 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE); |
| 62 } | 61 } |
| 63 | 62 |
| 64 void UDPSocketLibevent::Close() { | 63 void UDPSocketLibevent::Close() { |
| 65 DCHECK(CalledOnValidThread()); | 64 DCHECK(CalledOnValidThread()); |
| 66 | 65 |
| 67 if (!is_connected()) | 66 if (!is_connected()) |
| 68 return; | 67 return; |
| 69 | 68 |
| 70 // Zero out any pending read/write callback state. | 69 // Zero out any pending read/write callback state. |
| 71 read_buf_ = NULL; | 70 read_buf_ = NULL; |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 write_buf_len_ = buf_len; | 204 write_buf_len_ = buf_len; |
| 206 DCHECK(!send_to_address_.get()); | 205 DCHECK(!send_to_address_.get()); |
| 207 if (address) { | 206 if (address) { |
| 208 send_to_address_.reset(new IPEndPoint(*address)); | 207 send_to_address_.reset(new IPEndPoint(*address)); |
| 209 } | 208 } |
| 210 write_callback_ = callback; | 209 write_callback_ = callback; |
| 211 return ERR_IO_PENDING; | 210 return ERR_IO_PENDING; |
| 212 } | 211 } |
| 213 | 212 |
| 214 int UDPSocketLibevent::Connect(const IPEndPoint& address) { | 213 int UDPSocketLibevent::Connect(const IPEndPoint& address) { |
| 215 net_log_.BeginEvent( | 214 net_log_.BeginEvent(NetLog::TYPE_UDP_CONNECT, |
| 216 NetLog::TYPE_UDP_CONNECT, | 215 CreateNetLogUDPConnectCallback(&address)); |
| 217 make_scoped_refptr(new NetLogStringParameter("address", | |
| 218 address.ToString()))); | |
| 219 int rv = InternalConnect(address); | 216 int rv = InternalConnect(address); |
| 220 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_UDP_CONNECT, rv); | 217 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_UDP_CONNECT, rv); |
| 221 return rv; | 218 return rv; |
| 222 } | 219 } |
| 223 | 220 |
| 224 int UDPSocketLibevent::InternalConnect(const IPEndPoint& address) { | 221 int UDPSocketLibevent::InternalConnect(const IPEndPoint& address) { |
| 225 DCHECK(CalledOnValidThread()); | 222 DCHECK(CalledOnValidThread()); |
| 226 DCHECK(!is_connected()); | 223 DCHECK(!is_connected()); |
| 227 DCHECK(!remote_address_.get()); | 224 DCHECK(!remote_address_.get()); |
| 228 int rv = CreateSocket(address); | 225 int rv = CreateSocket(address); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 } | 316 } |
| 320 | 317 |
| 321 if (net_log_.IsLoggingAllEvents()) { | 318 if (net_log_.IsLoggingAllEvents()) { |
| 322 DCHECK(addr_len > 0); | 319 DCHECK(addr_len > 0); |
| 323 DCHECK(addr); | 320 DCHECK(addr); |
| 324 | 321 |
| 325 IPEndPoint address; | 322 IPEndPoint address; |
| 326 bool is_address_valid = address.FromSockAddr(addr, addr_len); | 323 bool is_address_valid = address.FromSockAddr(addr, addr_len); |
| 327 net_log_.AddEvent( | 324 net_log_.AddEvent( |
| 328 NetLog::TYPE_UDP_BYTES_RECEIVED, | 325 NetLog::TYPE_UDP_BYTES_RECEIVED, |
| 329 make_scoped_refptr( | 326 CreateNetLogUDPDataTranferCallback( |
| 330 new UDPDataTransferNetLogParam( | 327 result, bytes, |
| 331 result, bytes, net_log_.IsLoggingBytes(), | 328 is_address_valid ? &address : NULL)); |
| 332 is_address_valid ? &address : NULL))); | |
| 333 } | 329 } |
| 334 | 330 |
| 335 base::StatsCounter read_bytes("udp.read_bytes"); | 331 base::StatsCounter read_bytes("udp.read_bytes"); |
| 336 read_bytes.Add(result); | 332 read_bytes.Add(result); |
| 337 } | 333 } |
| 338 | 334 |
| 339 int UDPSocketLibevent::CreateSocket(const IPEndPoint& address) { | 335 int UDPSocketLibevent::CreateSocket(const IPEndPoint& address) { |
| 340 socket_ = socket(address.GetFamily(), SOCK_DGRAM, 0); | 336 socket_ = socket(address.GetFamily(), SOCK_DGRAM, 0); |
| 341 if (socket_ == kInvalidSocket) | 337 if (socket_ == kInvalidSocket) |
| 342 return MapSystemError(errno); | 338 return MapSystemError(errno); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 365 const char* bytes, | 361 const char* bytes, |
| 366 const IPEndPoint* address) const { | 362 const IPEndPoint* address) const { |
| 367 if (result < 0) { | 363 if (result < 0) { |
| 368 net_log_.AddEventWithNetErrorCode(NetLog::TYPE_UDP_SEND_ERROR, result); | 364 net_log_.AddEventWithNetErrorCode(NetLog::TYPE_UDP_SEND_ERROR, result); |
| 369 return; | 365 return; |
| 370 } | 366 } |
| 371 | 367 |
| 372 if (net_log_.IsLoggingAllEvents()) { | 368 if (net_log_.IsLoggingAllEvents()) { |
| 373 net_log_.AddEvent( | 369 net_log_.AddEvent( |
| 374 NetLog::TYPE_UDP_BYTES_SENT, | 370 NetLog::TYPE_UDP_BYTES_SENT, |
| 375 make_scoped_refptr( | 371 CreateNetLogUDPDataTranferCallback(result, bytes, address)); |
| 376 new UDPDataTransferNetLogParam(result, bytes, | |
| 377 net_log_.IsLoggingBytes(), | |
| 378 address))); | |
| 379 } | 372 } |
| 380 | 373 |
| 381 base::StatsCounter write_bytes("udp.write_bytes"); | 374 base::StatsCounter write_bytes("udp.write_bytes"); |
| 382 write_bytes.Add(result); | 375 write_bytes.Add(result); |
| 383 } | 376 } |
| 384 | 377 |
| 385 int UDPSocketLibevent::InternalRecvFrom(IOBuffer* buf, int buf_len, | 378 int UDPSocketLibevent::InternalRecvFrom(IOBuffer* buf, int buf_len, |
| 386 IPEndPoint* address) { | 379 IPEndPoint* address) { |
| 387 int bytes_transferred; | 380 int bytes_transferred; |
| 388 int flags = 0; | 381 int flags = 0; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 453 | 446 |
| 454 for (int i = 0; i < kBindRetries; ++i) { | 447 for (int i = 0; i < kBindRetries; ++i) { |
| 455 int rv = DoBind(IPEndPoint(ip, rand_int_cb_.Run(kPortStart, kPortEnd))); | 448 int rv = DoBind(IPEndPoint(ip, rand_int_cb_.Run(kPortStart, kPortEnd))); |
| 456 if (rv == OK || rv != ERR_ADDRESS_IN_USE) | 449 if (rv == OK || rv != ERR_ADDRESS_IN_USE) |
| 457 return rv; | 450 return rv; |
| 458 } | 451 } |
| 459 return DoBind(IPEndPoint(ip, 0)); | 452 return DoBind(IPEndPoint(ip, 0)); |
| 460 } | 453 } |
| 461 | 454 |
| 462 } // namespace net | 455 } // namespace net |
| OLD | NEW |