-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.h
More file actions
217 lines (180 loc) · 10.2 KB
/
Copy pathprotocol.h
File metadata and controls
217 lines (180 loc) · 10.2 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
// protocol.h – Kayles network game protocol definitions.
// Covers:
// - Type aliases for domain clarity
// - Protocol constants and limits
// - Enumerations for message types and game status
// - Packed structs for fixed-length client -> server messages
// - Plain struct for in-RAM game state + serialisation helpers
// - MSG_WRONG_MSG layout
//
// All multi-byte values are stored / transmitted in network byte order.
// Packed structs may be used directly with send/recv; the variable-length
// MSG_GAME_STATE is serialised manually via serialize_game_state().
*/
#ifndef PROTOCOL_H
#define PROTOCOL_H
#include <arpa/inet.h> // htonl, htons, ntohl, ntohs
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <array>
// ────────────────────────────────────────────────────────────────────────────
// Domain type aliases
// ────────────────────────────────────────────────────────────────────────────
using player_id_t = uint32_t;
using game_id_t = uint32_t;
// ────────────────────────────────────────────────────────────────────────────
// Protocol limits
// ────────────────────────────────────────────────────────────────────────────
constexpr uint8_t MAX_PAWN = 255;
constexpr size_t MAX_PAWN_ROW_SIZE = MAX_PAWN / 8 + 1; // 32 bytes
/// Maximum UDP payload we ever need to receive from a client.
/// Largest client message: MSG_MOVE_2 = 1 + 4 + 4 + 1 = 10 bytes.
/// We accept up to 512 to detect oversized datagrams.
constexpr size_t MAX_CLIENT_MSG = 512;
/// Maximum UDP payload the server ever sends.
/// MSG_GAME_STATE header (14 bytes) + pawn_row (up to 32 bytes) = 46 bytes.
constexpr size_t MAX_SERVER_MSG = 46;
// ────────────────────────────────────────────────────────────────────────────
// Message type enumeration (client -> server)
// ────────────────────────────────────────────────────────────────────────────
enum class MsgType : uint8_t {
join = 0,
move_one = 1,
move_two = 2,
keep_alive = 3,
give_up = 4,
};
// ────────────────────────────────────────────────────────────────────────────
// Game status enumeration
// ────────────────────────────────────────────────────────────────────────────
enum class GameStatus : uint8_t {
waiting_for_opponent = 0,
turn_a = 1,
turn_b = 2,
win_a = 3,
win_b = 4,
};
// ────────────────────────────────────────────────────────────────────────────
// Packed structs – fixed-length client -> server messages
//
// These structs are laid out exactly as transmitted on the wire (network byte
// order for multi-byte fields). Use htonl/ntohl when filling / reading them.
// ────────────────────────────────────────────────────────────────────────────
struct __attribute__((__packed__)) MsgJoin {
uint8_t msg_type; // MsgType::join == 0
uint32_t player_id; // network byte order; must be non-zero
};
struct __attribute__((__packed__)) MsgMoveOne {
uint8_t msg_type; // MsgType::move_one == 1
uint32_t player_id; // network byte order
uint32_t game_id; // network byte order
uint8_t pawn; // pion do zbicia
};
struct __attribute__((__packed__)) MsgMoveTwo {
uint8_t msg_type; // MsgType::move_two == 2
uint32_t player_id; // network byte order
uint32_t game_id; // network byte order
uint8_t pawn; // zbijane są piony pawn i pawn+1
};
struct __attribute__((__packed__)) MsgKeepAlive {
uint8_t msg_type; // MsgType::keep_alive == 3
uint32_t player_id; // network byte order
uint32_t game_id; // network byte order
};
struct __attribute__((__packed__)) MsgGiveUp {
uint8_t msg_type; // MsgType::give_up == 4
uint32_t player_id; // network byte order
uint32_t game_id; // network byte order
};
// Expected sizes (compile-time assertions for knowing the correctness from compiling).
static_assert(sizeof(MsgJoin) == 5, "MsgJoin size mismatch");
static_assert(sizeof(MsgMoveOne) == 10, "MsgMoveOne size mismatch");
static_assert(sizeof(MsgMoveTwo) == 10, "MsgMoveTwo size mismatch");
static_assert(sizeof(MsgKeepAlive) == 9, "MsgKeepAlive size mismatch");
static_assert(sizeof(MsgGiveUp) == 9, "MsgGiveUp size mismatch");
// ────────────────────────────────────────────────────────────────────────────
// In-RAM game state (host byte order; serialised before sending)
// ────────────────────────────────────────────────────────────────────────────
struct GameState {
game_id_t game_id {0};
player_id_t player_a {0};
player_id_t player_b {0};
GameStatus status {GameStatus::waiting_for_opponent};
uint8_t max_pawn {0};
/// Bitmap: bit i set ↔ pion i is present.
/// Pion 0 is MSB of byte 0.
std::array<uint8_t, MAX_PAWN_ROW_SIZE> pawn_row {};
/// Returns the number of bytes used in pawn_row for this game.
[[nodiscard]] size_t pawn_row_size() const noexcept {
return static_cast<size_t>(max_pawn) / 8 + 1;
}
/// Returns true if pion p is present on the board.
[[nodiscard]] bool pawn_present(uint8_t p) const noexcept {
return (pawn_row[p / 8] >> (7 - (p % 8))) & 1u;
}
/// Sets pion p : (present = true -> to 1), (present = false -> to 0).
void set_pawn(uint8_t p, bool present) noexcept {
uint8_t &byte = pawn_row[p / 8];
const uint8_t bit = static_cast<uint8_t>(1u << (7 - (p % 8)));
if (present) byte |= bit;
else byte &= ~bit;
}
/// Returns true if any pion remains on the board.
[[nodiscard]] bool any_pawn_present() const noexcept {
const size_t sz = pawn_row_size();
for (size_t i = 0; i < sz; ++i)
if (pawn_row[i]) return true;
return false;
}
};
// ────────────────────────────────────────────────────────────────────────────
// MSG_GAME_STATE serialisation
//
// Layout (variable length):
// [0..3] game_id (4 bytes, network byte order)
// [4..7] player_a_id (4 bytes, network byte order)
// [8..11] player_b_id (4 bytes, network byte order)
// [12] status (1 byte, byte order doesn't matter)
// [13] max_pawn (1 byte, byte order doesn't matter)
// [14..] pawn_row (floor(max_pawn/8)+1 bytes)
//
// Returns total number of bytes written into buf.
// buf must be at least MAX_SERVER_MSG bytes (46).
// ────────────────────────────────────────────────────────────────────────────
inline size_t serialize_game_state(const GameState &gs, uint8_t *buf) noexcept {
size_t off = 0;
const uint32_t game_id_net = htonl(gs.game_id);
const uint32_t player_a_net = htonl(gs.player_a);
const uint32_t player_b_net = htonl(gs.player_b);
std::memcpy(buf + off, &game_id_net, 4); off += 4;
std::memcpy(buf + off, &player_a_net, 4); off += 4;
std::memcpy(buf + off, &player_b_net, 4); off += 4;
buf[off++] = static_cast<uint8_t>(gs.status);
buf[off++] = gs.max_pawn;
const size_t pr_size = gs.pawn_row_size();
std::memcpy(buf + off, gs.pawn_row.data(), pr_size);
off += pr_size;
return off;
}
// ────────────────────────────────────────────────────────────────────────────
// MSG_WRONG_MSG serialisation
//
// Layout:
// [0..11] first 12 bytes of client datagram (zero-padded)
// [12] status = 255
// [13] error_index
//
// Total: 14 bytes.
// ────────────────────────────────────────────────────────────────────────────
constexpr size_t WRONG_MSG_SIZE = 14;
inline void serialize_wrong_msg(const uint8_t *client_buf, size_t client_len,
uint8_t error_index, uint8_t *out) noexcept {
std::memset(out, 0, WRONG_MSG_SIZE);
const size_t copy_len = (client_len < 12) ? client_len : 12;
std::memcpy(out, client_buf, copy_len);
out[12] = 255u;
out[13] = error_index;
}
#endif // PROTOCOL_H