forked from garrettjoecox/anchor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
444 lines (386 loc) · 11.1 KB
/
mod.ts
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import { writeAll } from "https://deno.land/[email protected]/streams/write_all.ts";
import { readLines } from "https://deno.land/[email protected]/io/read_lines.ts";
const decoder = new TextDecoder();
const encoder = new TextEncoder();
type ClientData = Record<string, any>;
interface BasePacket {
clientId?: number;
roomId?: string;
quiet?: boolean;
targetClientId?: number;
}
interface UpdateClientDataPacket extends BasePacket {
type: "UPDATE_CLIENT_DATA";
data: ClientData;
}
interface AllClientDataPacket extends BasePacket {
type: "ALL_CLIENT_DATA";
clients: ClientData[];
}
interface ServerMessagePacket extends BasePacket {
type: "SERVER_MESSAGE";
message: string;
}
interface DisableAnchorPacket extends BasePacket {
type: "DISABLE_ANCHOR";
}
interface OtherPackets extends BasePacket {
type: "REQUEST_SAVE_STATE" | "PUSH_SAVE_STATE";
}
type Packet =
| UpdateClientDataPacket
| DisableAnchorPacket
| ServerMessagePacket
| AllClientDataPacket
| OtherPackets;
class Server {
private listener?: Deno.Listener;
public clients: Client[] = [];
public rooms: Room[] = [];
async start() {
this.listener = Deno.listen({ port: 43384 });
this.log("Server Started");
try {
for await (const connection of this.listener) {
try {
const client = new Client(connection, this);
this.clients.push(client);
} catch (error) {
this.log(`Error connecting client: ${error.message}`);
}
}
} catch (error) {
this.log(`Error starting server: ${error.message}`);
}
}
removeClient(client: Client) {
const index = this.clients.indexOf(client);
this.clients.splice(index, 1);
}
getOrCreateRoom(id: string) {
const room = this.rooms.find((room) => room.id === id);
if (room) {
return room;
}
const newRoom = new Room(id, this);
this.rooms.push(newRoom);
return newRoom;
}
removeRoom(room: Room) {
const index = this.rooms.indexOf(room);
this.rooms.splice(index, 1);
}
log(message: string) {
console.log(`[Server]: ${message}`);
}
}
class Client {
public id: number;
public data: ClientData = {};
private connection: Deno.Conn;
public server: Server;
public room?: Room;
constructor(connection: Deno.Conn, server: Server) {
this.connection = connection;
this.server = server;
this.id = connection.rid;
this.log("Connected");
this.waitForData();
}
async waitForData() {
const buffer = new Uint8Array(1024);
let data = new Uint8Array(0);
while (true) {
let count: null | number = 0;
try {
count = await this.connection.read(buffer);
} catch (error) {
this.log(`Error reading from connection: ${error.message}`);
this.disconnect();
break;
}
if (!count) {
this.disconnect();
break;
}
// Concatenate received data with the existing data
const receivedData = buffer.subarray(0, count);
data = concatUint8Arrays(data, receivedData);
// Handle all complete packets (while loop in case multiple packets were received at once)
while (true) {
const delimiterIndex = findDelimiterIndex(data);
if (delimiterIndex === -1) {
break; // Incomplete packet, wait for more data
}
// Extract the packet
const packet = data.subarray(0, delimiterIndex + 1);
data = data.subarray(delimiterIndex + 1);
this.handlePacket(packet);
}
}
}
handlePacket(packet: Uint8Array) {
try {
const packetString = decoder.decode(packet);
const packetObject: Packet = JSON.parse(packetString);
packetObject.clientId = this.id;
if (!packetObject.quiet) {
this.log(`-> ${packetObject.type} packet`);
}
if (packetObject.type === "UPDATE_CLIENT_DATA") {
this.data = packetObject.data;
}
if (packetObject.roomId && !this.room) {
this.server.getOrCreateRoom(packetObject.roomId).addClient(this);
}
if (!this.room) {
this.log("Not in a room, ignoring packet");
return;
}
if (packetObject.targetClientId) {
const targetClient = this.room.clients.find((client) =>
client.id === packetObject.targetClientId
);
if (targetClient) {
targetClient.sendPacket(packetObject);
} else {
this.log(`Target client ${packetObject.targetClientId} not found`);
}
return;
}
if (packetObject.type === "REQUEST_SAVE_STATE") {
if (this.room.clients.length > 1) {
this.room.requestingStateClients.push(this);
this.room.broadcastPacket(packetObject, this);
}
} else if (packetObject.type === "PUSH_SAVE_STATE") {
const roomStateRequests = this.room.requestingStateClients;
roomStateRequests.forEach((client) => {
client.sendPacket(packetObject);
});
this.room.requestingStateClients = [];
} else {
this.room.broadcastPacket(packetObject, this);
}
} catch (error) {
this.log(`Error handling packet: ${error.message}`);
}
}
async sendPacket(packetObject: Packet) {
try {
if (!packetObject.quiet) {
this.log(`<- ${packetObject.type} packet`);
}
const packetString = JSON.stringify(packetObject);
const packet = encoder.encode(packetString + "\n");
await writeAll(this.connection, packet);
} catch (error) {
this.log(`Error sending packet: ${error.message}`);
this.disconnect();
}
}
disconnect() {
try {
if (this.room) {
this.room.removeClient(this);
}
this.server.removeClient(this);
this.connection.close();
} catch (error) {
this.log(`Error disconnecting: ${error.message}`);
} finally {
this.log("Disconnected");
}
}
log(message: string) {
console.log(`[Client ${this.id}]: ${message}`);
}
}
class Room {
public id: string;
public server: Server;
public clients: Client[] = [];
public requestingStateClients: Client[] = [];
constructor(id: string, server: Server) {
this.id = id;
this.server = server;
this.log("Created");
}
addClient(client: Client) {
this.log(`Adding client ${client.id}`);
this.clients.push(client);
client.room = this;
this.broadcastAllClientData();
}
removeClient(client: Client) {
this.log(`Removing client ${client.id}`);
const index = this.clients.indexOf(client);
this.clients.splice(index, 1);
client.room = undefined;
if (this.clients.length) {
this.broadcastAllClientData();
} else {
this.log("No clients left, removing room");
this.server.removeRoom(this);
}
}
broadcastAllClientData() {
this.log("<- ALL_CLIENT_DATA packet");
for (const client of this.clients) {
const packetObject = {
type: "ALL_CLIENT_DATA" as const,
roomId: this.id,
clients: this.clients.filter((c) => c !== client).map((c) => ({
clientId: c.id,
...c.data,
})),
};
client.sendPacket(packetObject);
}
}
broadcastPacket(packetObject: Packet, sender: Client) {
if (!packetObject.quiet) {
this.log(`<- ${packetObject.type} packet from ${sender.id}`);
}
for (const client of this.clients) {
if (client !== sender) {
client.sendPacket(packetObject);
}
}
}
log(message: string) {
console.log(`[Room ${this.id}]: ${message}`);
}
}
function concatUint8Arrays(a: Uint8Array, b: Uint8Array): Uint8Array {
const result = new Uint8Array(a.length + b.length);
result.set(a, 0);
result.set(b, a.length);
return result;
}
function findDelimiterIndex(data: Uint8Array): number {
for (let i = 0; i < data.length; i++) {
if (data[i] === 10 /* newline character */) {
return i;
}
}
return -1;
}
const server = new Server();
server.start();
function sendServerMessage(client: Client, message: string) {
return client.sendPacket({
type: "SERVER_MESSAGE",
message,
});
}
function sendDisable(client: Client, message: string) {
sendServerMessage(client, message)
.finally(() =>
client.sendPacket({
type: "DISABLE_ANCHOR",
})
);
}
async function stop(message = "Server restarting") {
await Promise.all(
server.clients.map((client) =>
sendServerMessage(client, message)
.finally(() => {
client.disconnect();
})
),
);
Deno.exit();
}
(async () => {
try {
for await (const line of readLines(Deno.stdin)) {
const [command, ...args] = line.split(" ");
switch (command) {
default:
case "help": {
console.log(
`Available commands:
help: Show this help message
roomCount: Show the number of rooms
clientCount: Show the number of clients
list: List all rooms and clients
stop <message>: Stop the server
message <clientId> <message>: Send a message to a client
messageAll <message>: Send a message to all clients
disable <clientId> <message>: Disable anchor on a client
disableAll <message>: Disable anchor on all clients`,
);
break;
}
case "roomCount": {
console.log(`Room count: ${server.rooms.length}`);
break;
}
case "clientCount": {
console.log(`Client count: ${server.clients.length}`);
break;
}
case "list": {
for (const room of server.rooms) {
console.log(`Room ${room.id}:`);
for (const client of room.clients) {
console.log(
` Client ${client.id}: ${JSON.stringify(client.data)}`,
);
}
}
break;
}
case "disable": {
const [clientId, ...messageParts] = args;
const message = messageParts.join(" ");
const client = server.clients.find((c) =>
c.id === parseInt(clientId, 10)
);
if (client) {
sendDisable(client, message);
} else {
console.log(`Client ${clientId} not found`);
}
break;
}
case "disableAll": {
const message = args.join(" ");
for (const client of server.clients) {
sendDisable(client, message);
}
break;
}
case "message": {
const [clientId, ...messageParts] = args;
const message = messageParts.join(" ");
const client = server.clients.find((c) =>
c.id === parseInt(clientId, 10)
);
if (client) {
sendServerMessage(client, message);
} else {
console.log(`Client ${clientId} not found`);
}
break;
}
case "messageAll": {
const message = args.join(" ");
for (const client of server.clients) {
sendServerMessage(client, message);
}
break;
}
case "stop": {
const message = args.join(" ");
stop(message);
break;
}
}
}
} catch (error) {
console.error("Error readingt from stdin: ", error.message);
}
})();