[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [gnugo-devel] socket support for Windows: patch
From: |
Nando |
Subject: |
Re: [gnugo-devel] socket support for Windows: patch |
Date: |
Fri, 17 Dec 2004 21:24:12 +0100 |
Hi,
Just barging in to make a small point in the discussion: it _is_ possible
to use Windows sockets with buffered i/o functions like fgets(). The
appended patch illustrates how it could be done.
I made a small GTP session connecting to a listening gnugo process with
telnet. I didn't test fancy things, but everything seemed ok (boardsize,
version, genmove, showboard, etc)
Caveats:
- it will work on NT/2K/XP/2K3, I'm less sure about Win9x/ME, but if the
MS docs are correct (*cough*), it should.
- it will definitely _not_ work on WinCE : as I explained a couple days
ago, read() in the VC++ CRT is based on the Win32 ReadFile() API, which
doesn't accept sockets as file handles in WinCE (but does in Win32)
As a conclusion : I think it is possible to make a light patch that allows
Windows builds to use sockets. But it won't help even a bit for WinCE
platforms.
-- nando
PS: the patch below is meant for discussion, the small change to config.vc
was intentionnally omitted.
Index: interface/main.c
===================================================================
RCS file: /cvsroot/gnugo/gnugo/interface/main.c,v
retrieving revision 1.109
diff -u -r1.109 main.c
--- interface/main.c 7 Dec 2004 04:50:02 -0000 1.109
+++ interface/main.c 17 Dec 2004 19:56:40 -0000
@@ -1686,12 +1686,40 @@
#if ENABLE_SOCKET_SUPPORT
-
+#ifdef HAVE_VISUAL_C
+#include <fcntl.h>
+#include <winsock.h>
+#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
+#endif
+
+#ifdef HAVE_VISUAL_C
+
+static void
+init_winsock(void)
+{
+ WORD wVersionRequested;
+ WSADATA wsaData;
+ int err;
+ int sock_opt = SO_SYNCHRONOUS_NONALERT;
+
+ wVersionRequested = MAKEWORD(1, 1);
+ err = WSAStartup(wVersionRequested, &wsaData);
+ if (err != 0) {
+ fprintf(stderr, "WSAStartup failed\n");
+ exit(EXIT_FAILURE);
+ }
+/* Enable the use of sockets as filehandles
+ */
+ setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (char *)&sock_opt,
+ sizeof(sock_opt));
+}
+
+#endif
static void
socket_connect_to(const char *host_name, unsigned int port,
@@ -1702,6 +1730,12 @@
struct hostent *host_data;
char **address_pointer;
+#ifdef HAVE_VISUAL_C
+ int osfh;
+
+ init_winsock();
+#endif
+
if (!host_name)
host_name = "127.0.0.1";
@@ -1736,8 +1770,14 @@
exit(EXIT_FAILURE);
}
+#ifdef HAVE_VISUAL_C
+ osfh = _open_osfhandle(connection_socket, _O_TEXT);
+ *input_file = fdopen(osfh, "r");
+ *output_file = fdopen(dup(osfh), "w");
+#else
*input_file = fdopen(connection_socket, "r");
*output_file = fdopen(dup(connection_socket), "w");
+#endif
}
@@ -1749,6 +1789,12 @@
int listening_socket;
int connection_socket;
+#ifdef HAVE_VISUAL_C
+ int osfh;
+
+ init_winsock();
+#endif
+
if (host_name) {
struct hostent *host_data;
@@ -1800,8 +1846,14 @@
close(listening_socket);
+#ifdef HAVE_VISUAL_C
+ osfh = _open_osfhandle(connection_socket, _O_TEXT);
+ *input_file = fdopen(osfh, "r");
+ *output_file = fdopen(dup(osfh), "w");
+#else
*input_file = fdopen(connection_socket, "r");
*output_file = fdopen(dup(connection_socket), "w");
+#endif
}