-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodel.h
More file actions
364 lines (308 loc) · 8.26 KB
/
model.h
File metadata and controls
364 lines (308 loc) · 8.26 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
Kage game server.
Copyright 2019 Shuouma <dreamcast-talk.com>
Copyright 2025 Flyinghead <flyinghead.github@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "kage.h"
#include <dcserver/asio.hpp>
#include <stdint.h>
#include <string>
#include <array>
#include <deque>
#include <map>
#include <chrono>
class Player;
class Room;
class Lobby;
class LobbyServer;
class Packet;
using Clock = std::chrono::steady_clock;
using time_point = std::chrono::time_point<Clock>;
class Player
{
public:
Player(LobbyServer& server, const asio::ip::udp::endpoint& endpoint, uint32_t id, asio::io_context& io_context)
: server(server), id(id), endpoint(endpoint), timer(io_context)
{
setAlive();
}
~Player();
uint32_t getId() const {
return id;
}
const std::string& getName() const {
return name;
}
void setName(const std::string& name) {
this->name = name;
}
const asio::ip::udp::endpoint& getEndpoint() const {
return endpoint;
}
const std::vector<uint8_t>& getExtraData() const {
return extraData;
}
void setExtraData(const uint8_t *data, unsigned size)
{
extraData.resize(size);
if (size != 0)
memcpy(extraData.data(), data, size);
}
void setStatus(uint32_t status) {
this->status = status;
}
Lobby *getLobby() const {
return lobby;
}
void setLobby(Lobby *lobby) {
this->lobby = lobby;
}
Room *getRoom() const {
return room;
}
void setRoom(Room *room) {
this->room = room;
}
void send(Packet& packet);
static void sendToAll(Packet& packet, const std::vector<Player *>& players, Player *except = nullptr);
uint32_t getUnrelSeqAndInc() {
return unrelSeq++;
}
void notifyRoomOnAck() {
waitingForSeq = relSeq;
}
void ackRUdp(uint32_t seq);
void ackPacket(Packet& outPacket, const uint8_t *inPacket);
bool packetAcked(const uint8_t *packet);
void setAlive();
bool timedOut() const;
time_point getLastTimeSeen() const {
return lastTime;
}
float getPing() const {
return ping;
}
private:
void sendRel(Packet& packet, uint32_t seq);
void resendTimer(const std::error_code& ec);
LobbyServer& server;
uint32_t id = 0;
std::string name;
asio::ip::udp::endpoint endpoint;
std::vector<uint8_t> extraData;
uint32_t status = 0;
Lobby *lobby = nullptr;
Room *room = nullptr;
uint32_t relSeq = 0; // must start at 0
int ackedRelSeq = -1;
uint32_t unrelSeq = 0;
int waitingForSeq = -1;
time_point lastTime;
Packet lastRelPacket;
std::deque<std::pair<uint32_t, Packet>> relQueue;
asio::steady_timer timer;
int sendCount = 0;
float ping = 100.f;
time_point lastRUdpSend;
int ackedClientSeq = -1;
};
class Room
{
public:
static constexpr uint32_t SERVER_READY = 0x00000001;
static constexpr uint32_t PASSSWORD = 0x01000000;
static constexpr uint32_t TEAM = 0x02000000;
// 04000000: can start?
static constexpr uint32_t LOCKED = 0x40000000;
static constexpr uint32_t PLAYING = 0x80000000;
Room(Lobby& lobby, uint32_t id, const std::string& name, uint32_t attributes, Player *owner, asio::io_context& io_context);
virtual ~Room();
uint32_t getId() const {
return id;
}
const std::string& getName() const {
return name;
}
void setName(const std::string& name) {
this->name = name;
}
Player *getOwner() const {
return owner;
}
uint32_t getAttributes() const {
return attributes;
}
virtual void setAttributes(uint32_t attributes) {
this->attributes = attributes;
}
uint32_t getMaxPlayers() const {
return maxPlayers;
}
void setMaxPlayers(uint32_t maxPlayers) {
this->maxPlayers = maxPlayers;
}
const std::string& getPassword() const {
return password;
}
void setPassword(const std::string& password) {
this->password = password;
}
virtual uint32_t getPlayerCount() const {
return (uint32_t)players.size();
}
int getPlayerIndex(const Player *player) const;
virtual void addPlayer(Player *player);
virtual bool removePlayer(Player *player);
const std::vector<Player *>& getPlayers() const {
return players;
}
virtual void rudpAcked(Player *player) {
}
virtual void createJoinRoomReply(Packet& reply, Packet& relay, Player *player) {
}
void writeNetdump(const uint8_t *data, uint32_t len, const asio::ip::udp::endpoint& endpoint) const;
static bool DumpNetData;
protected:
virtual void onRemovePlayer(Player *player, int index) {
}
void openNetdump();
void closeNetdump() {
if (netdump != nullptr)
fclose(netdump);
}
Lobby& lobby;
const uint32_t id;
std::string name;
uint32_t attributes;
Player *owner;
uint32_t maxPlayers = 0;
std::string password;
std::vector<Player *> players;
LobbyServer& server;
const Game game;
FILE *netdump = nullptr;
};
class Lobby
{
public:
Lobby(LobbyServer& server, uint32_t id, const std::string& name)
: server(server), id(id), name(name)
{
assert(name.length() <= 16);
}
LobbyServer& getServer() const {
return server;
}
const uint32_t getId() const {
return id;
}
const std::string& getName() const {
return name;
}
uint32_t getPlayerCount() const {
return (uint32_t)players.size();
}
void addPlayer(Player *player);
void removePlayer(Player *player);
const std::vector<Player *>& getPlayers() const {
return players;
}
uint32_t getRoomCount() const {
return (uint32_t)rooms.size();
}
Room *getRoom(uint32_t id) const;
std::vector<Room *> getRooms() const;
void addRoom(Room *room);
void removeRoom(Room *room);
private:
LobbyServer& server;
const uint32_t id;
std::string name;
std::vector<Player *> players;
std::map<uint32_t, Room *> rooms;
};
class Server
{
public:
virtual ~Server() {}
Server(uint16_t port, asio::io_context& io_context)
: io_context(io_context),
socket(io_context, asio::ip::udp::endpoint(asio::ip::udp::v4(), port))
{
asio::socket_base::reuse_address option(true);
socket.set_option(option);
}
void start() {
read();
}
protected:
void read();
// Called for each packet in the datagram
virtual void handlePacket(const uint8_t *data, size_t len) = 0;
// Called after all packets have been handled
virtual void handlePacketDone() {
}
// Hook to dump all UDP data received
virtual void dump(const uint8_t* data, size_t len) {
}
asio::io_context& io_context;
asio::ip::udp::socket socket;
std::array<uint8_t, 1510> recvbuf;
asio::ip::udp::endpoint source; // source endpoint when receiving packets
};
class LobbyServer : public Server
{
public:
LobbyServer(Game game, uint16_t port, asio::io_context& io_context);
void addLobby(const std::string& name)
{
assert(lobbies.size() < 10);
uint32_t id = (uint32_t)(lobbies.size() + LOBBY_ID_BASE);
lobbies.emplace_back(*this, id, name);
}
Lobby *getLobby(uint32_t id)
{
id -= LOBBY_ID_BASE;
if (id >= lobbies.size())
return nullptr;
return &lobbies[id];
}
void addPlayer(Player *player);
void removePlayer(Player *player);
void send(Packet& packet, const asio::ip::udp::endpoint& endpoint);
virtual Room *addRoom(const std::string& name, uint32_t attributes, Player *owner);
const Game game;
protected:
void dump(const uint8_t* data, size_t len) override;
void handlePacket(const uint8_t *data, size_t len) override;
void handlePacketDone() override;
// Game-specific packet handling called before normal handling to be overridden by subclasses.
// Returns true if the packet was handled.
virtual bool handlePacket(Player *player, const uint8_t *data, size_t len) {
return false;
}
void startTimer();
std::vector<Lobby> lobbies;
uint32_t nextRoomId = 0x2001;
using PlayerMap = std::map<asio::ip::udp::endpoint, Player *>;
PlayerMap players;
asio::steady_timer timer;
// Current player and packets during packet handling
Player *player = nullptr;
Packet replyPacket;
Packet relayPacket;
bool rudpSeen = false;
bool rudpIgnore = false;
static constexpr uint32_t LOBBY_ID_BASE = 0x3001;
};