From c56cc12c154b3f21411d71e10f4e8a5a9682355b Mon Sep 17 00:00:00 2001 From: Aargh Rai Date: Thu, 28 May 2026 00:09:50 +0530 Subject: the moves now make it to the frontend i know the issue which exists, where the server can't handle the request if i reload the page --- src/engine.c | 39 +++++++++++++++++++++++---------------- src/engine/moves.c | 9 +++++++++ src/engine/moves.h | 2 ++ src/engine/moves/king.c | 1 - src/ipc.c | 15 +++++++++++++-- src/server/comms.c | 28 ++++++++++++++++++---------- 6 files changed, 65 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/engine.c b/src/engine.c index eec420d..9c848dd 100644 --- a/src/engine.c +++ b/src/engine.c @@ -13,6 +13,14 @@ int main(int argc, char** argv) { return 0; } +struct sqrstr { + char data[2]; +}; +struct sqrstr sqr_to_str(int i) { + assert(i < 64); + return (struct sqrstr) { .data = {(i / 10) + '0', (i % 10) + '0' } }; +} + #include "ipc.c" void comms_main() { mqd_t tx = mq_open("/engine_to_server", O_WRONLY); @@ -21,22 +29,21 @@ void comms_main() { moves_t moves = moves_init(); position_t position = position_starting(); - send_queue(tx, "hello"); - send_queue(tx, "hello2"); - // send_queue(tx, "hello"); - - // moves_t moves = moves_init(); - // position_t position = position_starting(); - // // get_queen_moves(&moves, position); - // - // bitboard_t b = 0; - // - // for (int i = 0; i < moves.length; i++) { - // b |= (bitboard_t)1 << moves.moves[i].to; - // printf("%d %d | ", (moves.moves[i].from), (moves.moves[i].to)); - // } - // - // print_bitboard(b); + get_moves(&moves, position); + char buf[128]; + for (int i = 0; i <= moves.length / 32; i++) { + for (int ii = 0; ii < 32; ii++) { + if (moves.length <= 4 * i + ii) continue; + // printf("%d\n", 4 * i + ii); + struct sqrstr from_str = sqr_to_str(moves.moves[4 * i + ii].from); + struct sqrstr to_str = sqr_to_str(moves.moves[4 * i + ii].to); + buf[4 * ii + 0] = from_str.data[0]; + buf[4 * ii + 1] = from_str.data[1]; + buf[4 * ii + 2] = to_str.data[0]; + buf[4 * ii + 3] = to_str.data[1]; + } + send_queue(tx, buf, sizeof(buf)); + } } int direct_run() { diff --git a/src/engine/moves.c b/src/engine/moves.c index 662aa54..ac9fbba 100644 --- a/src/engine/moves.c +++ b/src/engine/moves.c @@ -5,3 +5,12 @@ #include "moves/rook.c" #include "moves/bishop.c" #include "moves/queen.c" + +void get_moves(moves_t* moves, position_t position) { + get_pawn_moves(moves, position); + get_knight_moves(moves, position); + get_king_moves(moves, position); + get_rook_moves(moves, position); + get_bishop_moves(moves, position); + get_queen_moves(moves, position); +} diff --git a/src/engine/moves.h b/src/engine/moves.h index 8081ffe..b63b9e2 100644 --- a/src/engine/moves.h +++ b/src/engine/moves.h @@ -30,6 +30,8 @@ void get_rook_moves(moves_t* moves, position_t position); void get_bishop_moves(moves_t* moves, position_t position); void get_queen_moves(moves_t* moves, position_t position); +void get_moves(moves_t* moves, position_t position); + void __forloop_rook_moves_gen( moves_t* moves, bitboard_t friendly_type, diff --git a/src/engine/moves/king.c b/src/engine/moves/king.c index 0637109..6e8d105 100644 --- a/src/engine/moves/king.c +++ b/src/engine/moves/king.c @@ -28,7 +28,6 @@ void get_king_moves(moves_t* moves, position_t position) { } movement &= ~friendly_pieces; - print_bitboard(movement); while (movement) { int to = __builtin_ctzll(movement); movement &= movement - 1; diff --git a/src/ipc.c b/src/ipc.c index efdb8ce..a55e22e 100644 --- a/src/ipc.c +++ b/src/ipc.c @@ -21,8 +21,19 @@ char* read_queue(mqd_t target) { return buf; } -void send_queue(mqd_t target, const char* msg) { - mq_send(target, msg, sizeof(msg), 0); +void send_queue(mqd_t target, const char* msg, int n) { + mq_send(target, msg, n, 0); +} + +int get_num_messages(mqd_t target) { + struct mq_attr target_attr; + + if (mq_getattr(target, &target_attr) == -1) { + perror("mq_getattr"); + exit(1); + } + + return target_attr.mq_curmsgs; } #endif // IPC_C diff --git a/src/server/comms.c b/src/server/comms.c index c96afa8..4549bdd 100644 --- a/src/server/comms.c +++ b/src/server/comms.c @@ -43,18 +43,26 @@ void add_str(struct strs* strs, char* str) { } struct strs handle_input(mqd_t tx, mqd_t rx, char* payload, int len) { - printf("Message: %.*s\n", len, payload); - struct strs output = init_strs(); + printf("Message: %.*s", len, payload); + fflush(stdout); - char* buf = read_queue(rx); - printf("%p\n", buf); - add_str(&output, buf); - if (buf) free(buf); + struct strs output = init_strs(); - buf = read_queue(rx); - printf("%p\n", buf); - add_str(&output, buf); - if (buf) free(buf); + int n = get_num_messages(rx); + while (n > 0) { + char* buf = read_queue(rx); + add_str(&output, buf); + free(buf); + n--; + if (n == 0) n = get_num_messages(rx); + } + printf(" -> done\n"); + // if (buf) free(buf); + // + // buf = read_queue(rx); + // printf("%p\n", buf); + // add_str(&output, buf); + // if (buf) free(buf); return output; } -- cgit v1.2.3