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, NULL); |
| } |
| read_buf_ = buf; |
| @@ -192,19 +193,16 @@ |
| 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 (nwrite >= 0) |
| + return LogWrite(nwrite, buf->data(), address); |
|
Sergey Ulanov
2011/10/17 17:48:13
Can we move this to InternalSendTo() so that it is
mmenke
2011/10/17 19:41:38
Done.
|
| if (errno != EAGAIN && errno != EWOULDBLOCK) |
| - return MapSystemError(errno); |
| + return LogWrite(MapSystemError(errno), NULL, NULL); |
| 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 +216,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 +297,44 @@ |
| } |
| } |
| +int UDPSocketLibevent::LogRead(int result, |
| + const char* bytes, |
| + socklen_t addr_len, |
| + const sockaddr* addr, |
| + IPEndPoint* address) const { |
| + DCHECK(result < 0 || addr_len > 0); |
| + DCHECK(result < 0 || addr != NULL); |
| + |
| + if (result >= 0 && address) { |
| + if (!address->FromSockAddr(addr, addr_len)) |
|
Sergey Ulanov
2011/10/17 17:48:13
I think that conversion of the address should stay
mmenke
2011/10/17 19:41:38
Done. If address is NULL and we're logging, I sti
|
| + result = ERR_FAILED; |
| + } |
| + |
| + if (result < 0) { |
| + net_log_.AddEventWithNetErrorCode(NetLog::TYPE_UDP_RECEIVE_ERROR, result); |
| + return result; |
| + } |
| + |
| + if (net_log_.IsLoggingAllEvents()) { |
| + // Get address for logging, if |address| is NULL. |
| + IPEndPoint address_on_stack; |
| + if (!address) { |
| + if (address_on_stack.FromSockAddr(addr, addr_len)) |
| + address = &address_on_stack; |
| + } |
| + net_log_.AddEvent( |
| + NetLog::TYPE_UDP_BYTES_RECEIVED, |
| + make_scoped_refptr( |
| + new UDPDataTransferNetLogParam(result, bytes, |
| + net_log_.IsLoggingBytes(), |
| + address))); |
| + } |
| + |
| + 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,13 +350,12 @@ |
| 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 { |
| + if (result < 0) |
| result = MapSystemError(errno); |
| - } |
| + if (result != ERR_IO_PENDING) |
| + result = LogWrite(result, write_buf_->data(), send_to_address_.get()); |
| + |
| if (result != ERR_IO_PENDING) { |
| write_buf_ = NULL; |
| write_buf_len_ = 0; |
| @@ -320,6 +365,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 +406,11 @@ |
| 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; |
| - } |
| } else { |
| result = MapSystemError(errno); |
| } |
| + if (result != ERR_IO_PENDING) |
| + LogRead(result, buf->data(), addr_len, addr, address); |
| return result; |
| } |