@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,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 {
|
|
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
|
|
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
|
-
*
|
|
93
|
-
* @param
|
|
94
|
-
* @param
|
|
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
|
-
|
|
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.
|
|
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
|
-
|
|
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.
|
|
286
|
+
socket.writeAndFlush(new DatagramPacket(bytes, sender))
|
|
297
287
|
}
|
|
298
288
|
|
|
299
289
|
/**
|
package/src/photon/enet/index.ts
CHANGED
|
@@ -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
|
|
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
|
-
|
|
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():
|
|
160
|
-
|
|
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.
|
|
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
|
-
|
|
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.
|
|
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
|
-
|
|
215
|
+
this.channel.ended()
|
|
197
216
|
this.settling.resolve(undefined)
|
|
198
217
|
this.owner.released(this)
|
|
199
218
|
}
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ByteBuf,
|
|
3
|
+
type Outbound,
|
|
4
|
+
type OutboundEvents,
|
|
5
|
+
type OutboundFactory,
|
|
6
|
+
type OutboundLimits,
|
|
7
|
+
QueueFullError,
|
|
8
|
+
type Transport,
|
|
9
|
+
} from '@netifyjs/netify'
|
|
10
|
+
|
|
11
|
+
import { Frame } from '../../frame/index.ts'
|
|
12
|
+
import type { EnetPeer } from '../peer/index.ts'
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Someone waiting for the peer to send a certain number of bytes
|
|
16
|
+
*/
|
|
17
|
+
type Waiter = {
|
|
18
|
+
/**
|
|
19
|
+
* Sent bytes that settle it
|
|
20
|
+
*/
|
|
21
|
+
readonly target: number
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The promise handed out
|
|
25
|
+
*/
|
|
26
|
+
readonly result: PromiseWithResolvers<undefined>
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Hands a virtual channel's messages to its reliable UDP connection, one command each
|
|
31
|
+
* @class
|
|
32
|
+
*/
|
|
33
|
+
export class EnetOutbound implements Outbound {
|
|
34
|
+
/**
|
|
35
|
+
* Nothing is ever staged
|
|
36
|
+
*/
|
|
37
|
+
private static readonly unstaged: Uint8Array = new Uint8Array(0)
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The connection messages leave on
|
|
41
|
+
*/
|
|
42
|
+
private readonly peer: EnetPeer
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Water marks and cap
|
|
46
|
+
*/
|
|
47
|
+
private readonly limits: OutboundLimits
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Who hears about writability and failures
|
|
51
|
+
*/
|
|
52
|
+
private readonly events: OutboundEvents
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* People waiting on flushed()
|
|
56
|
+
*/
|
|
57
|
+
private readonly waiters: Waiter[]
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Whether it's below the high water mark
|
|
61
|
+
*/
|
|
62
|
+
private writable: boolean
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Error it stopped with, or null
|
|
66
|
+
*/
|
|
67
|
+
private failure: Error | null
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @param peer the connection messages leave on
|
|
71
|
+
* @param limits water marks and cap
|
|
72
|
+
* @param events who hears about writability and failures
|
|
73
|
+
*/
|
|
74
|
+
private constructor(
|
|
75
|
+
peer: EnetPeer,
|
|
76
|
+
limits: OutboundLimits,
|
|
77
|
+
events: OutboundEvents,
|
|
78
|
+
) {
|
|
79
|
+
this.peer = peer
|
|
80
|
+
this.limits = limits
|
|
81
|
+
this.events = events
|
|
82
|
+
this.waiters = []
|
|
83
|
+
this.writable = true
|
|
84
|
+
this.failure = null
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Builds the outbound side for a channel on a connection
|
|
89
|
+
* @param peer the connection messages leave on
|
|
90
|
+
* @param built hears about the outbound once it exists
|
|
91
|
+
*/
|
|
92
|
+
public static factory(
|
|
93
|
+
peer: EnetPeer,
|
|
94
|
+
built: (outbound: EnetOutbound) => void,
|
|
95
|
+
): OutboundFactory {
|
|
96
|
+
return (
|
|
97
|
+
_transport: Transport,
|
|
98
|
+
limits: OutboundLimits,
|
|
99
|
+
events: OutboundEvents,
|
|
100
|
+
): EnetOutbound => {
|
|
101
|
+
const outbound = new EnetOutbound(peer, limits, events)
|
|
102
|
+
built(outbound)
|
|
103
|
+
|
|
104
|
+
return outbound
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Whether it's below the high water mark
|
|
110
|
+
*/
|
|
111
|
+
public get isWritable(): boolean {
|
|
112
|
+
return this.writable
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Payload bytes the connection still has queued
|
|
117
|
+
*/
|
|
118
|
+
public get pendingBytes(): number {
|
|
119
|
+
return this.peer.queuedBytes
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Payload bytes the connection put on the wire
|
|
124
|
+
*/
|
|
125
|
+
public get writtenBytes(): number {
|
|
126
|
+
return this.peer.sentBytes
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Always empty
|
|
131
|
+
*/
|
|
132
|
+
public get stagingBuffer(): Uint8Array {
|
|
133
|
+
return EnetOutbound.unstaged
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Queues a frame, or bytes as a reliable message on channel 0
|
|
138
|
+
* @param message a Frame, a ByteBuf or bytes
|
|
139
|
+
*/
|
|
140
|
+
public write(message: unknown): void {
|
|
141
|
+
if (message instanceof Frame) {
|
|
142
|
+
this.add(message)
|
|
143
|
+
return
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (message instanceof ByteBuf) {
|
|
147
|
+
this.add(Frame.reliable(message.toUint8Array()))
|
|
148
|
+
return
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (message instanceof Uint8Array) {
|
|
152
|
+
this.add(Frame.reliable(message))
|
|
153
|
+
return
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
throw new TypeError('Message type is not supported')
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Frames can't be written into directly
|
|
161
|
+
*/
|
|
162
|
+
public reserve(): number {
|
|
163
|
+
return -1
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Nothing is ever reserved
|
|
168
|
+
*/
|
|
169
|
+
public commit(): void {
|
|
170
|
+
return
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* The connection sends on its own clock
|
|
175
|
+
*/
|
|
176
|
+
public flush(): void {
|
|
177
|
+
return
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* The connection sends on its own clock
|
|
182
|
+
*/
|
|
183
|
+
public send(): void {
|
|
184
|
+
return
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* The socket underneath never pushes back on a virtual channel
|
|
189
|
+
*/
|
|
190
|
+
public drain(): void {
|
|
191
|
+
return
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Settles once everything queued so far is on the wire
|
|
196
|
+
*/
|
|
197
|
+
public flushed(): Promise<void> {
|
|
198
|
+
if (this.failure !== null) return Promise.reject(this.failure)
|
|
199
|
+
if (this.peer.queuedBytes === 0) return Promise.resolve()
|
|
200
|
+
|
|
201
|
+
const target = this.peer.sentBytes + this.peer.queuedBytes
|
|
202
|
+
const last = this.waiters.at(-1)
|
|
203
|
+
if (last?.target === target) return last.result.promise
|
|
204
|
+
|
|
205
|
+
const result = Promise.withResolvers<undefined>()
|
|
206
|
+
this.waiters.push({ target, result })
|
|
207
|
+
return result.promise
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Reads don't change how frames leave
|
|
212
|
+
*/
|
|
213
|
+
public reading(): void {
|
|
214
|
+
return
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Refuses later writes, drops an overfull queue and lets go of anyone waiting
|
|
219
|
+
* @param error why it stopped
|
|
220
|
+
*/
|
|
221
|
+
public fail(error: Error): void {
|
|
222
|
+
if (this.failure !== null) return
|
|
223
|
+
|
|
224
|
+
this.failure = error
|
|
225
|
+
if (error instanceof QueueFullError) this.peer.discard()
|
|
226
|
+
for (const waiter of this.waiters.splice(0)) waiter.result.reject(error)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Notes that queued messages went on the wire
|
|
231
|
+
*/
|
|
232
|
+
public relieved(): void {
|
|
233
|
+
const sent = this.peer.sentBytes
|
|
234
|
+
let settled = 0
|
|
235
|
+
while (settled < this.waiters.length) {
|
|
236
|
+
const waiter = this.waiters[settled]
|
|
237
|
+
if (waiter === undefined || waiter.target > sent) break
|
|
238
|
+
|
|
239
|
+
waiter.result.resolve(undefined)
|
|
240
|
+
settled += 1
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (settled > 0) this.waiters.splice(0, settled)
|
|
244
|
+
this.measure()
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Hands a frame to the connection unless the queue is full
|
|
249
|
+
* @param frame what to send
|
|
250
|
+
*/
|
|
251
|
+
private add(frame: Frame): void {
|
|
252
|
+
if (this.failure !== null) return
|
|
253
|
+
if (
|
|
254
|
+
this.peer.queuedBytes + frame.payload.length >
|
|
255
|
+
this.limits.maxPendingBytes
|
|
256
|
+
) {
|
|
257
|
+
this.events.failed(new QueueFullError())
|
|
258
|
+
return
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
this.peer.send(frame)
|
|
262
|
+
this.measure()
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Tells the channel if the queue crossed a water mark
|
|
267
|
+
*/
|
|
268
|
+
private measure(): void {
|
|
269
|
+
const queued = this.peer.queuedBytes
|
|
270
|
+
|
|
271
|
+
if (this.writable && queued > this.limits.highWaterMark) {
|
|
272
|
+
this.writable = false
|
|
273
|
+
this.events.writability(false)
|
|
274
|
+
} else if (!this.writable && queued < this.limits.lowWaterMark) {
|
|
275
|
+
this.writable = true
|
|
276
|
+
this.events.writability(true)
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|