@colyseus/core 0.17.0 → 0.17.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/rooms/RankedQueueRoom.js.map +2 -2
- package/build/rooms/RankedQueueRoom.mjs.map +2 -2
- package/package.json +7 -6
- package/src/Debug.ts +37 -0
- package/src/IPC.ts +124 -0
- package/src/Logger.ts +30 -0
- package/src/MatchMaker.ts +1119 -0
- package/src/Protocol.ts +160 -0
- package/src/Room.ts +1797 -0
- package/src/Server.ts +325 -0
- package/src/Stats.ts +107 -0
- package/src/Transport.ts +207 -0
- package/src/errors/RoomExceptions.ts +141 -0
- package/src/errors/SeatReservationError.ts +5 -0
- package/src/errors/ServerError.ts +17 -0
- package/src/index.ts +81 -0
- package/src/matchmaker/Lobby.ts +68 -0
- package/src/matchmaker/LocalDriver/LocalDriver.ts +92 -0
- package/src/matchmaker/LocalDriver/Query.ts +94 -0
- package/src/matchmaker/RegisteredHandler.ts +172 -0
- package/src/matchmaker/controller.ts +64 -0
- package/src/matchmaker/driver.ts +191 -0
- package/src/presence/LocalPresence.ts +331 -0
- package/src/presence/Presence.ts +263 -0
- package/src/rooms/LobbyRoom.ts +135 -0
- package/src/rooms/RankedQueueRoom.ts +425 -0
- package/src/rooms/RelayRoom.ts +90 -0
- package/src/router/default_routes.ts +58 -0
- package/src/router/index.ts +43 -0
- package/src/serializer/NoneSerializer.ts +16 -0
- package/src/serializer/SchemaSerializer.ts +194 -0
- package/src/serializer/SchemaSerializerDebug.ts +148 -0
- package/src/serializer/Serializer.ts +9 -0
- package/src/utils/DevMode.ts +133 -0
- package/src/utils/StandardSchema.ts +20 -0
- package/src/utils/Utils.ts +169 -0
- package/src/utils/nanoevents.ts +20 -0
package/src/Protocol.ts
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { Packr } from '@colyseus/msgpackr';
|
|
2
|
+
import { encode, type Iterator } from '@colyseus/schema';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Colyseus protocol codes range between 0~100
|
|
6
|
+
*/
|
|
7
|
+
export const Protocol = {
|
|
8
|
+
// Room-related (10~19)
|
|
9
|
+
JOIN_ROOM: 10,
|
|
10
|
+
ERROR: 11,
|
|
11
|
+
LEAVE_ROOM: 12,
|
|
12
|
+
ROOM_DATA: 13,
|
|
13
|
+
ROOM_STATE: 14,
|
|
14
|
+
ROOM_STATE_PATCH: 15,
|
|
15
|
+
// ROOM_DATA_SCHEMA: 16, // DEPRECATED: used to send schema instances via room.send()
|
|
16
|
+
ROOM_DATA_BYTES: 17,
|
|
17
|
+
PING: 18,
|
|
18
|
+
} as const;
|
|
19
|
+
export type Protocol = typeof Protocol[keyof typeof Protocol];
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* WebSocket close codes
|
|
23
|
+
* (See https://github.com/Luka967/websocket-close-codes)
|
|
24
|
+
*/
|
|
25
|
+
export const CloseCode = {
|
|
26
|
+
NORMAL_CLOSURE: 1000,
|
|
27
|
+
GOING_AWAY: 1001,
|
|
28
|
+
NO_STATUS_RECEIVED: 1005,
|
|
29
|
+
ABNORMAL_CLOSURE: 1006,
|
|
30
|
+
|
|
31
|
+
CONSENTED: 4000,
|
|
32
|
+
SERVER_SHUTDOWN: 4001,
|
|
33
|
+
WITH_ERROR: 4002,
|
|
34
|
+
DEVMODE_RESTART: 4010
|
|
35
|
+
} as const;
|
|
36
|
+
export type CloseCode = typeof CloseCode[keyof typeof CloseCode];
|
|
37
|
+
|
|
38
|
+
export const ErrorCode = {
|
|
39
|
+
// HTTP MatchMaking Error Codes
|
|
40
|
+
MATCHMAKE_NO_HANDLER: 520,
|
|
41
|
+
MATCHMAKE_INVALID_CRITERIA: 521,
|
|
42
|
+
MATCHMAKE_INVALID_ROOM_ID: 522,
|
|
43
|
+
MATCHMAKE_UNHANDLED: 523, // generic exception during onCreate/onJoin
|
|
44
|
+
MATCHMAKE_EXPIRED: 524, // generic exception during onCreate/onJoin
|
|
45
|
+
AUTH_FAILED: 525,
|
|
46
|
+
APPLICATION_ERROR: 526,
|
|
47
|
+
|
|
48
|
+
INVALID_PAYLOAD: 4217,
|
|
49
|
+
} as const;
|
|
50
|
+
export type ErrorCode = typeof ErrorCode[keyof typeof ErrorCode];
|
|
51
|
+
|
|
52
|
+
// Inter-process communication protocol
|
|
53
|
+
export const IpcProtocol = {
|
|
54
|
+
SUCCESS: 0,
|
|
55
|
+
ERROR: 1,
|
|
56
|
+
TIMEOUT: 2,
|
|
57
|
+
} as const;
|
|
58
|
+
export type IpcProtocol = typeof IpcProtocol[keyof typeof IpcProtocol];
|
|
59
|
+
|
|
60
|
+
const packr = new Packr({
|
|
61
|
+
useRecords: false, // increased compatibility with decoders other than "msgpackr"
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// msgpackr workaround: initialize buffer
|
|
65
|
+
packr.encode(undefined);
|
|
66
|
+
|
|
67
|
+
export const getMessageBytes = {
|
|
68
|
+
[Protocol.JOIN_ROOM]: (reconnectionToken: string, serializerId: string, handshake?: Uint8Array) => {
|
|
69
|
+
const it: Iterator = { offset: 1 };
|
|
70
|
+
packr.buffer[0] = Protocol.JOIN_ROOM;
|
|
71
|
+
|
|
72
|
+
packr.buffer[it.offset++] = Buffer.byteLength(reconnectionToken, "utf8");
|
|
73
|
+
encode.utf8Write(packr.buffer as Buffer, reconnectionToken, it);
|
|
74
|
+
|
|
75
|
+
packr.buffer[it.offset++] = Buffer.byteLength(serializerId, "utf8");
|
|
76
|
+
encode.utf8Write(packr.buffer as Buffer, serializerId, it);
|
|
77
|
+
|
|
78
|
+
let handshakeLength = handshake?.byteLength || 0;
|
|
79
|
+
|
|
80
|
+
// check if buffer needs to be resized
|
|
81
|
+
if (handshakeLength > packr.buffer.byteLength - it.offset) {
|
|
82
|
+
packr.useBuffer(Buffer.alloc(it.offset + handshakeLength, packr.buffer));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (handshakeLength > 0) {
|
|
86
|
+
packr.buffer.set(handshake, it.offset);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return Buffer.from(packr.buffer.subarray(0, it.offset + handshakeLength));
|
|
90
|
+
},
|
|
91
|
+
|
|
92
|
+
[Protocol.ERROR]: (code: number, message: string = '') => {
|
|
93
|
+
const it: Iterator = { offset: 1 };
|
|
94
|
+
packr.buffer[0] = Protocol.ERROR;
|
|
95
|
+
|
|
96
|
+
encode.number(packr.buffer as Buffer, code, it);
|
|
97
|
+
encode.string(packr.buffer as Buffer, message, it);
|
|
98
|
+
|
|
99
|
+
return Buffer.from(packr.buffer.subarray(0, it.offset));
|
|
100
|
+
},
|
|
101
|
+
|
|
102
|
+
[Protocol.ROOM_STATE]: (bytes: number[]) => {
|
|
103
|
+
return [Protocol.ROOM_STATE, ...bytes];
|
|
104
|
+
},
|
|
105
|
+
|
|
106
|
+
[Protocol.PING]: () => {
|
|
107
|
+
packr.buffer[0] = Protocol.PING;
|
|
108
|
+
return Buffer.from(packr.buffer.subarray(0, 1));
|
|
109
|
+
},
|
|
110
|
+
|
|
111
|
+
raw: (code: Protocol, type: string | number, message?: any, rawMessage?: Uint8Array | Buffer) => {
|
|
112
|
+
const it: Iterator = { offset: 1 };
|
|
113
|
+
packr.buffer[0] = code;
|
|
114
|
+
|
|
115
|
+
if (typeof (type) === 'string') {
|
|
116
|
+
encode.string(packr.buffer as Buffer, type, it);
|
|
117
|
+
|
|
118
|
+
} else {
|
|
119
|
+
encode.number(packr.buffer as Buffer, type, it);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (message !== undefined) {
|
|
123
|
+
// force to encode from offset
|
|
124
|
+
packr.position = 0;
|
|
125
|
+
|
|
126
|
+
//
|
|
127
|
+
// TODO: remove this after issue is fixed https://github.com/kriszyp/msgpackr/issues/139
|
|
128
|
+
//
|
|
129
|
+
// - This check is only required when running integration tests.
|
|
130
|
+
// (colyseus.js' usage of msgpackr/buffer is conflicting)
|
|
131
|
+
//
|
|
132
|
+
if (process.env.NODE_ENV !== "production") {
|
|
133
|
+
packr.useBuffer(packr.buffer);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// pack message into the same packr.buffer
|
|
137
|
+
const endOfBufferOffset = packr.pack(message, 2048 + it.offset).byteLength;
|
|
138
|
+
// 2048 = RESERVE_START_SPACE
|
|
139
|
+
return Buffer.from(packr.buffer.subarray(0, endOfBufferOffset));
|
|
140
|
+
|
|
141
|
+
} else if (rawMessage !== undefined) {
|
|
142
|
+
|
|
143
|
+
// check if buffer needs to be resized
|
|
144
|
+
// TODO: can we avoid this?
|
|
145
|
+
if (rawMessage.length + it.offset > packr.buffer.byteLength) {
|
|
146
|
+
packr.useBuffer(Buffer.alloc(it.offset + rawMessage.length, packr.buffer));
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// copy raw message into packr.buffer
|
|
150
|
+
packr.buffer.set(rawMessage, it.offset);
|
|
151
|
+
|
|
152
|
+
return Buffer.from(packr.buffer.subarray(0, it.offset + rawMessage.byteLength));
|
|
153
|
+
|
|
154
|
+
} else {
|
|
155
|
+
return Buffer.from(packr.buffer.subarray(0, it.offset));
|
|
156
|
+
}
|
|
157
|
+
},
|
|
158
|
+
|
|
159
|
+
};
|
|
160
|
+
|