Chromium Code Reviews| Index: net/udp/udp_socket_libevent.cc |
| =================================================================== |
| --- net/udp/udp_socket_libevent.cc (revision 105148) |
| +++ net/udp/udp_socket_libevent.cc (working copy) |
| @@ -19,6 +19,7 @@ |
| #include "net/base/net_errors.h" |
| #include "net/base/net_log.h" |
| #include "net/base/net_util.h" |
| +#include "net/udp/udp_data_transfer_param.h" |
| #if defined(OS_POSIX) |
| #include <netinet/in.h> |
| #endif |
| @@ -48,7 +49,7 @@ |
| write_buf_len_(0), |
| read_callback_(NULL), |
| write_callback_(NULL), |
| - net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) { |
| + net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_UDP_SOCKET)) { |
| scoped_refptr<NetLog::EventParameters> params; |
| if (source.is_valid()) |
| params = new NetLogSourceParameter("source_dependency", source); |
| @@ -151,14 +152,21 @@ |
| DCHECK_GT(buf_len, 0); |
| int nread = InternalRecvFrom(buf, buf_len, address); |
| - if (nread != ERR_IO_PENDING) |
| + if (nread != ERR_IO_PENDING) { |
| + if (nread < 0) { |
|
Sergey Ulanov
2011/10/14 20:15:47
nit: remove braces here - they are not used in thi
mmenke
2011/10/17 17:35:20
Done.
|
| + net_log_.AddEventWithNetErrorCode(NetLog::TYPE_UDP_RECEIVE_ERROR, nread); |
| + } |
| return nread; |
| + } |
| if (!MessageLoopForIO::current()->WatchFileDescriptor( |
| socket_, true, MessageLoopForIO::WATCH_READ, |
| &read_socket_watcher_, &read_watcher_)) { |
| PLOG(ERROR) << "WatchFileDescriptor failed on read"; |
| - return MapSystemError(errno); |
| + int net_error = MapSystemError(errno); |
| + net_log_.AddEventWithNetErrorCode(NetLog::TYPE_UDP_RECEIVE_ERROR, |
| + net_error); |
| + return net_error; |
| } |
| read_buf_ = buf; |
| @@ -193,18 +201,24 @@ |
| int nwrite = InternalSendTo(buf, buf_len, address); |
| if (nwrite >= 0) { |
| - base::StatsCounter write_bytes("udp.write_bytes"); |
| - write_bytes.Add(nwrite); |
| + ProcessSuccessfulWrite(nwrite, buf->data(), address); |
| return nwrite; |
| } |
| - if (errno != EAGAIN && errno != EWOULDBLOCK) |
| - return MapSystemError(errno); |
| + if (errno != EAGAIN && errno != EWOULDBLOCK) { |
| + int net_error = MapSystemError(errno); |
| + net_log_.AddEventWithNetErrorCode(NetLog::TYPE_UDP_SEND_ERROR, |
|
Sergey Ulanov
2011/10/14 20:15:47
optional nit: This code is duplicated in DidComple
mmenke
2011/10/17 17:35:20
Done. I really like this idea. Results in a lot
|
| + net_error); |
| + return net_error; |
| + } |
| if (!MessageLoopForIO::current()->WatchFileDescriptor( |
| socket_, true, MessageLoopForIO::WATCH_WRITE, |
| &write_socket_watcher_, &write_watcher_)) { |
| DVLOG(1) << "WatchFileDescriptor failed on write, errno " << errno; |
| - return MapSystemError(errno); |
| + int net_error = MapSystemError(errno); |
| + net_log_.AddEventWithNetErrorCode(NetLog::TYPE_UDP_SEND_ERROR, |
| + net_error); |
| + return net_error; |
| } |
| write_buf_ = buf; |
| @@ -218,6 +232,16 @@ |
| } |
| int UDPSocketLibevent::Connect(const IPEndPoint& address) { |
| + net_log_.BeginEvent( |
| + NetLog::TYPE_UDP_CONNECT, |
| + make_scoped_refptr(new NetLogStringParameter("address", |
| + address.ToString()))); |
| + int rv = InternalConnect(address); |
| + net_log_.EndEventWithNetErrorCode(NetLog::TYPE_UDP_CONNECT, rv); |
| + return rv; |
| +} |
| + |
| +int UDPSocketLibevent::InternalConnect(const IPEndPoint& address) { |
| DCHECK(!is_connected()); |
| DCHECK(!remote_address_.get()); |
| int rv = CreateSocket(address); |
| @@ -289,6 +313,22 @@ |
| } |
| } |
| +void UDPSocketLibevent::ProcessSuccessfulRead(int num_bytes, |
| + const char* bytes, |
| + const IPEndPoint* address) const { |
| + base::StatsCounter read_bytes("udp.read_bytes"); |
| + read_bytes.Add(num_bytes); |
| + |
| + if (net_log_.IsLoggingAllEvents()) { |
| + net_log_.AddEvent( |
| + NetLog::TYPE_UDP_BYTES_RECEIVED, |
| + make_scoped_refptr( |
| + new UDPDataTransferNetLogParam(num_bytes, bytes, |
| + net_log_.IsLoggingBytes(), |
| + address))); |
| + } |
| +} |
| + |
| int UDPSocketLibevent::CreateSocket(const IPEndPoint& address) { |
| socket_ = socket(address.GetFamily(), SOCK_DGRAM, 0); |
| if (socket_ == kInvalidSocket) |
| @@ -305,10 +345,11 @@ |
| int result = InternalSendTo(write_buf_, write_buf_len_, |
| send_to_address_.get()); |
| if (result >= 0) { |
| - base::StatsCounter write_bytes("udp.write_bytes"); |
| - write_bytes.Add(result); |
| + ProcessSuccessfulWrite(result, write_buf_->data(), send_to_address_.get()); |
| } else { |
| result = MapSystemError(errno); |
| + if (result != ERR_IO_PENDING) |
| + net_log_.AddEventWithNetErrorCode(NetLog::TYPE_UDP_SEND_ERROR, result); |
| } |
| if (result != ERR_IO_PENDING) { |
| @@ -320,6 +361,22 @@ |
| } |
| } |
| +void UDPSocketLibevent::ProcessSuccessfulWrite( |
| + int num_bytes, |
| + const char* bytes, |
| + const IPEndPoint* address) const { |
| + base::StatsCounter write_bytes("udp.write_bytes"); |
| + write_bytes.Add(num_bytes); |
| + if (net_log_.IsLoggingAllEvents()) { |
| + net_log_.AddEvent( |
| + NetLog::TYPE_UDP_BYTES_SENT, |
| + make_scoped_refptr( |
| + new UDPDataTransferNetLogParam(num_bytes, bytes, |
| + net_log_.IsLoggingBytes(), |
| + address))); |
| + } |
| +} |
| + |
| int UDPSocketLibevent::InternalRecvFrom(IOBuffer* buf, int buf_len, |
| IPEndPoint* address) { |
| int bytes_transferred; |
| @@ -339,8 +396,7 @@ |
| int result; |
| if (bytes_transferred >= 0) { |
| result = bytes_transferred; |
| - base::StatsCounter read_bytes("udp.read_bytes"); |
| - read_bytes.Add(bytes_transferred); |
| + ProcessSuccessfulRead(bytes_transferred, buf->data(), address); |
| if (address) { |
| if (!address->FromSockAddr(addr, addr_len)) |
| result = ERR_FAILED; |