@netifyjs/protocol 1.0.0-alpha.2 → 1.0.0-beta.1
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/package.json +2 -2
- package/src/codec/index.ts +82 -157
- package/src/dispatch/dispatcher/index.ts +116 -33
- package/src/dispatch/session/index.ts +9 -3
- package/src/index.ts +2 -4
- package/src/photon/balancing/codec/index.ts +27 -53
- package/src/photon/balancing/format/authenticate/request/index.ts +52 -6
- package/src/photon/balancing/format/friends/request/index.ts +21 -5
- package/src/photon/balancing/format/game/create/request/index.ts +45 -8
- package/src/photon/balancing/format/game/debug/request/index.ts +19 -4
- package/src/photon/balancing/format/game/join/request/index.ts +65 -13
- package/src/photon/balancing/format/game/list/request/index.ts +29 -3
- package/src/photon/balancing/format/game/random/request/index.ts +58 -10
- package/src/photon/balancing/format/groups/request/index.ts +19 -3
- package/src/photon/balancing/format/index.ts +7 -0
- package/src/photon/balancing/format/leave/request/index.ts +19 -3
- package/src/photon/balancing/format/lobby/join/request/index.ts +23 -3
- package/src/photon/balancing/format/lobby/stats/request/index.ts +19 -3
- package/src/photon/balancing/format/once/request/index.ts +31 -8
- package/src/photon/balancing/format/properties/get/request/index.ts +31 -4
- package/src/photon/balancing/format/properties/set/request/index.ts +36 -7
- package/src/photon/balancing/format/raise/request/index.ts +76 -10
- package/src/photon/balancing/format/reader/index.ts +75 -6
- package/src/photon/balancing/format/regions/request/index.ts +16 -1
- package/src/photon/balancing/format/rpc/request/index.ts +26 -3
- package/src/photon/balancing/format/settings/request/index.ts +15 -2
- package/src/photon/balancing/format/shared/credentials/index.ts +77 -33
- package/src/photon/balancing/format/shared/lobby/index.ts +41 -5
- package/src/photon/balancing/format/shared/options/index.ts +56 -24
- package/src/photon/balancing/format/table/index.ts +399 -0
- package/src/photon/balancing/index.ts +11 -1
- package/src/photon/balancing/travel/index.ts +15 -12
- package/src/photon/binary/v18/index.ts +26 -1
- package/src/photon/binary/value/index.ts +52 -3
- package/src/photon/client/connection/index.ts +6 -6
- package/src/photon/cluster/back/index.ts +32 -45
- package/src/photon/cluster/dresser/index.ts +7 -8
- package/src/photon/cluster/front/index.ts +25 -49
- package/src/photon/cluster/grid/index.ts +1 -1
- package/src/photon/cluster/mover/index.ts +12 -12
- package/src/photon/cluster/press/index.ts +2 -18
- package/src/photon/cluster/route/game/index.ts +9 -11
- package/src/photon/cluster/route/master/index.ts +15 -34
- package/src/photon/cluster/route/name/index.ts +4 -21
- package/src/photon/cluster/seat/game/index.ts +6 -26
- package/src/photon/cluster/seat/master/index.ts +1 -1
- package/src/photon/cluster/sessions/index.ts +5 -6
- package/src/photon/cluster/spread/index.ts +8 -147
- package/src/photon/enet/client/index.ts +1 -8
- package/src/photon/enet/gateway/index.ts +5 -15
- package/src/photon/enet/index.ts +1 -1
- package/src/photon/enet/link/index.ts +34 -15
- package/src/photon/enet/outbound/index.ts +279 -0
- package/src/photon/enet/peer/index.ts +59 -0
- package/src/photon/enet/transport/index.ts +8 -24
- package/src/photon/game/admission/index.ts +353 -0
- package/src/photon/game/bag/index.ts +13 -0
- package/src/photon/game/broadcast/index.ts +79 -0
- package/src/photon/game/editor/index.ts +446 -0
- package/src/photon/game/gate/index.ts +9 -8
- package/src/photon/game/handler/index.ts +30 -3
- package/src/photon/game/history/index.ts +234 -0
- package/src/photon/game/hooked/index.ts +16 -15
- package/src/photon/game/index.ts +12 -1
- package/src/photon/game/lifecycle/index.ts +193 -0
- package/src/photon/game/peer/index.ts +1 -10
- package/src/photon/game/properties/index.ts +342 -0
- package/src/photon/game/room/index.ts +435 -1793
- package/src/photon/game/rooms/index.ts +2 -1
- package/src/photon/game/seats/index.ts +237 -0
- package/src/photon/game/server/index.ts +12 -2
- package/src/photon/game/services/index.ts +41 -0
- package/src/photon/game/settings/index.ts +22 -0
- package/src/photon/game/snapshot/index.ts +133 -0
- package/src/photon/game/throttle/index.ts +60 -0
- package/src/photon/game/touch/index.ts +2 -5
- package/src/photon/game/venue/index.ts +31 -0
- package/src/photon/master/application/index.ts +7 -0
- package/src/photon/master/gate/index.ts +12 -8
- package/src/photon/master/handler/index.ts +36 -748
- package/src/photon/master/index.ts +5 -0
- package/src/photon/master/list/sql/index.ts +37 -3
- package/src/photon/master/listing/index.ts +133 -0
- package/src/photon/master/lobby/index.ts +9 -0
- package/src/photon/master/login/index.ts +424 -0
- package/src/photon/master/matchmaking/index.ts +282 -0
- package/src/photon/master/query/index.ts +45 -0
- package/src/photon/master/redirect/index.ts +8 -7
- package/src/photon/master/services/index.ts +35 -0
- package/src/photon/master/settings/index.ts +44 -0
- package/src/photon/master/sql/cost/index.ts +263 -0
- package/src/photon/master/sql/index.ts +1 -0
- package/src/photon/message/binary/index.ts +35 -1
- package/src/photon/message/client/index.ts +14 -11
- package/src/photon/message/format/index.ts +9 -0
- package/src/photon/message/handler/index.ts +48 -63
- package/src/photon/message/pipeline/index.ts +12 -12
- package/src/photon/message/server/index.ts +87 -43
- package/src/photon/message/watch/index.ts +127 -15
- package/src/photon/server/config/index.ts +5 -0
- package/src/photon/server/deferral/index.ts +57 -0
- package/src/photon/server/host/index.ts +36 -10
- package/src/photon/server/index.ts +1 -0
- package/src/photon/server/listeners/index.ts +11 -0
- package/src/photon/tcp/client/index.ts +1 -1
- package/src/photon/tcp/codec/index.ts +41 -38
- package/src/photon/tcp/framing/index.ts +24 -4
- package/src/photon/tcp/server/index.ts +4 -4
- package/src/photon/websocket/bridge/index.ts +16 -20
- package/src/photon/websocket/message/index.ts +27 -39
- package/src/photon/websocket/path/index.ts +2 -5
- package/src/scale/batch/index.ts +14 -6
- package/src/scale/fanout/index.ts +6 -6
- package/src/scale/index.ts +2 -4
- package/src/scale/outbound/index.ts +192 -0
- package/src/scale/relay/index.ts +24 -16
- package/src/scale/sink/index.ts +6 -11
- package/src/scale/transport/index.ts +27 -44
- package/src/scale/tunnel/index.ts +56 -86
- package/src/smartfox/common/bluebox/gateway/index.ts +401 -0
- package/src/smartfox/common/bluebox/index.ts +4 -0
- package/src/smartfox/{x2 → common}/bluebox/transport/index.ts +36 -23
- package/src/smartfox/common/index.ts +5 -0
- package/src/smartfox/common/strikes/index.ts +74 -0
- package/src/smartfox/pro/bluebox/index.ts +176 -4
- package/src/smartfox/pro/codec/index.ts +8 -7
- package/src/smartfox/pro/escalation/index.ts +28 -20
- package/src/smartfox/pro/flood/index.ts +2 -2
- package/src/smartfox/pro/format/extension/raw/index.ts +12 -2
- package/src/smartfox/pro/handler/chat/index.ts +11 -8
- package/src/smartfox/pro/handler/login/index.ts +2 -25
- package/src/smartfox/pro/handler/shared/index.ts +4 -3
- package/src/smartfox/pro/index.ts +11 -5
- package/src/smartfox/pro/login/gate/index.ts +166 -0
- package/src/smartfox/pro/login/index.ts +1 -0
- package/src/smartfox/pro/moderation/index.ts +220 -0
- package/src/smartfox/pro/options/index.ts +5 -5
- package/src/smartfox/pro/protocol/index.ts +62 -8
- package/src/smartfox/pro/web/index.ts +33 -26
- package/src/smartfox/pro/zone/index.ts +59 -282
- package/src/smartfox/pro/zones/index.ts +3 -3
- package/src/smartfox/x2/bluebox/index.ts +149 -4
- package/src/smartfox/x2/chat/index.ts +9 -19
- package/src/smartfox/x2/codec/index.ts +34 -40
- package/src/smartfox/x2/config/index.ts +6 -1
- package/src/smartfox/x2/data/array/index.ts +96 -60
- package/src/smartfox/x2/data/object/index.ts +144 -82
- package/src/smartfox/x2/data/serializer/index.ts +41 -23
- package/src/smartfox/x2/flood/gate/index.ts +19 -13
- package/src/smartfox/x2/format/buddy/init/response/index.ts +4 -3
- package/src/smartfox/x2/format/find/rooms/response/index.ts +4 -3
- package/src/smartfox/x2/format/find/users/response/index.ts +4 -3
- package/src/smartfox/x2/format/group/subscribe/response/index.ts +4 -3
- package/src/smartfox/x2/format/login/response/index.ts +4 -3
- package/src/smartfox/x2/format/mmo/position/response/index.ts +8 -6
- package/src/smartfox/x2/format/room/join/response/index.ts +4 -3
- package/src/smartfox/x2/games/index.ts +35 -8
- package/src/smartfox/x2/hall/index.ts +0 -6
- package/src/smartfox/x2/handler/session/index.ts +1 -1
- package/src/smartfox/x2/index.ts +8 -3
- package/src/smartfox/x2/member/index.ts +10 -3
- package/src/smartfox/x2/packet/login/response/index.ts +12 -0
- package/src/smartfox/x2/protocol/index.ts +11 -1
- package/src/smartfox/x2/rooms/index.ts +1 -2
- package/src/smartfox/x2/sessions/index.ts +6 -0
- package/src/smartfox/x2/udp/index.ts +61 -24
- package/src/smartfox/x2/web/index.ts +35 -17
- package/src/smartfox/x2/websocket/index.ts +18 -36
- package/src/smartfox/x2/zone/index.ts +77 -28
- package/src/smartfox/x2/zones/index.ts +17 -5
- package/src/photon/enet/handler/index.ts +0 -65
- package/src/scale/hosted/index.ts +0 -68
- package/src/scale/outlet/index.ts +0 -121
- package/src/smartfox/pro/bluebox/gateway/index.ts +0 -310
- package/src/smartfox/pro/bluebox/session/index.ts +0 -63
- package/src/smartfox/pro/bluebox/transport/index.ts +0 -104
- package/src/smartfox/x2/bluebox/gateway/index.ts +0 -315
- /package/src/smartfox/{x2 → common}/bluebox/session/index.ts +0 -0
- /package/src/smartfox/{pro → common}/counters/index.ts +0 -0
- /package/src/smartfox/{pro → common}/counters/memory/index.ts +0 -0
- /package/src/smartfox/{pro → common}/counters/offender/index.ts +0 -0
- /package/src/smartfox/{pro → common}/counters/store/index.ts +0 -0
- /package/src/smartfox/{pro → common}/sequence/index.ts +0 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { HandlerContext } from '@netifyjs/netify'
|
|
1
|
+
import type { HandlerContext, Outcome } from '@netifyjs/netify'
|
|
2
2
|
|
|
3
3
|
import type { ConnectionEncryption } from '../../crypto/connection/index.ts'
|
|
4
4
|
import type { WebSocketNegotiation } from '../../websocket/negotiation/index.ts'
|
|
@@ -64,13 +64,21 @@ export class MessageServerHandler extends MessageHandlerAdapter {
|
|
|
64
64
|
* Welcomes a WebSocket client that sends no init request
|
|
65
65
|
* @param context the connection
|
|
66
66
|
*/
|
|
67
|
-
public override
|
|
67
|
+
public override channelActive(context: HandlerContext): Outcome {
|
|
68
68
|
const upgrade = this.upgrade
|
|
69
|
-
|
|
70
|
-
|
|
69
|
+
const welcome =
|
|
70
|
+
upgrade !== null && (upgrade.json || !upgrade.query.xInit)
|
|
71
|
+
? this.welcome(context, InitRequest.upgraded(upgrade), 0)
|
|
72
|
+
: undefined
|
|
73
|
+
|
|
74
|
+
if (welcome === undefined) {
|
|
75
|
+
context.fireChannelActive()
|
|
76
|
+
return
|
|
71
77
|
}
|
|
72
78
|
|
|
73
|
-
|
|
79
|
+
return welcome.then((): void => {
|
|
80
|
+
context.fireChannelActive()
|
|
81
|
+
})
|
|
74
82
|
}
|
|
75
83
|
|
|
76
84
|
/**
|
|
@@ -78,25 +86,29 @@ export class MessageServerHandler extends MessageHandlerAdapter {
|
|
|
78
86
|
* @param context the connection
|
|
79
87
|
* @param unit a frame, JSON text, or anything else
|
|
80
88
|
*/
|
|
81
|
-
public override channelRead(
|
|
82
|
-
context: HandlerContext,
|
|
83
|
-
unit: unknown,
|
|
84
|
-
): void | Promise<void> {
|
|
89
|
+
public override channelRead(context: HandlerContext, unit: unknown): Outcome {
|
|
85
90
|
let message: WireMessage | null
|
|
86
91
|
try {
|
|
87
92
|
message = this.format.decode(unit, this.peer)
|
|
88
93
|
} catch (err) {
|
|
89
|
-
|
|
94
|
+
this.reject(context, 'Message is unreadable', err)
|
|
95
|
+
return
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (message === null) {
|
|
99
|
+
context.fireChannelRead(unit)
|
|
100
|
+
return
|
|
90
101
|
}
|
|
91
102
|
|
|
92
|
-
if (message === null) return context.fireChannelRead(unit)
|
|
93
103
|
if (this.peer.init === null) return this.open(context, message)
|
|
94
104
|
|
|
95
105
|
switch (message.type) {
|
|
96
106
|
case MessageType.OPERATION:
|
|
97
|
-
|
|
107
|
+
this.deliver(context, message)
|
|
108
|
+
return
|
|
98
109
|
case MessageType.INTERNAL_REQUEST:
|
|
99
|
-
|
|
110
|
+
this.serve(context, message)
|
|
111
|
+
return
|
|
100
112
|
default:
|
|
101
113
|
this.drop(context, 'Message type is unexpected')
|
|
102
114
|
}
|
|
@@ -108,15 +120,11 @@ export class MessageServerHandler extends MessageHandlerAdapter {
|
|
|
108
120
|
* @param text what happened
|
|
109
121
|
* @param err what went wrong, or nothing
|
|
110
122
|
*/
|
|
111
|
-
private reject(
|
|
112
|
-
context: HandlerContext,
|
|
113
|
-
text: string,
|
|
114
|
-
err?: unknown,
|
|
115
|
-
): void | Promise<void> {
|
|
123
|
+
private reject(context: HandlerContext, text: string, err?: unknown): void {
|
|
116
124
|
this.drop(context, text, err)
|
|
117
125
|
if (this.peer.init !== null) return
|
|
118
126
|
|
|
119
|
-
|
|
127
|
+
void context.close()
|
|
120
128
|
}
|
|
121
129
|
|
|
122
130
|
/**
|
|
@@ -124,47 +132,77 @@ export class MessageServerHandler extends MessageHandlerAdapter {
|
|
|
124
132
|
* @param context the connection
|
|
125
133
|
* @param message what arrived first
|
|
126
134
|
*/
|
|
127
|
-
private open(
|
|
128
|
-
context: HandlerContext,
|
|
129
|
-
message: WireMessage,
|
|
130
|
-
): void | Promise<void> {
|
|
135
|
+
private open(context: HandlerContext, message: WireMessage): Outcome {
|
|
131
136
|
const init = message.body
|
|
132
137
|
if (message.type !== MessageType.INIT || !(init instanceof InitRequest)) {
|
|
133
|
-
|
|
138
|
+
this.reject(context, 'First message is not an init')
|
|
139
|
+
return
|
|
134
140
|
}
|
|
135
141
|
|
|
136
142
|
return this.welcome(context, init, message.channel)
|
|
137
143
|
}
|
|
138
144
|
|
|
139
145
|
/**
|
|
140
|
-
* Keeps an init,
|
|
146
|
+
* Keeps an init and answers it, holding reads while the gate thinks
|
|
141
147
|
* @param context the connection
|
|
142
148
|
* @param init how the client introduced itself
|
|
143
149
|
* @param channel channel the init came in on
|
|
144
150
|
*/
|
|
145
|
-
private
|
|
151
|
+
private welcome(
|
|
146
152
|
context: HandlerContext,
|
|
147
153
|
init: InitRequest,
|
|
148
154
|
channel: number,
|
|
149
|
-
): Promise<void> {
|
|
155
|
+
): Promise<void> | undefined {
|
|
150
156
|
this.peer.initialise(init)
|
|
151
157
|
|
|
152
|
-
const encryption =
|
|
158
|
+
const encryption = this.admit(context, init)
|
|
159
|
+
if (!(encryption instanceof Promise)) {
|
|
160
|
+
this.answer(context, init, channel, encryption)
|
|
161
|
+
return
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const connection = context.channel
|
|
165
|
+
connection.pauseReads()
|
|
166
|
+
return encryption
|
|
167
|
+
.then(
|
|
168
|
+
(settled: ConnectionEncryption | null): void => {
|
|
169
|
+
this.answer(context, init, channel, settled)
|
|
170
|
+
},
|
|
171
|
+
(cause: unknown): never => MessageServerHandler.failed(context, cause),
|
|
172
|
+
)
|
|
173
|
+
.finally((): void => {
|
|
174
|
+
connection.resumeReads()
|
|
175
|
+
})
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Sets up an init's encryption and answers it
|
|
180
|
+
* @param context the connection
|
|
181
|
+
* @param init how the client introduced itself
|
|
182
|
+
* @param channel channel the init came in on
|
|
183
|
+
* @param encryption what the token set up, or null
|
|
184
|
+
*/
|
|
185
|
+
private answer(
|
|
186
|
+
context: HandlerContext,
|
|
187
|
+
init: InitRequest,
|
|
188
|
+
channel: number,
|
|
189
|
+
encryption: ConnectionEncryption | null,
|
|
190
|
+
): void {
|
|
153
191
|
if (encryption !== null && encryption.refusal === null) {
|
|
154
192
|
this.peer.encrypt(encryption)
|
|
155
193
|
}
|
|
156
194
|
|
|
157
|
-
|
|
195
|
+
this.send(context, WireMessage.initResponse(channel))
|
|
158
196
|
if (encryption !== null && encryption.refusal !== null) {
|
|
159
|
-
|
|
197
|
+
this.send(
|
|
160
198
|
context,
|
|
161
199
|
WireMessage.enveloped(OnceAnswer.refused(encryption.refusal)),
|
|
162
200
|
)
|
|
163
|
-
|
|
201
|
+
void context.close()
|
|
164
202
|
return
|
|
165
203
|
}
|
|
166
204
|
|
|
167
|
-
|
|
205
|
+
context.fireUserEventTriggered(init)
|
|
168
206
|
}
|
|
169
207
|
|
|
170
208
|
/**
|
|
@@ -172,37 +210,43 @@ export class MessageServerHandler extends MessageHandlerAdapter {
|
|
|
172
210
|
* @param context the connection
|
|
173
211
|
* @param init how the client introduced itself
|
|
174
212
|
*/
|
|
175
|
-
private
|
|
213
|
+
private admit(
|
|
176
214
|
context: HandlerContext,
|
|
177
215
|
init: InitRequest,
|
|
178
|
-
): Promise<ConnectionEncryption | null> {
|
|
216
|
+
): ConnectionEncryption | null | Promise<ConnectionEncryption | null> {
|
|
179
217
|
if (this.gate === null || init.token === null) return null
|
|
180
218
|
|
|
181
219
|
try {
|
|
182
|
-
return
|
|
220
|
+
return this.gate(init, this.peer)
|
|
183
221
|
} catch (cause) {
|
|
184
|
-
|
|
185
|
-
throw new Error('Init gate failed', { cause })
|
|
222
|
+
return MessageServerHandler.failed(context, cause)
|
|
186
223
|
}
|
|
187
224
|
}
|
|
188
225
|
|
|
226
|
+
/**
|
|
227
|
+
* Hangs up after the gate broke
|
|
228
|
+
* @param context the connection
|
|
229
|
+
* @param cause what the gate threw
|
|
230
|
+
*/
|
|
231
|
+
private static failed(context: HandlerContext, cause: unknown): never {
|
|
232
|
+
void context.close()
|
|
233
|
+
throw new Error('Init gate failed', { cause })
|
|
234
|
+
}
|
|
235
|
+
|
|
189
236
|
/**
|
|
190
237
|
* Answers an internal operation on the channel it came in on
|
|
191
238
|
* @param context the connection
|
|
192
239
|
* @param message an internal request
|
|
193
240
|
*/
|
|
194
|
-
private serve(context: HandlerContext, message: WireMessage):
|
|
241
|
+
private serve(context: HandlerContext, message: WireMessage): void {
|
|
195
242
|
const request = message.request()
|
|
196
|
-
if (request === null) return
|
|
243
|
+
if (request === null) return
|
|
197
244
|
|
|
198
245
|
const response = this.internal.answer(request, this.peer)
|
|
199
246
|
if (response.returnCode !== 0) {
|
|
200
247
|
this.drop(context, response.debugMessage ?? 'Internal operation failed')
|
|
201
248
|
}
|
|
202
249
|
|
|
203
|
-
|
|
204
|
-
context,
|
|
205
|
-
WireMessage.internalResponse(response, message.channel),
|
|
206
|
-
)
|
|
250
|
+
this.send(context, WireMessage.internalResponse(response, message.channel))
|
|
207
251
|
}
|
|
208
252
|
}
|
|
@@ -1,30 +1,142 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
IdleStateEvent,
|
|
3
|
+
InboundHandlerAdapter,
|
|
4
|
+
type HandlerContext,
|
|
5
|
+
} from '@netifyjs/netify'
|
|
2
6
|
|
|
3
7
|
/**
|
|
4
|
-
* Notices a client that stopped sending,
|
|
8
|
+
* Notices a client that stopped sending, and stays out of the way of writes
|
|
5
9
|
* @class
|
|
6
10
|
*/
|
|
7
|
-
export class QuietWatch extends
|
|
11
|
+
export class QuietWatch extends InboundHandlerAdapter {
|
|
12
|
+
/**
|
|
13
|
+
* Time allowed between received data in ms
|
|
14
|
+
*/
|
|
15
|
+
private readonly timeout: number
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Pending check, or null
|
|
19
|
+
*/
|
|
20
|
+
private timer: Timer | null
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Clock time of the last read
|
|
24
|
+
*/
|
|
25
|
+
private lastRead: number
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Whether the next idle event is the first of this quiet spell
|
|
29
|
+
*/
|
|
30
|
+
private first: boolean
|
|
31
|
+
|
|
8
32
|
/**
|
|
9
33
|
* @param timeout time allowed between received data in ms
|
|
10
34
|
*/
|
|
11
35
|
public constructor(timeout: number) {
|
|
12
|
-
super(
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
36
|
+
super()
|
|
37
|
+
|
|
38
|
+
this.timeout = timeout
|
|
39
|
+
this.timer = null
|
|
40
|
+
this.lastRead = 0
|
|
41
|
+
this.first = true
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Starts watching a connection that's already up
|
|
46
|
+
* @param context its place in the pipeline
|
|
47
|
+
*/
|
|
48
|
+
public handlerAdded(context: HandlerContext): void {
|
|
49
|
+
if (context.channel.isActive) this.start(context)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Stops watching
|
|
54
|
+
*/
|
|
55
|
+
public handlerRemoved(): void {
|
|
56
|
+
this.stop()
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Starts watching once the connection is up
|
|
61
|
+
* @param context the connection
|
|
62
|
+
*/
|
|
63
|
+
public override channelActive(context: HandlerContext): void {
|
|
64
|
+
this.start(context)
|
|
65
|
+
context.fireChannelActive()
|
|
18
66
|
}
|
|
19
67
|
|
|
20
68
|
/**
|
|
21
|
-
*
|
|
22
|
-
* @param context
|
|
23
|
-
* @param message what's being written
|
|
69
|
+
* Stops watching once the connection is gone
|
|
70
|
+
* @param context the connection
|
|
24
71
|
*/
|
|
25
|
-
public
|
|
26
|
-
|
|
72
|
+
public override channelInactive(context: HandlerContext): void {
|
|
73
|
+
this.stop()
|
|
74
|
+
context.fireChannelInactive()
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Notes that the client spoke
|
|
79
|
+
* @param context the connection
|
|
80
|
+
* @param message what arrived
|
|
81
|
+
*/
|
|
82
|
+
public override channelRead(context: HandlerContext, message: unknown): void {
|
|
83
|
+
this.lastRead = performance.now()
|
|
84
|
+
this.first = true
|
|
85
|
+
context.fireChannelRead(message)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Starts the clock
|
|
90
|
+
* @param context the connection
|
|
91
|
+
*/
|
|
92
|
+
private start(context: HandlerContext): void {
|
|
93
|
+
if (this.timer !== null) return
|
|
94
|
+
|
|
95
|
+
this.lastRead = performance.now()
|
|
96
|
+
this.first = true
|
|
97
|
+
this.schedule(context, this.timeout)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Stops the clock
|
|
102
|
+
*/
|
|
103
|
+
private stop(): void {
|
|
104
|
+
if (this.timer !== null) clearTimeout(this.timer)
|
|
105
|
+
this.timer = null
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Looks again after a while
|
|
110
|
+
* @param context the connection
|
|
111
|
+
* @param delay how long to wait in ms
|
|
112
|
+
*/
|
|
113
|
+
private schedule(context: HandlerContext, delay: number): void {
|
|
114
|
+
const timer = setTimeout((): void => {
|
|
115
|
+
this.check(context)
|
|
116
|
+
}, delay)
|
|
117
|
+
timer.unref()
|
|
118
|
+
|
|
119
|
+
this.timer = timer
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Raises an idle event if the client has been quiet too long
|
|
124
|
+
* @param context the connection
|
|
125
|
+
*/
|
|
126
|
+
private check(context: HandlerContext): void {
|
|
127
|
+
if (this.timer === null) return
|
|
128
|
+
|
|
129
|
+
const held = context.channel.isReadPaused
|
|
130
|
+
const remaining = this.timeout - (performance.now() - this.lastRead)
|
|
131
|
+
if (held || remaining > 0) {
|
|
132
|
+
this.schedule(context, held ? this.timeout : remaining)
|
|
133
|
+
return
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
this.schedule(context, this.timeout)
|
|
27
137
|
|
|
28
|
-
|
|
138
|
+
const first = this.first
|
|
139
|
+
this.first = false
|
|
140
|
+
context.fireUserEventTriggered(new IdleStateEvent('reader', first))
|
|
29
141
|
}
|
|
30
142
|
}
|
|
@@ -224,6 +224,11 @@ export type PhotonConfig = {
|
|
|
224
224
|
*/
|
|
225
225
|
readonly udp?: EnetOptions
|
|
226
226
|
|
|
227
|
+
/**
|
|
228
|
+
* Milliseconds TCP and WebSocket writes wait to go out together, off by default
|
|
229
|
+
*/
|
|
230
|
+
readonly flushDelay?: number
|
|
231
|
+
|
|
227
232
|
/**
|
|
228
233
|
* Runs the waits of the Game Servers, the wall clock by default
|
|
229
234
|
*/
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type ChannelSetup,
|
|
3
|
+
type HandlerContext,
|
|
4
|
+
InboundHandlerAdapter,
|
|
5
|
+
} from '@netifyjs/netify'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Holds a connection back while its server is being built
|
|
9
|
+
* @class
|
|
10
|
+
*/
|
|
11
|
+
export class Deferral extends InboundHandlerAdapter {
|
|
12
|
+
/**
|
|
13
|
+
* Pipeline name it sits under
|
|
14
|
+
*/
|
|
15
|
+
public static readonly NAME: string = 'photon:deferral'
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Resolves with the real setup
|
|
19
|
+
*/
|
|
20
|
+
private readonly ready: Promise<ChannelSetup>
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @param ready resolves with the real setup
|
|
24
|
+
*/
|
|
25
|
+
public constructor(ready: Promise<ChannelSetup>) {
|
|
26
|
+
super()
|
|
27
|
+
|
|
28
|
+
this.ready = ready
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Waits for the server, then swaps itself for the real pipeline
|
|
33
|
+
* @param context the connection
|
|
34
|
+
*/
|
|
35
|
+
public override channelActive(context: HandlerContext): Promise<void> {
|
|
36
|
+
const channel = context.channel
|
|
37
|
+
channel.pauseReads()
|
|
38
|
+
|
|
39
|
+
return this.ready
|
|
40
|
+
.then(
|
|
41
|
+
(setup: ChannelSetup): void => {
|
|
42
|
+
if (!channel.isActive) return
|
|
43
|
+
|
|
44
|
+
context.pipeline.remove(context.name)
|
|
45
|
+
setup(channel)
|
|
46
|
+
channel.pipeline.fireChannelActive()
|
|
47
|
+
},
|
|
48
|
+
(cause: unknown): never => {
|
|
49
|
+
void channel.close()
|
|
50
|
+
throw new Error('Server setup failed', { cause })
|
|
51
|
+
},
|
|
52
|
+
)
|
|
53
|
+
.finally((): void => {
|
|
54
|
+
channel.resumeReads()
|
|
55
|
+
})
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -25,6 +25,7 @@ import type {
|
|
|
25
25
|
PhotonConfig,
|
|
26
26
|
RoleName,
|
|
27
27
|
} from '../config/index.ts'
|
|
28
|
+
import { Deferral } from '../deferral/index.ts'
|
|
28
29
|
import { GateAuthenticator } from '../gatekeeper/index.ts'
|
|
29
30
|
import { Listeners, type Mountable } from '../listeners/index.ts'
|
|
30
31
|
import { PortTable, type RolePorts } from '../ports/index.ts'
|
|
@@ -550,6 +551,10 @@ export class PhotonServer {
|
|
|
550
551
|
* Timing and limits of reliable UDP
|
|
551
552
|
*/
|
|
552
553
|
udp: this.config.udp ?? {},
|
|
554
|
+
/**
|
|
555
|
+
* Off unless the config asks for it
|
|
556
|
+
*/
|
|
557
|
+
flushDelay: this.config.flushDelay ?? 0,
|
|
553
558
|
logger: this.config.logger,
|
|
554
559
|
})
|
|
555
560
|
|
|
@@ -566,16 +571,37 @@ export class PhotonServer {
|
|
|
566
571
|
ready: Promise<Mountable>,
|
|
567
572
|
transport: 'tcp' | 'udp' | 'webSocket' | 'secure',
|
|
568
573
|
): ChannelSetup {
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
574
|
+
let built: Mountable | null = null
|
|
575
|
+
const later = ready.then((server: Mountable): Mountable => {
|
|
576
|
+
built = server
|
|
577
|
+
return server
|
|
578
|
+
})
|
|
579
|
+
|
|
580
|
+
return (channel: Channel): void => {
|
|
581
|
+
if (built !== null) {
|
|
582
|
+
PhotonServer.pick(built, transport)(channel)
|
|
583
|
+
return
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
const setup = later.then((server: Mountable): ChannelSetup =>
|
|
587
|
+
PhotonServer.pick(server, transport),
|
|
588
|
+
)
|
|
589
|
+
channel.pipeline.addLast<Deferral>(Deferral.NAME, new Deferral(setup))
|
|
579
590
|
}
|
|
580
591
|
}
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
* The setup a server has for one listener
|
|
595
|
+
* @param server builds the pipelines
|
|
596
|
+
* @param transport which listener
|
|
597
|
+
*/
|
|
598
|
+
private static pick(
|
|
599
|
+
server: Mountable,
|
|
600
|
+
transport: 'tcp' | 'udp' | 'webSocket' | 'secure',
|
|
601
|
+
): ChannelSetup {
|
|
602
|
+
if (transport === 'tcp') return server.tcp()
|
|
603
|
+
if (transport === 'udp') return server.udp()
|
|
604
|
+
|
|
605
|
+
return server.webSocket(transport === 'secure')
|
|
606
|
+
}
|
|
581
607
|
}
|
|
@@ -60,6 +60,11 @@ export type ListenOptions = {
|
|
|
60
60
|
*/
|
|
61
61
|
readonly udp: EnetOptions
|
|
62
62
|
|
|
63
|
+
/**
|
|
64
|
+
* Milliseconds stream writes wait to go out together, 0 for none
|
|
65
|
+
*/
|
|
66
|
+
readonly flushDelay: number
|
|
67
|
+
|
|
63
68
|
/**
|
|
64
69
|
* Hears about a socket buffer the system wouldn't grant
|
|
65
70
|
*/
|
|
@@ -233,6 +238,12 @@ export class Listeners {
|
|
|
233
238
|
*/
|
|
234
239
|
reusePort: options.reusePort,
|
|
235
240
|
})
|
|
241
|
+
.childOptions({
|
|
242
|
+
/**
|
|
243
|
+
* Lets writes build up before they go out
|
|
244
|
+
*/
|
|
245
|
+
flushDelay: options.flushDelay,
|
|
246
|
+
})
|
|
236
247
|
.childHandler(setup)
|
|
237
248
|
.bind({
|
|
238
249
|
...(options.hostname === undefined
|
|
@@ -50,7 +50,7 @@ export class TcpClientCodec extends TcpCodec<Frame | TcpPong> {
|
|
|
50
50
|
|
|
51
51
|
const size = input.getUint32(start + 1)
|
|
52
52
|
const reason = this.framing.refusal(size)
|
|
53
|
-
if (reason !== null) this.fail(
|
|
53
|
+
if (reason !== null) this.fail(input, reason)
|
|
54
54
|
if (input.readableBytes < size) return
|
|
55
55
|
|
|
56
56
|
input.skipBytes(5)
|
|
@@ -7,8 +7,7 @@ import {
|
|
|
7
7
|
type HandlerContext,
|
|
8
8
|
} from '@netifyjs/netify'
|
|
9
9
|
|
|
10
|
-
import {
|
|
11
|
-
import { Frame } from '../../frame/index.ts'
|
|
10
|
+
import { type DeliveryMode, Frame } from '../../frame/index.ts'
|
|
12
11
|
import { ServerClock } from '../clock/index.ts'
|
|
13
12
|
import { TcpFraming } from '../framing/index.ts'
|
|
14
13
|
|
|
@@ -64,25 +63,8 @@ export abstract class TcpCodec<O = unknown> extends ByteToMessageDecoder<O> {
|
|
|
64
63
|
* @param context where the bytes go
|
|
65
64
|
* @param message anything heading out
|
|
66
65
|
*/
|
|
67
|
-
public write(context: HandlerContext, message: unknown):
|
|
68
|
-
|
|
69
|
-
try {
|
|
70
|
-
bytes = this.encode(message)
|
|
71
|
-
} catch (cause) {
|
|
72
|
-
void context.close()
|
|
73
|
-
return Promise.reject(new EncoderError('Encoding failed', { cause }))
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
return context.write(bytes === null ? message : ByteBuf.wrap(bytes))
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Frames and writes without a promise, false for other messages
|
|
81
|
-
* @param context where the bytes go
|
|
82
|
-
* @param message anything heading out
|
|
83
|
-
*/
|
|
84
|
-
public writeSync(context: HandlerContext, message: unknown): boolean {
|
|
85
|
-
if (message instanceof Frame && this.staged(context, message)) return true
|
|
66
|
+
public write(context: HandlerContext, message: unknown): void {
|
|
67
|
+
if (message instanceof Frame && this.staged(context, message)) return
|
|
86
68
|
|
|
87
69
|
let bytes: Uint8Array | null
|
|
88
70
|
try {
|
|
@@ -92,10 +74,7 @@ export abstract class TcpCodec<O = unknown> extends ByteToMessageDecoder<O> {
|
|
|
92
74
|
throw new EncoderError('Encoding failed', { cause })
|
|
93
75
|
}
|
|
94
76
|
|
|
95
|
-
|
|
96
|
-
context.writeSync(ByteBuf.wrap(bytes))
|
|
97
|
-
|
|
98
|
-
return true
|
|
77
|
+
context.write(bytes ?? message)
|
|
99
78
|
}
|
|
100
79
|
|
|
101
80
|
/**
|
|
@@ -118,23 +97,48 @@ export abstract class TcpCodec<O = unknown> extends ByteToMessageDecoder<O> {
|
|
|
118
97
|
}
|
|
119
98
|
|
|
120
99
|
/**
|
|
121
|
-
*
|
|
122
|
-
* @param context the connection
|
|
100
|
+
* Gives up on a stream that can't be read
|
|
123
101
|
* @param input bytes received so far
|
|
124
102
|
* @param reason what's wrong with it
|
|
125
103
|
*/
|
|
126
|
-
protected fail(
|
|
127
|
-
context: HandlerContext,
|
|
128
|
-
input: ByteBuf,
|
|
129
|
-
reason: string,
|
|
130
|
-
): never {
|
|
104
|
+
protected fail(input: ByteBuf, reason: string): never {
|
|
131
105
|
this.broken = true
|
|
132
106
|
input.skipBytes(input.readableBytes)
|
|
133
|
-
void context.close()
|
|
134
107
|
|
|
135
108
|
throw new DecoderError(reason)
|
|
136
109
|
}
|
|
137
110
|
|
|
111
|
+
/**
|
|
112
|
+
* Frames payload bytes straight into the socket's outbound bytes, or false
|
|
113
|
+
* @param context the codec's place in the pipeline
|
|
114
|
+
* @param channel the message's channel
|
|
115
|
+
* @param delivery how it's delivered
|
|
116
|
+
* @param source holds the payload, copied before this returns
|
|
117
|
+
* @param start where it begins
|
|
118
|
+
* @param length how many bytes it takes
|
|
119
|
+
*/
|
|
120
|
+
public stage(
|
|
121
|
+
context: HandlerContext,
|
|
122
|
+
channel: number,
|
|
123
|
+
delivery: DeliveryMode,
|
|
124
|
+
source: Uint8Array,
|
|
125
|
+
start: number,
|
|
126
|
+
length: number,
|
|
127
|
+
): boolean {
|
|
128
|
+
const size = TcpFraming.HEADER + length
|
|
129
|
+
if (this.framing.refusal(size) !== null) return false
|
|
130
|
+
|
|
131
|
+
const offset = context.writesToSocket ? context.channel.reserve(size) : -1
|
|
132
|
+
if (offset < 0) return false
|
|
133
|
+
|
|
134
|
+
const target = context.channel.stagingBuffer
|
|
135
|
+
this.framing.head(channel, delivery, length, target, offset)
|
|
136
|
+
ByteBuf.copyBytes(source, start, target, offset + TcpFraming.HEADER, length)
|
|
137
|
+
context.channel.commit()
|
|
138
|
+
|
|
139
|
+
return true
|
|
140
|
+
}
|
|
141
|
+
|
|
138
142
|
/**
|
|
139
143
|
* Frames a message straight into the socket's outbound bytes, or false
|
|
140
144
|
* @param context where the bytes go
|
|
@@ -144,12 +148,11 @@ export abstract class TcpCodec<O = unknown> extends ByteToMessageDecoder<O> {
|
|
|
144
148
|
const size = TcpFraming.HEADER + frame.payload.length
|
|
145
149
|
if (this.framing.refusal(size) !== null) return false
|
|
146
150
|
|
|
147
|
-
const
|
|
148
|
-
|
|
149
|
-
if (staging === null || offset < 0) return false
|
|
151
|
+
const offset = context.writesToSocket ? context.channel.reserve(size) : -1
|
|
152
|
+
if (offset < 0) return false
|
|
150
153
|
|
|
151
|
-
this.framing.write(frame,
|
|
152
|
-
|
|
154
|
+
this.framing.write(frame, context.channel.stagingBuffer, offset)
|
|
155
|
+
context.channel.commit()
|
|
153
156
|
|
|
154
157
|
return true
|
|
155
158
|
}
|