PROTOCOL.md

Sun, 23 Aug 2026 11:38:27 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 23 Aug 2026 11:38:27 +0200
changeset 183
39aa77b7188b
parent 182
04c65336777f
child 184
93c81539b702
permissions
-rw-r--r--

fix missing byte-order conversion in ntoh_move() and change protocol byte order to little-endian

relates to #976

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
183
39aa77b7188b fix missing byte-order conversion in ntoh_move() and change protocol byte order to little-endian
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
32 * WORD — two bytes integer (little-endian)
39aa77b7188b fix missing byte-order conversion in ntoh_move() and change protocol byte order to little-endian
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
33 * DWORD — four bytes integer (little-endian)
182
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
183
39aa77b7188b fix missing byte-order conversion in ntoh_move() and change protocol byte order to little-endian
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
44 Note carefully that the byte order used by this protocol is little-endian while
39aa77b7188b fix missing byte-order conversion in ntoh_move() and change protocol byte order to little-endian
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
45 the standard network byte order would be big-endian. But since the vast majority
39aa77b7188b fix missing byte-order conversion in ntoh_move() and change protocol byte order to little-endian
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
46 of processors use little-endian nowadays, this protocol uses little-endian, too.
39aa77b7188b fix missing byte-order conversion in ntoh_move() and change protocol byte order to little-endian
Mike Becker <universe@uap-core.de>
parents: 182
diff changeset
47
182
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 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
49
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 | Piece | Value (Hex) |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 |--------------|-------------|
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 | White Pawn | 0x11 |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 | White Rook | 0x12 |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 | White Knight | 0x13 |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 | White Bishop | 0x14 |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 | White Queen | 0x15 |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 | White King | 0x16 |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 | Black Pawn | 0x21 |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 | Black Rook | 0x22 |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 | Black Knight | 0x23 |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 | Black Bishop | 0x24 |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 | Black Queen | 0x25 |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 | Black King | 0x26 |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 ## Handshake
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 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
68 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
69 client.
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 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
71 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
72 compare their own version with the received version.
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 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
74 The users SHOULD be informed about the protocol mismatch.
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 ## Game Setup
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 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
79 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
80 continue an unfinished game.
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 ### Starting a New Game
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 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
85 message. The payload of this message is as follows:
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 * 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
87 (16 or 0x10 = white, 32 or 0x20 = black)
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 * 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
89 * one WORD for the initial clock time in seconds
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 * 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
91 * 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
92
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93 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
94 left uninitialized, but they SHALL be sent anyway.
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96 ### Continuing a Game
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 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
99 message. The payload of this message is as follows:
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100 * 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
101 *
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 ### Clock Setup
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 For clock synchronization, the following rules SHALL be applied:
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
107 * 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
108 * the _increment_ is added _after_ a move, except…
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109 * … 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
110 * 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
111 * 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
112
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113 ## Message Code Table
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 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
116
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117 | Code Name | Byte | With Payload |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
118 |----------------------|------|--------------|
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
119 | NETCODE_ACCEPT | 0x02 | no |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
120 | NETCODE_DECLINE | 0x04 | no |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
121 | NETCODE_DECLINE_MOVE | 0x05 | yes |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
122 | NETCODE_GAMEINFO | 0x10 | yes |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123 | NETCODE_PGNDATA | 0x11 | yes |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
124 | NETCODE_MOVE | 0x20 | yes |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
125 | NETCODE_CHECK | 0x22 | no |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
126 | NETCODE_CHECKMATE | 0x23 | no |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
127 | NETCODE_STALEMATE | 0x28 | no |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
128 | NETCODE_NOMATERIAL | 0x29 | no |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
129 | NETCODE_THREEFOLD | 0x30 | no |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
130 | NETCODE_RESIGN | 0x41 | no |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
131 | NETCODE_REMIS | 0x42 | no |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
132 | NETCODE_TAUNT | 0x43 | no |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
133 | NETCODE_TIMEOVER | 0x44 | no |
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 The following codes are reserved for implementation.
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
136 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
137 for functions to indicate errors.
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
138
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
139 | Code Name | Byte |
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 | NETCODE_AGAIN | 0x70 |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
142 | NETCODE_CONNLOST | 0x80 |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
143 | NETCODE_ERROR | 0xFF |
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
144
04c65336777f start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
diff changeset
145

mercurial