tExperimental Unix socket server code - vaccinewars - be a doctor and try to vaccinate the world
git clone git://src.adamsgaard.dk/vaccinewars
Log
Files
Refs
README
LICENSE
---
commit 87b12c1916c79c572b09130d122ddb2d9ff540c0
parent 8a6c2d8ac4ed2160d53739ec55efc20f0847974f
Author: Ben Webb 
Date:   Tue, 30 Oct 2001 18:59:11 +0000

Experimental Unix socket server code


Diffstat:
  M src/serverside.c                    |      92 ++++++++++++++++++++++++++++++-

1 file changed, 91 insertions(+), 1 deletion(-)
---
diff --git a/src/serverside.c b/src/serverside.c
t@@ -34,6 +34,7 @@
 #else
 #include  /* For struct sockaddr etc. */
 #include  /* For struct sockaddr_in etc. */
+#include      /* For struct sockaddr_un */
 #include   /* For socklen_t */
 #endif /* CYGWIN */
 
t@@ -880,11 +881,45 @@ void RemovePlayerFromServer(Player *Play) {
    FirstServer=RemovePlayer(Play,FirstServer);
 }
 
+#ifndef CYGWIN
+static void CloseLocalSocket(int localsock) {
+  if (localsock>=0) close(localsock);
+  unlink("/tmp/.dopewars/socket");
+  rmdir("/tmp/.dopewars");
+}
+
+static int SetupLocalSocket(void) {
+  int sock;
+  struct sockaddr_un addr;
+
+  CloseLocalSocket(-1);
+
+  sock = socket(PF_UNIX,SOCK_STREAM,0);
+  if (sock==-1) return -1;
+
+  SetBlocking(sock,FALSE);
+
+  mkdir("/tmp/.dopewars",S_IRUSR|S_IWUSR|S_IXUSR);
+
+  addr.sun_family = AF_UNIX;
+  strncpy(addr.sun_path,"/tmp/.dopewars/socket",sizeof(addr.sun_path));
+  addr.sun_path[sizeof(addr.sun_path)-1]='\0';
+
+  bind(sock,(struct sockaddr *)&addr,sizeof(struct sockaddr_un));
+
+  chmod("/tmp/.dopewars/socket",S_IRUSR|S_IWUSR);
+
+  listen(sock,10);
+  
+  return sock;
+}
+#endif
+
 void ServerLoop() {
 /* Initialises server, processes network and interactive messages, and */
 /* finally cleans up the server on exit.                               */
    Player *tmp;
-   GSList *list,*nextlist;
+   GSList *list,*nextlist,*localconn=NULL;
    fd_set readfs,writefs,errorfs;
    int topsock;
 // gboolean InputClosed=FALSE;
t@@ -893,11 +928,18 @@ void ServerLoop() {
    GString *LineBuf;
    gboolean EndOfLine,DoneOK;
    gchar *buf;
+#ifndef CYGWIN
+   int localsock;
+#endif
 
 // if (fork()>0) return;
 
    StartServer();
 
+#ifndef CYGWIN
+   localsock=SetupLocalSocket();
+#endif
+
    LineBuf=g_string_new("");
    while (1) {
       FD_ZERO(&readfs);
t@@ -907,6 +949,15 @@ void ServerLoop() {
       FD_SET(ListenSock,&readfs);
       FD_SET(ListenSock,&errorfs);
       topsock=ListenSock+1;
+#ifndef CYGWIN
+      FD_SET(localsock,&readfs);
+      topsock=MAX(topsock,localsock+1);
+      for (list=localconn;list;list=g_slist_next(list)) {
+        NetworkBuffer *netbuf;
+        netbuf = (NetworkBuffer *)list->data;
+        SetSelectForNetworkBuffer(netbuf,&readfs,&writefs,&errorfs,&topsock);
+      }
+#endif
       if (MetaConn) {
          SetSelectForNetworkBuffer(&MetaConn->NetBuf,&readfs,&writefs,
                                    &errorfs,&topsock);
t@@ -957,6 +1008,42 @@ void ServerLoop() {
       if (FD_ISSET(ListenSock,&readfs)) {
          HandleNewConnection();
       }
+#ifndef CYGWIN
+      if (FD_ISSET(localsock,&readfs)) {
+        int newlocal;
+        NetworkBuffer *netbuf;
+        newlocal = accept(localsock,NULL,NULL);
+        netbuf=g_new(NetworkBuffer,1);
+        InitNetworkBuffer(netbuf,'\n','\r',NULL);
+        BindNetworkBufferToSocket(netbuf,newlocal);
+        SetBlocking(newlocal,FALSE);
+        localconn = g_slist_append(localconn,netbuf);
+g_print("New connection on Unix socket\n");
+      }
+      list=localconn;
+      while (list) {
+        NetworkBuffer *netbuf;
+        nextlist=g_slist_next(list);
+        netbuf = (NetworkBuffer *)list->data;
+        if (netbuf) {
+          if (RespondToSelect(netbuf,&readfs,&writefs,&errorfs,&DoneOK)) {
+            while((buf=GetWaitingMessage(netbuf))!=NULL) {
+              g_print("Unix message received: %s\n",buf);
+              HandleServerCommand(buf);
+              g_free(buf);
+            }
+          }
+          if (!DoneOK) {
+g_print("Unix socket closed\n");
+            localconn = g_slist_remove(localconn,netbuf);
+            ShutdownNetworkBuffer(netbuf);
+            g_free(netbuf);
+          }
+          list=nextlist;
+        }
+      }
+      if (IsServerShutdown()) break;
+#endif
       if (MetaConn) {
          if (RespondToSelect(&MetaConn->NetBuf,&readfs,&writefs,
                              &errorfs,&DoneOK)) {
t@@ -997,6 +1084,9 @@ void ServerLoop() {
       }
       if (list && IsServerShutdown()) break;
    }
+#ifndef CYGWIN
+   CloseLocalSocket(localsock);
+#endif
    StopServer();
    g_string_free(LineBuf,TRUE);
 }