From 2b6dcd8e9058ceb6ea32a04aa1e1d4a13d682529 Mon Sep 17 00:00:00 2001 From: Michael Pavone Date: Thu, 30 Apr 2020 23:15:50 -0700 Subject: Get WIP net play code compiling on Windows and cleanup some unistd.h includes --- util.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) (limited to 'util.c') diff --git a/util.c b/util.c index 489f159..848fca4 100644 --- a/util.c +++ b/util.c @@ -7,7 +7,6 @@ #include #include -#include #include #ifdef __ANDROID__ @@ -532,6 +531,8 @@ void disable_stdout_messages(void) } #ifdef _WIN32 +#define WINVER 0x501 +#include #include #include @@ -679,7 +680,76 @@ int ensure_dir_exists(const char *path) return CreateDirectory(path, NULL); } +static WSADATA wsa_data; +static void socket_cleanup(void) +{ + WSACleanup(); +} + +void socket_init(void) +{ + static uint8_t started; + if (!started) { + started = 1; + WSAStartup(MAKEWORD(2,2), &wsa_data); + atexit(socket_cleanup); + } +} + +int socket_blocking(int sock, int should_block) +{ + u_long param = !should_block; + if (ioctlsocket(sock, FIONBIO, ¶m)) { + return WSAGetLastError(); + } + return 0; +} + +void socket_close(int sock) +{ + closesocket(sock); +} + +int socket_last_error(void) +{ + return WSAGetLastError(); +} + +int socket_error_is_wouldblock(void) +{ + return WSAGetLastError() == WSAEWOULDBLOCK; +} + #else +#include +#include + +void socket_init(void) +{ +} + +int socket_blocking(int sock, int should_block) +{ + if (fcntl(listen_sock, F_SETFL, should_block ? 0 : O_NONBLOCK)) { + return errno; + } + return 0; +} + +void socket_close(int sock) +{ + close(sock); +} + +int socket_last_error(void) +{ + return errno; +} + +int socket_error_is_wouldblock(void) +{ + return errno == EAGAIN || errno == EWOULDBLOCK; +} char * get_home_dir() { -- cgit v1.2.3