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 <net/if.h> | 10 #include <net/if.h> |
11 #include <netinet/in.h> | 11 #include <netinet/in.h> |
12 #include <sys/ioctl.h> | 12 #include <sys/ioctl.h> |
13 #include <sys/socket.h> | 13 #include <sys/socket.h> |
14 | 14 |
15 #include "base/callback.h" | 15 #include "base/callback.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
18 #include "base/metrics/sparse_histogram.h" | 18 #include "base/metrics/sparse_histogram.h" |
19 #include "base/metrics/stats_counters.h" | 19 #include "base/metrics/stats_counters.h" |
20 #include "base/posix/eintr_wrapper.h" | 20 #include "base/posix/eintr_wrapper.h" |
21 #include "base/rand_util.h" | 21 #include "base/rand_util.h" |
22 #include "net/base/io_buffer.h" | 22 #include "net/base/io_buffer.h" |
23 #include "net/base/ip_endpoint.h" | 23 #include "net/base/ip_endpoint.h" |
24 #include "net/base/net_errors.h" | 24 #include "net/base/net_errors.h" |
25 #include "net/base/net_log.h" | 25 #include "net/base/net_log.h" |
26 #include "net/base/net_util.h" | 26 #include "net/base/net_util.h" |
| 27 #include "net/base/network_activity_monitor.h" |
27 #include "net/socket/socket_descriptor.h" | 28 #include "net/socket/socket_descriptor.h" |
28 #include "net/udp/udp_net_log_parameters.h" | 29 #include "net/udp/udp_net_log_parameters.h" |
29 | 30 |
30 | 31 |
31 namespace net { | 32 namespace net { |
32 | 33 |
33 namespace { | 34 namespace { |
34 | 35 |
35 const int kBindRetries = 10; | 36 const int kBindRetries = 10; |
36 const int kPortStart = 1024; | 37 const int kPortStart = 1024; |
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
407 bool is_address_valid = address.FromSockAddr(addr, addr_len); | 408 bool is_address_valid = address.FromSockAddr(addr, addr_len); |
408 net_log_.AddEvent( | 409 net_log_.AddEvent( |
409 NetLog::TYPE_UDP_BYTES_RECEIVED, | 410 NetLog::TYPE_UDP_BYTES_RECEIVED, |
410 CreateNetLogUDPDataTranferCallback( | 411 CreateNetLogUDPDataTranferCallback( |
411 result, bytes, | 412 result, bytes, |
412 is_address_valid ? &address : NULL)); | 413 is_address_valid ? &address : NULL)); |
413 } | 414 } |
414 | 415 |
415 base::StatsCounter read_bytes("udp.read_bytes"); | 416 base::StatsCounter read_bytes("udp.read_bytes"); |
416 read_bytes.Add(result); | 417 read_bytes.Add(result); |
| 418 NetworkActivityMonitor::GetInstance()->IncrementBytesReceived(result); |
417 } | 419 } |
418 | 420 |
419 int UDPSocketLibevent::CreateSocket(int addr_family) { | 421 int UDPSocketLibevent::CreateSocket(int addr_family) { |
420 addr_family_ = addr_family; | 422 addr_family_ = addr_family; |
421 socket_ = CreatePlatformSocket(addr_family_, SOCK_DGRAM, 0); | 423 socket_ = CreatePlatformSocket(addr_family_, SOCK_DGRAM, 0); |
422 if (socket_ == kInvalidSocket) | 424 if (socket_ == kInvalidSocket) |
423 return MapSystemError(errno); | 425 return MapSystemError(errno); |
424 if (SetNonBlocking(socket_)) { | 426 if (SetNonBlocking(socket_)) { |
425 const int err = MapSystemError(errno); | 427 const int err = MapSystemError(errno); |
426 Close(); | 428 Close(); |
(...skipping 24 matching lines...) Expand all Loading... |
451 } | 453 } |
452 | 454 |
453 if (net_log_.IsLogging()) { | 455 if (net_log_.IsLogging()) { |
454 net_log_.AddEvent( | 456 net_log_.AddEvent( |
455 NetLog::TYPE_UDP_BYTES_SENT, | 457 NetLog::TYPE_UDP_BYTES_SENT, |
456 CreateNetLogUDPDataTranferCallback(result, bytes, address)); | 458 CreateNetLogUDPDataTranferCallback(result, bytes, address)); |
457 } | 459 } |
458 | 460 |
459 base::StatsCounter write_bytes("udp.write_bytes"); | 461 base::StatsCounter write_bytes("udp.write_bytes"); |
460 write_bytes.Add(result); | 462 write_bytes.Add(result); |
| 463 NetworkActivityMonitor::GetInstance()->IncrementBytesSent(result); |
461 } | 464 } |
462 | 465 |
463 int UDPSocketLibevent::InternalRecvFrom(IOBuffer* buf, int buf_len, | 466 int UDPSocketLibevent::InternalRecvFrom(IOBuffer* buf, int buf_len, |
464 IPEndPoint* address) { | 467 IPEndPoint* address) { |
465 int bytes_transferred; | 468 int bytes_transferred; |
466 int flags = 0; | 469 int flags = 0; |
467 | 470 |
468 SockaddrStorage storage; | 471 SockaddrStorage storage; |
469 | 472 |
470 bytes_transferred = | 473 bytes_transferred = |
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
767 return MapSystemError(errno); | 770 return MapSystemError(errno); |
768 | 771 |
769 return OK; | 772 return OK; |
770 } | 773 } |
771 | 774 |
772 void UDPSocketLibevent::DetachFromThread() { | 775 void UDPSocketLibevent::DetachFromThread() { |
773 base::NonThreadSafe::DetachFromThread(); | 776 base::NonThreadSafe::DetachFromThread(); |
774 } | 777 } |
775 | 778 |
776 } // namespace net | 779 } // namespace net |
OLD | NEW |