summaryrefslogtreecommitdiff
path: root/src/fscomch.c
diff options
context:
space:
mode:
authorKai Stevenson <kai@kaistevenson.com>2023-03-20 02:19:02 -0700
committerKai Stevenson <kai@kaistevenson.com>2023-03-20 02:19:02 -0700
commit1a3b3fb05f86e357b099812c0a09a084934fd111 (patch)
treef3d4c0b9961d70b672e9e2220ae5d17a20e77eb3 /src/fscomch.c
parente33dc4755fb08ed08f30b5e1af65457556d5bcb7 (diff)
minimum viable implementation of fscomch works now
Diffstat (limited to 'src/fscomch.c')
-rw-r--r--src/fscomch.c32
1 files changed, 27 insertions, 5 deletions
diff --git a/src/fscomch.c b/src/fscomch.c
index a9fc27b..412f9cf 100644
--- a/src/fscomch.c
+++ b/src/fscomch.c
@@ -4,6 +4,7 @@
#include <unistd.h>
#include <time.h>
#include <sys/stat.h>
+#include <string.h>
#include "fscomch.h"
match_t init_match(char* uuid, FILE* streamIn, FILE* streamOut) {
@@ -38,16 +39,15 @@ match_t await_match(FILE* matchFile, char* cancelToken) {
struct timespec req;
req.tv_sec = 0;
req.tv_nsec = awaitMatchNs;
- printf("sleeping\n");
nanosleep(&req, NULL);
}
- /* connect to files created by joining player */
+ /* connect to streams */
char aName[256];
char bName[256];
snprintf(aName, 256, "/tmp/chess/%s.a", uuid);
snprintf(bName, 256, "/tmp/chess/%s.b", uuid);
FILE* streamIn = fopen(aName, "r");
- FILE* streamOut = fopen(bName, "w");
+ FILE* streamOut = fopen(bName, "a");
/* connection complete, delete the match file and the join lock */
remove("/tmp/chess/match");
remove("/tmp/chess/matchjoin");
@@ -65,21 +65,43 @@ match_t join_match() {
/* this is definitely the only process joining the game
proceed to generate a uuid */
char* uuid = make_uuid();
- /* make the .a and .b files based on the uuid */
+ /* create and connect to streams */
char aName[256];
char bName[256];
snprintf(aName, 256, "/tmp/chess/%s.a", uuid);
snprintf(bName, 256, "/tmp/chess/%s.b", uuid);
FILE* streamIn = fopen(bName, "w+");
- FILE* streamOut = fopen(aName, "w");
+ FILE* streamOut = fopen(aName, "a");
match_t match = init_match(uuid, streamIn, streamOut);
/* send uuid to the host */
FILE* matchFile = fopen("/tmp/chess/match", "a");
fputs(uuid, matchFile);
+ fclose(matchFile);
/* joining match complete, close the descriptor */
close(fd);
return match;
}
+void send_message(char* content, match_t match) {
+ fprintf(match.streamOut, "%s\n", content);
+ fflush(match.streamOut);
+}
+char* read_message(match_t match, char* cancelToken) {
+ char* buffer = calloc(256, sizeof(char));
+ while (cancelToken == NULL || *cancelToken) {
+ if (fgets(buffer, 256, match.streamIn) != NULL) {
+ break;
+ }
+ /* wtf? */
+ fseek(match.streamIn, ftell(match.streamIn), SEEK_SET);
+ struct timespec req;
+ req.tv_sec = 0;
+ req.tv_nsec = awaitMoveNs;
+ nanosleep(&req, NULL);
+ }
+ /* overwrite potential newline */
+ buffer[strlen(buffer) - 1] = '\0';
+ return buffer;
+}
char* make_uuid() {
char sym[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c', 'd', 'e', 'f'};
char* out = calloc(37, sizeof(char));