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

Unified Diff: runtime/bin/socket_base_win.cc

Issue 2797993005: Re-land socket refactor with fixes for Windows. (Closed)
Patch Set: Rebased + reverted original revert 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 | « runtime/bin/socket_base_win.h ('k') | runtime/bin/socket_fuchsia.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/bin/socket_base_win.cc
diff --git a/runtime/bin/socket_win.cc b/runtime/bin/socket_base_win.cc
similarity index 55%
copy from runtime/bin/socket_win.cc
copy to runtime/bin/socket_base_win.cc
index 3b0efe507e10a728d82de5da4e54cf76efcb7d58..27448a3838637563d83ec396466255c6f7f2bfc3 100644
--- a/runtime/bin/socket_win.cc
+++ b/runtime/bin/socket_base_win.cc
@@ -7,14 +7,14 @@
#include "platform/globals.h"
#if defined(HOST_OS_WINDOWS)
-#include "bin/socket.h"
-#include "bin/socket_win.h"
+#include "bin/socket_base.h"
#include "bin/builtin.h"
#include "bin/eventhandler.h"
#include "bin/file.h"
#include "bin/lockers.h"
#include "bin/log.h"
+#include "bin/socket_base_win.h"
#include "bin/thread.h"
#include "bin/utils.h"
#include "bin/utils_win.h"
@@ -28,7 +28,8 @@ SocketAddress::SocketAddress(struct sockaddr* sockaddr) {
// Clear the port before calling WSAAddressToString as WSAAddressToString
// includes the port in the formatted string.
- int err = Socket::FormatNumericAddress(*raw, as_string_, INET6_ADDRSTRLEN);
+ int err =
+ SocketBase::FormatNumericAddress(*raw, as_string_, INET6_ADDRSTRLEN);
if (err != 0) {
as_string_[0] = 0;
@@ -38,34 +39,10 @@ SocketAddress::SocketAddress(struct sockaddr* sockaddr) {
}
-bool Socket::FormatNumericAddress(const RawAddr& addr, char* address, int len) {
- socklen_t salen = SocketAddress::GetAddrLength(addr);
- DWORD l = len;
- RawAddr& raw = const_cast<RawAddr&>(addr);
- return WSAAddressToStringA(&raw.addr, salen, NULL, address, &l) != 0;
-}
-
-
-Socket::Socket(intptr_t fd) : ReferenceCounted(), fd_(fd), port_(ILLEGAL_PORT) {
- ASSERT(fd_ != kClosedFd);
- Handle* handle = reinterpret_cast<Handle*>(fd_);
- ASSERT(handle != NULL);
-}
-
-
-void Socket::SetClosedFd() {
- ASSERT(fd_ != kClosedFd);
- Handle* handle = reinterpret_cast<Handle*>(fd_);
- ASSERT(handle != NULL);
- handle->Release();
- fd_ = kClosedFd;
-}
-
-
static Mutex* init_mutex = new Mutex();
static bool socket_initialized = false;
-bool Socket::Initialize() {
+bool SocketBase::Initialize() {
MutexLocker lock(init_mutex);
if (socket_initialized) {
return true;
@@ -83,38 +60,50 @@ bool Socket::Initialize() {
}
-intptr_t Socket::Available(intptr_t fd) {
+bool SocketBase::FormatNumericAddress(const RawAddr& addr,
+ char* address,
+ int len) {
+ socklen_t salen = SocketAddress::GetAddrLength(addr);
+ DWORD l = len;
+ RawAddr& raw = const_cast<RawAddr&>(addr);
+ return WSAAddressToStringA(&raw.addr, salen, NULL, address, &l) != 0;
+}
+
+
+intptr_t SocketBase::Available(intptr_t fd) {
ClientSocket* client_socket = reinterpret_cast<ClientSocket*>(fd);
return client_socket->Available();
}
-intptr_t Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) {
+intptr_t SocketBase::Read(intptr_t fd, void* buffer, intptr_t num_bytes) {
Handle* handle = reinterpret_cast<Handle*>(fd);
return handle->Read(buffer, num_bytes);
}
-intptr_t Socket::RecvFrom(intptr_t fd,
- void* buffer,
- intptr_t num_bytes,
- RawAddr* addr) {
+intptr_t SocketBase::RecvFrom(intptr_t fd,
+ void* buffer,
+ intptr_t num_bytes,
+ RawAddr* addr) {
Handle* handle = reinterpret_cast<Handle*>(fd);
socklen_t addr_len = sizeof(addr->ss);
return handle->RecvFrom(buffer, num_bytes, &addr->addr, addr_len);
}
-intptr_t Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) {
+intptr_t SocketBase::Write(intptr_t fd,
+ const void* buffer,
+ intptr_t num_bytes) {
Handle* handle = reinterpret_cast<Handle*>(fd);
return handle->Write(buffer, num_bytes);
}
-intptr_t Socket::SendTo(intptr_t fd,
- const void* buffer,
- intptr_t num_bytes,
- const RawAddr& addr) {
+intptr_t SocketBase::SendTo(intptr_t fd,
+ const void* buffer,
+ intptr_t num_bytes,
+ const RawAddr& addr) {
Handle* handle = reinterpret_cast<Handle*>(fd);
RawAddr& raw = const_cast<RawAddr&>(addr);
return handle->SendTo(buffer, num_bytes, &raw.addr,
@@ -122,7 +111,7 @@ intptr_t Socket::SendTo(intptr_t fd,
}
-intptr_t Socket::GetPort(intptr_t fd) {
+intptr_t SocketBase::GetPort(intptr_t fd) {
ASSERT(reinterpret_cast<Handle*>(fd)->is_socket());
SocketHandle* socket_handle = reinterpret_cast<SocketHandle*>(fd);
RawAddr raw;
@@ -134,7 +123,7 @@ intptr_t Socket::GetPort(intptr_t fd) {
}
-SocketAddress* Socket::GetRemotePeer(intptr_t fd, intptr_t* port) {
+SocketAddress* SocketBase::GetRemotePeer(intptr_t fd, intptr_t* port) {
ASSERT(reinterpret_cast<Handle*>(fd)->is_socket());
SocketHandle* socket_handle = reinterpret_cast<SocketHandle*>(fd);
RawAddr raw;
@@ -150,123 +139,19 @@ SocketAddress* Socket::GetRemotePeer(intptr_t fd, intptr_t* port) {
}
-static intptr_t Create(const RawAddr& addr) {
- SOCKET s = socket(addr.ss.ss_family, SOCK_STREAM, 0);
- if (s == INVALID_SOCKET) {
- return -1;
- }
-
- linger l;
- l.l_onoff = 1;
- l.l_linger = 10;
- int status = setsockopt(s, SOL_SOCKET, SO_LINGER, reinterpret_cast<char*>(&l),
- sizeof(l));
- if (status != NO_ERROR) {
- FATAL("Failed setting SO_LINGER on socket");
- }
-
- ClientSocket* client_socket = new ClientSocket(s);
- return reinterpret_cast<intptr_t>(client_socket);
-}
-
-
-static intptr_t Connect(intptr_t fd,
- const RawAddr& addr,
- const RawAddr& bind_addr) {
- ASSERT(reinterpret_cast<Handle*>(fd)->is_client_socket());
- ClientSocket* handle = reinterpret_cast<ClientSocket*>(fd);
- SOCKET s = handle->socket();
-
- int status =
- bind(s, &bind_addr.addr, SocketAddress::GetAddrLength(bind_addr));
- if (status != NO_ERROR) {
- int rc = WSAGetLastError();
- handle->mark_closed(); // Destructor asserts that socket is marked closed.
- handle->Release();
- closesocket(s);
- SetLastError(rc);
- return -1;
- }
-
- LPFN_CONNECTEX connectEx = NULL;
- GUID guid_connect_ex = WSAID_CONNECTEX;
- DWORD bytes;
- status = WSAIoctl(s, SIO_GET_EXTENSION_FUNCTION_POINTER, &guid_connect_ex,
- sizeof(guid_connect_ex), &connectEx, sizeof(connectEx),
- &bytes, NULL, NULL);
- DWORD rc;
- if (status != SOCKET_ERROR) {
- handle->EnsureInitialized(EventHandler::delegate());
-
- OverlappedBuffer* overlapped = OverlappedBuffer::AllocateConnectBuffer();
-
- status = connectEx(s, &addr.addr, SocketAddress::GetAddrLength(addr), NULL,
- 0, NULL, overlapped->GetCleanOverlapped());
-
-
- if (status == TRUE) {
- handle->ConnectComplete(overlapped);
- return fd;
- } else if (WSAGetLastError() == ERROR_IO_PENDING) {
- return fd;
- }
- rc = WSAGetLastError();
- // Cleanup in case of error.
- OverlappedBuffer::DisposeBuffer(overlapped);
- handle->Release();
- } else {
- rc = WSAGetLastError();
- }
- handle->Close();
- handle->Release();
- SetLastError(rc);
- return -1;
-}
-
-
-intptr_t Socket::CreateConnect(const RawAddr& addr) {
- intptr_t fd = Create(addr);
- if (fd < 0) {
- return fd;
- }
-
- RawAddr bind_addr;
- memset(&bind_addr, 0, sizeof(bind_addr));
- bind_addr.ss.ss_family = addr.ss.ss_family;
- if (addr.ss.ss_family == AF_INET) {
- bind_addr.in.sin_addr.s_addr = INADDR_ANY;
- } else {
- bind_addr.in6.sin6_addr = in6addr_any;
- }
-
- return Connect(fd, addr, bind_addr);
-}
-
-
-intptr_t Socket::CreateBindConnect(const RawAddr& addr,
- const RawAddr& source_addr) {
- intptr_t fd = Create(addr);
- if (fd < 0) {
- return fd;
- }
-
- return Connect(fd, addr, source_addr);
-}
-
-
-bool Socket::IsBindError(intptr_t error_number) {
+bool SocketBase::IsBindError(intptr_t error_number) {
return error_number == WSAEADDRINUSE || error_number == WSAEADDRNOTAVAIL ||
error_number == WSAEINVAL;
}
-void Socket::GetError(intptr_t fd, OSError* os_error) {
+void SocketBase::GetError(intptr_t fd, OSError* os_error) {
Handle* handle = reinterpret_cast<Handle*>(fd);
os_error->SetCodeAndMessage(OSError::kSystem, handle->last_error());
}
-int Socket::GetType(intptr_t fd) {
+int SocketBase::GetType(intptr_t fd) {
Handle* handle = reinterpret_cast<Handle*>(fd);
switch (GetFileType(handle->handle())) {
case FILE_TYPE_CHAR:
@@ -281,7 +166,7 @@ int Socket::GetType(intptr_t fd) {
}
-intptr_t Socket::GetStdioHandle(intptr_t num) {
+intptr_t SocketBase::GetStdioHandle(intptr_t num) {
if (num != 0) {
return -1;
}
@@ -289,28 +174,16 @@ intptr_t Socket::GetStdioHandle(intptr_t num) {
if (handle == INVALID_HANDLE_VALUE) {
return -1;
}
- StdHandle* std_handle = StdHandle::Stdin(handle);
- std_handle->Retain();
+ StdHandle* std_handle = new StdHandle(handle);
std_handle->MarkDoesNotSupportOverlappedIO();
std_handle->EnsureInitialized(EventHandler::delegate());
return reinterpret_cast<intptr_t>(std_handle);
}
-intptr_t ServerSocket::Accept(intptr_t fd) {
- ListenSocket* listen_socket = reinterpret_cast<ListenSocket*>(fd);
- ClientSocket* client_socket = listen_socket->Accept();
- if (client_socket != NULL) {
- return reinterpret_cast<intptr_t>(client_socket);
- } else {
- return -1;
- }
-}
-
-
-AddressList<SocketAddress>* Socket::LookupAddress(const char* host,
- int type,
- OSError** os_error) {
+AddressList<SocketAddress>* SocketBase::LookupAddress(const char* host,
+ int type,
+ OSError** os_error) {
Initialize();
// Perform a name lookup for a host name.
@@ -354,10 +227,10 @@ AddressList<SocketAddress>* Socket::LookupAddress(const char* host,
}
-bool Socket::ReverseLookup(const RawAddr& addr,
- char* host,
- intptr_t host_len,
- OSError** os_error) {
+bool SocketBase::ReverseLookup(const RawAddr& addr,
+ char* host,
+ intptr_t host_len,
+ OSError** os_error) {
ASSERT(host_len >= NI_MAXHOST);
int status = getnameinfo(&addr.addr, SocketAddress::GetAddrLength(addr), host,
host_len, NULL, 0, NI_NAMEREQD);
@@ -372,7 +245,7 @@ bool Socket::ReverseLookup(const RawAddr& addr,
}
-bool Socket::ParseAddress(int type, const char* address, RawAddr* addr) {
+bool SocketBase::ParseAddress(int type, const char* address, RawAddr* addr) {
int result;
Utf8ToWideScope system_address(address);
if (type == SocketAddress::TYPE_IPV4) {
@@ -385,45 +258,12 @@ bool Socket::ParseAddress(int type, const char* address, RawAddr* addr) {
}
-intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) {
- SOCKET s = socket(addr.ss.ss_family, SOCK_DGRAM, IPPROTO_UDP);
- if (s == INVALID_SOCKET) {
- return -1;
- }
-
- int status;
- if (reuseAddress) {
- BOOL optval = true;
- status = setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
- reinterpret_cast<const char*>(&optval), sizeof(optval));
- if (status == SOCKET_ERROR) {
- DWORD rc = WSAGetLastError();
- closesocket(s);
- SetLastError(rc);
- return -1;
- }
- }
-
- status = bind(s, &addr.addr, SocketAddress::GetAddrLength(addr));
- if (status == SOCKET_ERROR) {
- DWORD rc = WSAGetLastError();
- closesocket(s);
- SetLastError(rc);
- return -1;
- }
-
- DatagramSocket* datagram_socket = new DatagramSocket(s);
- datagram_socket->EnsureInitialized(EventHandler::delegate());
- return reinterpret_cast<intptr_t>(datagram_socket);
-}
-
-
-bool Socket::ListInterfacesSupported() {
+bool SocketBase::ListInterfacesSupported() {
return true;
}
-AddressList<InterfaceSocketAddress>* Socket::ListInterfaces(
+AddressList<InterfaceSocketAddress>* SocketBase::ListInterfaces(
int type,
OSError** os_error) {
Initialize();
@@ -473,95 +313,13 @@ AddressList<InterfaceSocketAddress>* Socket::ListInterfaces(
}
-intptr_t ServerSocket::CreateBindListen(const RawAddr& addr,
- intptr_t backlog,
- bool v6_only) {
- SOCKET s = socket(addr.ss.ss_family, SOCK_STREAM, IPPROTO_TCP);
- if (s == INVALID_SOCKET) {
- return -1;
- }
-
- BOOL optval = true;
- int status =
- setsockopt(s, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
- reinterpret_cast<const char*>(&optval), sizeof(optval));
- if (status == SOCKET_ERROR) {
- DWORD rc = WSAGetLastError();
- closesocket(s);
- SetLastError(rc);
- return -1;
- }
-
- if (addr.ss.ss_family == AF_INET6) {
- optval = v6_only;
- setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
- reinterpret_cast<const char*>(&optval), sizeof(optval));
- }
-
- status = bind(s, &addr.addr, SocketAddress::GetAddrLength(addr));
- if (status == SOCKET_ERROR) {
- DWORD rc = WSAGetLastError();
- closesocket(s);
- SetLastError(rc);
- return -1;
- }
-
- ListenSocket* listen_socket = new ListenSocket(s);
-
- // Test for invalid socket port 65535 (some browsers disallow it).
- if ((SocketAddress::GetAddrPort(addr) == 0) &&
- (Socket::GetPort(reinterpret_cast<intptr_t>(listen_socket)) == 65535)) {
- // Don't close fd until we have created new. By doing that we ensure another
- // port.
- intptr_t new_s = CreateBindListen(addr, backlog, v6_only);
- DWORD rc = WSAGetLastError();
- closesocket(s);
- listen_socket->Release();
- SetLastError(rc);
- return new_s;
- }
-
- status = listen(s, backlog > 0 ? backlog : SOMAXCONN);
- if (status == SOCKET_ERROR) {
- DWORD rc = WSAGetLastError();
- closesocket(s);
- listen_socket->Release();
- SetLastError(rc);
- return -1;
- }
-
- return reinterpret_cast<intptr_t>(listen_socket);
-}
-
-
-bool ServerSocket::StartAccept(intptr_t fd) {
- ListenSocket* listen_socket = reinterpret_cast<ListenSocket*>(fd);
- listen_socket->EnsureInitialized(EventHandler::delegate());
- // Always keep 5 outstanding accepts going, to enhance performance.
- for (int i = 0; i < 5; i++) {
- if (!listen_socket->IssueAccept()) {
- DWORD rc = WSAGetLastError();
- listen_socket->Close();
- if (!listen_socket->HasPendingAccept()) {
- // Delete socket now, if there are no pending accepts. Otherwise,
- // the event-handler will take care of deleting it.
- listen_socket->Release();
- }
- SetLastError(rc);
- return false;
- }
- }
- return true;
-}
-
-
-void Socket::Close(intptr_t fd) {
+void SocketBase::Close(intptr_t fd) {
ClientSocket* client_socket = reinterpret_cast<ClientSocket*>(fd);
client_socket->Close();
}
-bool Socket::GetNoDelay(intptr_t fd, bool* enabled) {
+bool SocketBase::GetNoDelay(intptr_t fd, bool* enabled) {
SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd);
int on;
socklen_t len = sizeof(on);
@@ -574,7 +332,7 @@ bool Socket::GetNoDelay(intptr_t fd, bool* enabled) {
}
-bool Socket::SetNoDelay(intptr_t fd, bool enabled) {
+bool SocketBase::SetNoDelay(intptr_t fd, bool enabled) {
SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd);
int on = enabled ? 1 : 0;
return setsockopt(handle->socket(), IPPROTO_TCP, TCP_NODELAY,
@@ -582,7 +340,9 @@ bool Socket::SetNoDelay(intptr_t fd, bool enabled) {
}
-bool Socket::GetMulticastLoop(intptr_t fd, intptr_t protocol, bool* enabled) {
+bool SocketBase::GetMulticastLoop(intptr_t fd,
+ intptr_t protocol,
+ bool* enabled) {
SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd);
uint8_t on;
socklen_t len = sizeof(on);
@@ -598,7 +358,9 @@ bool Socket::GetMulticastLoop(intptr_t fd, intptr_t protocol, bool* enabled) {
}
-bool Socket::SetMulticastLoop(intptr_t fd, intptr_t protocol, bool enabled) {
+bool SocketBase::SetMulticastLoop(intptr_t fd,
+ intptr_t protocol,
+ bool enabled) {
SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd);
int on = enabled ? 1 : 0;
int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6;
@@ -609,7 +371,7 @@ bool Socket::SetMulticastLoop(intptr_t fd, intptr_t protocol, bool enabled) {
}
-bool Socket::GetMulticastHops(intptr_t fd, intptr_t protocol, int* value) {
+bool SocketBase::GetMulticastHops(intptr_t fd, intptr_t protocol, int* value) {
SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd);
uint8_t v;
socklen_t len = sizeof(v);
@@ -625,7 +387,7 @@ bool Socket::GetMulticastHops(intptr_t fd, intptr_t protocol, int* value) {
}
-bool Socket::SetMulticastHops(intptr_t fd, intptr_t protocol, int value) {
+bool SocketBase::SetMulticastHops(intptr_t fd, intptr_t protocol, int value) {
SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd);
int v = value;
int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6;
@@ -636,7 +398,7 @@ bool Socket::SetMulticastHops(intptr_t fd, intptr_t protocol, int value) {
}
-bool Socket::GetBroadcast(intptr_t fd, bool* enabled) {
+bool SocketBase::GetBroadcast(intptr_t fd, bool* enabled) {
SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd);
int on;
socklen_t len = sizeof(on);
@@ -649,7 +411,7 @@ bool Socket::GetBroadcast(intptr_t fd, bool* enabled) {
}
-bool Socket::SetBroadcast(intptr_t fd, bool enabled) {
+bool SocketBase::SetBroadcast(intptr_t fd, bool enabled) {
SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd);
int on = enabled ? 1 : 0;
return setsockopt(handle->socket(), SOL_SOCKET, SO_BROADCAST,
@@ -657,10 +419,10 @@ bool Socket::SetBroadcast(intptr_t fd, bool enabled) {
}
-bool Socket::JoinMulticast(intptr_t fd,
- const RawAddr& addr,
- const RawAddr&,
- int interfaceIndex) {
+bool SocketBase::JoinMulticast(intptr_t fd,
+ const RawAddr& addr,
+ const RawAddr&,
+ int interfaceIndex) {
SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd);
int proto = addr.addr.sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
struct group_req mreq;
@@ -671,10 +433,10 @@ bool Socket::JoinMulticast(intptr_t fd,
}
-bool Socket::LeaveMulticast(intptr_t fd,
- const RawAddr& addr,
- const RawAddr&,
- int interfaceIndex) {
+bool SocketBase::LeaveMulticast(intptr_t fd,
+ const RawAddr& addr,
+ const RawAddr&,
+ int interfaceIndex) {
SocketHandle* handle = reinterpret_cast<SocketHandle*>(fd);
int proto = addr.addr.sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
struct group_req mreq;
« no previous file with comments | « runtime/bin/socket_base_win.h ('k') | runtime/bin/socket_fuchsia.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698