@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.
Files changed (175) hide show
  1. package/package.json +2 -2
  2. package/src/codec/index.ts +51 -221
  3. package/src/dispatch/dispatcher/index.ts +89 -47
  4. package/src/dispatch/session/index.ts +6 -24
  5. package/src/index.ts +2 -4
  6. package/src/photon/balancing/codec/index.ts +27 -53
  7. package/src/photon/balancing/format/authenticate/request/index.ts +52 -6
  8. package/src/photon/balancing/format/friends/request/index.ts +21 -5
  9. package/src/photon/balancing/format/game/create/request/index.ts +45 -8
  10. package/src/photon/balancing/format/game/debug/request/index.ts +19 -4
  11. package/src/photon/balancing/format/game/join/request/index.ts +65 -13
  12. package/src/photon/balancing/format/game/list/request/index.ts +29 -3
  13. package/src/photon/balancing/format/game/random/request/index.ts +58 -10
  14. package/src/photon/balancing/format/groups/request/index.ts +19 -3
  15. package/src/photon/balancing/format/index.ts +7 -0
  16. package/src/photon/balancing/format/leave/request/index.ts +19 -3
  17. package/src/photon/balancing/format/lobby/join/request/index.ts +23 -3
  18. package/src/photon/balancing/format/lobby/stats/request/index.ts +19 -3
  19. package/src/photon/balancing/format/once/request/index.ts +31 -8
  20. package/src/photon/balancing/format/properties/get/request/index.ts +31 -4
  21. package/src/photon/balancing/format/properties/set/request/index.ts +36 -7
  22. package/src/photon/balancing/format/raise/request/index.ts +76 -10
  23. package/src/photon/balancing/format/reader/index.ts +75 -6
  24. package/src/photon/balancing/format/regions/request/index.ts +16 -1
  25. package/src/photon/balancing/format/rpc/request/index.ts +26 -3
  26. package/src/photon/balancing/format/settings/request/index.ts +15 -2
  27. package/src/photon/balancing/format/shared/credentials/index.ts +77 -33
  28. package/src/photon/balancing/format/shared/lobby/index.ts +41 -5
  29. package/src/photon/balancing/format/shared/options/index.ts +56 -24
  30. package/src/photon/balancing/format/table/index.ts +399 -0
  31. package/src/photon/balancing/index.ts +11 -1
  32. package/src/photon/balancing/travel/index.ts +15 -12
  33. package/src/photon/client/connection/index.ts +6 -6
  34. package/src/photon/cluster/back/index.ts +32 -45
  35. package/src/photon/cluster/dresser/index.ts +7 -8
  36. package/src/photon/cluster/front/index.ts +25 -49
  37. package/src/photon/cluster/grid/index.ts +1 -1
  38. package/src/photon/cluster/mover/index.ts +12 -12
  39. package/src/photon/cluster/press/index.ts +2 -18
  40. package/src/photon/cluster/route/game/index.ts +9 -11
  41. package/src/photon/cluster/route/master/index.ts +15 -34
  42. package/src/photon/cluster/route/name/index.ts +4 -21
  43. package/src/photon/cluster/seat/game/index.ts +6 -26
  44. package/src/photon/cluster/seat/master/index.ts +1 -1
  45. package/src/photon/cluster/sessions/index.ts +5 -6
  46. package/src/photon/cluster/spread/index.ts +8 -147
  47. package/src/photon/enet/client/index.ts +1 -8
  48. package/src/photon/enet/gateway/index.ts +5 -15
  49. package/src/photon/enet/index.ts +1 -1
  50. package/src/photon/enet/link/index.ts +34 -15
  51. package/src/photon/enet/outbound/index.ts +279 -0
  52. package/src/photon/enet/peer/index.ts +59 -0
  53. package/src/photon/enet/transport/index.ts +8 -24
  54. package/src/photon/game/admission/index.ts +353 -0
  55. package/src/photon/game/bag/index.ts +13 -0
  56. package/src/photon/game/broadcast/index.ts +79 -0
  57. package/src/photon/game/editor/index.ts +446 -0
  58. package/src/photon/game/gate/index.ts +9 -8
  59. package/src/photon/game/handler/index.ts +30 -3
  60. package/src/photon/game/history/index.ts +234 -0
  61. package/src/photon/game/hooked/index.ts +16 -15
  62. package/src/photon/game/index.ts +12 -1
  63. package/src/photon/game/lifecycle/index.ts +193 -0
  64. package/src/photon/game/peer/index.ts +1 -10
  65. package/src/photon/game/properties/index.ts +342 -0
  66. package/src/photon/game/room/index.ts +435 -1793
  67. package/src/photon/game/rooms/index.ts +2 -1
  68. package/src/photon/game/seats/index.ts +237 -0
  69. package/src/photon/game/server/index.ts +12 -2
  70. package/src/photon/game/services/index.ts +41 -0
  71. package/src/photon/game/settings/index.ts +22 -0
  72. package/src/photon/game/snapshot/index.ts +133 -0
  73. package/src/photon/game/throttle/index.ts +60 -0
  74. package/src/photon/game/touch/index.ts +2 -5
  75. package/src/photon/game/venue/index.ts +31 -0
  76. package/src/photon/master/application/index.ts +7 -0
  77. package/src/photon/master/gate/index.ts +12 -8
  78. package/src/photon/master/handler/index.ts +36 -748
  79. package/src/photon/master/index.ts +5 -0
  80. package/src/photon/master/list/sql/index.ts +37 -3
  81. package/src/photon/master/listing/index.ts +133 -0
  82. package/src/photon/master/lobby/index.ts +9 -0
  83. package/src/photon/master/login/index.ts +424 -0
  84. package/src/photon/master/matchmaking/index.ts +282 -0
  85. package/src/photon/master/query/index.ts +45 -0
  86. package/src/photon/master/redirect/index.ts +8 -7
  87. package/src/photon/master/services/index.ts +35 -0
  88. package/src/photon/master/settings/index.ts +44 -0
  89. package/src/photon/master/sql/cost/index.ts +263 -0
  90. package/src/photon/master/sql/index.ts +1 -0
  91. package/src/photon/message/client/index.ts +14 -11
  92. package/src/photon/message/handler/index.ts +22 -64
  93. package/src/photon/message/pipeline/index.ts +12 -12
  94. package/src/photon/message/server/index.ts +87 -43
  95. package/src/photon/message/watch/index.ts +14 -37
  96. package/src/photon/server/deferral/index.ts +57 -0
  97. package/src/photon/server/host/index.ts +32 -10
  98. package/src/photon/server/index.ts +1 -0
  99. package/src/photon/tcp/client/index.ts +1 -1
  100. package/src/photon/tcp/codec/index.ts +13 -42
  101. package/src/photon/tcp/server/index.ts +4 -4
  102. package/src/photon/websocket/bridge/index.ts +16 -20
  103. package/src/photon/websocket/message/index.ts +27 -39
  104. package/src/photon/websocket/path/index.ts +2 -5
  105. package/src/scale/fanout/index.ts +6 -6
  106. package/src/scale/index.ts +2 -4
  107. package/src/scale/outbound/index.ts +192 -0
  108. package/src/scale/relay/index.ts +20 -31
  109. package/src/scale/sink/index.ts +6 -11
  110. package/src/scale/transport/index.ts +27 -44
  111. package/src/scale/tunnel/index.ts +37 -91
  112. package/src/smartfox/common/bluebox/gateway/index.ts +401 -0
  113. package/src/smartfox/common/bluebox/index.ts +4 -0
  114. package/src/smartfox/{x2 → common}/bluebox/transport/index.ts +36 -23
  115. package/src/smartfox/common/index.ts +5 -0
  116. package/src/smartfox/common/strikes/index.ts +74 -0
  117. package/src/smartfox/pro/bluebox/index.ts +176 -4
  118. package/src/smartfox/pro/codec/index.ts +8 -7
  119. package/src/smartfox/pro/escalation/index.ts +28 -20
  120. package/src/smartfox/pro/flood/index.ts +2 -2
  121. package/src/smartfox/pro/format/extension/raw/index.ts +12 -2
  122. package/src/smartfox/pro/handler/chat/index.ts +11 -8
  123. package/src/smartfox/pro/handler/login/index.ts +2 -25
  124. package/src/smartfox/pro/handler/shared/index.ts +4 -3
  125. package/src/smartfox/pro/index.ts +11 -5
  126. package/src/smartfox/pro/login/gate/index.ts +166 -0
  127. package/src/smartfox/pro/login/index.ts +1 -0
  128. package/src/smartfox/pro/moderation/index.ts +220 -0
  129. package/src/smartfox/pro/options/index.ts +5 -5
  130. package/src/smartfox/pro/protocol/index.ts +62 -8
  131. package/src/smartfox/pro/web/index.ts +33 -26
  132. package/src/smartfox/pro/zone/index.ts +59 -282
  133. package/src/smartfox/pro/zones/index.ts +3 -3
  134. package/src/smartfox/x2/bluebox/index.ts +149 -4
  135. package/src/smartfox/x2/chat/index.ts +9 -19
  136. package/src/smartfox/x2/codec/index.ts +31 -39
  137. package/src/smartfox/x2/config/index.ts +6 -1
  138. package/src/smartfox/x2/data/array/index.ts +96 -60
  139. package/src/smartfox/x2/data/object/index.ts +144 -82
  140. package/src/smartfox/x2/data/serializer/index.ts +41 -23
  141. package/src/smartfox/x2/flood/gate/index.ts +19 -13
  142. package/src/smartfox/x2/format/buddy/init/response/index.ts +4 -3
  143. package/src/smartfox/x2/format/find/rooms/response/index.ts +4 -3
  144. package/src/smartfox/x2/format/find/users/response/index.ts +4 -3
  145. package/src/smartfox/x2/format/group/subscribe/response/index.ts +4 -3
  146. package/src/smartfox/x2/format/login/response/index.ts +4 -3
  147. package/src/smartfox/x2/format/mmo/position/response/index.ts +8 -6
  148. package/src/smartfox/x2/format/room/join/response/index.ts +4 -3
  149. package/src/smartfox/x2/games/index.ts +35 -8
  150. package/src/smartfox/x2/hall/index.ts +0 -6
  151. package/src/smartfox/x2/handler/session/index.ts +1 -1
  152. package/src/smartfox/x2/index.ts +8 -3
  153. package/src/smartfox/x2/member/index.ts +10 -3
  154. package/src/smartfox/x2/packet/login/response/index.ts +12 -0
  155. package/src/smartfox/x2/protocol/index.ts +11 -1
  156. package/src/smartfox/x2/rooms/index.ts +1 -2
  157. package/src/smartfox/x2/sessions/index.ts +6 -0
  158. package/src/smartfox/x2/udp/index.ts +61 -24
  159. package/src/smartfox/x2/web/index.ts +35 -17
  160. package/src/smartfox/x2/websocket/index.ts +18 -36
  161. package/src/smartfox/x2/zone/index.ts +77 -28
  162. package/src/smartfox/x2/zones/index.ts +17 -5
  163. package/src/photon/enet/handler/index.ts +0 -65
  164. package/src/scale/hosted/index.ts +0 -68
  165. package/src/scale/outlet/index.ts +0 -121
  166. package/src/smartfox/pro/bluebox/gateway/index.ts +0 -310
  167. package/src/smartfox/pro/bluebox/session/index.ts +0 -63
  168. package/src/smartfox/pro/bluebox/transport/index.ts +0 -104
  169. package/src/smartfox/x2/bluebox/gateway/index.ts +0 -315
  170. /package/src/smartfox/{x2 → common}/bluebox/session/index.ts +0 -0
  171. /package/src/smartfox/{pro → common}/counters/index.ts +0 -0
  172. /package/src/smartfox/{pro → common}/counters/memory/index.ts +0 -0
  173. /package/src/smartfox/{pro → common}/counters/offender/index.ts +0 -0
  174. /package/src/smartfox/{pro → common}/counters/store/index.ts +0 -0
  175. /package/src/smartfox/{pro → common}/sequence/index.ts +0 -0
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@netifyjs/protocol",
3
3
  "description": "Ready-made game server protocols for Netify, starting with SmartFoxServer Pro and 2X",
4
- "version": "1.0.0-alpha.3",
4
+ "version": "1.0.0-beta.1",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  ".": "./src/index.ts",
@@ -37,7 +37,7 @@
37
37
  "url": "https://github.com/netifyjs/protocol/issues"
38
38
  },
39
39
  "peerDependencies": {
40
- "@netifyjs/netify": ">=1.0.0-alpha.12"
40
+ "@netifyjs/netify": ">=1.0.0-beta.1"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@eslint/js": "^10.0.1",
@@ -3,77 +3,37 @@ import {
3
3
  ByteBuf,
4
4
  ByteToMessageDecoder,
5
5
  EncoderError,
6
- QueueFullError,
7
6
  type Channel,
8
7
  type DecoderOptions,
9
8
  type HandlerContext,
9
+ type OutboundHandler,
10
10
  } from '@netifyjs/netify'
11
11
 
12
12
  import type { DirectWriter, Protocol } from '../protocol/index.ts'
13
- import { TunnelOutlet } from '../scale/outlet/index.ts'
14
13
 
15
14
  /**
16
- * Takes encoded bytes written straight into its room
17
- */
18
- export type Staging = {
19
- /**
20
- * Where reserved bytes get written
21
- */
22
- readonly stagingBuffer: Uint8Array
23
-
24
- /**
25
- * Makes room and gives where it starts, or -1
26
- * @param size bytes to make room for
27
- */
28
- reserve(size: number): number
29
-
30
- /**
31
- * Sends what was written into the reserved room
32
- * @param unused bytes of the room left over
33
- */
34
- commitStaged(unused?: number): void
35
- }
36
-
37
- /**
38
- * Base for a protocol's pipeline handler, with the outbound half done
15
+ * Base for a protocol's pipeline handler, decoding packets in and encoding them out
39
16
  * @class
40
17
  */
41
- export abstract class PacketCodec<O = unknown> extends ByteToMessageDecoder<O> {
42
- /**
43
- * Causes looked through before giving up
44
- */
45
- private static readonly causes: number = 8
46
-
18
+ export abstract class PacketCodec<O = unknown>
19
+ extends ByteToMessageDecoder<O>
20
+ implements OutboundHandler
21
+ {
47
22
  /**
48
23
  * Where a channel keeps its codec
49
24
  */
50
25
  private static readonly key: AttributeKey<PacketCodec> =
51
26
  new AttributeKey<PacketCodec>('netify:protocol:codec')
52
27
 
53
- /**
54
- * Its place in the pipeline, or null
55
- */
56
- private context: HandlerContext | null
57
-
58
- /**
59
- * Pipeline version the last check of what follows belongs to
60
- */
61
- private checked: number
62
-
63
- /**
64
- * Whether no outbound handler follows it
65
- */
66
- private last: boolean
67
-
68
28
  /**
69
29
  * The protocol that knows the wire format
70
30
  */
71
- private readonly speaks: Protocol
31
+ protected readonly protocol: Protocol
72
32
 
73
33
  /**
74
34
  * Writes some packets straight into the outbound bytes, or null
75
35
  */
76
- private readonly straight: DirectWriter | null
36
+ private readonly direct: DirectWriter | null
77
37
 
78
38
  /**
79
39
  * Where each packet of the last decode began
@@ -92,13 +52,10 @@ export abstract class PacketCodec<O = unknown> extends ByteToMessageDecoder<O> {
92
52
  protected constructor(protocol: Protocol, options: DecoderOptions = {}) {
93
53
  super(options)
94
54
 
95
- this.speaks = protocol
96
- this.straight = protocol.straight()
55
+ this.protocol = protocol
56
+ this.direct = protocol.straight()
97
57
  this.starts = []
98
58
  this.produced = []
99
- this.context = null
100
- this.checked = -1
101
- this.last = false
102
59
  }
103
60
 
104
61
  /**
@@ -110,19 +67,10 @@ export abstract class PacketCodec<O = unknown> extends ByteToMessageDecoder<O> {
110
67
  }
111
68
 
112
69
  /**
113
- * Whether it still sits in a pipeline
114
- */
115
- public get attached(): boolean {
116
- return this.context !== null
117
- }
118
-
119
- /**
120
- * Remembers where it sits and puts itself on the channel
70
+ * Puts itself on the channel
121
71
  * @param context its place in the pipeline
122
72
  */
123
73
  public handlerAdded(context: HandlerContext): void {
124
- this.context = context
125
- this.checked = -1
126
74
  context.channel.attributes.set<PacketCodec>(PacketCodec.key, this)
127
75
  }
128
76
 
@@ -130,44 +78,52 @@ export abstract class PacketCodec<O = unknown> extends ByteToMessageDecoder<O> {
130
78
  * Takes itself off the channel and hands on leftover bytes
131
79
  * @param context its place in the pipeline
132
80
  */
133
- public override handlerRemoved(context: HandlerContext): Promise<void> {
134
- this.context = null
135
-
81
+ public override handlerRemoved(context: HandlerContext): void {
136
82
  const attributes = context.channel.attributes
137
83
  if (attributes.get<PacketCodec>(PacketCodec.key) === this) {
138
84
  attributes.delete<PacketCodec>(PacketCodec.key)
139
85
  }
140
86
 
141
- return super.handlerRemoved(context)
87
+ super.handlerRemoved(context)
142
88
  }
143
89
 
144
90
  /**
145
- * Encodes a packet without walking the pipeline, false if something else has to see it
146
- * @param packet anything heading out
91
+ * Encodes packets and lets everything else through
92
+ * @param context where the bytes go
93
+ * @param message anything heading out
147
94
  */
148
- public send(packet: unknown): boolean {
149
- const context = this.context
150
- if (context === null) return false
95
+ public write(context: HandlerContext, message: unknown): void {
96
+ if (message instanceof ByteBuf || message instanceof Uint8Array) {
97
+ context.write(message)
98
+ return
99
+ }
100
+
101
+ if (context.writesToSocket && this.stage(context, message)) return
151
102
 
152
- const version = context.pipeline.version
153
- if (this.checked !== version) {
154
- this.checked = version
155
- this.last = PacketCodec.alone(context)
103
+ let bytes: Uint8Array | null
104
+ try {
105
+ bytes = this.protocol.encode(message)
106
+ } catch (cause) {
107
+ throw new EncoderError('Encoding failed', { cause })
156
108
  }
157
109
 
158
- return this.last && this.writeSync(context, packet)
110
+ context.write(bytes ?? message)
159
111
  }
160
112
 
161
113
  /**
162
- * Whether no outbound handler sits between it and the tail
163
- * @param context its place in the pipeline
114
+ * Passes a flush on
115
+ * @param context where it goes
164
116
  */
165
- private static alone(context: HandlerContext): boolean {
166
- for (let next = context.next; next !== null; next = next.next) {
167
- if ('write' in next.handler) return false
168
- }
117
+ public flush(context: HandlerContext): void {
118
+ context.flush()
119
+ }
169
120
 
170
- return true
121
+ /**
122
+ * Passes a close on
123
+ * @param context where it goes
124
+ */
125
+ public close(context: HandlerContext): void {
126
+ void context.close()
171
127
  }
172
128
 
173
129
  /**
@@ -198,154 +154,28 @@ export abstract class PacketCodec<O = unknown> extends ByteToMessageDecoder<O> {
198
154
  }
199
155
 
200
156
  /**
201
- * Hangs up on a client too slow to take its frames
202
- * @param context the connection
203
- * @param error what went wrong
204
- */
205
- public override exceptionCaught(
206
- context: HandlerContext,
207
- error: unknown,
208
- ): Promise<void> {
209
- if (PacketCodec.overflowed(error)) void context.close()
210
-
211
- return context.fireExceptionCaught(error)
212
- }
213
-
214
- /**
215
- * Encodes packets and lets everything else through
216
- * @param context where the bytes go
157
+ * Writes a packet straight into the channel's staging slab, false if it can't
158
+ * @param context next to the socket
217
159
  * @param message anything heading out
218
160
  */
219
- public write(context: HandlerContext, message: unknown): Promise<void> {
220
- let bytes: Uint8Array | null
221
- try {
222
- bytes = this.speaks.encode(message)
223
- } catch (cause) {
224
- return Promise.reject(new EncoderError('Encoding failed', { cause }))
225
- }
226
-
227
- return context.write(bytes === null ? message : ByteBuf.wrap(bytes))
228
- }
229
-
230
- /**
231
- * Encodes and writes without a promise, false for other messages
232
- * @param context where the bytes go
233
- * @param message anything heading out
234
- */
235
- public writeSync(context: HandlerContext, message: unknown): boolean {
236
- if (message instanceof ByteBuf) return PacketCodec.pass(context, message)
237
- if (this.direct(context, message)) return true
238
-
239
- let bytes: Uint8Array | null
240
- try {
241
- bytes = this.speaks.encode(message)
242
- } catch (cause) {
243
- throw new EncoderError('Encoding failed', { cause })
244
- }
245
-
246
- if (bytes === null) return false
247
- PacketCodec.put(context, bytes)
248
-
249
- return true
250
- }
251
-
252
- /**
253
- * Lets the protocol write a packet straight into the outbound bytes, or false
254
- * @param context where the bytes go
255
- * @param message anything heading out
256
- */
257
- private direct(context: HandlerContext, message: unknown): boolean {
258
- const writer = this.straight
161
+ private stage(context: HandlerContext, message: unknown): boolean {
162
+ const writer = this.direct
259
163
  const room = writer === null ? -1 : writer.measure(message)
260
- const staging = room < 0 ? null : PacketCodec.staging(context)
261
- if (writer === null || staging === null) return false
164
+ if (writer === null || room < 0) return false
262
165
 
263
- const offset = staging.reserve(room)
166
+ const channel = context.channel
167
+ const offset = channel.reserve(room)
264
168
  if (offset < 0) return false
265
169
 
266
170
  let taken = -1
267
171
  try {
268
- taken = writer.write(message, staging.stagingBuffer, offset)
172
+ taken = writer.write(message, channel.stagingBuffer, offset)
173
+ } catch (cause) {
174
+ throw new EncoderError('Encoding failed', { cause })
269
175
  } finally {
270
- staging.commitStaged(taken < 0 ? room : room - taken)
176
+ channel.commit(taken < 0 ? room : room - taken)
271
177
  }
272
178
 
273
179
  return taken >= 0
274
180
  }
275
-
276
- /**
277
- * Whether an error comes from a full outbox
278
- * @param error what the pipeline caught
279
- */
280
- public static overflowed(error: unknown): boolean {
281
- let current: unknown = error
282
-
283
- for (let step = 0; step < PacketCodec.causes; step++) {
284
- if (current instanceof QueueFullError) return true
285
- if (!(current instanceof Error)) return false
286
-
287
- current = current.cause
288
- }
289
-
290
- return false
291
- }
292
-
293
- /**
294
- * Where the next write can be staged directly, or null
295
- * @param context the codec's place in the pipeline
296
- */
297
- public static staging(context: HandlerContext): Staging | null {
298
- if (context.writesToSocket) return context.channel
299
-
300
- const outlet = context.previous?.handler
301
- return outlet instanceof TunnelOutlet ? outlet : null
302
- }
303
-
304
- /**
305
- * Writes encoded bytes without a promise
306
- * @param context where the bytes go
307
- * @param bytes one or more whole frames
308
- */
309
- protected static put(context: HandlerContext, bytes: Uint8Array): void {
310
- if (PacketCodec.stage(context, bytes, 0, bytes.length)) return
311
-
312
- context.writeSync(ByteBuf.wrap(bytes))
313
- }
314
-
315
- /**
316
- * Writes a buffer of encoded bytes straight to the socket, false if a handler has to see it
317
- * @param context where the bytes go
318
- * @param frame one or more whole frames
319
- */
320
- protected static pass(context: HandlerContext, frame: ByteBuf): boolean {
321
- return PacketCodec.stage(
322
- context,
323
- frame.array,
324
- frame.arrayOffset + frame.readerIndex,
325
- frame.readableBytes,
326
- )
327
- }
328
-
329
- /**
330
- * Copies bytes into the outbound room, false if there's none
331
- * @param context where the bytes go
332
- * @param source array the bytes live in
333
- * @param start where they begin
334
- * @param size how many there are
335
- */
336
- private static stage(
337
- context: HandlerContext,
338
- source: Uint8Array,
339
- start: number,
340
- size: number,
341
- ): boolean {
342
- const staging = PacketCodec.staging(context)
343
- const offset = staging === null ? -1 : staging.reserve(size)
344
- if (staging === null || offset < 0) return false
345
-
346
- ByteBuf.copyBytes(source, start, staging.stagingBuffer, offset, size)
347
- staging.commitStaged()
348
-
349
- return true
350
- }
351
181
  }
@@ -2,9 +2,9 @@ import {
2
2
  InboundHandlerAdapter,
3
3
  type Channel,
4
4
  type HandlerContext,
5
+ type Outcome,
5
6
  } from '@netifyjs/netify'
6
7
 
7
- import { Tunnel } from '../../scale/tunnel/index.ts'
8
8
  import { Closed, Opened } from '../event/index.ts'
9
9
  import { Routes, type Keyed, type Route } from '../route/index.ts'
10
10
  import { Session } from '../session/index.ts'
@@ -12,7 +12,7 @@ import { Session } from '../session/index.ts'
12
12
  /**
13
13
  * A handler method tied to its instance
14
14
  */
15
- type Bound = (session: Session, packet: unknown) => void | Promise<void>
15
+ type Bound = (session: Session, packet: unknown) => Outcome
16
16
 
17
17
  /**
18
18
  * Hands each packet to the method decorated for it
@@ -78,20 +78,26 @@ export class PacketDispatcher<U = unknown> extends InboundHandlerAdapter {
78
78
  }
79
79
 
80
80
  /**
81
- * Registers every decorated method of a handler, up its prototype chain
81
+ * Registers every decorated method of a handler, calling its most derived version
82
82
  * @param handler an instance with decorated methods
83
83
  */
84
84
  public add<T extends object>(handler: T): this {
85
+ const seen = new Set<string>()
85
86
  let prototype: unknown = Object.getPrototypeOf(handler)
86
87
 
87
88
  while (typeof prototype === 'object' && prototype !== null) {
88
89
  for (const name of Object.getOwnPropertyNames(prototype)) {
90
+ if (seen.has(name)) continue
91
+
89
92
  const method: unknown = Object.getOwnPropertyDescriptor(
90
93
  prototype,
91
94
  name,
92
95
  )?.value
93
96
  const route = Routes.shared.find(method)
94
- if (route !== null) this.register(handler, route)
97
+ if (route === null) continue
98
+
99
+ seen.add(name)
100
+ this.register(handler, name, route)
95
101
  }
96
102
 
97
103
  prototype = Object.getPrototypeOf(prototype)
@@ -105,12 +111,10 @@ export class PacketDispatcher<U = unknown> extends InboundHandlerAdapter {
105
111
  * @param context the connection
106
112
  * @param message a decoded packet
107
113
  */
108
- public override channelRead(
109
- context: HandlerContext,
110
- message: unknown,
111
- ): void | Promise<void> {
114
+ public override channelRead(context: HandlerContext, message: unknown): void {
112
115
  if (typeof message !== 'object' || message === null) {
113
- return context.fireChannelRead(message)
116
+ context.fireChannelRead(message)
117
+ return
114
118
  }
115
119
 
116
120
  const type: unknown = message.constructor
@@ -121,50 +125,34 @@ export class PacketDispatcher<U = unknown> extends InboundHandlerAdapter {
121
125
  }
122
126
 
123
127
  const bound = this.lastKeys?.get((message as Keyed).route) ?? this.lastTyped
124
- if (bound === null) return context.fireChannelRead(message)
125
-
126
- const session = this.session(context.channel)
127
- session.reading = true
128
- return bound(session, message)
129
- }
130
-
131
- /**
132
- * Sends everything the read's handlers wrote in one flush
133
- * @param context the connection
134
- */
135
- public override channelReadComplete(context: HandlerContext): Promise<void> {
136
- const session = this.session(context.channel)
137
- if (session.reading) {
138
- session.reading = false
139
- void context.channel.flush()
128
+ if (bound === null) {
129
+ context.fireChannelRead(message)
130
+ return
140
131
  }
141
132
 
142
- return context.fireChannelReadComplete()
133
+ const outcome = bound(this.session(context.channel), message)
134
+ if (outcome instanceof Promise) PacketDispatcher.hold(context, outcome)
143
135
  }
144
136
 
145
137
  /**
146
138
  * Tells listeners a connection came up, once across shards
147
139
  * @param context the connection
148
140
  */
149
- public override async channelActive(context: HandlerContext): Promise<void> {
150
- if (!Tunnel.carries(context.channel)) {
151
- await this.emit(context, Opened.now())
152
- }
141
+ public override channelActive(context: HandlerContext): void {
142
+ if (!this.session(context.channel).moved) this.emit(context, Opened.now())
153
143
 
154
- await context.fireChannelActive()
144
+ context.fireChannelActive()
155
145
  }
156
146
 
157
147
  /**
158
148
  * Tells listeners a connection went away
159
149
  * @param context the connection
160
150
  */
161
- public override async channelInactive(
162
- context: HandlerContext,
163
- ): Promise<void> {
151
+ public override channelInactive(context: HandlerContext): void {
152
+ this.emit(context, Closed.now())
164
153
  if (this.lastSession?.channel === context.channel) this.lastSession = null
165
154
 
166
- await this.emit(context, Closed.now())
167
- await context.fireChannelInactive()
155
+ context.fireChannelInactive()
168
156
  }
169
157
 
170
158
  /**
@@ -172,12 +160,33 @@ export class PacketDispatcher<U = unknown> extends InboundHandlerAdapter {
172
160
  * @param context the connection
173
161
  * @param event whatever an earlier handler raised
174
162
  */
175
- public override async userEventTriggered(
163
+ public override userEventTriggered(
176
164
  context: HandlerContext,
177
165
  event: unknown,
178
- ): Promise<void> {
179
- await this.emit(context, event)
180
- await context.fireUserEventTriggered(event)
166
+ ): void {
167
+ this.emit(context, event)
168
+ context.fireUserEventTriggered(event)
169
+ }
170
+
171
+ /**
172
+ * Holds later reads back while an async handler runs
173
+ * @param context the connection
174
+ * @param outcome what the handler is still doing
175
+ */
176
+ private static hold(context: HandlerContext, outcome: Promise<void>): void {
177
+ const channel = context.channel
178
+ const paused = channel.isActive
179
+
180
+ if (paused) channel.pauseReads()
181
+ outcome.then(
182
+ (): void => {
183
+ if (paused) channel.resumeReads()
184
+ },
185
+ (error: unknown): void => {
186
+ if (paused) channel.resumeReads()
187
+ context.fireExceptionCaught(error)
188
+ },
189
+ )
181
190
  }
182
191
 
183
192
  /**
@@ -185,16 +194,42 @@ export class PacketDispatcher<U = unknown> extends InboundHandlerAdapter {
185
194
  * @param context the connection
186
195
  * @param event what happened
187
196
  */
188
- private async emit(context: HandlerContext, event: unknown): Promise<void> {
197
+ private emit(context: HandlerContext, event: unknown): void {
189
198
  if (typeof event !== 'object' || event === null) return
190
199
 
191
200
  const listeners = this.events.get(event.constructor)
192
201
  if (listeners === undefined) return
193
202
 
194
- const session = Session.of<U>(context.channel)
195
- for (const listener of listeners) {
196
- await listener(session, event)
203
+ const session = this.session(context.channel)
204
+ const rest = PacketDispatcher.run(listeners, 0, session, event)
205
+ if (rest !== null) PacketDispatcher.hold(context, rest)
206
+ }
207
+
208
+ /**
209
+ * Calls listeners from one on, waiting for any that return a promise
210
+ * @param listeners every listener of the event
211
+ * @param from the first one to call
212
+ * @param session the connection's session
213
+ * @param event what happened
214
+ */
215
+ private static run(
216
+ listeners: readonly Bound[],
217
+ from: number,
218
+ session: Session,
219
+ event: unknown,
220
+ ): Promise<void> | null {
221
+ for (let index = from; index < listeners.length; index++) {
222
+ const outcome = listeners[index]?.(session, event)
223
+ if (!(outcome instanceof Promise)) continue
224
+
225
+ return outcome.then(
226
+ (): Promise<void> | undefined =>
227
+ PacketDispatcher.run(listeners, index + 1, session, event) ??
228
+ undefined,
229
+ )
197
230
  }
231
+
232
+ return null
198
233
  }
199
234
 
200
235
  /**
@@ -213,10 +248,15 @@ export class PacketDispatcher<U = unknown> extends InboundHandlerAdapter {
213
248
  /**
214
249
  * Files one method under its packet class and key
215
250
  * @param handler the instance it runs on
251
+ * @param name the method's name, looked up on the instance
216
252
  * @param route what the method answers to
217
253
  */
218
- private register(handler: object, route: Route): void {
219
- const bound: Bound = route.method.bind(handler)
254
+ private register(handler: object, name: string, route: Route): void {
255
+ const method: unknown = Reflect.get(handler, name)
256
+ const bound: Bound =
257
+ typeof method === 'function'
258
+ ? (method as Route['method']).bind(handler)
259
+ : route.method.bind(handler)
220
260
  this.lastType = null
221
261
 
222
262
  if (route.event) {
@@ -227,8 +267,10 @@ export class PacketDispatcher<U = unknown> extends InboundHandlerAdapter {
227
267
  }
228
268
 
229
269
  if (route.key === null) {
230
- if (this.typed.has(route.type))
270
+ if (this.typed.has(route.type)) {
231
271
  throw new Error('Packet is already handled')
272
+ }
273
+
232
274
  this.typed.set(route.type, bound)
233
275
  return
234
276
  }
@@ -1,7 +1,5 @@
1
1
  import { AttributeKey, type Channel } from '@netifyjs/netify'
2
2
 
3
- import { PacketCodec } from '../../codec/index.ts'
4
-
5
3
  /**
6
4
  * One connection and whoever is logged in on it
7
5
  * @class
@@ -24,14 +22,9 @@ export class Session<U = unknown> {
24
22
  public user: U | null
25
23
 
26
24
  /**
27
- * Whether a read is under way, and its sends wait for one flush at the end
28
- */
29
- public reading: boolean
30
-
31
- /**
32
- * Codec sends go straight to, or null
25
+ * Whether it came over from another shard with its state
33
26
  */
34
- private codec: PacketCodec | null
27
+ public moved: boolean
35
28
 
36
29
  /**
37
30
  * Starts with nobody logged in
@@ -40,8 +33,7 @@ export class Session<U = unknown> {
40
33
  private constructor(channel: Channel) {
41
34
  this.channel = channel
42
35
  this.user = null
43
- this.reading = false
44
- this.codec = null
36
+ this.moved = false
45
37
  }
46
38
 
47
39
  /**
@@ -58,25 +50,15 @@ export class Session<U = unknown> {
58
50
  }
59
51
 
60
52
  /**
61
- * Sends a packet through the pipeline
53
+ * Sends a packet through the pipeline, going out at the end of the read or turn
62
54
  * @param packet anything the codec knows how to encode
63
55
  */
64
56
  public send(packet: unknown): void {
65
- if (this.codec?.attached !== true) {
66
- this.codec = PacketCodec.on(this.channel)
67
- }
68
-
69
- if (this.codec?.send(packet) === true) {
70
- if (!this.reading) void this.channel.flush()
71
- return
72
- }
73
-
74
- if (this.reading) this.channel.writeSync(packet)
75
- else this.channel.writeAndFlushSync(packet)
57
+ this.channel.writeAndFlush(packet)
76
58
  }
77
59
 
78
60
  /**
79
- * Hangs up
61
+ * Hangs up once what's queued has gone out
80
62
  */
81
63
  public close(): Promise<void> {
82
64
  return this.channel.close()