+ 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);
+