PROTOCOL.md

Sat, 22 Aug 2026 16:22:05 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 22 Aug 2026 16:22:05 +0200
changeset 182
04c65336777f
permissions
-rw-r--r--

start fixing the network protocol

relates to #976
documentation issue #938

182
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 # terminal-chess Network Protocol
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 The network protocol for terminal-chess is designed in an interoperable manner,
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 allowing for playing chess with other clients that implement the same protocol.
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 This document describes the protocol in detail, including the message formats
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 and the expected behavior of clients and the server.
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 The current protocol version is 24.
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 ## Message Format
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 The terminal-chess network protocol has two different message types:
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 * a code consisting of a single byte
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * a message composed of a single byte indicating the type plus a payload
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 An example implementation covering both types is the following struct:
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 ```C
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 struct message {
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 unsigned char code;
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 unsigned char message[]; // flexible array member
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 }
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 ```
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 A table with all message codes can be found [below](#message-code-table).
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 The datatypes used in the message payloads are as follows:
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 * BYTE — a single byte
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 * WORD — two bytes integer (big-endian)
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33 * DWORD — four bytes integer (big-endian)
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34 * MOVE — a data structure describing a move, consisting of
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 * a DWORD for the elapsed move time in seconds
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 * a DWORD for additional elapsed move time in microseconds
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 * a BYTE describing the moved piece (see below)
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 * a BYTE index for the file the piece was moved from
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 * a BYTE index for the row the piece was moved from
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 * a BYTE index for the file the piece was moved to
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 * a BYTE index for the row the piece was moved to
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42 * a BYTE describing the selected piece for promotion (otherwise zero)
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44 The following table shows the possible values for the BYTEs describing a piece.
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 | Piece | Value (Hex) |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 |--------------|-------------|
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 | White Pawn | 0x11 |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 | White Rook | 0x12 |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 | White Knight | 0x13 |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 | White Bishop | 0x14 |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 | White Queen | 0x15 |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 | White King | 0x16 |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 | Black Pawn | 0x21 |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 | Black Rook | 0x22 |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 | Black Knight | 0x23 |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 | Black Bishop | 0x24 |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 | Black Queen | 0x25 |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 | Black King | 0x26 |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 ## Handshake
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 Immediately after a connection between a server and a client is established,
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 the server SHALL send one single byte denoting the protocol version to the
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 client.
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 The client SHALL then immediately answer with their protocol version.
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 After both parties have sent their version to the other party, they SHALL both
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 compare their own version with the received version.
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 When the versions do not match, both parties SHALL terminate the connection.
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 The users SHOULD be informed about the protocol mismatch.
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 ## Game Setup
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 After a successful handshake, the server SHALL send the client information about
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 the game they want to play. The server can set up a completely new game or
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 continue an unfinished game.
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 ### Starting a New Game
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 When the server wants to start a new game, they SHALL send a `NETCODE_GAMEINFO`
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 message. The payload of this message is as follows:
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 * one BYTE denoting the color the server wants to play
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 (16 or 0x10 = white, 32 or 0x20 = black)
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 * one BYTE indicating if the game is played with time control (1 = yes, 0 = no)
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 * one WORD for the initial clock time in seconds
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 * one WORD for the number of seconds that are added per move
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 * one WORD for the delay in seconds for each move
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 When the game is played without time control, the last three WORDs MAY be
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 left uninitialized, but they SHALL be sent anyway.
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 ### Continuing a Game
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94 When the server wants to continue a game, they SHALL send a `NETCODE_PGNDATA`
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 message. The payload of this message is as follows:
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96 * the same two BYTES and three WORDS as in `NETCODE_GAMEINFO`, followed by
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 *
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99 ### Clock Setup
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101 For clock synchronization, the following rules SHALL be applied:
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 * the clock of each player starts after their first move
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104 * the _increment_ is added _after_ a move, except…
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 * … when the initial clock is zero, then the clock starts with the increment
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106 * when the used time for a move is less than the delay, the move costs no time
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
107 * the delay is always subtracted from the actual move time
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
108
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109 ## Message Code Table
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111 The following table shows the definitions of the network codes.
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
112
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113 | Code Name | Byte | With Payload |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
114 |----------------------|------|--------------|
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
115 | NETCODE_ACCEPT | 0x02 | no |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116 | NETCODE_DECLINE | 0x04 | no |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117 | NETCODE_DECLINE_MOVE | 0x05 | yes |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
118 | NETCODE_GAMEINFO | 0x10 | yes |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
119 | NETCODE_PGNDATA | 0x11 | yes |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
120 | NETCODE_MOVE | 0x20 | yes |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
121 | NETCODE_CHECK | 0x22 | no |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
122 | NETCODE_CHECKMATE | 0x23 | no |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123 | NETCODE_STALEMATE | 0x28 | no |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
124 | NETCODE_NOMATERIAL | 0x29 | no |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
125 | NETCODE_THREEFOLD | 0x30 | no |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
126 | NETCODE_RESIGN | 0x41 | no |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
127 | NETCODE_REMIS | 0x42 | no |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
128 | NETCODE_TAUNT | 0x43 | no |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
129 | NETCODE_TIMEOVER | 0x44 | no |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
130
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
131 The following codes are reserved for implementation.
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
132 They are not used during transmissions and can be used as return values
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
133 for functions to indicate errors.
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
134
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
135 | Code Name | Byte |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
136 |----------------------|------|
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
137 | NETCODE_AGAIN | 0x70 |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
138 | NETCODE_CONNLOST | 0x80 |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
139 | NETCODE_ERROR | 0xFF |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
140
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
141

mercurial