1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
#ifndef IPC_C
#define IPC_C
#include <stdlib.h>
#include <sys/mman.h>
#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
|