From aa7870394be1d6615b2460ecc47f0916f5c2ffc2 Mon Sep 17 00:00:00 2001 From: nishi Date: Sat, 8 Jun 2024 09:24:58 +0000 Subject: [PATCH] windows works git-svn-id: file:///raid/svn-main/nishi-dataworks/trunk@191 d4a5a174-5a4a-5b4b-b672-37683c10d7d5 --- Platforms/win32.mk | 2 +- Platforms/win64.mk | 2 +- Server/Makefile | 9 ++++-- Server/tcpip.c | 69 +++++++++++++++++++++++++++++++++++++++------- common-decl.mk | 2 +- 5 files changed, 68 insertions(+), 16 deletions(-) diff --git a/Platforms/win32.mk b/Platforms/win32.mk index e46aa4e..a70b341 100644 --- a/Platforms/win32.mk +++ b/Platforms/win32.mk @@ -7,7 +7,6 @@ RANLIB = i686-w64-mingw32-ranlib WINDRES = i686-w64-mingw32-windres LIB_PREFIX = LIB_SUFFIX = .dll -SERVER = EXEC_SUFFIX = .exe STATICLIB_PREFIX = STATICLIB_SUFFIX = .a @@ -15,3 +14,4 @@ PLATFORM_M = i686 PLATFORM_P = i686 LDFLAGS += -Wl,--out-implib,./dataworks.lib LINK_LIB += dataworks.res +SERVLINK_LIB += -lws2_32 diff --git a/Platforms/win64.mk b/Platforms/win64.mk index c5db322..80bb12d 100644 --- a/Platforms/win64.mk +++ b/Platforms/win64.mk @@ -7,7 +7,6 @@ RANLIB = x86_64-w64-mingw32-ranlib WINDRES = x86_64-w64-mingw32-windres LIB_PREFIX = LIB_SUFFIX = .dll -SERVER = EXEC_SUFFIX = .exe STATICLIB_PREFIX = STATICLIB_SUFFIX = .a @@ -15,3 +14,4 @@ PLATFORM_M = x86_64 PLATFORM_P = x86_64 LDFLAGS += -Wl,--out-implib,./dataworks.lib LINK_LIB += dataworks.res +SERVLINK_LIB += -lws2_32 diff --git a/Server/Makefile b/Server/Makefile index e017f92..d2e5c62 100644 --- a/Server/Makefile +++ b/Server/Makefile @@ -7,11 +7,14 @@ OBJS = server.o $(SERVER_MODULE) all: dataworks_server$(EXEC_SUFFIX) -dataworks_server$(EXEC_SUFFIX): $(OBJS) - $(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBS) $(LINK_LIB) +dataworks.res: ../Client/dataworks.res + cp ../Client/dataworks.res ./ + +dataworks_server$(EXEC_SUFFIX): $(OBJS) $(LINK_LIB) + $(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBS) $(LINK_LIB) $(SERVLINK_LIB) .c.o: $(CC) $(CFLAGS) -I../Library -c -o $@ $< clean: - rm -f *.o *.so *.dll *.exe *.com dataworks_server *.a *.lib + rm -f *.o *.so *.dll *.exe *.com dataworks_server *.a *.lib *.res diff --git a/Server/tcpip.c b/Server/tcpip.c index edfd78a..a7f9d8f 100644 --- a/Server/tcpip.c +++ b/Server/tcpip.c @@ -41,11 +41,17 @@ extern char** argv; extern bool auth; extern char* authfile; +#ifdef __MINGW32__ +#include +#include +#include +#else #include #include #include #include #include +#endif #include void protocol_init(int sock); @@ -55,7 +61,11 @@ bool option(const char* str, const char* shortopt, const char* longopt); int port = 4096; int server_socket; +#ifdef __MINGW32__ +struct sockaddr_in server_address; +#else struct sockaddr_in6 server_address; +#endif extern struct dataworks_db* db; @@ -87,59 +97,98 @@ int server_init(void) { db = dataworks_database_open(argv[i]); } } +#ifdef __MINGW32__ + WSADATA wsa; + WSAStartup(MAKEWORD(2, 0), &wsa); +#endif +#ifdef __MINGW32__ + if((server_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) { +#else if((server_socket = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP)) < 0) { - fprintf(stderr, "BSD TCP/IP initialization fail\n"); +#endif + fprintf(stderr, "BSD TCP/IP initialization fail (socket)\n"); return 1; } int yes = 1; - if(setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) { - fprintf(stderr, "BSD TCP/IP initialization fail\n"); + if(setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof(yes)) < 0) { + fprintf(stderr, "BSD TCP/IP initialization fail (setsockopt)\n"); close(server_socket); return 1; } - if(setsockopt(server_socket, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)) < 0) { - fprintf(stderr, "BSD TCP/IP initialization fail\n"); + if(setsockopt(server_socket, IPPROTO_TCP, TCP_NODELAY, (void*)&yes, sizeof(yes)) < 0) { + fprintf(stderr, "BSD TCP/IP initialization fail (setsockopt)\n"); close(server_socket); return 1; } +#ifdef __MINGW32__ +#else int no = 0; if(setsockopt(server_socket, IPPROTO_IPV6, IPV6_V6ONLY, &no, sizeof(no)) < 0) { - fprintf(stderr, "BSD TCP/IP initialization fail\n"); + fprintf(stderr, "BSD TCP/IP initialization fail (setsockopt)\n"); close(server_socket); return 1; } +#endif memset(&server_address, 0, sizeof(server_address)); +#ifdef __MINGW32__ + server_address.sin_family = AF_INET; + server_address.sin_addr.S_un.S_addr = INADDR_ANY; + server_address.sin_port = htons(port); +#else server_address.sin6_family = AF_INET6; server_address.sin6_addr = in6addr_any; server_address.sin6_port = htons(port); +#endif if(bind(server_socket, (struct sockaddr*)&server_address, sizeof(server_address)) < 0) { - fprintf(stderr, "BSD TCP/IP initialization fail\n"); + fprintf(stderr, "BSD TCP/IP initialization fail (bind)\n"); close(server_socket); return 1; } if(listen(server_socket, 128) < 0) { - fprintf(stderr, "BSD TCP/IP initialization fail\n"); + fprintf(stderr, "BSD TCP/IP initialization fail (listen)\n"); close(server_socket); return 1; } +#ifdef __MINGW32__ +#else signal(SIGCHLD, SIG_IGN); +#endif fprintf(stderr, "BSD TCP/IP initialization successful\n"); return 0; } +#ifdef __MINGW32__ +unsigned int WINAPI pass_sock(LPVOID sockptr){ + int sock = *(int*)sockptr; +#else +void pass_sock(int sock){ +#endif + protocol_init(sock); + protocol_loop(sock); +#ifdef __MINGW32__ + closesocket(sock); +#else + close(sock); +#endif +} + void server_loop(void) { while(1) { struct sockaddr_in claddr; int clen = sizeof(claddr); int sock = accept(server_socket, (struct sockaddr*)&claddr, &clen); +#ifdef __MINGW32__ + HANDLE thread; + thread = (HANDLE)_beginthreadex(NULL, 0, pass_sock, &sock, 0, NULL); +#else pid_t p = fork(); if(p == 0) { - protocol_init(sock); - protocol_loop(sock); + pass_sock(sock); _exit(0); } else { close(sock); } +#endif } } diff --git a/common-decl.mk b/common-decl.mk index b9e0d23..6c99d47 100644 --- a/common-decl.mk +++ b/common-decl.mk @@ -25,4 +25,4 @@ SERVER = ./Server SERVER_MODULE = tcpip.o VERSION = `cat Library/dataworks.c | grep " dataworks_version " | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9]+)?'` -COMPILE_FLAGS = CC="$(CC)" CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" LIBS="$(LIBS)" LIB_PREFIX="$(LIB_PREFIX)" LIB_SUFFIX="$(LIB_SUFFIX)" EXEC_SUFFIX="$(EXEC_SUFFIX)" PLATFORM_M="$(PLATFORM_M)" PLATFORM_P="$(PLATFORM_P)" STATICLIB_PREFIX="$(STATICLIB_PREFIX)" STATICLIB_SUFFIX="$(STATICLIB_SUFFIX)" AR="$(AR)" RANLIB="$(RANLIB)" SHCC="$(SHCC)" LINK_LIB="$(LINK_LIB)" AR_ARGS="$(AR_ARGS)" WINDRES="$(WINDRES)" YACC="$(YACC)" LEX="$(LEX)" LFLAGS="$(LFLAGS)" YFLAGS="$(YFLAGS)" SERVER_MODULE="$(SERVER_MODULE)" +COMPILE_FLAGS = CC="$(CC)" CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" LIBS="$(LIBS)" LIB_PREFIX="$(LIB_PREFIX)" LIB_SUFFIX="$(LIB_SUFFIX)" EXEC_SUFFIX="$(EXEC_SUFFIX)" PLATFORM_M="$(PLATFORM_M)" PLATFORM_P="$(PLATFORM_P)" STATICLIB_PREFIX="$(STATICLIB_PREFIX)" STATICLIB_SUFFIX="$(STATICLIB_SUFFIX)" AR="$(AR)" RANLIB="$(RANLIB)" SHCC="$(SHCC)" LINK_LIB="$(LINK_LIB)" AR_ARGS="$(AR_ARGS)" WINDRES="$(WINDRES)" YACC="$(YACC)" LEX="$(LEX)" LFLAGS="$(LFLAGS)" YFLAGS="$(YFLAGS)" SERVER_MODULE="$(SERVER_MODULE)" SERVLINK_LIB="$(SERVLINK_LIB)" -- 2.43.0