summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAargh Rai <aargh.rai+git@gmail.com>2026-05-27 17:13:14 +0530
committerAargh Rai <aargh.rai+git@gmail.com>2026-05-27 17:13:14 +0530
commit49cc88def2f9edea59e79ca37f41400ad870579e (patch)
tree55d84a76920dd4af7a7a2054d553ce29d11e726c /src
parent380d67bada7544f5f10cecc8c9dff7ed2f79e187 (diff)
went through all, but comms is working
Diffstat (limited to 'src')
-rw-r--r--src/ipc.h8
-rw-r--r--src/main.c22
-rw-r--r--src/server.c89
-rw-r--r--src/server/base64.c27
-rw-r--r--src/server/comms.c52
-rw-r--r--src/server/ws_key.c28
6 files changed, 170 insertions, 56 deletions
diff --git a/src/ipc.h b/src/ipc.h
new file mode 100644
index 0000000..3a49f52
--- /dev/null
+++ b/src/ipc.h
@@ -0,0 +1,8 @@
+#include <mqueue.h>
+
+struct mq_attr attr = {
+ .mq_flags = 0,
+ .mq_maxmsg = 10,
+ .mq_msgsize = 1024,
+ .mq_curmsgs = 0,
+};
diff --git a/src/main.c b/src/main.c
index a929a54..24d1d84 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,8 +1,28 @@
#include <stdio.h>
+#include <assert.h>
#include "bitboard.h"
#include "search.h"
-int main() {
+void comms_main();
+int direct_run();
+
+int main(int argc, char** argv) {
+ if (argc == 1) return direct_run();
+ assert(argc == 2);
+ comms_main();
+ return 0;
+}
+
+#include "ipc.h"
+void comms_main() {
+ mqd_t tx = mq_open("/engine_to_server", O_WRONLY);
+ mqd_t rx = mq_open("/server_to_engine", O_RDONLY);
+
+ char msg[] = "hello";
+ mq_send(tx, msg, sizeof(msg), 0);
+}
+
+int direct_run() {
moves_t moves = moves_init();
position_t position = position_starting();
diff --git a/src/server.c b/src/server.c
index 8de6d01..83f9349 100644
--- a/src/server.c
+++ b/src/server.c
@@ -5,67 +5,35 @@
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
+#include <sys/wait.h>
+#include <assert.h>
#include <unistd.h>
#define PORT 3456
-static const char *GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
-static const char b64[] =
- "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- "abcdefghijklmnopqrstuvwxyz"
- "0123456789+/";
+#include "server/base64.c"
+#include "server/ws_key.c"
+#include "server/comms.c"
+#include "ipc.h"
-void base64_encode(
- const unsigned char *in,
- size_t len,
- char *out
-) {
- size_t i, j = 0;
+int main(int argc, char** argv) {
+ assert(argc == 2);
- for (i = 0; i < len; i += 3) {
- unsigned v = in[i] << 16;
+ mqd_t tx = mq_open("/server_to_engine", O_CREAT | O_WRONLY, 0666, &attr);
+ mqd_t rx = mq_open("/engine_to_server", O_CREAT | O_RDONLY, 0666, &attr);
- if (i + 1 < len) v |= in[i + 1] << 8;
- if (i + 2 < len) v |= in[i + 2];
-
- out[j++] = b64[(v >> 18) & 63];
- out[j++] = b64[(v >> 12) & 63];
-
- out[j++] = (i + 1 < len) ? b64[(v >> 6) & 63] : '=';
- out[j++] = (i + 2 < len) ? b64[v & 63] : '=';
+ if (fork() == 0) {
+ char* args[] = {argv[1], "listen", NULL};
+ execvp(args[0], args);
+ return 0;
}
- out[j] = 0;
-}
-
-void websocket_accept_key(const char *client_key, char *output) {
- char buf[256];
- snprintf(buf, sizeof(buf), "%s%s", client_key, GUID);
-
- unsigned char hash[SHA_DIGEST_LENGTH];
- SHA1((unsigned char *)buf, strlen(buf), hash);
- base64_encode(hash, SHA_DIGEST_LENGTH, output);
-}
-
-char *find_websocket_key(char *req) {
- char *p = strstr(req, "Sec-WebSocket-Key:");
+ char* buf = malloc(attr.mq_msgsize);
+ int n = mq_receive(rx, buf, attr.mq_msgsize, NULL);
+ printf("%s", buf);
+ return 0;
- if (!p) return NULL;
- p += strlen("Sec-WebSocket-Key:");
-
- while (*p == ' ') p++;
-
- static char key[128];
- int i = 0;
- while (*p && *p != '\r' && *p != '\n')
- key[i++] = *p++;
-
- key[i] = 0;
- return key;
-}
-
-int main(void) {
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
int opt = 1;
@@ -88,6 +56,7 @@ int main(void) {
int n = recv(client, req, sizeof(req) - 1, 0);
if (n <= 0) {
+ printf("Client close");
close(client);
continue;
}
@@ -140,14 +109,24 @@ int main(void) {
for (unsigned i = 0; i < len; i++)
payload[i] ^= mask[i % 4];
- printf("Message: %.*s\n", len, payload);
+ char* payload_ptr = payload;
+ struct strs response = handle_input(payload_ptr, len);
unsigned char out[128];
out[0] = 0x81;
- out[1] = len;
-
- memcpy(out + 2, payload, len);
- send(client, out, len + 2, 0);
+ for (int i = 0; i < response.count; i++) {
+ struct str item = response.data[i];
+ out[1] = item.len;
+
+ memcpy(out + 2, item.msg, item.len);
+ send(client, out, item.len + 2, 0);
+ }
+ char end_transmission[] = "END-TRANSMISSION";
+ int n = strlen(end_transmission);
+ out[1] = n;
+
+ memcpy(out + 2, end_transmission, n);
+ send(client, out, n + 2, 0);
}
close(client);
}
diff --git a/src/server/base64.c b/src/server/base64.c
new file mode 100644
index 0000000..3400cfb
--- /dev/null
+++ b/src/server/base64.c
@@ -0,0 +1,27 @@
+static const char b64[] =
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvwxyz"
+ "0123456789+/";
+
+void base64_encode(
+ const unsigned char *in,
+ size_t len,
+ char *out
+) {
+ size_t i, j = 0;
+
+ for (i = 0; i < len; i += 3) {
+ unsigned v = in[i] << 16;
+
+ if (i + 1 < len) v |= in[i + 1] << 8;
+ if (i + 2 < len) v |= in[i + 2];
+
+ out[j++] = b64[(v >> 18) & 63];
+ out[j++] = b64[(v >> 12) & 63];
+
+ out[j++] = (i + 1 < len) ? b64[(v >> 6) & 63] : '=';
+ out[j++] = (i + 2 < len) ? b64[v & 63] : '=';
+ }
+
+ out[j] = 0;
+}
diff --git a/src/server/comms.c b/src/server/comms.c
new file mode 100644
index 0000000..7b8001b
--- /dev/null
+++ b/src/server/comms.c
@@ -0,0 +1,52 @@
+#include <assert.h>
+
+struct str {
+ char msg[126];
+ int len;
+};
+
+void set_output(struct str* output, char* msg) {
+ int i = 0;
+ while (msg[i] != 0) {
+ assert(i < 126);
+ output->msg[i] = msg[i];
+ i++;
+ }
+ output->len = i;
+}
+
+struct strs {
+ struct str* data;
+ int capacity;
+ int count;
+};
+
+struct strs init_strs() {
+ return (struct strs) {
+ .data = malloc(sizeof(struct str) * 10),
+ .count = 0,
+ .capacity = 10,
+ };
+}
+void add_str(struct strs* strs, char* str) {
+ if (strs->count + 1 > strs->capacity) {
+ strs->capacity += 30;
+ strs->data = realloc(strs->data, sizeof(struct str) * strs->capacity);
+ }
+
+ struct str item;
+ set_output(&item, str);
+
+ strs->data[strs->count] = item;
+ strs->count++;
+}
+
+struct strs handle_input(char* payload, int len) {
+ printf("Message: %.*s\n", len, payload);
+ struct strs output = init_strs();
+
+ add_str(&output, "nigga");
+ add_str(&output, "nigga2");
+
+ return output;
+}
diff --git a/src/server/ws_key.c b/src/server/ws_key.c
new file mode 100644
index 0000000..ca66b81
--- /dev/null
+++ b/src/server/ws_key.c
@@ -0,0 +1,28 @@
+static const char *GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
+
+void websocket_accept_key(const char *client_key, char *output) {
+ char buf[256];
+ snprintf(buf, sizeof(buf), "%s%s", client_key, GUID);
+
+ unsigned char hash[SHA_DIGEST_LENGTH];
+ SHA1((unsigned char *)buf, strlen(buf), hash);
+ base64_encode(hash, SHA_DIGEST_LENGTH, output);
+}
+
+char *find_websocket_key(char *req) {
+ char *p = strstr(req, "Sec-WebSocket-Key:");
+
+ if (!p) return NULL;
+
+ p += strlen("Sec-WebSocket-Key:");
+
+ while (*p == ' ') p++;
+
+ static char key[128];
+ int i = 0;
+ while (*p && *p != '\r' && *p != '\n')
+ key[i++] = *p++;
+
+ key[i] = 0;
+ return key;
+}