Added a bidirectional pipe between two client instances for twoplayer mode.
authorAaron Taylor <ataylor@subgeniuskitty.com>
Thu, 27 May 2021 00:28:49 +0000 (17:28 -0700)
committerAaron Taylor <ataylor@subgeniuskitty.com>
Thu, 27 May 2021 00:28:49 +0000 (17:28 -0700)
interface/play_twoplayer.c

index e809cbf..908cbb3 100644 (file)
 #include "gnugo.h"
 
 #include <ctype.h>
 #include "gnugo.h"
 
 #include <ctype.h>
+#include <fcntl.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#define _XOPEN_SOURCE 500
 
 #if READLINE
 #include <readline/history.h>
 
 #if READLINE
 #include <readline/history.h>
@@ -50,6 +56,9 @@ showmoyo         display moyo\n\
 showterri        display territory\n\
 "
 
 showterri        display territory\n\
 "
 
+#define MASTER_FIFO_NAME "/tmp/sgkgo-fifo-master"
+#define SLAVE_FIFO_NAME  "/tmp/sgkgo-fifo-slave"
+
 /* ANSI color codes */
 #define RESETCOLOR "\x1B[0m"
 #define WHITECOLOR "\x1B[41m"
 /* ANSI color codes */
 #define RESETCOLOR "\x1B[0m"
 #define WHITECOLOR "\x1B[41m"
@@ -657,6 +666,37 @@ void play_twoplayer(SGFTree* tree, Gameinfo* gameinfo, char* filename, char* unt
 {
     int sz;
 
 {
     int sz;
 
+    int transmit_fifo_fd, receive_fifo_fd;
+    printf("DEBUG: Attempting to setup connection with other player.\n");
+    if (mknod(MASTER_FIFO_NAME, S_IFIFO | 0666, 0) == 0) {
+        printf("DEBUG: We are the master. Created %s.\n", MASTER_FIFO_NAME);
+        // TODO: Choose black for the master.
+        printf("DEBUG: Waiting for other player to connect.\n");
+        transmit_fifo_fd = open(MASTER_FIFO_NAME, O_WRONLY);
+        printf("DEBUG: Slave connected to our FIFO. Now we connect to the slave's FIFO.\n");
+        receive_fifo_fd = open(SLAVE_FIFO_NAME, O_RDONLY);
+        printf("DEBUG: Bidirectional communication established.\n");
+    } else {
+        printf("DEBUG: Failed to create master FIFO. Perhaps we are the slave?\n");
+        // TODO: Choose white for the slave.
+        if (mknod(SLAVE_FIFO_NAME, S_IFIFO | 0666, 0) == 0) {
+            printf("DEBUG: We are the slave. Created %s.\n", SLAVE_FIFO_NAME);
+            receive_fifo_fd = open(MASTER_FIFO_NAME, O_RDONLY);
+            printf("DEBUG: We connected to master's FIFO. Now we wait for master's connection to our FIFO.\n");
+            transmit_fifo_fd = open(SLAVE_FIFO_NAME, O_WRONLY);
+            printf("DEBUG: Bidirectional communication established.\n");
+        } else {
+            printf("DEBUG: Failed to create slave FIFO. This is an unrecoverable error.\n");
+            exit(EXIT_FAILURE);
+        }
+    }
+    // Allow both master and slave to attempt removal. Ignore error code since
+    // this is a best-effort situation for now.
+    unlink(MASTER_FIFO_NAME);
+    unlink(SLAVE_FIFO_NAME);
+while(1){sleep(1);}
+exit(EXIT_SUCCESS);
+
     setvbuf(stdout, (char*)NULL, _IONBF, 0); /* No buffering. */
 
     sgftree = *tree;
     setvbuf(stdout, (char*)NULL, _IONBF, 0); /* No buffering. */
 
     sgftree = *tree;
@@ -684,6 +724,9 @@ void play_twoplayer(SGFTree* tree, Gameinfo* gameinfo, char* filename, char* unt
     do_play_twoplayer(gameinfo);
     printf("\nThanks! for playing GNU Go.\n\n");
 
     do_play_twoplayer(gameinfo);
     printf("\nThanks! for playing GNU Go.\n\n");
 
+    close(transmit_fifo_fd);
+    close(receive_fifo_fd);
+
     /* main() frees the tree and we might have changed it. */
     *tree = sgftree;
 }
     /* main() frees the tree and we might have changed it. */
     *tree = sgftree;
 }