Files
mtgodot-poc/src/port/common/shim/win32/winsock.h
T
shenleiandClaude Opus 5.5 a46093104c layout: restructure into src/ tests/ android/ scripts/ tools/
- extension/src/{port,platform,codepage} -> src/; native_render -> src/host
  (+ dxt, shaders/); libgr2 -> src/gr2; extension/third_party -> third_party
- extension/tests -> tests/port, libgr2/tests -> tests/gr2
- android-native -> android (build.sh, push-client.sh moved in)
- script -> scripts; tools/40250 -> tools/server; oracle -> tools/granny-oracle;
  perf tools -> tools/perf
- all build trees under build/ (native, release, android, port-gate)
- xrender:: CMake aliases -> mt::; port-map ledger impl paths rewritten

No code changes. ctest 15/15, port_gate macos+android PASS, port_map check 0 errors.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 19:08:19 +09:00

90 lines
3.0 KiB
C

#pragma once
// Shim for <winsock.h> on non-Windows targets: the BSD socket API under its Winsock names, with the
// Winsock semantics 40250 relies on where the two differ (see the notes on each function).
#include "windows.h"
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <unistd.h>
typedef int SOCKET;
typedef struct sockaddr SOCKADDR;
typedef struct sockaddr* PSOCKADDR;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct in_addr IN_ADDR;
typedef struct timeval TIMEVAL;
typedef struct hostent HOSTENT;
#define INVALID_SOCKET (-1)
#define SOCKET_ERROR (-1)
#define WSAEWOULDBLOCK EWOULDBLOCK
typedef struct WSAData
{
WORD wVersion;
WORD wHighVersion;
} WSADATA;
// There is no Winsock DLL to start. A send on a socket the peer has closed raises SIGPIPE on POSIX
// where Winsock returns an error, so the process ignores it from here on.
inline int WSAStartup(WORD version, WSADATA* data)
{
signal(SIGPIPE, SIG_IGN);
if (data)
data->wVersion = data->wHighVersion = version;
return 0;
}
inline int WSACleanup() { return 0; }
// A non-blocking connect() answers WSAEWOULDBLOCK on Winsock and EINPROGRESS on POSIX.
inline int WSAGetLastError() { return errno == EINPROGRESS ? WSAEWOULDBLOCK : errno; }
inline int closesocket(SOCKET s) { return close(s); }
inline int ioctlsocket(SOCKET s, long cmd, DWORD* argp)
{
int value = static_cast<int>(*argp);
return ioctl(s, static_cast<unsigned long>(cmd), &value);
}
// Winsock ignores nfds, and a socket whose connect failed is reported only in exceptfds; POSIX needs
// nfds and marks such a socket writable. Callers pass 0 and treat "writable" as "connected"
// (CNetworkStream::Process), so writable sockets without a peer are dropped from the write set.
inline int mt_winsock_select(fd_set* readfds, fd_set* writefds, fd_set* exceptfds, TIMEVAL* timeout)
{
int ret = ::select(FD_SETSIZE, readfds, writefds, exceptfds, timeout);
if (ret <= 0 || !writefds)
return ret;
for (int fd = 0; fd < FD_SETSIZE; ++fd)
{
if (!FD_ISSET(fd, writefds))
continue;
sockaddr_in peer;
socklen_t len = sizeof(peer);
if (getpeername(fd, reinterpret_cast<sockaddr*>(&peer), &len) != 0)
FD_CLR(fd, writefds);
}
return ret;
}
#define select(nfds, readfds, writefds, exceptfds, timeout) mt_winsock_select(readfds, writefds, exceptfds, timeout)
inline int mt_winsock_recvfrom(SOCKET s, void* buf, size_t len, int flags, struct sockaddr* from, int* fromlen)
{
socklen_t slen = fromlen ? static_cast<socklen_t>(*fromlen) : 0;
ssize_t ret = ::recvfrom(s, buf, len, flags, from, fromlen ? &slen : nullptr);
if (fromlen)
*fromlen = static_cast<int>(slen);
return static_cast<int>(ret);
}
inline int mt_winsock_recvfrom(SOCKET s, void* buf, size_t len, int flags, struct sockaddr* from, socklen_t* fromlen)
{
return static_cast<int>(::recvfrom(s, buf, len, flags, from, fromlen));
}
#define recvfrom mt_winsock_recvfrom