Chromium Code Reviews| Index: net/udp/udp_socket_libevent.cc |
| =================================================================== |
| --- net/udp/udp_socket_libevent.cc (revision 105801) |
| +++ 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); |
| @@ -158,7 +159,7 @@ |
| socket_, true, MessageLoopForIO::WATCH_READ, |
| &read_socket_watcher_, &read_watcher_)) { |
| PLOG(ERROR) << "WatchFileDescriptor failed on read"; |
| - return MapSystemError(errno); |
| + return LogRead(MapSystemError(errno), NULL, 0, NULL); |
|
Sergey Ulanov
2011/10/18 01:29:25
LogRead() always logs |result|. Maybe change this
mmenke
2011/10/18 15:16:58
Done.
|
| } |
| read_buf_ = buf; |
| @@ -191,20 +192,15 @@ |
| DCHECK(callback); // Synchronous operation not supported |
| DCHECK_GT(buf_len, 0); |
| - int nwrite = InternalSendTo(buf, buf_len, address); |
| - if (nwrite >= 0) { |
| - base::StatsCounter write_bytes("udp.write_bytes"); |
| - write_bytes.Add(nwrite); |
| - return nwrite; |
| - } |
| - if (errno != EAGAIN && errno != EWOULDBLOCK) |
| - return MapSystemError(errno); |
| + int result = InternalSendTo(buf, buf_len, address); |
| + if (result != ERR_IO_PENDING) |
| + return result; |
| 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); |
| + return LogWrite(MapSystemError(errno), NULL, NULL); |
| } |
| write_buf_ = buf; |
| @@ -218,6 +214,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 +295,34 @@ |
| } |
| } |
| +int UDPSocketLibevent::LogRead(int result, |
| + const char* bytes, |
| + socklen_t addr_len, |
| + const sockaddr* addr) const { |
| + if (result < 0) { |
| + net_log_.AddEventWithNetErrorCode(NetLog::TYPE_UDP_RECEIVE_ERROR, result); |
| + return result; |
| + } |
| + |
| + if (net_log_.IsLoggingAllEvents()) { |
| + DCHECK(addr_len > 0); |
| + DCHECK(addr); |
| + |
| + IPEndPoint address; |
| + bool is_address_valid = address.FromSockAddr(addr, addr_len); |
| + net_log_.AddEvent( |
| + NetLog::TYPE_UDP_BYTES_RECEIVED, |
| + make_scoped_refptr( |
| + new UDPDataTransferNetLogParam( |
| + result, bytes, net_log_.IsLoggingBytes(), |
| + is_address_valid ? &address : NULL))); |
| + } |
| + |
| + base::StatsCounter read_bytes("udp.read_bytes"); |
| + read_bytes.Add(result); |
| + return result; |
| +} |
| + |
| int UDPSocketLibevent::CreateSocket(const IPEndPoint& address) { |
| socket_ = socket(address.GetFamily(), SOCK_DGRAM, 0); |
| if (socket_ == kInvalidSocket) |
| @@ -304,12 +338,6 @@ |
| void UDPSocketLibevent::DidCompleteWrite() { |
| 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); |
| - } else { |
| - result = MapSystemError(errno); |
| - } |
| if (result != ERR_IO_PENDING) { |
| write_buf_ = NULL; |
| @@ -320,6 +348,28 @@ |
| } |
| } |
| +int UDPSocketLibevent::LogWrite(int result, |
| + const char* bytes, |
| + const IPEndPoint* address) const { |
| + if (result < 0) { |
| + net_log_.AddEventWithNetErrorCode(NetLog::TYPE_UDP_SEND_ERROR, result); |
| + return result; |
| + } |
| + |
| + if (net_log_.IsLoggingAllEvents()) { |
| + net_log_.AddEvent( |
| + NetLog::TYPE_UDP_BYTES_SENT, |
| + make_scoped_refptr( |
| + new UDPDataTransferNetLogParam(result, bytes, |
| + net_log_.IsLoggingBytes(), |
| + address))); |
| + } |
| + |
| + base::StatsCounter write_bytes("udp.write_bytes"); |
| + write_bytes.Add(result); |
| + return result; |
| +} |
| + |
| int UDPSocketLibevent::InternalRecvFrom(IOBuffer* buf, int buf_len, |
| IPEndPoint* address) { |
| int bytes_transferred; |
| @@ -339,15 +389,14 @@ |
| int result; |
| if (bytes_transferred >= 0) { |
| result = bytes_transferred; |
| - base::StatsCounter read_bytes("udp.read_bytes"); |
| - read_bytes.Add(bytes_transferred); |
| - if (address) { |
| - if (!address->FromSockAddr(addr, addr_len)) |
| - result = ERR_FAILED; |
| - } |
| + if (!address->FromSockAddr(addr, addr_len)) |
| + result = ERR_FAILED; |
| } else { |
| result = MapSystemError(errno); |
| } |
| + if (result != ERR_IO_PENDING) { |
|
Sergey Ulanov
2011/10/18 01:29:25
nit: remove braces
mmenke
2011/10/18 15:16:58
Done.
|
| + LogRead(result, buf->data(), addr_len, addr); |
| + } |
| return result; |
| } |
| @@ -362,15 +411,20 @@ |
| addr_len = 0; |
| } else { |
| if (!address->ToSockAddr(addr, &addr_len)) |
| - return ERR_FAILED; |
| + return LogWrite(ERR_FAILED, NULL, NULL); |
| } |
| - return HANDLE_EINTR(sendto(socket_, |
| - buf->data(), |
| - buf_len, |
| - 0, |
| - addr, |
| - addr_len)); |
| + int result = HANDLE_EINTR(sendto(socket_, |
| + buf->data(), |
| + buf_len, |
| + 0, |
| + addr, |
| + addr_len)); |
| + if (result < 0) |
| + result = MapSystemError(errno); |
| + if (result != ERR_IO_PENDING) |
| + LogWrite(result, buf->data(), address); |
| + return result; |
| } |
| int UDPSocketLibevent::DoBind(const IPEndPoint& address) { |