@netifyjs/protocol 1.0.0-alpha.3 → 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 +51 -221
- package/src/dispatch/dispatcher/index.ts +89 -47
- package/src/dispatch/session/index.ts +6 -24
- 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/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/client/index.ts +14 -11
- package/src/photon/message/handler/index.ts +22 -64
- 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 +14 -37
- package/src/photon/server/deferral/index.ts +57 -0
- package/src/photon/server/host/index.ts +32 -10
- package/src/photon/server/index.ts +1 -0
- package/src/photon/tcp/client/index.ts +1 -1
- package/src/photon/tcp/codec/index.ts +13 -42
- 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/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 +20 -31
- package/src/scale/sink/index.ts +6 -11
- package/src/scale/transport/index.ts +27 -44
- package/src/scale/tunnel/index.ts +37 -91
- 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 +31 -39
- 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
|
}
|
|
@@ -20,15 +20,10 @@ export class QuietWatch extends InboundHandlerAdapter {
|
|
|
20
20
|
private timer: Timer | null
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
|
-
* Clock time the last read
|
|
23
|
+
* Clock time of the last read
|
|
24
24
|
*/
|
|
25
25
|
private lastRead: number
|
|
26
26
|
|
|
27
|
-
/**
|
|
28
|
-
* Whether a read is under way
|
|
29
|
-
*/
|
|
30
|
-
private reading: boolean
|
|
31
|
-
|
|
32
27
|
/**
|
|
33
28
|
* Whether the next idle event is the first of this quiet spell
|
|
34
29
|
*/
|
|
@@ -43,7 +38,6 @@ export class QuietWatch extends InboundHandlerAdapter {
|
|
|
43
38
|
this.timeout = timeout
|
|
44
39
|
this.timer = null
|
|
45
40
|
this.lastRead = 0
|
|
46
|
-
this.reading = false
|
|
47
41
|
this.first = true
|
|
48
42
|
}
|
|
49
43
|
|
|
@@ -66,20 +60,18 @@ export class QuietWatch extends InboundHandlerAdapter {
|
|
|
66
60
|
* Starts watching once the connection is up
|
|
67
61
|
* @param context the connection
|
|
68
62
|
*/
|
|
69
|
-
public override channelActive(context: HandlerContext):
|
|
63
|
+
public override channelActive(context: HandlerContext): void {
|
|
70
64
|
this.start(context)
|
|
71
|
-
|
|
72
|
-
return context.fireChannelActive()
|
|
65
|
+
context.fireChannelActive()
|
|
73
66
|
}
|
|
74
67
|
|
|
75
68
|
/**
|
|
76
69
|
* Stops watching once the connection is gone
|
|
77
70
|
* @param context the connection
|
|
78
71
|
*/
|
|
79
|
-
public override channelInactive(context: HandlerContext):
|
|
72
|
+
public override channelInactive(context: HandlerContext): void {
|
|
80
73
|
this.stop()
|
|
81
|
-
|
|
82
|
-
return context.fireChannelInactive()
|
|
74
|
+
context.fireChannelInactive()
|
|
83
75
|
}
|
|
84
76
|
|
|
85
77
|
/**
|
|
@@ -87,25 +79,10 @@ export class QuietWatch extends InboundHandlerAdapter {
|
|
|
87
79
|
* @param context the connection
|
|
88
80
|
* @param message what arrived
|
|
89
81
|
*/
|
|
90
|
-
public override channelRead(
|
|
91
|
-
context: HandlerContext,
|
|
92
|
-
message: unknown,
|
|
93
|
-
): void | Promise<void> {
|
|
94
|
-
this.reading = true
|
|
95
|
-
this.first = true
|
|
96
|
-
|
|
97
|
-
return context.fireChannelRead(message)
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Notes when the client last spoke
|
|
102
|
-
* @param context the connection
|
|
103
|
-
*/
|
|
104
|
-
public override channelReadComplete(context: HandlerContext): Promise<void> {
|
|
105
|
-
this.reading = false
|
|
82
|
+
public override channelRead(context: HandlerContext, message: unknown): void {
|
|
106
83
|
this.lastRead = performance.now()
|
|
107
|
-
|
|
108
|
-
|
|
84
|
+
this.first = true
|
|
85
|
+
context.fireChannelRead(message)
|
|
109
86
|
}
|
|
110
87
|
|
|
111
88
|
/**
|
|
@@ -116,7 +93,6 @@ export class QuietWatch extends InboundHandlerAdapter {
|
|
|
116
93
|
if (this.timer !== null) return
|
|
117
94
|
|
|
118
95
|
this.lastRead = performance.now()
|
|
119
|
-
this.reading = false
|
|
120
96
|
this.first = true
|
|
121
97
|
this.schedule(context, this.timeout)
|
|
122
98
|
}
|
|
@@ -136,7 +112,7 @@ export class QuietWatch extends InboundHandlerAdapter {
|
|
|
136
112
|
*/
|
|
137
113
|
private schedule(context: HandlerContext, delay: number): void {
|
|
138
114
|
const timer = setTimeout((): void => {
|
|
139
|
-
|
|
115
|
+
this.check(context)
|
|
140
116
|
}, delay)
|
|
141
117
|
timer.unref()
|
|
142
118
|
|
|
@@ -147,12 +123,13 @@ export class QuietWatch extends InboundHandlerAdapter {
|
|
|
147
123
|
* Raises an idle event if the client has been quiet too long
|
|
148
124
|
* @param context the connection
|
|
149
125
|
*/
|
|
150
|
-
private
|
|
126
|
+
private check(context: HandlerContext): void {
|
|
151
127
|
if (this.timer === null) return
|
|
152
128
|
|
|
129
|
+
const held = context.channel.isReadPaused
|
|
153
130
|
const remaining = this.timeout - (performance.now() - this.lastRead)
|
|
154
|
-
if (
|
|
155
|
-
this.schedule(context,
|
|
131
|
+
if (held || remaining > 0) {
|
|
132
|
+
this.schedule(context, held ? this.timeout : remaining)
|
|
156
133
|
return
|
|
157
134
|
}
|
|
158
135
|
|
|
@@ -160,6 +137,6 @@ export class QuietWatch extends InboundHandlerAdapter {
|
|
|
160
137
|
|
|
161
138
|
const first = this.first
|
|
162
139
|
this.first = false
|
|
163
|
-
|
|
140
|
+
context.fireUserEventTriggered(new IdleStateEvent('reader', first))
|
|
164
141
|
}
|
|
165
142
|
}
|
|
@@ -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'
|
|
@@ -570,16 +571,37 @@ export class PhotonServer {
|
|
|
570
571
|
ready: Promise<Mountable>,
|
|
571
572
|
transport: 'tcp' | 'udp' | 'webSocket' | 'secure',
|
|
572
573
|
): ChannelSetup {
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
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))
|
|
583
590
|
}
|
|
584
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
|
+
}
|
|
585
607
|
}
|
|
@@ -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,7 +7,6 @@ import {
|
|
|
7
7
|
type HandlerContext,
|
|
8
8
|
} from '@netifyjs/netify'
|
|
9
9
|
|
|
10
|
-
import { PacketCodec } from '../../../codec/index.ts'
|
|
11
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'
|
|
@@ -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,19 +97,13 @@ 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
|
}
|
|
@@ -155,14 +128,13 @@ export abstract class TcpCodec<O = unknown> extends ByteToMessageDecoder<O> {
|
|
|
155
128
|
const size = TcpFraming.HEADER + length
|
|
156
129
|
if (this.framing.refusal(size) !== null) return false
|
|
157
130
|
|
|
158
|
-
const
|
|
159
|
-
|
|
160
|
-
if (staging === null || offset < 0) return false
|
|
131
|
+
const offset = context.writesToSocket ? context.channel.reserve(size) : -1
|
|
132
|
+
if (offset < 0) return false
|
|
161
133
|
|
|
162
|
-
const target =
|
|
134
|
+
const target = context.channel.stagingBuffer
|
|
163
135
|
this.framing.head(channel, delivery, length, target, offset)
|
|
164
136
|
ByteBuf.copyBytes(source, start, target, offset + TcpFraming.HEADER, length)
|
|
165
|
-
|
|
137
|
+
context.channel.commit()
|
|
166
138
|
|
|
167
139
|
return true
|
|
168
140
|
}
|
|
@@ -176,12 +148,11 @@ export abstract class TcpCodec<O = unknown> extends ByteToMessageDecoder<O> {
|
|
|
176
148
|
const size = TcpFraming.HEADER + frame.payload.length
|
|
177
149
|
if (this.framing.refusal(size) !== null) return false
|
|
178
150
|
|
|
179
|
-
const
|
|
180
|
-
|
|
181
|
-
if (staging === null || offset < 0) return false
|
|
151
|
+
const offset = context.writesToSocket ? context.channel.reserve(size) : -1
|
|
152
|
+
if (offset < 0) return false
|
|
182
153
|
|
|
183
|
-
this.framing.write(frame,
|
|
184
|
-
|
|
154
|
+
this.framing.write(frame, context.channel.stagingBuffer, offset)
|
|
155
|
+
context.channel.commit()
|
|
185
156
|
|
|
186
157
|
return true
|
|
187
158
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ByteBuf,
|
|
1
|
+
import type { ByteBuf, HandlerContext } from '@netifyjs/netify'
|
|
2
2
|
|
|
3
3
|
import { Frame } from '../../frame/index.ts'
|
|
4
4
|
import { TcpCodec, type TcpOptions } from '../codec/index.ts'
|
|
@@ -49,14 +49,14 @@ export class TcpServerCodec extends TcpCodec<Frame> {
|
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
if (first !== TcpFraming.MAGIC) {
|
|
52
|
-
this.fail(
|
|
52
|
+
this.fail(input, 'Frame is malformed')
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
if (input.readableBytes < 5) return
|
|
56
56
|
|
|
57
57
|
const size = input.getUint32(start + 1)
|
|
58
58
|
const reason = this.framing.refusal(size)
|
|
59
|
-
if (reason !== null) this.fail(
|
|
59
|
+
if (reason !== null) this.fail(input, reason)
|
|
60
60
|
if (input.readableBytes < size) return
|
|
61
61
|
|
|
62
62
|
input.skipBytes(5)
|
|
@@ -76,6 +76,6 @@ export class TcpServerCodec extends TcpCodec<Frame> {
|
|
|
76
76
|
private answer(context: HandlerContext, ping: TcpPing): void {
|
|
77
77
|
const pong = TcpPong.of(this.clock.now(), ping.clientTime)
|
|
78
78
|
|
|
79
|
-
context.
|
|
79
|
+
context.writeAndFlush(pong.encode())
|
|
80
80
|
}
|
|
81
81
|
}
|