@netifyjs/protocol 1.0.0-alpha.1 → 1.0.0-alpha.3

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.
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  AttributeKey,
3
+ ByteBuf,
3
4
  ShutdownEvent,
4
5
  type Channel,
5
6
  type ChannelHandler,
@@ -7,9 +8,11 @@ import {
7
8
  type Logger,
8
9
  } from '@netifyjs/netify'
9
10
 
11
+ import { Batch, type BatchVisitor } from '../batch/index.ts'
10
12
  import { Courier } from '../courier/index.ts'
11
13
  import { HostedChannel } from '../hosted/index.ts'
12
14
  import { type OutletSink, TunnelOutlet } from '../outlet/index.ts'
15
+ import { Pending } from '../pending/index.ts'
13
16
  import type { Portable, Reach } from '../portable/index.ts'
14
17
  import { TunnelRelay } from '../relay/index.ts'
15
18
  import { TunnelTransport } from '../transport/index.ts'
@@ -210,6 +213,26 @@ export class Tunnel<S = unknown> {
210
213
  */
211
214
  private readonly flusher: () => void
212
215
 
216
+ /**
217
+ * Replies waiting to go back to the clients' shards
218
+ */
219
+ private readonly downs: Pending
220
+
221
+ /**
222
+ * Client bytes waiting to go to the sessions' shards
223
+ */
224
+ private readonly ups: Pending
225
+
226
+ /**
227
+ * Hands one entry of an arriving batch to its client
228
+ */
229
+ private readonly delivering: BatchVisitor
230
+
231
+ /**
232
+ * Hands one entry of an arriving batch to its session
233
+ */
234
+ private readonly arriving: BatchVisitor
235
+
213
236
  /**
214
237
  * Last session id used
215
238
  */
@@ -236,16 +259,56 @@ export class Tunnel<S = unknown> {
236
259
 
237
260
  this.touched.clear()
238
261
  }
262
+ this.downs = Pending.of(
263
+ this.courier,
264
+ 'down',
265
+ (front: number, ids: readonly number[]): void => {
266
+ this.lose(front, ids)
267
+ },
268
+ )
269
+ this.ups = Pending.of(
270
+ this.courier,
271
+ 'up',
272
+ (_shard: number, ids: readonly number[]): void => {
273
+ this.strand(ids)
274
+ },
275
+ )
276
+ this.delivering = (
277
+ id: number,
278
+ bytes: Uint8Array,
279
+ start: number,
280
+ size: number,
281
+ ): void => {
282
+ this.fronted.get(id)?.relay.deliver(bytes, start, size)
283
+ }
284
+ this.arriving = (
285
+ id: number,
286
+ bytes: Uint8Array,
287
+ start: number,
288
+ size: number,
289
+ from: number,
290
+ ): void => {
291
+ const guest = this.guests.get(from)?.get(id)
292
+ if (guest === undefined) return
293
+
294
+ void guest.channel.handleMessage(
295
+ new ByteBuf(
296
+ bytes,
297
+ size,
298
+ ByteBuf.defaultMaxCapacity,
299
+ start,
300
+ start + size,
301
+ ),
302
+ size,
303
+ )
304
+ }
239
305
 
240
306
  this.courier
241
307
  .on('open', (data: unknown, from: number): void => {
242
308
  this.host(data as Opening<S>, from)
243
309
  })
244
310
  .onBytes('up', (bytes: Uint8Array, from: number): void => {
245
- void this.guests
246
- .get(from)
247
- ?.get(Tunnel.idAt(bytes, 0))
248
- ?.channel.handleData(bytes.subarray(Tunnel.ID))
311
+ Batch.read(bytes, from, this.arriving)
249
312
  })
250
313
  .on('stop', (data: unknown, from: number): void => {
251
314
  const [id, deadline] = data as Stopping
@@ -255,10 +318,8 @@ export class Tunnel<S = unknown> {
255
318
  const guest = this.guests.get(from)?.get(data as number)
256
319
  if (guest !== undefined) this.evict(guest, false)
257
320
  })
258
- .onBytes('down', (bytes: Uint8Array): void => {
259
- this.fronted
260
- .get(Tunnel.idAt(bytes, 0))
261
- ?.relay.deliver(bytes.subarray(Tunnel.ID))
321
+ .onBytes('down', (bytes: Uint8Array, from: number): void => {
322
+ Batch.read(bytes, from, this.delivering)
262
323
  })
263
324
  .onBytes('share', (bytes: Uint8Array): void => {
264
325
  this.hand(bytes)
@@ -301,8 +362,8 @@ export class Tunnel<S = unknown> {
301
362
  * Passes the client's bytes on
302
363
  * @param read what arrived
303
364
  */
304
- read: (read: Uint8Array): void => {
305
- this.up(id, read)
365
+ read: (source: Uint8Array, start: number, size: number): void => {
366
+ this.up(id, source, start, size)
306
367
  },
307
368
  /**
308
369
  * Passes on word of a server stop
@@ -377,17 +438,23 @@ export class Tunnel<S = unknown> {
377
438
  ids: number | readonly number[],
378
439
  bytes: Uint8Array,
379
440
  ): void {
380
- const sent =
381
- typeof ids === 'number'
382
- ? this.courier.sendBytes(front, 'down', Tunnel.addressed(ids, bytes))
383
- : this.courier.sendBytes(front, 'share', Tunnel.shared(ids, bytes))
384
- if (sent) return
441
+ if (typeof ids === 'number') {
442
+ this.downs.hold(front, ids, bytes, 0, bytes.length)
443
+ return
444
+ }
385
445
 
386
- const guests = this.guests.get(front)
387
- for (const id of typeof ids === 'number' ? [ids] : ids) {
388
- const guest = guests?.get(id)
389
- if (guest !== undefined) this.evict(guest, true)
446
+ if (this.courier.sendBytes(front, 'share', Tunnel.shared(ids, bytes))) {
447
+ return
390
448
  }
449
+
450
+ this.lose(front, ids)
451
+ }
452
+
453
+ /**
454
+ * Sends every waiting reply now
455
+ */
456
+ public settle(): void {
457
+ this.downs.settle()
391
458
  }
392
459
 
393
460
  /**
@@ -454,7 +521,7 @@ export class Tunnel<S = unknown> {
454
521
  * @param bytes what the session wrote
455
522
  */
456
523
  wrote: (bytes: Uint8Array): void => {
457
- this.down(from, opening.id, bytes)
524
+ this.downs.hold(from, opening.id, bytes, 0, bytes.length)
458
525
  },
459
526
  /**
460
527
  * Ends the session with its connection
@@ -464,8 +531,14 @@ export class Tunnel<S = unknown> {
464
531
  this.courier.tell(from, 'end', opening.id)
465
532
  },
466
533
  })
467
- const sink: OutletSink = (bytes: Uint8Array): void => {
468
- if (!transport.ended) this.down(from, opening.id, bytes)
534
+ const sink: OutletSink = (
535
+ source: Uint8Array,
536
+ start: number,
537
+ size: number,
538
+ ): void => {
539
+ if (!transport.ended) {
540
+ this.downs.hold(from, opening.id, source, start, size)
541
+ }
469
542
  }
470
543
  const channel = new HostedChannel(transport, sink)
471
544
  const guest: Hosted = {
@@ -499,34 +572,48 @@ export class Tunnel<S = unknown> {
499
572
  void channel.handleData(opening.bytes)
500
573
  }
501
574
 
575
+ /**
576
+ * Ends sessions whose bytes couldn't reach their client's shard
577
+ * @param front the client's shard
578
+ * @param ids the sessions there
579
+ */
580
+ private lose(front: number, ids: readonly number[]): void {
581
+ const guests = this.guests.get(front)
582
+
583
+ for (const id of ids) {
584
+ const guest = guests?.get(id)
585
+ if (guest !== undefined) this.evict(guest, true)
586
+ }
587
+ }
588
+
502
589
  /**
503
590
  * Passes a client's bytes to its session
504
591
  * @param id the session's id on this shard
505
- * @param bytes what arrived
506
- */
507
- private up(id: number, bytes: Uint8Array): void {
592
+ * @param source holds what arrived
593
+ * @param start where it begins
594
+ * @param size how many bytes arrived
595
+ */
596
+ private up(
597
+ id: number,
598
+ source: Uint8Array,
599
+ start: number,
600
+ size: number,
601
+ ): void {
508
602
  const session = this.fronted.get(id)
509
- if (session === undefined) return
510
-
511
- const sent = this.courier.sendBytes(
512
- session.shard,
513
- 'up',
514
- Tunnel.addressed(id, bytes),
515
- )
516
- if (!sent) this.cut(session, true)
603
+ if (session !== undefined) {
604
+ this.ups.hold(session.shard, id, source, start, size)
605
+ }
517
606
  }
518
607
 
519
608
  /**
520
- * Bytes with the id of their session in front
521
- * @param id the session's id on the client's shard
522
- * @param bytes whole frames
609
+ * Hangs up on clients whose bytes couldn't reach their sessions
610
+ * @param ids the clients here
523
611
  */
524
- private static addressed(id: number, bytes: Uint8Array): Uint8Array {
525
- const packed = new Uint8Array(Tunnel.ID + bytes.length)
526
- Tunnel.put(packed, 0, id)
527
- packed.set(bytes, Tunnel.ID)
528
-
529
- return packed
612
+ private strand(ids: readonly number[]): void {
613
+ for (const id of ids) {
614
+ const session = this.fronted.get(id)
615
+ if (session !== undefined) this.cut(session, true)
616
+ }
530
617
  }
531
618
 
532
619
  /**
@@ -588,7 +675,7 @@ export class Tunnel<S = unknown> {
588
675
  if (session === undefined) continue
589
676
 
590
677
  if (session.reach === 'socket') this.share(session.channel, bytes)
591
- else session.relay.deliver(bytes)
678
+ else session.relay.deliver(bytes, 0, bytes.length)
592
679
  }
593
680
  }
594
681
 
@@ -666,6 +753,9 @@ export class Tunnel<S = unknown> {
666
753
  * @param shard the one that dropped
667
754
  */
668
755
  private abandon(shard: number): void {
756
+ this.downs.drop(shard)
757
+ this.ups.drop(shard)
758
+
669
759
  for (const session of [...this.fronted.values()]) {
670
760
  if (session.shard === shard) this.cut(session, false)
671
761
  }
@@ -117,7 +117,9 @@ export class SmartFox2XCodec extends PacketCodec<object> {
117
117
  * Puts the protocol's gate behind this codec
118
118
  * @param context the connection
119
119
  */
120
- public handlerAdded(context: HandlerContext): void {
120
+ public override handlerAdded(context: HandlerContext): void {
121
+ super.handlerAdded(context)
122
+
121
123
  const gate = this.protocol.gate
122
124
  if (gate === null) return
123
125