#ifndef IPC_C #define IPC_C #include #include #include "engine/fen.h" typedef struct { struct uci_move *moves; int count; int capacity; } comm_moves; void add_comm_move(comm_moves *moves, struct uci_move move) { if (moves->count + 1 > moves->capacity) { moves->capacity += 50; moves->moves = realloc(moves->moves, moves->capacity * sizeof(struct uci_move)); } moves->moves[moves->count++] = move; } typedef struct { bool engine_message_ready; int depth; int seldepth; int multipv; int score_cp; int nodes; int nps; int hashfull; int tbhits; int time; comm_moves pv; bool uci_message_ready; struct fen_load from_position; comm_moves moves; } comms; // https://stackoverflow.com/a/5656561 comms* create_shared_memory() { int protection = PROT_READ | PROT_WRITE; int visibility = MAP_SHARED | MAP_ANONYMOUS; comms* state = (comms*)mmap( NULL, sizeof(comms), protection, visibility, -1, 0 ); state->engine_message_ready = false; state->depth = 0; state->seldepth = 0; state->multipv = 0; state->score_cp = 0; state->nodes = 0; state->nps = 0; state->hashfull = 0; state->tbhits = 0; state->time = 0; state->pv = (comm_moves) { (comms*)mmap( NULL, sizeof(struct uci_move) * 50, protection, visibility, -1, 0 ), 0, 50 }; state->uci_message_ready = false; state->from_position = {0}; state->moves = (comm_moves) { (comms*)mmap( NULL, sizeof(struct uci_move) * 50, protection, visibility, -1, 0 ), 0, 50 }; return state; } void send_uci_message( comms *comms, struct fen_load from_position ) { comms->from_position = from_position; comms->uci_message_ready = true; } void send_engine_message( comms *comms, int depth, int seldepth, int multipv, int score_cp, int nodes, int nps, int hashfull, int tbhits, int time ) { comms->depth = depth; comms->seldepth = seldepth; comms->multipv = multipv; comms->score_cp = score_cp; comms->nodes = nodes; comms->nps = nps; comms->hashfull = hashfull; comms->tbhits = tbhits; comms->time = time; comms->engine_message_ready = true; } bool receive_uci_message(comms *comms) { if (comms->uci_message_ready) { comms->uci_message_ready = false; return true; } return false; } // TODO: think about dealing with multiple engine messages bool receive_engine_message(comms *comms) { if (comms->engine_message_ready) { comms->engine_message_ready = false; return true; } return false; } #endif // IPC_C