summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--assets/board.js53
-rw-r--r--build.c17
-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
8 files changed, 208 insertions, 88 deletions
diff --git a/assets/board.js b/assets/board.js
index 6e20344..8bd6a61 100644
--- a/assets/board.js
+++ b/assets/board.js
@@ -1,16 +1,6 @@
const assets = [
- "wk.webp",
- "wq.webp",
- "wr.png",
- "wb.webp",
- "wn.png",
- "wp.png",
- "bk.webp",
- "bq.png",
- "br.webp",
- "bb.webp",
- "bn.webp",
- "bp.png"
+ "wk.webp", "wq.webp", "wr.png", "wb.webp", "wn.png", "wp.png",
+ "bk.webp", "bq.png", "br.webp", "bb.webp", "bn.webp", "bp.png"
].map(x => "./assets/pieces/" + x);
let hoveringPiece = -1;
@@ -44,11 +34,8 @@ const BLACK_PAWN = 12;
let counting_bitboard = 0n;
function trackBitboard(i, selected, output) {
const mask = 1n << BigInt(i);
- if (selected) {
- counting_bitboard |= mask;
- } else {
- counting_bitboard &= ~mask;
- }
+ if (selected) counting_bitboard |= mask;
+ else counting_bitboard &= ~mask;
output.textContent = counting_bitboard.toString();
}
@@ -67,7 +54,6 @@ const selectedAction = trackBitboard;
window.onkeydown = (e) => {
if (e.key == 'b') {
- console.log('Bitboards');
console.log(`
position.bitboards[WHITE_KING] = ${bitboards[0].toString()}ULL;
position.bitboards[WHITE_QUEEN] = ${bitboards[1].toString()}ULL;
@@ -91,11 +77,8 @@ let renderingMoves = [];
function updateRenderingMoves() {
for (let i = 0; i < 64; i++) {
const square = document.getElementById(`square-${i}`);
- if (i in renderingMoves) {
- square.children[0].classList.add("possible_move");
- } else {
- square.children[0].classList.remove("possible_move");
- }
+ if (i in renderingMoves) square.children[0].classList.add("possible_move");
+ else square.children[0].classList.remove("possible_move");
}
}
@@ -110,9 +93,20 @@ function addMoves(moves) {
console.log(validMoves);
}
+function handleTransmission(data) {
+ console.log(data);
+}
+
window.onload = () => {
const socket = new WebSocket("ws://localhost:3456");
- socket.onmessage = e => console.log(e.data);
+ let data = [];
+ socket.onmessage = e => {
+ if (e.data == "END-TRANSMISSION") {
+ handleTransmission(data);
+ data = [];
+ }
+ data.push(e.data);
+ }
socket.onopen = () => socket.send("hello");
const output = document.getElementById("output");
@@ -140,11 +134,8 @@ window.onload = () => {
if (e.ctrlKey) {
const selected = square.classList.contains("selected");
selectedAction(bitIndex, !selected, output);
- if (selected) {
- square.classList.remove("selected");
- } else {
- square.classList.add("selected");
- }
+ if (selected) square.classList.remove("selected");
+ else square.classList.add("selected");
return;
}
@@ -154,9 +145,7 @@ window.onload = () => {
(hoveringPiece >= 0 && piece != null)
) return;
if (hoveringPiece < 0) {
- if (validMoves[bitIndex]) {
- renderingMoves = validMoves[bitIndex];
- }
+ if (validMoves[bitIndex]) renderingMoves = validMoves[bitIndex];
setHoverpiece(piece);
square.style.backgroundImage = `none`;
bitboards[piece] &= ~(1n << BigInt(bitIndex));
diff --git a/build.c b/build.c
index 7e5e892..7f378f4 100644
--- a/build.c
+++ b/build.c
@@ -92,6 +92,14 @@ int main(int argc, char** argv) {
if (strcmp(argv[1], "server") == 0) {
printf("Server mode\n");
if (fork() == 0) {
+ char* args[] = {"rm", "/dev/mqueue/server_to_engine", "/dev/mqueue/engine_to_server", NULL};
+ print_cmd(args);
+ execvp(args[0], args);
+ return 0;
+ } else {
+ wait(NULL);
+ }
+ if (fork() == 0) {
char* args[] = {"gcc", "src/server.c", "-lcrypto", "-o", "output/server.o", NULL};
print_cmd(args);
execvp(args[0], args);
@@ -100,6 +108,15 @@ int main(int argc, char** argv) {
wait(NULL);
}
+ if (fork() == 0) {
+ char* args[] = {"gcc", "src/main.c", "-o", "output/main.o", NULL};
+ print_cmd(args);
+ execvp(args[0], args);
+ return 0;
+ } else {
+ wait(NULL);
+ }
+
char* args[] = {"./output/server.o", "./output/main.o", NULL};
print_cmd(args);
execvp(args[0], args);
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;
+}