]> Nishi Git Mirror - dataworks.git/commitdiff
windows works
authornishi <nishi@d4a5a174-5a4a-5b4b-b672-37683c10d7d5>
Sat, 8 Jun 2024 09:24:58 +0000 (09:24 +0000)
committernishi <nishi@d4a5a174-5a4a-5b4b-b672-37683c10d7d5>
Sat, 8 Jun 2024 09:24:58 +0000 (09:24 +0000)
git-svn-id: file:///raid/svn-main/nishi-dataworks/trunk@191 d4a5a174-5a4a-5b4b-b672-37683c10d7d5

Platforms/win32.mk
Platforms/win64.mk
Server/Makefile
Server/tcpip.c
common-decl.mk

index e46aa4e0b66b6eee1275edcdad30f69ba3b3f5ff..a70b3415545cb11d128a83e649e97f67dd1875c6 100644 (file)
@@ -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
index c5db3228eeeda058a7258e5a28b317e07f185961..80bb12d0d19d5cd3652724fe91cb10ed0e85ed77 100644 (file)
@@ -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
index e017f9238e2e27affbc2284ce071fea9b0f7fbf5..d2e5c62373e3b43f7b72cb291e6d8b0f7fbb33fd 100644 (file)
@@ -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
index edfd78a5647288f2a4919f849bf846e635ac569a..a7f9d8fd7e1cdab9e0731cd986b7fc0571b4edea 100644 (file)
@@ -41,11 +41,17 @@ extern char** argv;
 extern bool auth;
 extern char* authfile;
 
+#ifdef __MINGW32__
+#include <winsock2.h>
+#include <windows.h>
+#include <process.h>
+#else
 #include <arpa/inet.h>
 #include <netinet/in.h>
 #include <netinet/tcp.h>
 #include <signal.h>
 #include <sys/socket.h>
+#endif
 #include <unistd.h>
 
 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
        }
 }
 
index b9e0d23bef07a72fe41c0a7fb953939eda75b0c5..6c99d475b0f47925a3d2bcfd81803b62b2f37055 100644 (file)
@@ -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)"