@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
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One bracketed part of a filter while it's being read
|
|
3
|
+
*/
|
|
4
|
+
type Scope = {
|
|
5
|
+
/**
|
|
6
|
+
* Whether a SELECT starts in it
|
|
7
|
+
*/
|
|
8
|
+
query: boolean
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Tables and other row sources its FROM names
|
|
12
|
+
*/
|
|
13
|
+
sources: number
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Whether a FROM list is being read
|
|
17
|
+
*/
|
|
18
|
+
listing: boolean
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Rows the subqueries inside it visit for each of its own rows
|
|
22
|
+
*/
|
|
23
|
+
inner: number
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* How much work a SQL lobby filter can cause, read without running it
|
|
28
|
+
* @class
|
|
29
|
+
*/
|
|
30
|
+
export class FilterCost {
|
|
31
|
+
/**
|
|
32
|
+
* Words that end a FROM list
|
|
33
|
+
*/
|
|
34
|
+
private static readonly CLAUSES: ReadonlySet<string> = new Set<string>([
|
|
35
|
+
'WHERE',
|
|
36
|
+
'GROUP',
|
|
37
|
+
'HAVING',
|
|
38
|
+
'ORDER',
|
|
39
|
+
'LIMIT',
|
|
40
|
+
'WINDOW',
|
|
41
|
+
'UNION',
|
|
42
|
+
'EXCEPT',
|
|
43
|
+
'INTERSECT',
|
|
44
|
+
'ON',
|
|
45
|
+
'USING',
|
|
46
|
+
])
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Deepest bracket nesting
|
|
50
|
+
*/
|
|
51
|
+
public readonly depth: number
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Whether it asks for a recursive query
|
|
55
|
+
*/
|
|
56
|
+
public readonly recursive: boolean
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Row visits as powers of the game count, with their multipliers
|
|
60
|
+
*/
|
|
61
|
+
private readonly terms: readonly (readonly [number, number])[]
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* @param depth deepest bracket nesting
|
|
65
|
+
* @param recursive whether it asks for a recursive query
|
|
66
|
+
* @param terms row visits as powers of the game count
|
|
67
|
+
*/
|
|
68
|
+
private constructor(
|
|
69
|
+
depth: number,
|
|
70
|
+
recursive: boolean,
|
|
71
|
+
terms: readonly (readonly [number, number])[],
|
|
72
|
+
) {
|
|
73
|
+
this.depth = depth
|
|
74
|
+
this.recursive = recursive
|
|
75
|
+
this.terms = terms
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Reads a filter, skipping text, quoted names and comments
|
|
80
|
+
* @param filter what follows SELECT Id FROM Game WHERE
|
|
81
|
+
*/
|
|
82
|
+
public static of(filter: string): FilterCost {
|
|
83
|
+
const scopes: Scope[] = [FilterCost.scope(true, 1)]
|
|
84
|
+
const finished: Scope[][] = [[]]
|
|
85
|
+
let depth = 0
|
|
86
|
+
let deepest = 0
|
|
87
|
+
let recursive = false
|
|
88
|
+
let index = 0
|
|
89
|
+
|
|
90
|
+
while (index < filter.length) {
|
|
91
|
+
const char = filter[index] ?? ''
|
|
92
|
+
const end = FilterCost.skip(filter, index)
|
|
93
|
+
if (end > index) {
|
|
94
|
+
index = end
|
|
95
|
+
continue
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const scope = scopes.at(-1)
|
|
99
|
+
if (/[A-Za-z_]/.test(char)) {
|
|
100
|
+
const word = /^[A-Za-z_][A-Za-z0-9_$]*/.exec(filter.slice(index))
|
|
101
|
+
const text = (word?.[0] ?? char).toUpperCase()
|
|
102
|
+
index += text.length
|
|
103
|
+
if (text === 'RECURSIVE') recursive = true
|
|
104
|
+
if (scope !== undefined) FilterCost.note(scope, text)
|
|
105
|
+
continue
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
index += 1
|
|
109
|
+
if (char === ',' && scope?.listing === true) scope.sources += 1
|
|
110
|
+
if (char === '(') {
|
|
111
|
+
depth += 1
|
|
112
|
+
deepest = Math.max(deepest, depth)
|
|
113
|
+
scopes.push(FilterCost.scope(false, 0))
|
|
114
|
+
finished.push([])
|
|
115
|
+
} else if (char === ')' && depth > 0) {
|
|
116
|
+
depth -= 1
|
|
117
|
+
FilterCost.close(scopes, finished)
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
while (scopes.length > 1) FilterCost.close(scopes, finished)
|
|
122
|
+
|
|
123
|
+
const root = scopes[0] ?? FilterCost.scope(true, 1)
|
|
124
|
+
return new FilterCost(deepest, recursive, FilterCost.expand(root, finished))
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Most rows the filter may visit over some games
|
|
129
|
+
* @param games how many games the lobby holds
|
|
130
|
+
*/
|
|
131
|
+
public rows(games: number): number {
|
|
132
|
+
let total = 0
|
|
133
|
+
for (const [power, times] of this.terms) {
|
|
134
|
+
total += times * games ** power
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return total
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* A bracket that's just been opened
|
|
142
|
+
* @param query whether a SELECT starts in it
|
|
143
|
+
* @param sources row sources it starts with
|
|
144
|
+
*/
|
|
145
|
+
private static scope(query: boolean, sources: number): Scope {
|
|
146
|
+
return { query, sources, listing: false, inner: 0 }
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Where the text, quoted name or comment at a position ends, or the position itself
|
|
151
|
+
* @param filter the whole filter
|
|
152
|
+
* @param index where to look
|
|
153
|
+
*/
|
|
154
|
+
private static skip(filter: string, index: number): number {
|
|
155
|
+
const char = filter[index]
|
|
156
|
+
if (filter.startsWith('--', index)) {
|
|
157
|
+
const end = filter.indexOf('\n', index)
|
|
158
|
+
return end < 0 ? filter.length : end + 1
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (filter.startsWith('/*', index)) {
|
|
162
|
+
const end = filter.indexOf('*/', index + 2)
|
|
163
|
+
return end < 0 ? filter.length : end + 2
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const close =
|
|
167
|
+
char === "'" || char === '"' || char === '`'
|
|
168
|
+
? char
|
|
169
|
+
: char === '['
|
|
170
|
+
? ']'
|
|
171
|
+
: null
|
|
172
|
+
if (close === null) return index
|
|
173
|
+
|
|
174
|
+
let at = index + 1
|
|
175
|
+
while (at < filter.length) {
|
|
176
|
+
if (filter[at] !== close) {
|
|
177
|
+
at += 1
|
|
178
|
+
continue
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (close !== ']' && filter[at + 1] === close) {
|
|
182
|
+
at += 2
|
|
183
|
+
continue
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return at + 1
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return filter.length
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Notes what a keyword means for the bracket it's in
|
|
194
|
+
* @param scope the bracket
|
|
195
|
+
* @param word the keyword in upper case
|
|
196
|
+
*/
|
|
197
|
+
private static note(scope: Scope, word: string): void {
|
|
198
|
+
if (word === 'SELECT') {
|
|
199
|
+
scope.query = true
|
|
200
|
+
} else if (word === 'FROM') {
|
|
201
|
+
scope.sources += 1
|
|
202
|
+
scope.listing = true
|
|
203
|
+
} else if (word === 'JOIN') {
|
|
204
|
+
scope.sources += 1
|
|
205
|
+
} else if (FilterCost.CLAUSES.has(word)) {
|
|
206
|
+
scope.listing = false
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Closes the innermost bracket and hands what it found to the one around it
|
|
212
|
+
* @param scopes open brackets, innermost last
|
|
213
|
+
* @param finished closed brackets under each open one
|
|
214
|
+
*/
|
|
215
|
+
private static close(scopes: Scope[], finished: Scope[][]): void {
|
|
216
|
+
const scope = scopes.pop()
|
|
217
|
+
const children = finished.pop() ?? []
|
|
218
|
+
const parent = scopes.at(-1)
|
|
219
|
+
const siblings = finished.at(-1)
|
|
220
|
+
if (scope === undefined || parent === undefined || siblings === undefined) {
|
|
221
|
+
return
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (scope.query) {
|
|
225
|
+
scope.inner = FilterCost.weigh(children)
|
|
226
|
+
siblings.push(scope)
|
|
227
|
+
return
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
siblings.push(...children)
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Powers of the game count the closed subqueries of a bracket add up to
|
|
235
|
+
* @param children subqueries directly inside it
|
|
236
|
+
*/
|
|
237
|
+
private static weigh(children: readonly Scope[]): number {
|
|
238
|
+
let power = 0
|
|
239
|
+
for (const child of children) {
|
|
240
|
+
power = Math.max(power, child.sources + child.inner)
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
return power
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* The filter's row visits as powers of the game count
|
|
248
|
+
* @param root the filter itself
|
|
249
|
+
* @param finished closed brackets under it
|
|
250
|
+
*/
|
|
251
|
+
private static expand(
|
|
252
|
+
root: Scope,
|
|
253
|
+
finished: readonly Scope[][],
|
|
254
|
+
): readonly (readonly [number, number])[] {
|
|
255
|
+
const children = finished[0] ?? []
|
|
256
|
+
const terms: [number, number][] = [[root.sources, 1]]
|
|
257
|
+
for (const child of children) {
|
|
258
|
+
terms.push([root.sources + child.sources + child.inner, 1])
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
return terms
|
|
262
|
+
}
|
|
263
|
+
}
|
|
@@ -37,11 +37,11 @@ export class MessageClientHandler extends MessageHandlerAdapter {
|
|
|
37
37
|
* Sends the init request a transport needs
|
|
38
38
|
* @param context the connection
|
|
39
39
|
*/
|
|
40
|
-
public override
|
|
40
|
+
public override channelActive(context: HandlerContext): void {
|
|
41
41
|
this.peer.initialise(this.init)
|
|
42
42
|
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
this.send(context, WireMessage.init(this.init))
|
|
44
|
+
context.fireChannelActive()
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
/**
|
|
@@ -49,10 +49,7 @@ export class MessageClientHandler extends MessageHandlerAdapter {
|
|
|
49
49
|
* @param context the connection
|
|
50
50
|
* @param unit a frame, JSON text, or anything else
|
|
51
51
|
*/
|
|
52
|
-
public override channelRead(
|
|
53
|
-
context: HandlerContext,
|
|
54
|
-
unit: unknown,
|
|
55
|
-
): void | Promise<void> {
|
|
52
|
+
public override channelRead(context: HandlerContext, unit: unknown): void {
|
|
56
53
|
let message: WireMessage | null
|
|
57
54
|
try {
|
|
58
55
|
message = this.format.decode(unit, this.peer)
|
|
@@ -61,20 +58,26 @@ export class MessageClientHandler extends MessageHandlerAdapter {
|
|
|
61
58
|
return
|
|
62
59
|
}
|
|
63
60
|
|
|
64
|
-
if (message === null)
|
|
61
|
+
if (message === null) {
|
|
62
|
+
context.fireChannelRead(unit)
|
|
63
|
+
return
|
|
64
|
+
}
|
|
65
65
|
|
|
66
66
|
switch (message.type) {
|
|
67
67
|
case MessageType.OPERATION_RESPONSE:
|
|
68
68
|
case MessageType.EVENT:
|
|
69
69
|
case MessageType.DISCONNECT:
|
|
70
|
-
|
|
70
|
+
this.deliver(context, message)
|
|
71
|
+
return
|
|
71
72
|
case MessageType.INTERNAL_RESPONSE:
|
|
72
73
|
this.settle(context, message)
|
|
73
|
-
|
|
74
|
+
context.fireChannelRead(message)
|
|
75
|
+
return
|
|
74
76
|
case MessageType.INIT_RESPONSE:
|
|
75
77
|
case MessageType.MESSAGE:
|
|
76
78
|
case MessageType.RAW_MESSAGE:
|
|
77
|
-
|
|
79
|
+
context.fireChannelRead(message)
|
|
80
|
+
return
|
|
78
81
|
default:
|
|
79
82
|
this.drop(context, 'Message type is unexpected')
|
|
80
83
|
}
|
|
@@ -4,7 +4,6 @@ import {
|
|
|
4
4
|
type HandlerContext,
|
|
5
5
|
} from '@netifyjs/netify'
|
|
6
6
|
|
|
7
|
-
import { PacketCodec } from '../../../codec/index.ts'
|
|
8
7
|
import { Envelope } from '../../envelope/index.ts'
|
|
9
8
|
import { TcpCodec } from '../../tcp/codec/index.ts'
|
|
10
9
|
import type { MessageFormat } from '../format/index.ts'
|
|
@@ -37,61 +36,22 @@ export abstract class MessageHandlerAdapter extends DuplexHandlerAdapter {
|
|
|
37
36
|
this.format = format
|
|
38
37
|
}
|
|
39
38
|
|
|
40
|
-
/**
|
|
41
|
-
* Hangs up on a client too slow to take its frames
|
|
42
|
-
* @param context the connection
|
|
43
|
-
* @param error what went wrong
|
|
44
|
-
*/
|
|
45
|
-
public override exceptionCaught(
|
|
46
|
-
context: HandlerContext,
|
|
47
|
-
error: unknown,
|
|
48
|
-
): Promise<void> {
|
|
49
|
-
if (PacketCodec.overflowed(error)) void context.close()
|
|
50
|
-
|
|
51
|
-
return context.fireExceptionCaught(error)
|
|
52
|
-
}
|
|
53
|
-
|
|
54
39
|
/**
|
|
55
40
|
* Sends envelopes and messages in the connection's format
|
|
56
41
|
* @param context where the write goes
|
|
57
42
|
* @param message anything heading out
|
|
58
43
|
*/
|
|
59
|
-
public override write(
|
|
60
|
-
context: HandlerContext,
|
|
61
|
-
message: unknown,
|
|
62
|
-
): Promise<void> {
|
|
44
|
+
public override write(context: HandlerContext, message: unknown): void {
|
|
63
45
|
const wire = MessageHandlerAdapter.wire(message)
|
|
64
|
-
if (wire === null)
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
try {
|
|
68
|
-
unit = this.unit(wire)
|
|
69
|
-
} catch (cause) {
|
|
70
|
-
return Promise.reject(new EncoderError('Encoding failed', { cause }))
|
|
46
|
+
if (wire === null) {
|
|
47
|
+
context.write(message)
|
|
48
|
+
return
|
|
71
49
|
}
|
|
72
50
|
|
|
73
|
-
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Sends without a promise, false for other messages
|
|
78
|
-
* @param context where the write goes
|
|
79
|
-
* @param message anything heading out
|
|
80
|
-
*/
|
|
81
|
-
public writeSync(context: HandlerContext, message: unknown): boolean {
|
|
82
|
-
const wire = MessageHandlerAdapter.wire(message)
|
|
83
|
-
if (wire === null) return false
|
|
84
|
-
if (this.staged(context, wire)) return true
|
|
85
|
-
|
|
86
|
-
let unit: unknown
|
|
87
|
-
try {
|
|
88
|
-
unit = this.unit(wire)
|
|
89
|
-
} catch (cause) {
|
|
90
|
-
throw new EncoderError('Encoding failed', { cause })
|
|
91
|
-
}
|
|
51
|
+
if (this.staged(context, wire)) return
|
|
92
52
|
|
|
93
|
-
|
|
94
|
-
|
|
53
|
+
const unit = this.unit(wire)
|
|
54
|
+
if (unit !== null) context.write(unit)
|
|
95
55
|
}
|
|
96
56
|
|
|
97
57
|
/**
|
|
@@ -99,15 +59,9 @@ export abstract class MessageHandlerAdapter extends DuplexHandlerAdapter {
|
|
|
99
59
|
* @param context the connection
|
|
100
60
|
* @param message what to send
|
|
101
61
|
*/
|
|
102
|
-
protected send(context: HandlerContext, message: WireMessage):
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
unit = this.unit(message)
|
|
106
|
-
} catch (cause) {
|
|
107
|
-
return Promise.reject(new EncoderError('Encoding failed', { cause }))
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
return unit === null ? Promise.resolve() : context.writeAndFlush(unit)
|
|
62
|
+
protected send(context: HandlerContext, message: WireMessage): void {
|
|
63
|
+
const unit = this.unit(message)
|
|
64
|
+
if (unit !== null) context.writeAndFlush(unit)
|
|
111
65
|
}
|
|
112
66
|
|
|
113
67
|
/**
|
|
@@ -138,12 +92,10 @@ export abstract class MessageHandlerAdapter extends DuplexHandlerAdapter {
|
|
|
138
92
|
* @param context the connection
|
|
139
93
|
* @param message a request, response, event or disconnect message
|
|
140
94
|
*/
|
|
141
|
-
protected deliver(
|
|
142
|
-
context: HandlerContext,
|
|
143
|
-
message: WireMessage,
|
|
144
|
-
): void | Promise<void> {
|
|
95
|
+
protected deliver(context: HandlerContext, message: WireMessage): void {
|
|
145
96
|
if (message.body instanceof Uint8Array && this.peer.forwards) {
|
|
146
|
-
|
|
97
|
+
context.fireChannelRead(message)
|
|
98
|
+
return
|
|
147
99
|
}
|
|
148
100
|
|
|
149
101
|
const envelope = message.envelope()
|
|
@@ -152,7 +104,7 @@ export abstract class MessageHandlerAdapter extends DuplexHandlerAdapter {
|
|
|
152
104
|
return
|
|
153
105
|
}
|
|
154
106
|
|
|
155
|
-
|
|
107
|
+
context.fireChannelRead(envelope)
|
|
156
108
|
}
|
|
157
109
|
|
|
158
110
|
/**
|
|
@@ -196,8 +148,14 @@ export abstract class MessageHandlerAdapter extends DuplexHandlerAdapter {
|
|
|
196
148
|
*/
|
|
197
149
|
private unit(message: WireMessage): unknown {
|
|
198
150
|
const reason = this.refusal(message)
|
|
199
|
-
if (reason !== null)
|
|
151
|
+
if (reason !== null) {
|
|
152
|
+
throw new EncoderError('Encoding failed', { cause: new Error(reason) })
|
|
153
|
+
}
|
|
200
154
|
|
|
201
|
-
|
|
155
|
+
try {
|
|
156
|
+
return this.format.encode(message, this.peer)
|
|
157
|
+
} catch (cause) {
|
|
158
|
+
throw new EncoderError('Encoding failed', { cause })
|
|
159
|
+
}
|
|
202
160
|
}
|
|
203
161
|
}
|
|
@@ -92,14 +92,14 @@ export class MessagePipeline {
|
|
|
92
92
|
setup: ChannelSetup,
|
|
93
93
|
options: MessageServerOptions = {},
|
|
94
94
|
): ChannelSetup {
|
|
95
|
-
return
|
|
95
|
+
return (channel: Channel): void => {
|
|
96
96
|
channel.pipeline.addLast<TcpServerCodec>(
|
|
97
97
|
MessagePipeline.transport,
|
|
98
98
|
new TcpServerCodec(MessagePipeline.framing(options)),
|
|
99
99
|
)
|
|
100
100
|
MessagePipeline.serve(channel, ConnectionProtocol.TCP, options)
|
|
101
101
|
|
|
102
|
-
|
|
102
|
+
setup(channel)
|
|
103
103
|
}
|
|
104
104
|
}
|
|
105
105
|
|
|
@@ -112,14 +112,14 @@ export class MessagePipeline {
|
|
|
112
112
|
setup: ChannelSetup,
|
|
113
113
|
options: MessageServerOptions = {},
|
|
114
114
|
): ChannelSetup {
|
|
115
|
-
return WebSocketBridge.listener(
|
|
115
|
+
return WebSocketBridge.listener((channel: Channel): void => {
|
|
116
116
|
MessagePipeline.serve(
|
|
117
117
|
channel,
|
|
118
118
|
MessagePipeline.socket(options.secure),
|
|
119
119
|
options,
|
|
120
120
|
)
|
|
121
121
|
|
|
122
|
-
|
|
122
|
+
setup(channel)
|
|
123
123
|
}, MessagePipeline.bridging(options))
|
|
124
124
|
}
|
|
125
125
|
|
|
@@ -132,10 +132,10 @@ export class MessagePipeline {
|
|
|
132
132
|
setup: ChannelSetup,
|
|
133
133
|
options: MessageServerOptions = {},
|
|
134
134
|
): ChannelSetup {
|
|
135
|
-
return
|
|
135
|
+
return (channel: Channel): void => {
|
|
136
136
|
MessagePipeline.serve(channel, ConnectionProtocol.UDP, options)
|
|
137
137
|
|
|
138
|
-
|
|
138
|
+
setup(channel)
|
|
139
139
|
}
|
|
140
140
|
}
|
|
141
141
|
|
|
@@ -150,7 +150,7 @@ export class MessagePipeline {
|
|
|
150
150
|
setup: ChannelSetup,
|
|
151
151
|
options: MessageClientOptions = {},
|
|
152
152
|
): ChannelSetup {
|
|
153
|
-
return
|
|
153
|
+
return (channel: Channel): void => {
|
|
154
154
|
channel.pipeline.addLast<TcpClientCodec>(
|
|
155
155
|
MessagePipeline.transport,
|
|
156
156
|
new TcpClientCodec(MessagePipeline.framing(options)),
|
|
@@ -161,7 +161,7 @@ export class MessagePipeline {
|
|
|
161
161
|
MessagePipeline.framed(init),
|
|
162
162
|
)
|
|
163
163
|
|
|
164
|
-
|
|
164
|
+
setup(channel)
|
|
165
165
|
}
|
|
166
166
|
}
|
|
167
167
|
|
|
@@ -183,14 +183,14 @@ export class MessagePipeline {
|
|
|
183
183
|
return WebSocketBridge.connector(
|
|
184
184
|
`${address}${MessagePipeline.target(request)}`,
|
|
185
185
|
request.protocol,
|
|
186
|
-
|
|
186
|
+
(channel: Channel): void => {
|
|
187
187
|
MessagePipeline.dial(
|
|
188
188
|
channel,
|
|
189
189
|
MessagePipeline.socket(options.secure),
|
|
190
190
|
request,
|
|
191
191
|
)
|
|
192
192
|
|
|
193
|
-
|
|
193
|
+
setup(channel)
|
|
194
194
|
},
|
|
195
195
|
MessagePipeline.bridging(options),
|
|
196
196
|
)
|
|
@@ -205,14 +205,14 @@ export class MessagePipeline {
|
|
|
205
205
|
init: InitRequest | InitFields,
|
|
206
206
|
setup: ChannelSetup,
|
|
207
207
|
): ChannelSetup {
|
|
208
|
-
return
|
|
208
|
+
return (channel: Channel): void => {
|
|
209
209
|
MessagePipeline.dial(
|
|
210
210
|
channel,
|
|
211
211
|
ConnectionProtocol.UDP,
|
|
212
212
|
MessagePipeline.framed(init),
|
|
213
213
|
)
|
|
214
214
|
|
|
215
|
-
|
|
215
|
+
setup(channel)
|
|
216
216
|
}
|
|
217
217
|
}
|
|
218
218
|
|