Merge pull request #50 from lemonsqueeze/can_countercap
[pachi.git] / distributed / protocol.h
blobd0188d19dcc9716f24c355aae45e0f8ed299dbf8
1 #ifndef PACHI_DISTRIBUTED_PROTOCOL_H
2 #define PACHI_DISTRIBUTED_PROTOCOL_H
4 #ifdef _WIN32
5 #include <winsock2.h>
6 #include <ws2tcpip.h>
7 #else
8 #include <sys/socket.h>
9 #include <arpa/inet.h>
10 #endif
12 #include "board.h"
15 /* Each slave thread maintains a ring of 256 buffers holding
16 * incremental stats received from the slave. The oldest
17 * buffer is recycled to hold stats sent to the slave and
18 * received the next reply. */
19 #define BUFFERS_PER_SLAVE_BITS 8
20 #define BUFFERS_PER_SLAVE (1 << BUFFERS_PER_SLAVE_BITS)
22 struct slave_state;
23 typedef void (*buffer_hook)(void *buf, int size);
24 typedef void (*state_alloc_hook)(struct slave_state *sstate);
25 typedef int (*getargs_hook)(void *buf, struct slave_state *sstate, int cmd_id);
27 struct buf_state {
28 void *buf;
29 /* All buffers have the same physical size. size is the
30 * number of valid bytes. It is set only when the buffer
31 * is actually in the receive queueue. */
32 int size;
33 int queue_index;
34 int owner;
37 struct slave_state {
38 int max_buf_size;
39 int thread_id;
40 struct in_addr client; // for debugging only
41 state_alloc_hook alloc_hook;
42 buffer_hook insert_hook;
43 getargs_hook args_hook;
45 /* Index in received_queue of most recent processed
46 * buffer, -1 if none processed yet. */
47 int last_processed;
49 /* --- PRIVATE DATA for protocol.c --- */
51 struct buf_state b[BUFFERS_PER_SLAVE];
52 int newest_buf;
53 int slave_sock;
55 /* --- PRIVATE DATA for merge.c --- */
57 /* Hash table of incremental stats. */
58 struct incr_stats *stats_htable;
59 int stats_hbits;
60 int stats_id;
62 /* Hash indices updated by stats merge. */
63 int *merged;
64 int max_merged_nodes;
66 extern struct slave_state default_sstate;
68 void protocol_lock(void);
69 void protocol_unlock(void);
71 void logline(struct in_addr *client, char *prefix, char *s);
73 void clear_receive_queue(void);
74 void update_cmd(struct board *b, char *cmd, char *args, bool new_id);
75 void new_cmd(struct board *b, char *cmd, char *args);
76 void get_replies(double time_limit, int min_replies);
77 void protocol_init(char *slave_port, char *proxy_port, int max_slaves);
79 extern int reply_count;
80 extern char **gtp_replies;
81 extern int active_slaves;
83 /* All binary buffers received from all slaves in current move are in
84 * receive_queue[0..queue_length-1] */
85 extern struct buf_state **receive_queue;
86 extern int queue_length;
87 /* Queue age is incremented each time the queue is emptied. */
88 extern int queue_age;
90 /* Max size of all gtp commands for one game.
91 * 60 chars for the first line of genmoves plus 100 lines
92 * of 30 chars each for the stats at last move. */
93 #define CMDS_SIZE (60*MAX_GAMELEN + 30*100)
95 /* Max size for one line of reply or slave log. */
96 #define BSIZE 4096
98 #endif