mirror of
https://github.com/wassname/ray.git
synced 2026-08-16 11:27:09 +08:00
File HANDLE/descriptor translation layer for Windows (#7657)
* Use TCP sockets on Windows with custom HANDLE <-> FD translation layer * Get Plasma working on Windows Co-authored-by: Mehrdad <noreply@github.com>
This commit is contained in:
+4
-4
@@ -14,12 +14,12 @@
|
||||
|
||||
#include "asio.h"
|
||||
|
||||
#include "ray/util/logging.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <io.h>
|
||||
#include <win32fd.h>
|
||||
#endif
|
||||
|
||||
#include "ray/util/logging.h"
|
||||
|
||||
RedisAsioClient::RedisAsioClient(boost::asio::io_service &io_service,
|
||||
ray::gcs::RedisAsyncContext &redis_async_context)
|
||||
: redis_async_context_(redis_async_context),
|
||||
@@ -37,7 +37,7 @@ RedisAsioClient::RedisAsioClient(boost::asio::io_service &io_service,
|
||||
#ifdef _WIN32
|
||||
SOCKET sock = SOCKET_ERROR;
|
||||
WSAPROTOCOL_INFO pi;
|
||||
if (WSADuplicateSocket(_get_osfhandle(c->fd), GetCurrentProcessId(), &pi) == 0) {
|
||||
if (WSADuplicateSocket(fh_get(c->fd), GetCurrentProcessId(), &pi) == 0) {
|
||||
DWORD flag = WSA_FLAG_OVERLAPPED;
|
||||
sock = WSASocket(pi.iAddressFamily, pi.iSocketType, pi.iProtocol, &pi, 0, flag);
|
||||
}
|
||||
|
||||
@@ -25,6 +25,10 @@
|
||||
#include "ray/object_manager/object_store_notification_manager.h"
|
||||
#include "ray/util/util.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <win32fd.h>
|
||||
#endif
|
||||
|
||||
namespace ray {
|
||||
|
||||
ObjectStoreNotificationManager::ObjectStoreNotificationManager(
|
||||
@@ -38,10 +42,11 @@ ObjectStoreNotificationManager::ObjectStoreNotificationManager(
|
||||
exit_on_error_(exit_on_error) {
|
||||
RAY_ARROW_CHECK_OK(store_client_.Connect(store_socket_name.c_str(), "", 0, 300));
|
||||
|
||||
int c_socket; // TODO(mehrdadn): This should be type SOCKET for Windows
|
||||
RAY_ARROW_CHECK_OK(store_client_.Subscribe(&c_socket));
|
||||
int fd;
|
||||
RAY_ARROW_CHECK_OK(store_client_.Subscribe(&fd));
|
||||
boost::system::error_code ec;
|
||||
#ifdef _WIN32
|
||||
boost::asio::detail::socket_type c_socket = fh_release(fd);
|
||||
WSAPROTOCOL_INFO pi;
|
||||
size_t n = sizeof(pi);
|
||||
char *p = reinterpret_cast<char *>(&pi);
|
||||
@@ -63,7 +68,7 @@ ObjectStoreNotificationManager::ObjectStoreNotificationManager(
|
||||
}
|
||||
}
|
||||
#else
|
||||
socket_.assign(local_stream_protocol(), c_socket, ec);
|
||||
socket_.assign(local_stream_protocol(), fd, ec);
|
||||
#endif
|
||||
RAY_CHECK(!ec);
|
||||
NotificationWait();
|
||||
|
||||
@@ -37,7 +37,8 @@ class ClientConnectionTest : public ::testing::Test {
|
||||
#if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
|
||||
boost::asio::local::connect_pair(in_, out_);
|
||||
#else
|
||||
int pair[2] = {}; // TODO(mehrdadn): This should be type SOCKET for Windows
|
||||
boost::asio::detail::socket_type pair[2] = {boost::asio::detail::invalid_socket,
|
||||
boost::asio::detail::invalid_socket};
|
||||
RAY_CHECK(socketpair(AF_INET, SOCK_STREAM, 0, pair) == 0);
|
||||
in_.assign(local_stream_protocol::v4(), pair[0]);
|
||||
out_.assign(local_stream_protocol::v4(), pair[1]);
|
||||
|
||||
+11
-11
@@ -8,14 +8,14 @@
|
||||
|
||||
#pragma comment(lib, "IPHlpAPI.lib")
|
||||
|
||||
int socketpair(int domain, int type, int protocol, int sv[2]) {
|
||||
int socketpair(int domain, int type, int protocol, SOCKET sv[2]) {
|
||||
if ((domain != AF_UNIX && domain != AF_INET) || type != SOCK_STREAM) {
|
||||
return -1;
|
||||
}
|
||||
SOCKET sockets[2];
|
||||
int r = dumb_socketpair(sockets);
|
||||
sv[0] = (int)sockets[0];
|
||||
sv[1] = (int)sockets[1];
|
||||
sv[0] = sockets[0];
|
||||
sv[1] = sockets[1];
|
||||
return r;
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ static DWORD getsockpid(SOCKET client) {
|
||||
return pid;
|
||||
}
|
||||
|
||||
ssize_t sendmsg(int sockfd, struct msghdr *msg, int flags) {
|
||||
ssize_t sendmsg(SOCKET sock, struct msghdr *msg, int flags) {
|
||||
ssize_t result = -1;
|
||||
struct cmsghdr *header = CMSG_FIRSTHDR(msg);
|
||||
if (header->cmsg_level == SOL_SOCKET && header->cmsg_type == SCM_RIGHTS) {
|
||||
@@ -76,11 +76,11 @@ ssize_t sendmsg(int sockfd, struct msghdr *msg, int flags) {
|
||||
* that the child process closes AND that its process ID is reassigned
|
||||
* to another existing process.
|
||||
*/
|
||||
int *const pfd = (int *)CMSG_DATA(header);
|
||||
SOCKET *const pfd = (SOCKET *)CMSG_DATA(header);
|
||||
WSAPROTOCOL_INFO protocol_info = {0};
|
||||
/* assume socket if it's a pipe, until proven otherwise */
|
||||
BOOL is_socket = GetFileType((HANDLE)(SOCKET)(*pfd)) == FILE_TYPE_PIPE;
|
||||
DWORD const target_pid = getsockpid(sockfd);
|
||||
DWORD const target_pid = getsockpid(sock);
|
||||
HANDLE target_process = NULL;
|
||||
if (target_pid) {
|
||||
if (is_socket) {
|
||||
@@ -107,7 +107,7 @@ ssize_t sendmsg(int sockfd, struct msghdr *msg, int flags) {
|
||||
bufs[0].len = sizeof(protocol_info);
|
||||
memcpy(&bufs[1], msg->lpBuffers, msg->dwBufferCount * sizeof(*msg->lpBuffers));
|
||||
DWORD nb;
|
||||
result = WSASend(sockfd, bufs, nbufs, &nb, flags, NULL, NULL) == 0
|
||||
result = WSASend(sock, bufs, nbufs, &nb, flags, NULL, NULL) == 0
|
||||
? (ssize_t)(nb - sizeof(protocol_info))
|
||||
: -1;
|
||||
}
|
||||
@@ -126,7 +126,7 @@ ssize_t sendmsg(int sockfd, struct msghdr *msg, int flags) {
|
||||
return result;
|
||||
}
|
||||
|
||||
ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags) {
|
||||
ssize_t recvmsg(SOCKET sock, struct msghdr *msg, int flags) {
|
||||
int result = -1;
|
||||
struct cmsghdr *header = CMSG_FIRSTHDR(msg);
|
||||
if (msg->msg_controllen && flags == 0 /* We can't send flags on Windows... */) {
|
||||
@@ -138,13 +138,13 @@ ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags) {
|
||||
memcpy(&bufs[1], msg->lpBuffers, msg->dwBufferCount * sizeof(*msg->lpBuffers));
|
||||
DWORD nb;
|
||||
DWORD dwFlags = flags;
|
||||
result = WSARecv(sockfd, bufs, nbufs, &nb, &dwFlags, NULL, NULL) == 0
|
||||
result = WSARecv(sock, bufs, nbufs, &nb, &dwFlags, NULL, NULL) == 0
|
||||
? (ssize_t)(nb - sizeof(protocol_info))
|
||||
: -1;
|
||||
if (result != -1) {
|
||||
int *const pfd = (int *)CMSG_DATA(header);
|
||||
SOCKET *const pfd = (SOCKET *)CMSG_DATA(header);
|
||||
if (protocol_info.iSocketType == 0 && protocol_info.iProtocol == 0) {
|
||||
*pfd = *(int *)&protocol_info;
|
||||
*pfd = *(SOCKET *)&protocol_info;
|
||||
} else {
|
||||
*pfd = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
|
||||
&protocol_info, 0, WSA_FLAG_OVERLAPPED);
|
||||
|
||||
@@ -3,10 +3,10 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
static int strcasecmp(const char *s1, const char *s2) { return stricmp(s1, s2); }
|
||||
static int strcasecmp(const char *s1, const char *s2) { return _stricmp(s1, s2); }
|
||||
|
||||
static int strncasecmp(const char *s1, const char *s2, size_t n) {
|
||||
return strnicmp(s1, s2, n);
|
||||
return _strnicmp(s1, s2, n);
|
||||
}
|
||||
|
||||
#endif /* STRINGS_H */
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
#ifndef MMAN_H
|
||||
#define MMAN_H
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#define MAP_SHARED 0x0010 /* share changes */
|
||||
#define MAP_FAILED ((void *)-1)
|
||||
#define PROT_READ 0x04 /* pages can be read */
|
||||
#define PROT_WRITE 0x02 /* pages can be written */
|
||||
#define PROT_EXEC 0x01 /* pages can be executed */
|
||||
#ifndef FILE_MAP_ALL_ACCESS
|
||||
enum { FILE_MAP_ALL_ACCESS = 0xF001F };
|
||||
#endif
|
||||
EXTERN_C WINBASEAPI void *WINAPI MapViewOfFile(HANDLE hFileMappingObject,
|
||||
DWORD dwDesiredAccess,
|
||||
DWORD dwFileOffsetHigh,
|
||||
DWORD dwFileOffsetLow,
|
||||
SIZE_T dwNumberOfBytesToMap);
|
||||
EXTERN_C WINBASEAPI BOOL WINAPI UnmapViewOfFile(void const *lpBaseAddress);
|
||||
static void *mmap(void *addr, size_t len, int prot, int flags, int fd, off_t off) {
|
||||
void *result = (void *)(-1);
|
||||
if (!addr && (flags & MAP_SHARED)) {
|
||||
/* HACK: we're assuming handle sizes can't exceed 32 bits, which is wrong...
|
||||
* but works for now. */
|
||||
void *ptr = MapViewOfFile((HANDLE)(intptr_t)fd, FILE_MAP_ALL_ACCESS,
|
||||
(DWORD)(off >> (CHAR_BIT * sizeof(DWORD))), (DWORD)off,
|
||||
(SIZE_T)len);
|
||||
if (ptr) {
|
||||
result = ptr;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
static int munmap(void *addr, size_t length) {
|
||||
(void)length;
|
||||
return UnmapViewOfFile(addr) ? 0 : -1;
|
||||
}
|
||||
|
||||
#endif /* MMAN_H */
|
||||
@@ -29,18 +29,8 @@ typedef unsigned short sa_family_t;
|
||||
#define msg_flags dwFlags
|
||||
|
||||
int dumb_socketpair(SOCKET socks[2]);
|
||||
ssize_t sendmsg(int sockfd, struct msghdr *msg, int flags);
|
||||
ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags);
|
||||
int socketpair(int domain, int type, int protocol, int sv[2]);
|
||||
|
||||
#ifdef __cplusplus
|
||||
namespace {
|
||||
inline int send(SOCKET s, const void *buf, int len, int flags) {
|
||||
// Call the const char* overload version
|
||||
int (*psend)(SOCKET s, const char *buf, int len, int flags) = ::send;
|
||||
return (*psend)(s, (const char *)buf, len, flags);
|
||||
}
|
||||
} // namespace
|
||||
#endif
|
||||
ssize_t sendmsg(SOCKET sockfd, struct msghdr *msg, int flags);
|
||||
ssize_t recvmsg(SOCKET sockfd, struct msghdr *msg, int flags);
|
||||
int socketpair(int domain, int type, int protocol, SOCKET sv[2]);
|
||||
|
||||
#endif /* SOCKET_H */
|
||||
|
||||
@@ -0,0 +1,538 @@
|
||||
#undef WIN32_REPLACE_FD_APIS
|
||||
#define WIN32_REPLACE_FD_APIS 0 // make sure we don't replace the APIs for our own calls
|
||||
#include "win32fd.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <io.h>
|
||||
|
||||
#ifndef _WINSOCKAPI_
|
||||
#include <WinSock2.h>
|
||||
#endif
|
||||
|
||||
#ifndef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
enum { fh_prefer_fallback = 1 };
|
||||
static const char fh_magic_prefix[] = "Win32FD";
|
||||
static const size_t fh_magic_size = sizeof(fh_magic_prefix) + sizeof(void *);
|
||||
|
||||
static void fh_get_magic(unsigned char magic[fh_magic_size]) {
|
||||
memcpy(magic, fh_magic_prefix, sizeof(fh_magic_prefix));
|
||||
const void *const suffix = GetModuleHandle(NULL);
|
||||
memcpy(magic + sizeof(fh_magic_prefix), &suffix, sizeof(suffix));
|
||||
}
|
||||
|
||||
struct fh_payload_t {
|
||||
intptr_t handle;
|
||||
unsigned char magic[fh_magic_size];
|
||||
};
|
||||
|
||||
static int _fh_close(intptr_t handle) {
|
||||
int result = closesocket(handle);
|
||||
if (result != 0) {
|
||||
int error = WSAGetLastError();
|
||||
if (error == WSANOTINITIALISED || error == WSAENOTSOCK) {
|
||||
if (CloseHandle(reinterpret_cast<HANDLE>(handle))) {
|
||||
result = 0;
|
||||
} else {
|
||||
_set_errno(EBADF);
|
||||
}
|
||||
} else {
|
||||
_set_errno(EINVAL);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static int _fh_is_socket(intptr_t handle) {
|
||||
int result = 1;
|
||||
SOCKADDR_STORAGE addr;
|
||||
int addrlen = sizeof(addr);
|
||||
if (getsockname(handle, reinterpret_cast<sockaddr *>(&addr), &addrlen) != 0) {
|
||||
int error = WSAGetLastError();
|
||||
if (error == WSANOTINITIALISED || error == WSAENOTSOCK) {
|
||||
result = 0;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int fh_accept(int sockpfd, struct sockaddr *name, socklen_t *namelen) {
|
||||
int result = -1;
|
||||
intptr_t handle = fh_get(sockpfd);
|
||||
intptr_t accepted = WSAAccept(handle, name, namelen, NULL, NULL);
|
||||
if (accepted != -1) {
|
||||
result = fh_open(accepted, -1);
|
||||
if (result == -1) {
|
||||
closesocket(accepted);
|
||||
accepted = -1;
|
||||
}
|
||||
} else if (handle == -1) {
|
||||
_set_errno(EBADF);
|
||||
} else {
|
||||
_set_errno(WSAGetLastError());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int fh_bind(int sockpfd, const struct sockaddr *name, socklen_t namelen) {
|
||||
intptr_t handle = fh_get(sockpfd);
|
||||
int result = bind(handle, name, namelen);
|
||||
if (result == 0) {
|
||||
} else if (handle == -1) {
|
||||
_set_errno(EBADF);
|
||||
} else {
|
||||
_set_errno(WSAGetLastError());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int fh_close(int pfd) {
|
||||
intptr_t handle = fh_get(pfd);
|
||||
int result = _close(pfd);
|
||||
if (result == 0 && handle != -1) {
|
||||
result = _fh_close(handle);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int fh_connect(int sockpfd, const struct sockaddr *name, socklen_t namelen) {
|
||||
intptr_t handle = fh_get(sockpfd);
|
||||
int result = WSAConnect(handle, name, namelen, NULL, NULL, NULL, NULL);
|
||||
if (result == 0) {
|
||||
} else if (handle == -1) {
|
||||
_set_errno(EBADF);
|
||||
} else {
|
||||
int error = WSAGetLastError();
|
||||
switch (error) {
|
||||
case WSAEINVAL:
|
||||
case WSAEWOULDBLOCK:
|
||||
case WSA_IO_PENDING:
|
||||
error = WSAEINPROGRESS; // for Redis
|
||||
break;
|
||||
}
|
||||
_set_errno(error);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int fh_dup(int pfd) {
|
||||
int result = -1;
|
||||
intptr_t handle = fh_get(pfd);
|
||||
if (fh_prefer_fallback && handle == -1) {
|
||||
// Not our FD; fall back to default
|
||||
result = _dup(pfd);
|
||||
} else {
|
||||
intptr_t duped = -1;
|
||||
WSAPROTOCOL_INFO pi;
|
||||
if (WSADuplicateSocket(handle, GetCurrentProcessId(), &pi) == 0) {
|
||||
duped = WSASocket(pi.iAddressFamily, pi.iSocketType, pi.iProtocol, &pi, 0, 0);
|
||||
if (duped == -1) {
|
||||
_set_errno(EINVAL);
|
||||
}
|
||||
} else {
|
||||
int error = WSAGetLastError();
|
||||
if (error == WSANOTINITIALISED || error == WSAENOTSOCK) {
|
||||
if (!DuplicateHandle(GetCurrentProcess(), reinterpret_cast<HANDLE>(handle),
|
||||
GetCurrentProcess(), reinterpret_cast<HANDLE *>(&duped), 0,
|
||||
FALSE, DUPLICATE_SAME_ACCESS)) {
|
||||
duped = -1;
|
||||
_set_errno(EBADF);
|
||||
}
|
||||
} else {
|
||||
_set_errno(EBADF);
|
||||
}
|
||||
}
|
||||
if (duped != -1) {
|
||||
result = fh_open(duped, -1);
|
||||
if (result == -1) {
|
||||
_fh_close(duped);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
intptr_t fh_get(int pfd) {
|
||||
fh_payload_t payload = {-1};
|
||||
HANDLE pipe = reinterpret_cast<HANDLE>(_get_osfhandle(pfd));
|
||||
DWORD size = sizeof(payload), nbytes, avail, left;
|
||||
if (pipe != INVALID_HANDLE_VALUE) {
|
||||
if (GetFileType(pipe) != FILE_TYPE_PIPE) {
|
||||
_set_errno(EBADF); // if this triggers, you passed an invalid or incompatible FD
|
||||
} else if (PeekNamedPipe(pipe, &payload, size, &nbytes, &avail, &left)) {
|
||||
if (avail != size) {
|
||||
payload.handle = -1;
|
||||
_set_errno(EIO); // if this triggers, you accidentally wrote to the FD directly
|
||||
} else {
|
||||
unsigned char expected[fh_magic_size];
|
||||
fh_get_magic(expected);
|
||||
if (memcmp(payload.magic, expected, sizeof(expected)) != 0) {
|
||||
payload.handle = -1;
|
||||
_set_errno(EBADF); // if this triggers, this isn't a recognized FD
|
||||
}
|
||||
}
|
||||
} else {
|
||||
_set_errno(EIO); // if this triggers, you accidentally read from the FD directly
|
||||
}
|
||||
} else {
|
||||
_set_errno(EBADF);
|
||||
}
|
||||
return payload.handle;
|
||||
}
|
||||
|
||||
int fh_getpeername(int sockpfd, struct sockaddr *name, socklen_t *namelen) {
|
||||
intptr_t handle = fh_get(sockpfd);
|
||||
int result = getpeername(handle, name, namelen);
|
||||
if (result == 0) {
|
||||
} else if (handle == -1) {
|
||||
_set_errno(EBADF);
|
||||
} else {
|
||||
_set_errno(WSAGetLastError());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int fh_getsockname(int sockpfd, struct sockaddr *name, socklen_t *namelen) {
|
||||
intptr_t handle = fh_get(sockpfd);
|
||||
int result = getsockname(handle, name, namelen);
|
||||
if (result == 0) {
|
||||
} else if (handle == -1) {
|
||||
_set_errno(EBADF);
|
||||
} else {
|
||||
_set_errno(WSAGetLastError());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int fh_getsockopt(int sockpfd, int level, int name, void *val, socklen_t *len) {
|
||||
intptr_t handle = fh_get(sockpfd);
|
||||
int result = getsockopt(handle, level, name, static_cast<char *>(val), len);
|
||||
if (result == 0) {
|
||||
} else if (handle == -1) {
|
||||
_set_errno(EBADF);
|
||||
} else {
|
||||
_set_errno(WSAGetLastError());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int fh_ioctlsocket(int sockpfd, long cmd, unsigned long *argp) {
|
||||
intptr_t handle = fh_get(sockpfd);
|
||||
int result = ioctlsocket(handle, cmd, argp);
|
||||
if (result == 0) {
|
||||
} else if (handle == -1) {
|
||||
_set_errno(EBADF);
|
||||
} else {
|
||||
_set_errno(WSAGetLastError());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int fh_listen(int sockpfd, int backlog) {
|
||||
intptr_t handle = fh_get(sockpfd);
|
||||
int result = listen(handle, backlog);
|
||||
if (result == 0) {
|
||||
} else if (handle == -1) {
|
||||
_set_errno(EBADF);
|
||||
} else {
|
||||
_set_errno(WSAGetLastError());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int fh_open(intptr_t handle, int mode) {
|
||||
int pfd = -1;
|
||||
HANDLE r, w;
|
||||
if (mode == -1) {
|
||||
mode = _O_RDWR;
|
||||
}
|
||||
if (!(mode & _O_TEXT)) {
|
||||
mode |= _O_BINARY;
|
||||
}
|
||||
if (handle == -1 || handle == 0) {
|
||||
_set_errno(EBADF);
|
||||
} else if (fh_prefer_fallback &&
|
||||
GetFileType(reinterpret_cast<HANDLE>(handle)) != FILE_TYPE_UNKNOWN &&
|
||||
!_fh_is_socket(handle)) {
|
||||
// Already compatible with CRT FDs, so just fall back
|
||||
pfd = _open_osfhandle(handle, mode);
|
||||
} else {
|
||||
fh_payload_t payload = {handle};
|
||||
fh_get_magic(payload.magic);
|
||||
DWORD size = sizeof(payload), nbytes;
|
||||
if (CreatePipe(&r, &w, NULL, size)) {
|
||||
if (WriteFile(w, &payload, size, &nbytes, NULL) && size == nbytes) {
|
||||
pfd = _open_osfhandle(reinterpret_cast<intptr_t>(r), mode);
|
||||
if (pfd == -1) {
|
||||
CloseHandle(r);
|
||||
}
|
||||
} else {
|
||||
_set_errno(EIO);
|
||||
}
|
||||
CloseHandle(w);
|
||||
} else {
|
||||
_set_errno(EMFILE);
|
||||
}
|
||||
}
|
||||
return pfd;
|
||||
}
|
||||
|
||||
int fh_poll(struct pollfd *fds, nfds_t nfds, int timeout) {
|
||||
struct fd_sets_t {
|
||||
fd_set rfds, wfds, efds;
|
||||
} fdsets;
|
||||
int maxfd = -1;
|
||||
struct fd_sets_t *pfdsets =
|
||||
nfds <= FD_SETSIZE ? &fdsets
|
||||
: static_cast<struct fd_sets_t *>(operator new(
|
||||
(offsetof(fd_set, fd_array) + nfds * sizeof(SOCKET)) *
|
||||
(sizeof(struct fd_sets_t) / sizeof(fd_set))));
|
||||
FD_ZERO(&pfdsets->rfds);
|
||||
FD_ZERO(&pfdsets->wfds);
|
||||
FD_ZERO(&pfdsets->efds);
|
||||
for (nfds_t i = 0; i < nfds; ++i) {
|
||||
if (fds[i].events & POLLIN) {
|
||||
FD_SET(fds[i].fd, &pfdsets->rfds);
|
||||
}
|
||||
if (fds[i].events & POLLOUT) {
|
||||
FD_SET(fds[i].fd, &pfdsets->wfds);
|
||||
}
|
||||
if (fds[i].events & POLLERR) {
|
||||
FD_SET(fds[i].fd, &pfdsets->efds);
|
||||
}
|
||||
if (maxfd < fds[i].fd) {
|
||||
maxfd = static_cast<int>(fds[i].fd);
|
||||
}
|
||||
}
|
||||
int msec = timeout;
|
||||
struct timeval tv = {msec >= 0 ? msec / 1000 : 0, msec >= 0 ? (msec % 1000) * 1000 : 0};
|
||||
int result = fh_select(maxfd + 1, &pfdsets->rfds, &pfdsets->wfds, &pfdsets->efds,
|
||||
msec >= 0 ? &tv : NULL);
|
||||
if (result >= 0) {
|
||||
result = 0;
|
||||
for (nfds_t i = 0; i < nfds; ++i) {
|
||||
fds[i].revents = 0;
|
||||
if (FD_ISSET(fds[i].fd, &pfdsets->rfds)) {
|
||||
fds[i].revents |= POLLIN;
|
||||
}
|
||||
if (FD_ISSET(fds[i].fd, &pfdsets->wfds)) {
|
||||
fds[i].revents |= POLLOUT;
|
||||
}
|
||||
if (FD_ISSET(fds[i].fd, &pfdsets->efds)) {
|
||||
fds[i].revents |= POLLERR;
|
||||
}
|
||||
if (fds[i].revents) {
|
||||
++result;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (pfdsets != &fdsets) {
|
||||
operator delete(pfdsets);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
ssize_t fh_read(int pfd, void *buffer, size_t size) {
|
||||
ssize_t result = -1;
|
||||
intptr_t handle = fh_get(pfd);
|
||||
if (size >= INT_MAX) {
|
||||
size = INT_MAX;
|
||||
}
|
||||
if (fh_prefer_fallback && handle == -1) {
|
||||
// Not our FD; fall back to default
|
||||
result = _read(pfd, buffer, static_cast<unsigned int>(size));
|
||||
} else {
|
||||
WSABUF buf = {static_cast<unsigned long>(size), static_cast<char *>(buffer)};
|
||||
DWORD nbytes;
|
||||
DWORD flags = 0;
|
||||
if (WSARecv(handle, &buf, 1, &nbytes, &flags, NULL, NULL) == 0) {
|
||||
result = static_cast<ssize_t>(nbytes);
|
||||
} else {
|
||||
int error = WSAGetLastError();
|
||||
if (error == WSANOTINITIALISED || error == WSAENOTSOCK) {
|
||||
if (ReadFile(reinterpret_cast<HANDLE>(handle), buffer, static_cast<DWORD>(size),
|
||||
&nbytes, NULL)) {
|
||||
result = static_cast<ssize_t>(nbytes);
|
||||
} else {
|
||||
_set_errno(EINVAL);
|
||||
}
|
||||
} else {
|
||||
if (error == WSAEWOULDBLOCK) {
|
||||
error = EAGAIN; // for Redis
|
||||
}
|
||||
_set_errno(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
ssize_t fh_recv(int sockpfd, void *buffer, size_t size, int flags) {
|
||||
ssize_t result = -1;
|
||||
if (size >= INT_MAX) {
|
||||
size = INT_MAX;
|
||||
}
|
||||
intptr_t handle = fh_get(sockpfd);
|
||||
WSABUF buf = {static_cast<unsigned long>(size), static_cast<char *>(buffer)};
|
||||
DWORD nbytes;
|
||||
DWORD dwflags = static_cast<DWORD>(flags);
|
||||
if (WSARecv(handle, &buf, 1, &nbytes, &dwflags, NULL, NULL) == 0) {
|
||||
result = static_cast<ssize_t>(nbytes);
|
||||
} else if (handle == -1) {
|
||||
_set_errno(EBADF);
|
||||
} else {
|
||||
_set_errno(WSAGetLastError());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
intptr_t fh_release(int pfd) {
|
||||
intptr_t handle = fh_get(pfd);
|
||||
if (handle != -1) {
|
||||
if (_close(pfd) != 0) {
|
||||
handle = -1;
|
||||
}
|
||||
}
|
||||
return handle;
|
||||
}
|
||||
|
||||
int fh_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds,
|
||||
struct timeval *timeout) {
|
||||
int result = 0;
|
||||
fd_set rfds, wfds, efds;
|
||||
struct {
|
||||
fd_set *src, *dst;
|
||||
} translations[] = {
|
||||
{readfds, &rfds},
|
||||
{writefds, &wfds},
|
||||
{errorfds, &efds},
|
||||
};
|
||||
for (size_t i = 0; i < sizeof(translations) / sizeof(*translations); ++i) {
|
||||
fd_set *src = translations[i].src, *dst = translations[i].dst;
|
||||
if (src) {
|
||||
for (size_t j = 0; j < src->fd_count; ++j) {
|
||||
int sockpfd = static_cast<int>(src->fd_array[j]);
|
||||
intptr_t handle = fh_get(sockpfd);
|
||||
if (handle == -1) {
|
||||
result = -1;
|
||||
_set_errno(EBADF);
|
||||
}
|
||||
dst->fd_array[j] = handle;
|
||||
}
|
||||
dst->fd_count = src->fd_count;
|
||||
}
|
||||
}
|
||||
if (result != -1) {
|
||||
result = select(nfds, readfds ? &rfds : NULL, writefds ? &wfds : NULL,
|
||||
errorfds ? &efds : NULL, timeout);
|
||||
if (result != -1) {
|
||||
_set_errno(WSAGetLastError());
|
||||
}
|
||||
}
|
||||
if (result >= 0) {
|
||||
for (size_t i = 0; i < sizeof(translations) / sizeof(*translations); ++i) {
|
||||
fd_set *src = translations[i].src, *dst = translations[i].dst;
|
||||
if (src) {
|
||||
fd_set out;
|
||||
FD_ZERO(&out);
|
||||
for (size_t j = 0; j < src->fd_count; ++j) {
|
||||
int sockpfd = static_cast<int>(src->fd_array[j]);
|
||||
intptr_t handle = fh_get(sockpfd);
|
||||
if (FD_ISSET(handle, dst)) {
|
||||
FD_SET(sockpfd, &out);
|
||||
}
|
||||
}
|
||||
memcpy(src->fd_array, out.fd_array, out.fd_count * sizeof(*out.fd_array));
|
||||
src->fd_count = out.fd_count;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
ssize_t fh_send(int sockpfd, const void *buffer, size_t size, int flags) {
|
||||
ssize_t result = -1;
|
||||
if (size >= INT_MAX) {
|
||||
size = INT_MAX;
|
||||
}
|
||||
intptr_t handle = fh_get(sockpfd);
|
||||
WSABUF buf = {static_cast<unsigned long>(size),
|
||||
static_cast<char *>(const_cast<void *>(buffer))};
|
||||
DWORD nbytes;
|
||||
if (WSASend(handle, &buf, 1, &nbytes, flags, NULL, NULL) == 0) {
|
||||
result = static_cast<ssize_t>(nbytes);
|
||||
} else if (handle == -1) {
|
||||
_set_errno(EBADF);
|
||||
} else {
|
||||
_set_errno(WSAGetLastError());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int fh_setsockopt(int sockpfd, int level, int name, const void *val, socklen_t len) {
|
||||
intptr_t handle = fh_get(sockpfd);
|
||||
int result = setsockopt(handle, level, name, static_cast<const char *>(val), len);
|
||||
if (result == 0) {
|
||||
} else if (handle == -1) {
|
||||
_set_errno(EBADF);
|
||||
} else {
|
||||
_set_errno(WSAGetLastError());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int fh_socket(int domain, int type, int protocol) {
|
||||
int result = -1;
|
||||
DWORD flags = WSA_FLAG_OVERLAPPED; // because socket() is overlapped by default
|
||||
intptr_t handle = WSASocket(domain, type, protocol, NULL, 0, flags);
|
||||
if (handle != -1) {
|
||||
result = fh_open(handle, -1);
|
||||
if (result == -1) {
|
||||
closesocket(handle);
|
||||
handle = -1;
|
||||
}
|
||||
} else {
|
||||
_set_errno(WSAGetLastError());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
ssize_t fh_write(int pfd, const void *buffer, size_t size) {
|
||||
ssize_t result = -1;
|
||||
ssize_t handle = fh_get(pfd);
|
||||
if (size >= INT_MAX) {
|
||||
size = INT_MAX;
|
||||
}
|
||||
if (fh_prefer_fallback && handle == -1) {
|
||||
// Not our FD; fall back to default
|
||||
result = _write(pfd, buffer, static_cast<unsigned int>(size));
|
||||
} else {
|
||||
WSABUF buf = {static_cast<unsigned long>(size),
|
||||
static_cast<char *>(const_cast<void *>(buffer))};
|
||||
DWORD nbytes;
|
||||
DWORD flags = 0;
|
||||
if (WSASend(handle, &buf, 1, &nbytes, flags, NULL, NULL) == 0) {
|
||||
result = static_cast<ssize_t>(nbytes);
|
||||
} else {
|
||||
int error = WSAGetLastError();
|
||||
if (error == WSANOTINITIALISED || error == WSAENOTSOCK) {
|
||||
if (WriteFile(reinterpret_cast<HANDLE>(handle), buffer, static_cast<DWORD>(size),
|
||||
&nbytes, NULL)) {
|
||||
result = static_cast<ssize_t>(nbytes);
|
||||
} else {
|
||||
_set_errno(EINVAL);
|
||||
}
|
||||
} else {
|
||||
_set_errno(WSAGetLastError());
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
#ifndef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,100 @@
|
||||
#ifndef WIN32_FD_H
|
||||
#define WIN32_FD_H
|
||||
|
||||
#if defined(WIN32_REPLACE_FD_APIS) && WIN32_REPLACE_FD_APIS
|
||||
#ifndef _CRT_DECLARE_NONSTDC_NAMES
|
||||
#define _CRT_DECLARE_NONSTDC_NAMES 0
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#include <limits.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <errno.h>
|
||||
#undef ECONNRESET
|
||||
#undef EINPROGRESS
|
||||
#undef ETIMEDOUT
|
||||
|
||||
#include <WS2tcpip.h>
|
||||
#include <io.h>
|
||||
|
||||
enum {
|
||||
ECONNRESET = WSAECONNRESET,
|
||||
EINPROGRESS = WSAEINPROGRESS,
|
||||
ETIMEDOUT = WSAETIMEDOUT,
|
||||
};
|
||||
typedef ptrdiff_t ssize_t;
|
||||
typedef unsigned long int nfds_t;
|
||||
#endif
|
||||
|
||||
int fh_accept(int sockpfd, struct sockaddr *name, socklen_t *namelen);
|
||||
int fh_bind(int sockpfd, const struct sockaddr *name, socklen_t namelen);
|
||||
int fh_close(int pfd);
|
||||
int fh_connect(int socket, const struct sockaddr *name, socklen_t namelen);
|
||||
int fh_dup(int pfd);
|
||||
|
||||
/// Retrieves the underlying file handle for the given pseudo-file descriptor.
|
||||
intptr_t fh_get(int pfd);
|
||||
|
||||
int fh_getpeername(int sockpfd, struct sockaddr *name, socklen_t *namelen);
|
||||
int fh_getsockname(int sockpfd, struct sockaddr *name, socklen_t *namelen);
|
||||
int fh_getsockopt(int sockpfd, int level, int name, void *val, socklen_t *len);
|
||||
int fh_ioctlsocket(int sockpfd, long cmd, unsigned long *argp);
|
||||
int fh_listen(int sockpfd, int backlog);
|
||||
|
||||
/// Opens a pseudo-file descriptor around the given file handle. Sockets are supported.
|
||||
/// However, this is only intended for storing a HANDLE as an integer.
|
||||
/// The file descriptor must not be used with external file I/O functions.
|
||||
int fh_open(intptr_t handle, int mode);
|
||||
|
||||
int fh_poll(struct pollfd *fds, nfds_t nfds, int timeout);
|
||||
ssize_t fh_read(int pfd, void *buffer, size_t size);
|
||||
|
||||
/// Releases the underlying file handle for the given pseudo-file descriptor and then
|
||||
/// closes the pseudo-file descriptor.
|
||||
intptr_t fh_release(int pfd);
|
||||
|
||||
ssize_t fh_recv(int sockpfd, void *buffer, size_t size, int flags);
|
||||
int fh_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds,
|
||||
struct timeval *timeout);
|
||||
ssize_t fh_send(int sockpfd, const void *buffer, size_t size, int flags);
|
||||
int fh_setsockopt(int sockpfd, int level, int name, const void *val, socklen_t len);
|
||||
int fh_socket(int domain, int type, int protocol);
|
||||
ssize_t fh_write(int pfd, const void *buffer, size_t size);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(WIN32_REPLACE_FD_APIS) && WIN32_REPLACE_FD_APIS
|
||||
#define accept fh_accept
|
||||
#define bind fh_bind
|
||||
static int close(int pfd) { return fh_close(pfd); }
|
||||
#define connect fh_connect
|
||||
static int dup(int pfd) { return fh_dup(pfd); }
|
||||
#define getsockopt fh_getsockopt
|
||||
#define ioctlsocket fh_ioctlsocket
|
||||
#define listen fh_listen
|
||||
static int open(intptr_t handle, int mode) { return fh_open(handle, mode); }
|
||||
#define poll fh_poll
|
||||
static ssize_t read(int pfd, void *buffer, size_t size) {
|
||||
return fh_read(pfd, buffer, size);
|
||||
}
|
||||
#define recv fh_recv
|
||||
#define recvfrom fh_recvfrom
|
||||
#define select fh_select
|
||||
#define socket fh_socket
|
||||
#define send fh_send
|
||||
#define sendto fh_sendto
|
||||
#define setsockopt fh_setsockopt
|
||||
static ssize_t write(int pfd, const void *buffer, size_t size) {
|
||||
return fh_write(pfd, buffer, size);
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user