From: Aaron Taylor Date: Thu, 27 May 2021 00:28:49 +0000 (-0700) Subject: Added a bidirectional pipe between two client instances for twoplayer mode. X-Git-Url: http://git.subgeniuskitty.com/sgk-go/.git/commitdiff_plain/b0d6e9e52888e55bf5ce0725d8f898074b5f65f5 Added a bidirectional pipe between two client instances for twoplayer mode. --- diff --git a/interface/play_twoplayer.c b/interface/play_twoplayer.c index e809cbf..908cbb3 100644 --- a/interface/play_twoplayer.c +++ b/interface/play_twoplayer.c @@ -24,10 +24,16 @@ #include "gnugo.h" #include +#include #include #include #include #include +#include +#include +#include + +#define _XOPEN_SOURCE 500 #if READLINE #include @@ -50,6 +56,9 @@ showmoyo display moyo\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" @@ -657,6 +666,37 @@ void play_twoplayer(SGFTree* tree, Gameinfo* gameinfo, char* filename, char* unt { 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; @@ -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"); + close(transmit_fifo_fd); + close(receive_fifo_fd); + /* main() frees the tree and we might have changed it. */ *tree = sgftree; }