Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1042)

Unified Diff: net/socket/udp_socket_win.cc

Issue 2253753002: Always use NonBlocking IO for UDP sockets on Windows. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/socket/udp_socket_win.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/socket/udp_socket_win.cc
diff --git a/net/socket/udp_socket_win.cc b/net/socket/udp_socket_win.cc
index 83538c91c16e60a50924d5c98578fd408c3fda97..4a7c580edcc4623575ed45aa8b9e148efe5f1da0 100644
--- a/net/socket/udp_socket_win.cc
+++ b/net/socket/udp_socket_win.cc
@@ -41,131 +41,6 @@ const int kPortEnd = 65535;
namespace net {
-// This class encapsulates all the state that has to be preserved as long as
-// there is a network IO operation in progress. If the owner UDPSocketWin
-// is destroyed while an operation is in progress, the Core is detached and it
-// lives until the operation completes and the OS doesn't reference any resource
-// declared on this class anymore.
-class UDPSocketWin::Core : public base::RefCounted<Core> {
- public:
- explicit Core(UDPSocketWin* socket);
-
- // Start watching for the end of a read or write operation.
- void WatchForRead();
- void WatchForWrite();
-
- // The UDPSocketWin is going away.
- void Detach() { socket_ = NULL; }
-
- // The separate OVERLAPPED variables for asynchronous operation.
- OVERLAPPED read_overlapped_;
- OVERLAPPED write_overlapped_;
-
- // The buffers used in Read() and Write().
- scoped_refptr<IOBuffer> read_iobuffer_;
- scoped_refptr<IOBuffer> write_iobuffer_;
-
- // The address storage passed to WSARecvFrom().
- SockaddrStorage recv_addr_storage_;
-
- private:
- friend class base::RefCounted<Core>;
-
- class ReadDelegate : public base::win::ObjectWatcher::Delegate {
- public:
- explicit ReadDelegate(Core* core) : core_(core) {}
- ~ReadDelegate() override {}
-
- // base::ObjectWatcher::Delegate methods:
- void OnObjectSignaled(HANDLE object) override;
-
- private:
- Core* const core_;
- };
-
- class WriteDelegate : public base::win::ObjectWatcher::Delegate {
- public:
- explicit WriteDelegate(Core* core) : core_(core) {}
- ~WriteDelegate() override {}
-
- // base::ObjectWatcher::Delegate methods:
- void OnObjectSignaled(HANDLE object) override;
-
- private:
- Core* const core_;
- };
-
- ~Core();
-
- // The socket that created this object.
- UDPSocketWin* socket_;
-
- // |reader_| handles the signals from |read_watcher_|.
- ReadDelegate reader_;
- // |writer_| handles the signals from |write_watcher_|.
- WriteDelegate writer_;
-
- // |read_watcher_| watches for events from Read().
- base::win::ObjectWatcher read_watcher_;
- // |write_watcher_| watches for events from Write();
- base::win::ObjectWatcher write_watcher_;
-
- DISALLOW_COPY_AND_ASSIGN(Core);
-};
-
-UDPSocketWin::Core::Core(UDPSocketWin* socket)
- : socket_(socket),
- reader_(this),
- writer_(this) {
- memset(&read_overlapped_, 0, sizeof(read_overlapped_));
- memset(&write_overlapped_, 0, sizeof(write_overlapped_));
-
- read_overlapped_.hEvent = WSACreateEvent();
- write_overlapped_.hEvent = WSACreateEvent();
-}
-
-UDPSocketWin::Core::~Core() {
- // Make sure the message loop is not watching this object anymore.
- read_watcher_.StopWatching();
- write_watcher_.StopWatching();
-
- WSACloseEvent(read_overlapped_.hEvent);
- memset(&read_overlapped_, 0xaf, sizeof(read_overlapped_));
- WSACloseEvent(write_overlapped_.hEvent);
- memset(&write_overlapped_, 0xaf, sizeof(write_overlapped_));
-}
-
-void UDPSocketWin::Core::WatchForRead() {
- // We grab an extra reference because there is an IO operation in progress.
- // Balanced in ReadDelegate::OnObjectSignaled().
- AddRef();
- read_watcher_.StartWatchingOnce(read_overlapped_.hEvent, &reader_);
-}
-
-void UDPSocketWin::Core::WatchForWrite() {
- // We grab an extra reference because there is an IO operation in progress.
- // Balanced in WriteDelegate::OnObjectSignaled().
- AddRef();
- write_watcher_.StartWatchingOnce(write_overlapped_.hEvent, &writer_);
-}
-
-void UDPSocketWin::Core::ReadDelegate::OnObjectSignaled(HANDLE object) {
- DCHECK_EQ(object, core_->read_overlapped_.hEvent);
- if (core_->socket_)
- core_->socket_->DidCompleteRead();
-
- core_->Release();
-}
-
-void UDPSocketWin::Core::WriteDelegate::OnObjectSignaled(HANDLE object) {
- DCHECK_EQ(object, core_->write_overlapped_.hEvent);
- if (core_->socket_)
- core_->socket_->DidCompleteWrite();
-
- core_->Release();
-}
-//-----------------------------------------------------------------------------
-
QwaveAPI::QwaveAPI() : qwave_supported_(false) {
HMODULE qwave = LoadLibrary(L"qwave.dll");
if (!qwave)
@@ -255,7 +130,6 @@ UDPSocketWin::UDPSocketWin(DatagramSocket::BindType bind_type,
multicast_time_to_live_(1),
bind_type_(bind_type),
rand_int_cb_(rand_int_cb),
- use_non_blocking_io_(false),
read_iobuffer_len_(0),
write_iobuffer_len_(0),
recv_from_address_(NULL),
@@ -282,12 +156,8 @@ int UDPSocketWin::Open(AddressFamily address_family) {
socket_ = CreatePlatformSocket(addr_family_, SOCK_DGRAM, IPPROTO_UDP);
if (socket_ == INVALID_SOCKET)
return MapSystemError(WSAGetLastError());
- if (!use_non_blocking_io_) {
- core_ = new Core(this);
- } else {
- read_write_event_.Set(WSACreateEvent());
- WSAEventSelect(socket_, read_write_event_.Get(), FD_READ | FD_WRITE);
- }
+ read_write_event_.Set(WSACreateEvent());
+ WSAEventSelect(socket_, read_write_event_.Get(), FD_READ | FD_WRITE);
return OK;
}
@@ -316,11 +186,6 @@ void UDPSocketWin::Close() {
read_write_watcher_.StopWatching();
read_write_event_.Close();
-
- if (core_) {
- core_->Detach();
- core_ = NULL;
- }
}
int UDPSocketWin::GetPeerAddress(IPEndPoint* address) const {
@@ -386,8 +251,7 @@ int UDPSocketWin::RecvFrom(IOBuffer* buf,
DCHECK(!callback.is_null()); // Synchronous operation not supported.
DCHECK_GT(buf_len, 0);
- int nread = core_ ? InternalRecvFromOverlapped(buf, buf_len, address)
- : InternalRecvFromNonBlocking(buf, buf_len, address);
+ int nread = InternalRecvFromNonBlocking(buf, buf_len, address);
if (nread != ERR_IO_PENDING)
return nread;
@@ -420,8 +284,7 @@ int UDPSocketWin::SendToOrWrite(IOBuffer* buf,
DCHECK_GT(buf_len, 0);
DCHECK(!send_to_address_.get());
- int nwrite = core_ ? InternalSendToOverlapped(buf, buf_len, address)
- : InternalSendToNonBlocking(buf, buf_len, address);
+ int nwrite = InternalSendToNonBlocking(buf, buf_len, address);
if (nwrite != ERR_IO_PENDING)
return nwrite;
@@ -596,44 +459,6 @@ void UDPSocketWin::DoWriteCallback(int rv) {
c.Run(rv);
}
-void UDPSocketWin::DidCompleteRead() {
- DWORD num_bytes, flags;
- BOOL ok = WSAGetOverlappedResult(socket_, &core_->read_overlapped_,
- &num_bytes, FALSE, &flags);
- WSAResetEvent(core_->read_overlapped_.hEvent);
- int result = ok ? num_bytes : MapSystemError(WSAGetLastError());
- // Convert address.
- IPEndPoint address;
- IPEndPoint* address_to_log = NULL;
- if (result >= 0) {
- if (address.FromSockAddr(core_->recv_addr_storage_.addr,
- core_->recv_addr_storage_.addr_len)) {
- if (recv_from_address_)
- *recv_from_address_ = address;
- address_to_log = &address;
- } else {
- result = ERR_ADDRESS_INVALID;
- }
- }
- LogRead(result, core_->read_iobuffer_->data(), address_to_log);
- core_->read_iobuffer_ = NULL;
- recv_from_address_ = NULL;
- DoReadCallback(result);
-}
-
-void UDPSocketWin::DidCompleteWrite() {
- DWORD num_bytes, flags;
- BOOL ok = WSAGetOverlappedResult(socket_, &core_->write_overlapped_,
- &num_bytes, FALSE, &flags);
- WSAResetEvent(core_->write_overlapped_.hEvent);
- int result = ok ? num_bytes : MapSystemError(WSAGetLastError());
- LogWrite(result, core_->write_iobuffer_->data(), send_to_address_.get());
-
- send_to_address_.reset();
- core_->write_iobuffer_ = NULL;
- DoWriteCallback(result);
-}
-
void UDPSocketWin::OnObjectSignaled(HANDLE object) {
DCHECK(object == read_write_event_.Get());
WSANETWORKEVENTS network_events;
@@ -735,102 +560,6 @@ void UDPSocketWin::LogWrite(int result,
NetworkActivityMonitor::GetInstance()->IncrementBytesSent(result);
}
-int UDPSocketWin::InternalRecvFromOverlapped(IOBuffer* buf,
- int buf_len,
- IPEndPoint* address) {
- DCHECK(!core_->read_iobuffer_.get());
- SockaddrStorage& storage = core_->recv_addr_storage_;
- storage.addr_len = sizeof(storage.addr_storage);
-
- WSABUF read_buffer;
- read_buffer.buf = buf->data();
- read_buffer.len = buf_len;
-
- DWORD flags = 0;
- DWORD num;
- CHECK_NE(INVALID_SOCKET, socket_);
- AssertEventNotSignaled(core_->read_overlapped_.hEvent);
- int rv = WSARecvFrom(socket_, &read_buffer, 1, &num, &flags, storage.addr,
- &storage.addr_len, &core_->read_overlapped_, NULL);
- if (rv == 0) {
- if (ResetEventIfSignaled(core_->read_overlapped_.hEvent)) {
- int result = num;
- // Convert address.
- IPEndPoint address_storage;
- IPEndPoint* address_to_log = NULL;
- if (result >= 0) {
- if (address_storage.FromSockAddr(core_->recv_addr_storage_.addr,
- core_->recv_addr_storage_.addr_len)) {
- if (address)
- *address = address_storage;
- address_to_log = &address_storage;
- } else {
- result = ERR_ADDRESS_INVALID;
- }
- }
- LogRead(result, buf->data(), address_to_log);
- return result;
- }
- } else {
- int os_error = WSAGetLastError();
- if (os_error != WSA_IO_PENDING) {
- int result = MapSystemError(os_error);
- LogRead(result, NULL, NULL);
- return result;
- }
- }
- core_->WatchForRead();
- core_->read_iobuffer_ = buf;
- return ERR_IO_PENDING;
-}
-
-int UDPSocketWin::InternalSendToOverlapped(IOBuffer* buf,
- int buf_len,
- const IPEndPoint* address) {
- DCHECK(!core_->write_iobuffer_.get());
- SockaddrStorage storage;
- struct sockaddr* addr = storage.addr;
- // Convert address.
- if (!address) {
- addr = NULL;
- storage.addr_len = 0;
- } else {
- if (!address->ToSockAddr(addr, &storage.addr_len)) {
- int result = ERR_ADDRESS_INVALID;
- LogWrite(result, NULL, NULL);
- return result;
- }
- }
-
- WSABUF write_buffer;
- write_buffer.buf = buf->data();
- write_buffer.len = buf_len;
-
- DWORD flags = 0;
- DWORD num;
- AssertEventNotSignaled(core_->write_overlapped_.hEvent);
- int rv = WSASendTo(socket_, &write_buffer, 1, &num, flags,
- addr, storage.addr_len, &core_->write_overlapped_, NULL);
- if (rv == 0) {
- if (ResetEventIfSignaled(core_->write_overlapped_.hEvent)) {
- int result = num;
- LogWrite(result, buf->data(), address);
- return result;
- }
- } else {
- int os_error = WSAGetLastError();
- if (os_error != WSA_IO_PENDING) {
- int result = MapSystemError(os_error);
- LogWrite(result, NULL, NULL);
- return result;
- }
- }
-
- core_->WatchForWrite();
- core_->write_iobuffer_ = buf;
- return ERR_IO_PENDING;
-}
-
int UDPSocketWin::InternalRecvFromNonBlocking(IOBuffer* buf,
int buf_len,
IPEndPoint* address) {
@@ -1193,9 +922,4 @@ void UDPSocketWin::DetachFromThread() {
base::NonThreadSafe::DetachFromThread();
}
-void UDPSocketWin::UseNonBlockingIO() {
- DCHECK(!core_);
- use_non_blocking_io_ = true;
-}
-
} // namespace net
« no previous file with comments | « net/socket/udp_socket_win.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698