diff util.c @ 1949:5a76a7373823

Get WIP net play code compiling on Windows and cleanup some unistd.h includes
author Michael Pavone <pavone@retrodev.com>
date Thu, 30 Apr 2020 23:15:50 -0700
parents a4cae960fd08
children 417e0fc958cc
line wrap: on
line diff
--- a/util.c	Wed Apr 29 23:53:39 2020 -0700
+++ b/util.c	Thu Apr 30 23:15:50 2020 -0700
@@ -7,7 +7,6 @@
 
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <unistd.h>
 #include <errno.h>
 
 #ifdef __ANDROID__
@@ -532,6 +531,8 @@
 }
 
 #ifdef _WIN32
+#define WINVER 0x501
+#include <winsock2.h>
 #include <windows.h>
 #include <shlobj.h>
 
@@ -679,7 +680,76 @@
 	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, &param)) {
+		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 <fcntl.h>
+#include <unistd.h>
+
+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()
 {