@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.
Files changed (183) hide show
  1. package/package.json +2 -2
  2. package/src/codec/index.ts +82 -157
  3. package/src/dispatch/dispatcher/index.ts +116 -33
  4. package/src/dispatch/session/index.ts +9 -3
  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/binary/v18/index.ts +26 -1
  34. package/src/photon/binary/value/index.ts +52 -3
  35. package/src/photon/client/connection/index.ts +6 -6
  36. package/src/photon/cluster/back/index.ts +32 -45
  37. package/src/photon/cluster/dresser/index.ts +7 -8
  38. package/src/photon/cluster/front/index.ts +25 -49
  39. package/src/photon/cluster/grid/index.ts +1 -1
  40. package/src/photon/cluster/mover/index.ts +12 -12
  41. package/src/photon/cluster/press/index.ts +2 -18
  42. package/src/photon/cluster/route/game/index.ts +9 -11
  43. package/src/photon/cluster/route/master/index.ts +15 -34
  44. package/src/photon/cluster/route/name/index.ts +4 -21
  45. package/src/photon/cluster/seat/game/index.ts +6 -26
  46. package/src/photon/cluster/seat/master/index.ts +1 -1
  47. package/src/photon/cluster/sessions/index.ts +5 -6
  48. package/src/photon/cluster/spread/index.ts +8 -147
  49. package/src/photon/enet/client/index.ts +1 -8
  50. package/src/photon/enet/gateway/index.ts +5 -15
  51. package/src/photon/enet/index.ts +1 -1
  52. package/src/photon/enet/link/index.ts +34 -15
  53. package/src/photon/enet/outbound/index.ts +279 -0
  54. package/src/photon/enet/peer/index.ts +59 -0
  55. package/src/photon/enet/transport/index.ts +8 -24
  56. package/src/photon/game/admission/index.ts +353 -0
  57. package/src/photon/game/bag/index.ts +13 -0
  58. package/src/photon/game/broadcast/index.ts +79 -0
  59. package/src/photon/game/editor/index.ts +446 -0
  60. package/src/photon/game/gate/index.ts +9 -8
  61. package/src/photon/game/handler/index.ts +30 -3
  62. package/src/photon/game/history/index.ts +234 -0
  63. package/src/photon/game/hooked/index.ts +16 -15
  64. package/src/photon/game/index.ts +12 -1
  65. package/src/photon/game/lifecycle/index.ts +193 -0
  66. package/src/photon/game/peer/index.ts +1 -10
  67. package/src/photon/game/properties/index.ts +342 -0
  68. package/src/photon/game/room/index.ts +435 -1793
  69. package/src/photon/game/rooms/index.ts +2 -1
  70. package/src/photon/game/seats/index.ts +237 -0
  71. package/src/photon/game/server/index.ts +12 -2
  72. package/src/photon/game/services/index.ts +41 -0
  73. package/src/photon/game/settings/index.ts +22 -0
  74. package/src/photon/game/snapshot/index.ts +133 -0
  75. package/src/photon/game/throttle/index.ts +60 -0
  76. package/src/photon/game/touch/index.ts +2 -5
  77. package/src/photon/game/venue/index.ts +31 -0
  78. package/src/photon/master/application/index.ts +7 -0
  79. package/src/photon/master/gate/index.ts +12 -8
  80. package/src/photon/master/handler/index.ts +36 -748
  81. package/src/photon/master/index.ts +5 -0
  82. package/src/photon/master/list/sql/index.ts +37 -3
  83. package/src/photon/master/listing/index.ts +133 -0
  84. package/src/photon/master/lobby/index.ts +9 -0
  85. package/src/photon/master/login/index.ts +424 -0
  86. package/src/photon/master/matchmaking/index.ts +282 -0
  87. package/src/photon/master/query/index.ts +45 -0
  88. package/src/photon/master/redirect/index.ts +8 -7
  89. package/src/photon/master/services/index.ts +35 -0
  90. package/src/photon/master/settings/index.ts +44 -0
  91. package/src/photon/master/sql/cost/index.ts +263 -0
  92. package/src/photon/master/sql/index.ts +1 -0
  93. package/src/photon/message/binary/index.ts +35 -1
  94. package/src/photon/message/client/index.ts +14 -11
  95. package/src/photon/message/format/index.ts +9 -0
  96. package/src/photon/message/handler/index.ts +48 -63
  97. package/src/photon/message/pipeline/index.ts +12 -12
  98. package/src/photon/message/server/index.ts +87 -43
  99. package/src/photon/message/watch/index.ts +127 -15
  100. package/src/photon/server/config/index.ts +5 -0
  101. package/src/photon/server/deferral/index.ts +57 -0
  102. package/src/photon/server/host/index.ts +36 -10
  103. package/src/photon/server/index.ts +1 -0
  104. package/src/photon/server/listeners/index.ts +11 -0
  105. package/src/photon/tcp/client/index.ts +1 -1
  106. package/src/photon/tcp/codec/index.ts +41 -38
  107. package/src/photon/tcp/framing/index.ts +24 -4
  108. package/src/photon/tcp/server/index.ts +4 -4
  109. package/src/photon/websocket/bridge/index.ts +16 -20
  110. package/src/photon/websocket/message/index.ts +27 -39
  111. package/src/photon/websocket/path/index.ts +2 -5
  112. package/src/scale/batch/index.ts +14 -6
  113. package/src/scale/fanout/index.ts +6 -6
  114. package/src/scale/index.ts +2 -4
  115. package/src/scale/outbound/index.ts +192 -0
  116. package/src/scale/relay/index.ts +24 -16
  117. package/src/scale/sink/index.ts +6 -11
  118. package/src/scale/transport/index.ts +27 -44
  119. package/src/scale/tunnel/index.ts +56 -86
  120. package/src/smartfox/common/bluebox/gateway/index.ts +401 -0
  121. package/src/smartfox/common/bluebox/index.ts +4 -0
  122. package/src/smartfox/{x2 → common}/bluebox/transport/index.ts +36 -23
  123. package/src/smartfox/common/index.ts +5 -0
  124. package/src/smartfox/common/strikes/index.ts +74 -0
  125. package/src/smartfox/pro/bluebox/index.ts +176 -4
  126. package/src/smartfox/pro/codec/index.ts +8 -7
  127. package/src/smartfox/pro/escalation/index.ts +28 -20
  128. package/src/smartfox/pro/flood/index.ts +2 -2
  129. package/src/smartfox/pro/format/extension/raw/index.ts +12 -2
  130. package/src/smartfox/pro/handler/chat/index.ts +11 -8
  131. package/src/smartfox/pro/handler/login/index.ts +2 -25
  132. package/src/smartfox/pro/handler/shared/index.ts +4 -3
  133. package/src/smartfox/pro/index.ts +11 -5
  134. package/src/smartfox/pro/login/gate/index.ts +166 -0
  135. package/src/smartfox/pro/login/index.ts +1 -0
  136. package/src/smartfox/pro/moderation/index.ts +220 -0
  137. package/src/smartfox/pro/options/index.ts +5 -5
  138. package/src/smartfox/pro/protocol/index.ts +62 -8
  139. package/src/smartfox/pro/web/index.ts +33 -26
  140. package/src/smartfox/pro/zone/index.ts +59 -282
  141. package/src/smartfox/pro/zones/index.ts +3 -3
  142. package/src/smartfox/x2/bluebox/index.ts +149 -4
  143. package/src/smartfox/x2/chat/index.ts +9 -19
  144. package/src/smartfox/x2/codec/index.ts +34 -40
  145. package/src/smartfox/x2/config/index.ts +6 -1
  146. package/src/smartfox/x2/data/array/index.ts +96 -60
  147. package/src/smartfox/x2/data/object/index.ts +144 -82
  148. package/src/smartfox/x2/data/serializer/index.ts +41 -23
  149. package/src/smartfox/x2/flood/gate/index.ts +19 -13
  150. package/src/smartfox/x2/format/buddy/init/response/index.ts +4 -3
  151. package/src/smartfox/x2/format/find/rooms/response/index.ts +4 -3
  152. package/src/smartfox/x2/format/find/users/response/index.ts +4 -3
  153. package/src/smartfox/x2/format/group/subscribe/response/index.ts +4 -3
  154. package/src/smartfox/x2/format/login/response/index.ts +4 -3
  155. package/src/smartfox/x2/format/mmo/position/response/index.ts +8 -6
  156. package/src/smartfox/x2/format/room/join/response/index.ts +4 -3
  157. package/src/smartfox/x2/games/index.ts +35 -8
  158. package/src/smartfox/x2/hall/index.ts +0 -6
  159. package/src/smartfox/x2/handler/session/index.ts +1 -1
  160. package/src/smartfox/x2/index.ts +8 -3
  161. package/src/smartfox/x2/member/index.ts +10 -3
  162. package/src/smartfox/x2/packet/login/response/index.ts +12 -0
  163. package/src/smartfox/x2/protocol/index.ts +11 -1
  164. package/src/smartfox/x2/rooms/index.ts +1 -2
  165. package/src/smartfox/x2/sessions/index.ts +6 -0
  166. package/src/smartfox/x2/udp/index.ts +61 -24
  167. package/src/smartfox/x2/web/index.ts +35 -17
  168. package/src/smartfox/x2/websocket/index.ts +18 -36
  169. package/src/smartfox/x2/zone/index.ts +77 -28
  170. package/src/smartfox/x2/zones/index.ts +17 -5
  171. package/src/photon/enet/handler/index.ts +0 -65
  172. package/src/scale/hosted/index.ts +0 -68
  173. package/src/scale/outlet/index.ts +0 -121
  174. package/src/smartfox/pro/bluebox/gateway/index.ts +0 -310
  175. package/src/smartfox/pro/bluebox/session/index.ts +0 -63
  176. package/src/smartfox/pro/bluebox/transport/index.ts +0 -104
  177. package/src/smartfox/x2/bluebox/gateway/index.ts +0 -315
  178. /package/src/smartfox/{x2 → common}/bluebox/session/index.ts +0 -0
  179. /package/src/smartfox/{pro → common}/counters/index.ts +0 -0
  180. /package/src/smartfox/{pro → common}/counters/memory/index.ts +0 -0
  181. /package/src/smartfox/{pro → common}/counters/offender/index.ts +0 -0
  182. /package/src/smartfox/{pro → common}/counters/store/index.ts +0 -0
  183. /package/src/smartfox/{pro → common}/sequence/index.ts +0 -0
@@ -10,7 +10,6 @@ import { FrontBridge } from '../../front/index.ts'
10
10
  import type { SessionMover } from '../../mover/index.ts'
11
11
  import { GameRouter } from '../../route/game/index.ts'
12
12
  import { Seeds, type SessionSeed } from '../../seed/index.ts'
13
- import { SpreadHandler, type Spreader } from '../../spread/index.ts'
14
13
 
15
14
  /**
16
15
  * A Game Server's place among the shards
@@ -27,11 +26,6 @@ export class GameSeat {
27
26
  */
28
27
  private readonly mover: SessionMover | null
29
28
 
30
- /**
31
- * Gathers room broadcasts
32
- */
33
- private readonly spreader: Spreader
34
-
35
29
  /**
36
30
  * Seals and reads the tokens sessions take along
37
31
  */
@@ -45,20 +39,17 @@ export class GameSeat {
45
39
  /**
46
40
  * @param server the Game Server of this shard
47
41
  * @param mover carries sessions to other shards, or null
48
- * @param spreader gathers room broadcasts
49
42
  * @param authority seals and reads the tokens sessions take along
50
43
  * @param clock tells the time
51
44
  */
52
45
  public constructor(
53
46
  server: GameServer,
54
47
  mover: SessionMover | null,
55
- spreader: Spreader,
56
48
  authority: TokenAuthority,
57
49
  clock: Clock,
58
50
  ) {
59
51
  this.server = server
60
52
  this.mover = mover
61
- this.spreader = spreader
62
53
  this.authority = authority
63
54
  this.clock = clock
64
55
  }
@@ -69,17 +60,11 @@ export class GameSeat {
69
60
  */
70
61
  public outfit(channel: Channel): void {
71
62
  const pipeline = channel.pipeline
72
- pipeline
73
- .addAfter<FrontBridge>(
74
- MessagePipeline.handler,
75
- FrontBridge.NAME,
76
- new FrontBridge(),
77
- )
78
- .addBefore<SpreadHandler>(
79
- GameServer.dispatcher,
80
- SpreadHandler.NAME,
81
- new SpreadHandler(this.spreader),
82
- )
63
+ pipeline.addAfter<FrontBridge>(
64
+ MessagePipeline.handler,
65
+ FrontBridge.NAME,
66
+ new FrontBridge(),
67
+ )
83
68
  if (this.mover === null) return
84
69
 
85
70
  pipeline.addBefore<GameRouter>(
@@ -102,12 +87,7 @@ export class GameSeat {
102
87
  Seeds.open(channel, seed).initialise(init)
103
88
  channel.pipeline.addLast<BackBridge>(BackBridge.NAME, new BackBridge(init))
104
89
 
105
- void this.server.setup(false)(channel)
106
- channel.pipeline.addBefore<SpreadHandler>(
107
- GameServer.dispatcher,
108
- SpreadHandler.NAME,
109
- new SpreadHandler(this.spreader),
110
- )
90
+ this.server.setup(false)(channel)
111
91
  GamePeer.of(channel, this.clock.now()).token = token
112
92
  }
113
93
  }
@@ -78,7 +78,7 @@ export class MasterSeat {
78
78
 
79
79
  const pipeline = channel.pipeline
80
80
  pipeline.addLast<BackBridge>(BackBridge.NAME, new BackBridge(init))
81
- void this.server.setup(false)(channel)
81
+ this.server.setup(false)(channel)
82
82
 
83
83
  if (pipeline.context(SessionMover.DISPATCHER) === null) return
84
84
  pipeline.addBefore<MasterRouter>(
@@ -1,8 +1,8 @@
1
1
  import type { Channel, ChannelHandler } from '@netifyjs/netify'
2
2
 
3
3
  import { PacketDispatcher } from '../../../dispatch/dispatcher/index.ts'
4
+ import { Session } from '../../../dispatch/session/index.ts'
4
5
  import type { Portable, Reach } from '../../../scale/portable/index.ts'
5
- import { Tunnel } from '../../../scale/tunnel/index.ts'
6
6
  import { BalancingCodec } from '../../balancing/codec/index.ts'
7
7
  import { GameGate } from '../../game/gate/index.ts'
8
8
  import { SharedFrames } from '../../game/shared/index.ts'
@@ -14,7 +14,6 @@ import { MasterRouter } from '../route/master/index.ts'
14
14
  import type { GameSeat } from '../seat/game/index.ts'
15
15
  import type { MasterSeat } from '../seat/master/index.ts'
16
16
  import { type SeedRole, Seeds, type SessionSeed } from '../seed/index.ts'
17
- import { SpreadHandler } from '../spread/index.ts'
18
17
 
19
18
  /**
20
19
  * Photon's side of moving sessions and sharing broadcasts between shards
@@ -107,8 +106,7 @@ export class Sessions implements Portable<SessionSeed> {
107
106
  handler instanceof OperationGate ||
108
107
  handler instanceof TouchHandler ||
109
108
  handler instanceof MasterRouter ||
110
- handler instanceof GameRouter ||
111
- handler instanceof SpreadHandler
109
+ handler instanceof GameRouter
112
110
  )
113
111
  }
114
112
 
@@ -119,9 +117,10 @@ export class Sessions implements Portable<SessionSeed> {
119
117
  public reach(channel: Channel): Reach {
120
118
  if (SharedFrames.reads(channel)) return 'socket'
121
119
 
120
+ const bridge = channel.pipeline.get(FrontBridge.NAME)
122
121
  const away =
123
- Tunnel.carries(channel) ||
124
- channel.pipeline.get<FrontBridge>(FrontBridge.NAME)?.engaged === true
122
+ Session.of(channel).moved ||
123
+ (bridge instanceof FrontBridge && bridge.engaged)
125
124
 
126
125
  return away ? 'pipeline' : 'packet'
127
126
  }
@@ -1,21 +1,16 @@
1
- import {
2
- AttributeKey,
3
- type Channel,
4
- type HandlerContext,
5
- OutboundHandlerAdapter,
6
- } from '@netifyjs/netify'
1
+ import { AttributeKey, type Channel } from '@netifyjs/netify'
7
2
 
8
3
  import type { Fanout } from '../../../scale/fanout/index.ts'
9
- import { PacketAdapter } from '../../balancing/packet/adapter/index.ts'
4
+ import type { Broadcast } from '../../game/broadcast/index.ts'
10
5
  import type { Clock } from '../../game/clock/index.ts'
11
6
  import { GamePeer } from '../../game/peer/index.ts'
12
7
  import type { SessionSeed } from '../seed/index.ts'
13
8
 
14
9
  /**
15
- * Sends the copies of a packet a room writes as one broadcast
10
+ * Sends a room's broadcasts to sessions wherever their clients are
16
11
  * @class
17
12
  */
18
- export class Spreader {
13
+ export class Spreader implements Broadcast {
19
14
  /**
20
15
  * Scope prefix of rooms
21
16
  */
@@ -38,31 +33,6 @@ export class Spreader {
38
33
  */
39
34
  private readonly clock: Clock
40
35
 
41
- /**
42
- * Connections waiting for the gathered packet
43
- */
44
- private readonly waiting: HandlerContext[]
45
-
46
- /**
47
- * Sends what's gathered
48
- */
49
- private readonly release: () => void
50
-
51
- /**
52
- * Packet being gathered, or null
53
- */
54
- private packet: PacketAdapter | null
55
-
56
- /**
57
- * Whether a release is on its way
58
- */
59
- private scheduled: boolean
60
-
61
- /**
62
- * Whether a broadcast is being written
63
- */
64
- private busy: boolean
65
-
66
36
  /**
67
37
  * @param fanout sends broadcasts to sessions wherever their clients are
68
38
  * @param clock tells the time
@@ -70,14 +40,6 @@ export class Spreader {
70
40
  public constructor(fanout: Fanout<SessionSeed>, clock: Clock) {
71
41
  this.fanout = fanout
72
42
  this.clock = clock
73
- this.waiting = []
74
- this.packet = null
75
- this.scheduled = false
76
- this.busy = false
77
- this.release = (): void => {
78
- this.scheduled = false
79
- this.flush()
80
- }
81
43
  }
82
44
 
83
45
  /**
@@ -89,60 +51,11 @@ export class Spreader {
89
51
  }
90
52
 
91
53
  /**
92
- * Takes one write, true if it was kept for a broadcast
93
- * @param context the connection
94
- * @param message what's being written
95
- */
96
- public take(context: HandlerContext, message: unknown): boolean {
97
- if (this.busy) return false
98
- if (message !== this.packet) this.flush()
99
- if (!(message instanceof PacketAdapter)) return false
100
-
101
- this.packet = message
102
- this.waiting.push(context)
103
- if (this.scheduled) return true
104
-
105
- this.scheduled = true
106
- queueMicrotask(this.release)
107
-
108
- return true
109
- }
110
-
111
- /**
112
- * Sends what's gathered
113
- */
114
- public flush(): void {
115
- const packet = this.packet
116
- const waiting = this.waiting.splice(0)
117
- this.packet = null
118
- if (packet === null) return
119
-
120
- this.busy = true
121
- try {
122
- this.send(waiting, packet)
123
- } finally {
124
- this.busy = false
125
- }
126
- }
127
-
128
- /**
129
- * Writes a packet to one connection or broadcasts it to several
130
- * @param waiting connections the packet is for
131
- * @param packet what's being sent
54
+ * Sends a packet to several sessions, encoded once where it can be
55
+ * @param channels who gets it
56
+ * @param packet a typed packet
132
57
  */
133
- private send(
134
- waiting: readonly HandlerContext[],
135
- packet: PacketAdapter,
136
- ): void {
137
- const only = waiting.length === 1 ? waiting[0] : undefined
138
- if (only !== undefined) {
139
- only.writeAndFlushSync(packet)
140
- return
141
- }
142
-
143
- const channels = waiting.map<Channel>(
144
- (context: HandlerContext): Channel => context.channel,
145
- )
58
+ public spread(channels: readonly Channel[], packet: unknown): void {
146
59
  if (this.fanout.gated) this.retune(channels)
147
60
 
148
61
  this.fanout.spread(channels, packet)
@@ -182,55 +95,3 @@ export class Spreader {
182
95
  this.fanout.join(channel, `${Spreader.SCOPE}${next}`)
183
96
  }
184
97
  }
185
-
186
- /**
187
- * Hands a session's writes to the spreader of its Game Server
188
- * @class
189
- */
190
- export class SpreadHandler extends OutboundHandlerAdapter {
191
- /**
192
- * Pipeline name of the handler
193
- */
194
- public static readonly NAME: string = 'photon:cluster:spread'
195
-
196
- /**
197
- * Gathers the copies of one packet
198
- */
199
- private readonly spreader: Spreader
200
-
201
- /**
202
- * @param spreader gathers the copies of one packet
203
- */
204
- public constructor(spreader: Spreader) {
205
- super()
206
-
207
- this.spreader = spreader
208
- }
209
-
210
- /**
211
- * Passes a write on once everything gathered has gone
212
- * @param context the connection
213
- * @param message what's being written
214
- */
215
- public override write(
216
- context: HandlerContext,
217
- message: unknown,
218
- ): Promise<void> {
219
- this.spreader.flush()
220
-
221
- return context.write(message)
222
- }
223
-
224
- /**
225
- * Keeps a packet for a broadcast or passes the write on
226
- * @param context the connection
227
- * @param message what's being written
228
- */
229
- public writeSync(context: HandlerContext, message: unknown): boolean {
230
- if (this.spreader.take(context, message)) return true
231
-
232
- context.writeSync(message)
233
-
234
- return true
235
- }
236
- }
@@ -9,7 +9,6 @@ import {
9
9
  InboundHandlerAdapter,
10
10
  } from '@netifyjs/netify'
11
11
 
12
- import { DisconnectReason } from '../closure/index.ts'
13
12
  import { EnetDatagram } from '../datagram/index.ts'
14
13
  import { EnetLink, type EnetOwner } from '../link/index.ts'
15
14
  import { type EnetOptions, EnetSettings } from '../options/index.ts'
@@ -107,13 +106,7 @@ export class EnetClient
107
106
  const link = client.open(socket)
108
107
 
109
108
  link.peer.connect(options.channels ?? 2, options.crc ?? false)
110
-
111
- try {
112
- await link.settled
113
- } catch (cause) {
114
- link.peer.abort(DisconnectReason.CLIENT)
115
- throw new Error('Channel setup failed', { cause })
116
- }
109
+ await link.settled
117
110
 
118
111
  if (!link.peer.open) throw new Error('Connection failed')
119
112
  return client
@@ -125,7 +125,7 @@ export class EnetGateway
125
125
  if (sender === null) return
126
126
 
127
127
  if (EnetDatagram.probes(bytes)) {
128
- context.channel.writeAndFlushSync(packet.reply(bytes.slice()))
128
+ context.channel.writeAndFlush(packet.reply(bytes.slice()))
129
129
  return
130
130
  }
131
131
 
@@ -162,12 +162,9 @@ export class EnetGateway
162
162
  * Drops every client of a closed socket
163
163
  * @param context the datagram socket
164
164
  */
165
- public override channelInactive(
166
- context: HandlerContext,
167
- ): void | Promise<void> {
165
+ public override channelInactive(context: HandlerContext): void {
168
166
  this.shutdown()
169
-
170
- return context.fireChannelInactive()
167
+ context.fireChannelInactive()
171
168
  }
172
169
 
173
170
  /**
@@ -237,14 +234,7 @@ export class EnetGateway
237
234
  else this.links.set(key, link)
238
235
 
239
236
  link.peer.accept(id, datagram, connect, contested)
240
- const ready = contested ? link.settled : link.activate()
241
- ready.catch((cause: unknown): Promise<void> => {
242
- link.peer.abort(DisconnectReason.LOGIC)
243
-
244
- return context.fireExceptionCaught(
245
- new Error('Channel setup failed', { cause }),
246
- )
247
- })
237
+ if (!contested) link.activate()
248
238
  link.peer.receive(datagram)
249
239
  }
250
240
 
@@ -293,7 +283,7 @@ export class EnetGateway
293
283
  datagram.flags === EnetDatagram.checked,
294
284
  )
295
285
 
296
- socket.writeAndFlushSync(new DatagramPacket(bytes, sender))
286
+ socket.writeAndFlush(new DatagramPacket(bytes, sender))
297
287
  }
298
288
 
299
289
  /**
@@ -6,13 +6,13 @@ export { EnetDatagram } from './datagram/index.ts'
6
6
  export { Flight } from './flight/index.ts'
7
7
  export { EnetGateway } from './gateway/index.ts'
8
8
  export type { EnetGatewayOptions } from './gateway/index.ts'
9
- export { EnetFrameHandler } from './handler/index.ts'
10
9
  export { Admission, EnetLane } from './lane/index.ts'
11
10
  export type { AdmissionCode } from './lane/index.ts'
12
11
  export { EnetLink } from './link/index.ts'
13
12
  export type { EnetOwner } from './link/index.ts'
14
13
  export { EnetSettings } from './options/index.ts'
15
14
  export type { EnetOptions } from './options/index.ts'
15
+ export { EnetOutbound } from './outbound/index.ts'
16
16
  export { EnetPeer } from './peer/index.ts'
17
17
  export type { EnetEncryption, EnetHooks } from './peer/index.ts'
18
18
  export { EnetServer } from './server/index.ts'
@@ -7,8 +7,8 @@ import {
7
7
  } from '@netifyjs/netify'
8
8
 
9
9
  import type { Frame } from '../../frame/index.ts'
10
- import { EnetFrameHandler } from '../handler/index.ts'
11
10
  import type { EnetSettings } from '../options/index.ts'
11
+ import { EnetOutbound } from '../outbound/index.ts'
12
12
  import { type EnetHooks, EnetPeer } from '../peer/index.ts'
13
13
  import { EnetTransport } from '../transport/index.ts'
14
14
 
@@ -29,7 +29,7 @@ export type EnetOwner = {
29
29
  */
30
30
  export class EnetLink implements EnetHooks {
31
31
  /**
32
- * Pipeline name of the handler that takes frames
32
+ * Pipeline name of the handler that reads datagrams
33
33
  */
34
34
  public static readonly handler: string = 'enet'
35
35
 
@@ -53,6 +53,11 @@ export class EnetLink implements EnetHooks {
53
53
  */
54
54
  public readonly settled: Promise<void>
55
55
 
56
+ /**
57
+ * Where the virtual channel's frames wait, or null
58
+ */
59
+ private outbound: EnetOutbound | null
60
+
56
61
  /**
57
62
  * Whoever keeps track of links
58
63
  */
@@ -103,12 +108,24 @@ export class EnetLink implements EnetHooks {
103
108
  this.recipient = client ? null : remote
104
109
  this.settling = Promise.withResolvers<undefined>()
105
110
  this.settled = this.settling.promise
111
+ this.outbound = null
106
112
  this.peer = client
107
113
  ? EnetPeer.client(settings, this)
108
114
  : EnetPeer.server(settings, this)
109
115
  this.channel = new Channel(
110
116
  new EnetTransport(remote.hostname, remote.port, this.peer),
111
- options,
117
+ {
118
+ ...options,
119
+ /**
120
+ * One command per frame
121
+ */
122
+ outbound: EnetOutbound.factory(
123
+ this.peer,
124
+ (outbound: EnetOutbound): void => {
125
+ this.outbound = outbound
126
+ },
127
+ ),
128
+ },
112
129
  )
113
130
  this.channel.attributes.set<EnetPeer>(EnetPeer.key, this.peer)
114
131
  }
@@ -156,14 +173,8 @@ export class EnetLink implements EnetHooks {
156
173
  /**
157
174
  * Builds the virtual channel's pipeline and marks it live
158
175
  */
159
- public activate(): Promise<void> {
160
- return this.channel.activate(async (channel: Channel): Promise<void> => {
161
- channel.pipeline.addLast<EnetFrameHandler>(
162
- EnetLink.handler,
163
- new EnetFrameHandler(this.peer),
164
- )
165
- await this.setup(channel)
166
- })
176
+ public activate(): void {
177
+ this.channel.activate(this.setup)
167
178
  }
168
179
 
169
180
  /**
@@ -171,7 +182,7 @@ export class EnetLink implements EnetHooks {
171
182
  * @param datagram bytes ready to send
172
183
  */
173
184
  public transmit(datagram: Uint8Array): void {
174
- this.socket.writeAndFlushSync(new DatagramPacket(datagram, this.recipient))
185
+ this.socket.writeAndFlush(new DatagramPacket(datagram, this.recipient))
175
186
  }
176
187
 
177
188
  /**
@@ -179,21 +190,29 @@ export class EnetLink implements EnetHooks {
179
190
  * @param frame what arrived
180
191
  */
181
192
  public deliver(frame: Frame): void {
182
- void this.channel.handleMessage(frame, frame.payload.length)
193
+ this.channel.receive(frame, frame.payload.length)
183
194
  }
184
195
 
185
196
  /**
186
197
  * Brings the virtual channel up
187
198
  */
188
199
  public established(): void {
189
- this.settling.resolve(this.activate().then((): undefined => undefined))
200
+ this.activate()
201
+ this.settling.resolve(undefined)
202
+ }
203
+
204
+ /**
205
+ * Lets the virtual channel know its frames went out
206
+ */
207
+ public relieved(): void {
208
+ this.outbound?.relieved()
190
209
  }
191
210
 
192
211
  /**
193
212
  * Closes the virtual channel and tells the owner
194
213
  */
195
214
  public closed(): void {
196
- void this.channel.handleClose()
215
+ this.channel.ended()
197
216
  this.settling.resolve(undefined)
198
217
  this.owner.released(this)
199
218
  }