@rbxts/netweave 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +149 -2
- package/index.d.ts +1226 -0
- package/macros.d.ts +87 -0
- package/macros.luau +27 -0
- package/package.json +34 -5
- package/src/api/Audience.luau +156 -0
- package/src/api/Channel.luau +1288 -0
- package/src/api/Config.luau +884 -0
- package/src/api/Context.luau +385 -0
- package/src/api/Namespace.luau +540 -0
- package/src/api/Observer.luau +493 -0
- package/src/api/Policy.luau +457 -0
- package/src/api/Protocol.luau +593 -0
- package/src/api/Transport.luau +135 -0
- package/src/api/Trust.luau +393 -0
- package/src/api/View.luau +503 -0
- package/src/codec/Buffer.luau +1272 -0
- package/src/codec/Ir.luau +1353 -0
- package/src/codec/Serdes.luau +2694 -0
- package/src/netweave.luau +490 -0
- package/src/replication/Baseline.luau +314 -0
- package/src/replication/Delta.luau +613 -0
- package/src/replication/Store.luau +229 -0
- package/src/replication/Tick.luau +597 -0
- package/src/transport/Batch.luau +970 -0
- package/src/transport/Budget.luau +249 -0
- package/src/transport/Driver.luau +305 -0
- package/src/transport/Inbound.luau +1508 -0
- package/src/transport/Link.luau +162 -0
- package/src/transport/Outbound.luau +523 -0
- package/src/transport/Pacer.luau +120 -0
- package/src/transport/Query.luau +535 -0
- package/src/transport/Recipients.luau +507 -0
- package/src/types/init.luau +2064 -0
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
--!strict
|
|
2
|
+
--!optimize 2
|
|
3
|
+
|
|
4
|
+
--[=[
|
|
5
|
+
@class Budget
|
|
6
|
+
|
|
7
|
+
Guarantee G2 at runtime. Every inbound channel declares a rate, and this is what enforces it.
|
|
8
|
+
|
|
9
|
+
Internal.
|
|
10
|
+
]=]
|
|
11
|
+
|
|
12
|
+
--[[
|
|
13
|
+
Written against Warp, which is the only surveyed library that attempts byte accounting and gets
|
|
14
|
+
all three parts wrong (`Server/init.luau:196-200`, `RESEARCH §3.7-K`). Each failure is a
|
|
15
|
+
requirement here, stated in reverse.
|
|
16
|
+
|
|
17
|
+
**Real bytes.** Warp charges `math.max(buffer.len(b), 800)`, so a one-byte packet costs 800 and
|
|
18
|
+
the whole thing is a crude packet counter wearing a byte counter's name. What is charged here is
|
|
19
|
+
what arrived.
|
|
20
|
+
|
|
21
|
+
**A reset that cannot fail to run.** Warp resets its counter inside a send loop that begins with
|
|
22
|
+
`if #content == 0 then continue`, so a player the server has nothing to say to is *never* reset
|
|
23
|
+
— they send ten packets and are blackholed for the rest of the session, silently. The window
|
|
24
|
+
here is reset lazily, on the same call that charges it: there is no separate reset path, so
|
|
25
|
+
there is nothing that can fail to run.
|
|
26
|
+
|
|
27
|
+
**It runs where it is tested.** Warp wraps the check in `if not RunService:IsStudio()`, so no
|
|
28
|
+
test can ever catch it. There is no such branch below, and `tests/budget_runtime.luau` is the
|
|
29
|
+
proof rather than the promise.
|
|
30
|
+
]]
|
|
31
|
+
|
|
32
|
+
local Budget = {}
|
|
33
|
+
|
|
34
|
+
--[[
|
|
35
|
+
One second. `rate` is declared in packets per second (`DESIGN-API.md` §3), so the window is the
|
|
36
|
+
unit the declaration is written in — a shorter one would refuse bursts the declaration allows,
|
|
37
|
+
and a longer one would let a burst through that the declaration does not.
|
|
38
|
+
]]
|
|
39
|
+
local WINDOW = 1
|
|
40
|
+
|
|
41
|
+
type Record = {
|
|
42
|
+
tokens: number,
|
|
43
|
+
refilled: number,
|
|
44
|
+
opened: number,
|
|
45
|
+
packets: number,
|
|
46
|
+
bytes: number,
|
|
47
|
+
refused: number,
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export type Budget = {
|
|
51
|
+
admit: (channel: any, sender: any?, bytes: number) -> string?,
|
|
52
|
+
forget: (sender: any) -> (),
|
|
53
|
+
usage: (sender: any, channel: any) -> (number, number, number),
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
--[[
|
|
57
|
+
One refusal sentence per rate, built on the first refusal at that rate and kept. Its one
|
|
58
|
+
variable is the channel's rate, and building it per refusal cost 2.8x an admission — not an
|
|
59
|
+
allocation, since Luau interns short strings and hands the same object back, but the
|
|
60
|
+
interpolation itself, on the path a flood takes (SECURITY-REPORT-M4-1; `bench/residue`).
|
|
61
|
+
]]
|
|
62
|
+
local overRate: { [number]: string } = {}
|
|
63
|
+
|
|
64
|
+
local function overRateReason(rate: number): string
|
|
65
|
+
local reason = overRate[rate]
|
|
66
|
+
|
|
67
|
+
if not reason then
|
|
68
|
+
reason = `over {rate} packets per second`
|
|
69
|
+
overRate[rate] = reason
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
return reason
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
--[=[
|
|
76
|
+
@within Budget
|
|
77
|
+
@function new
|
|
78
|
+
@param clock (() -> number)? -- defaults to `os.clock`
|
|
79
|
+
@return Budget
|
|
80
|
+
|
|
81
|
+
The clock is injectable because a window is a second long, and a test that has to wait one is a
|
|
82
|
+
test nobody runs. `tests/budget_runtime.luau` advances it by hand.
|
|
83
|
+
]=]
|
|
84
|
+
function Budget.new(clock: (() -> number)?): Budget
|
|
85
|
+
local now = clock or os.clock
|
|
86
|
+
|
|
87
|
+
--[[
|
|
88
|
+
Per sender, per channel. The records are kept and refilled rather than rebuilt, so a
|
|
89
|
+
steady-state server allocates nothing here: the table for a player is made once when they
|
|
90
|
+
first send, and the record for a channel once when they first use it.
|
|
91
|
+
]]
|
|
92
|
+
local perSender: { [any]: { [any]: Record } } = {}
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
--[[
|
|
96
|
+
Runs **before** the payload is decoded, which is the security property rather than an
|
|
97
|
+
ordering detail. Refusing costs a lookup, a clock read and two comparisons; accepting
|
|
98
|
+
costs all of that plus a full deserialisation. Denial staying cheaper than acceptance is
|
|
99
|
+
what stops a flood being amplified into work (`PLAN-M2` R-2).
|
|
100
|
+
]]
|
|
101
|
+
admit = function(channel: any, sender: any?, bytes: number): string?
|
|
102
|
+
local rate = channel.rate
|
|
103
|
+
|
|
104
|
+
if not rate then
|
|
105
|
+
--[[ Outbound classes have no budget. ]]
|
|
106
|
+
return nil
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
if sender == nil then
|
|
110
|
+
--[[
|
|
111
|
+
No sender means the client side, whose peer is the server — and a server
|
|
112
|
+
rate-limiting itself would be a check that means nothing.
|
|
113
|
+
|
|
114
|
+
It is also a guard rather than only a shortcut. A crafted id can name a channel
|
|
115
|
+
that *does* declare a rate, and the table below is keyed by sender: `perSender[nil]`
|
|
116
|
+
is an error, thrown on the receive path, which is the one place nothing may throw
|
|
117
|
+
(`DESIGN-API.md` G4). A peer could have caused it.
|
|
118
|
+
]]
|
|
119
|
+
return nil
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
local byChannel = perSender[sender]
|
|
123
|
+
|
|
124
|
+
if not byChannel then
|
|
125
|
+
byChannel = {}
|
|
126
|
+
perSender[sender] = byChannel
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
local record = byChannel[channel]
|
|
130
|
+
local at = now()
|
|
131
|
+
|
|
132
|
+
--[[
|
|
133
|
+
The depth of the bucket. Declaring it is how a game says "this channel is bursty";
|
|
134
|
+
omitting it says one second's worth, which is the safe reading of silence.
|
|
135
|
+
|
|
136
|
+
**At least one token, whatever the rate.** A rate below one packet per second is legal
|
|
137
|
+
and ordinary — "one purchase every two seconds" — and a bucket 0.5 deep can never hold
|
|
138
|
+
the whole token the admission test wants, so the channel would be refused forever after
|
|
139
|
+
its first packet. That is Warp's failure (`RESEARCH §3.7-K`) reintroduced by the fix
|
|
140
|
+
meant to make it unreachable, and it took an audit rather than a test to find.
|
|
141
|
+
]]
|
|
142
|
+
local declared = channel.burst or rate
|
|
143
|
+
local burst = if declared < 1 then 1 else declared
|
|
144
|
+
|
|
145
|
+
if not record then
|
|
146
|
+
byChannel[channel] = {
|
|
147
|
+
tokens = burst - 1,
|
|
148
|
+
refilled = at,
|
|
149
|
+
opened = at,
|
|
150
|
+
packets = 1,
|
|
151
|
+
bytes = bytes,
|
|
152
|
+
refused = 0,
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return nil
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
--[[
|
|
159
|
+
A token bucket rather than a fixed window, and the difference is not academic.
|
|
160
|
+
|
|
161
|
+
A window that resets on a boundary admits a full allowance on each side of it, so a
|
|
162
|
+
channel declared `rate = 20` passes 39 across a span of ten milliseconds — measured,
|
|
163
|
+
and now a test. The game declared 20, got 39, and had no way to find out.
|
|
164
|
+
|
|
165
|
+
The bucket has no boundary to straddle: entitlement accrues continuously at `rate`
|
|
166
|
+
and is capped at `burst`, so the guarantee is "no more than `rate` per second in
|
|
167
|
+
*any* second" rather than "in the seconds we happen to have drawn"
|
|
168
|
+
(`PLAN-M3` D-3).
|
|
169
|
+
]]
|
|
170
|
+
local elapsed = at - record.refilled
|
|
171
|
+
|
|
172
|
+
--[[ A clock that goes backwards must not mint tokens. `os.clock` is monotonic so this
|
|
173
|
+
cannot happen with the default, but an injected one can — and the failure it would
|
|
174
|
+
cause is the limiter quietly ceasing to limit, which is worse than refusing. ]]
|
|
175
|
+
if elapsed > 0 then
|
|
176
|
+
local refilled = record.tokens + elapsed * rate
|
|
177
|
+
record.tokens = if refilled > burst then burst else refilled
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
record.refilled = at
|
|
181
|
+
|
|
182
|
+
--[[
|
|
183
|
+
The reporting window is separate from admission and stays a window on purpose:
|
|
184
|
+
`usage` answers "what did this player send in the last second", which is a question
|
|
185
|
+
about a span. Warp's reset bug lived in the admission path (`RESEARCH §3.7-K`);
|
|
186
|
+
there is nothing to skip here, because nothing depends on it.
|
|
187
|
+
]]
|
|
188
|
+
if at - record.opened >= WINDOW or at < record.opened then
|
|
189
|
+
record.opened = at
|
|
190
|
+
record.packets = 0
|
|
191
|
+
record.bytes = 0
|
|
192
|
+
record.refused = 0
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
if record.tokens < 1 then
|
|
196
|
+
record.refused += 1
|
|
197
|
+
|
|
198
|
+
--[[ The bytes are charged whether or not the packet is admitted. A refused packet
|
|
199
|
+
still crossed the wire, and a byte figure that only counted what was accepted
|
|
200
|
+
would understate exactly the traffic worth looking at. ]]
|
|
201
|
+
record.bytes += bytes
|
|
202
|
+
|
|
203
|
+
return overRateReason(rate)
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
record.tokens -= 1
|
|
207
|
+
record.packets += 1
|
|
208
|
+
record.bytes += bytes
|
|
209
|
+
|
|
210
|
+
return nil
|
|
211
|
+
end,
|
|
212
|
+
|
|
213
|
+
--[[ A player who left. Their records are the only thing here that grows with the player
|
|
214
|
+
count, so dropping them is what keeps a long-running server flat. ]]
|
|
215
|
+
forget = function(sender: any)
|
|
216
|
+
perSender[sender] = nil
|
|
217
|
+
end,
|
|
218
|
+
|
|
219
|
+
--[=[
|
|
220
|
+
What this sender has spent on this channel in the current window: packets, bytes, and
|
|
221
|
+
how many were refused.
|
|
222
|
+
|
|
223
|
+
Exposed because a budget nobody can read is not a security feature. `RESEARCH §3-G6`, as
|
|
224
|
+
corrected, records that the one surveyed library to attempt reporting this (Warp) gets it
|
|
225
|
+
wrong, and `§3.7-K` records what the absence costs — a player can be silently blackholed
|
|
226
|
+
and nothing anywhere says so.
|
|
227
|
+
]=]
|
|
228
|
+
usage = function(sender: any, channel: any): (number, number, number)
|
|
229
|
+
local byChannel = perSender[sender]
|
|
230
|
+
local record = byChannel and byChannel[channel]
|
|
231
|
+
|
|
232
|
+
if not record then
|
|
233
|
+
return 0, 0, 0
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
--[[ Reported against the same window the charge would use, so a caller reading between
|
|
237
|
+
bursts sees zero rather than the last window's total. ]]
|
|
238
|
+
local at = now()
|
|
239
|
+
|
|
240
|
+
if at - record.opened >= WINDOW or at < record.opened then
|
|
241
|
+
return 0, 0, 0
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
return record.packets, record.bytes, record.refused
|
|
245
|
+
end,
|
|
246
|
+
}
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
return Budget
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
--!strict
|
|
2
|
+
--!optimize 2
|
|
3
|
+
|
|
4
|
+
--[=[
|
|
5
|
+
@class Driver
|
|
6
|
+
|
|
7
|
+
Assembles the transport and fills the hole M1 left.
|
|
8
|
+
|
|
9
|
+
`src/api/Transport.luau` is the seam: every `send`, `publish`, `broadcast` and `invoke` in the
|
|
10
|
+
library ends there, and until this installs itself they all raise with a message naming this
|
|
11
|
+
milestone. Requiring `netweave` inside Roblox is what installs it.
|
|
12
|
+
|
|
13
|
+
Internal.
|
|
14
|
+
]=]
|
|
15
|
+
|
|
16
|
+
--[[
|
|
17
|
+
Nothing here is exposed on `nw`. A game that declared its channels correctly does not have a
|
|
18
|
+
transport to configure — the rate is on the channel, the audience is on the channel, and the
|
|
19
|
+
batching is not a decision anyone should be making per game. Adding knobs here would move the
|
|
20
|
+
security model out of the declaration file, which is the whole thing this design is built to
|
|
21
|
+
prevent (`DESIGN-API.md` §1).
|
|
22
|
+
]]
|
|
23
|
+
|
|
24
|
+
local Baseline = require("../replication/Baseline")
|
|
25
|
+
local Budget = require("./Budget")
|
|
26
|
+
local Inbound = require("./Inbound")
|
|
27
|
+
local Link = require("./Link")
|
|
28
|
+
local Namespace = require("../api/Namespace")
|
|
29
|
+
local Outbound = require("./Outbound")
|
|
30
|
+
local Pacer = require("./Pacer")
|
|
31
|
+
local Protocol = require("../api/Protocol")
|
|
32
|
+
local Query = require("./Query")
|
|
33
|
+
local Recipients = require("./Recipients")
|
|
34
|
+
local Tick = require("../replication/Tick")
|
|
35
|
+
local Transport = require("../api/Transport")
|
|
36
|
+
|
|
37
|
+
local Driver = {}
|
|
38
|
+
|
|
39
|
+
--[=[
|
|
40
|
+
@within Driver
|
|
41
|
+
@function install
|
|
42
|
+
|
|
43
|
+
Builds the transport and installs it. Called once, from `src/netweave.luau`, and only inside
|
|
44
|
+
Roblox — the link needs real remotes.
|
|
45
|
+
]=]
|
|
46
|
+
function Driver.install()
|
|
47
|
+
local RunService = game:GetService("RunService")
|
|
48
|
+
local Players = game:GetService("Players")
|
|
49
|
+
|
|
50
|
+
local isServer = RunService:IsServer()
|
|
51
|
+
|
|
52
|
+
local link = Link.roblox()
|
|
53
|
+
local roster = Recipients.roblox()
|
|
54
|
+
local outbound = Outbound.new(link, roster)
|
|
55
|
+
local budget = Budget.new()
|
|
56
|
+
|
|
57
|
+
--[[ A client's intent sends, held to the declared rate (`PLAN-M5` D-6). Built on the server too,
|
|
58
|
+
where nothing ever calls `send`, because one code path is cheaper to be right about than
|
|
59
|
+
two; an empty pacer's tick is one `#` on an empty array. ]]
|
|
60
|
+
local pacer = Pacer.new(function(channel: any, value: any)
|
|
61
|
+
outbound.push(channel, Outbound.SERVER, value)
|
|
62
|
+
end)
|
|
63
|
+
|
|
64
|
+
--[[
|
|
65
|
+
`task.spawn` is passed in rather than reached for inside `Query`, which keeps that module
|
|
66
|
+
loadable under lune where `task` does not exist — the same shape as `Budget`'s injectable
|
|
67
|
+
clock. It is the right primitive for both jobs it does here: it starts a handler on a fresh
|
|
68
|
+
thread, and it resumes a parked caller *propagating* whatever that thread raises, where
|
|
69
|
+
`coroutine.resume` would swallow the game's own error into a boolean nobody reads.
|
|
70
|
+
]]
|
|
71
|
+
local query = Query.new({
|
|
72
|
+
request = function(channel: any, call: number, value: any)
|
|
73
|
+
outbound.request(channel, call, value)
|
|
74
|
+
end,
|
|
75
|
+
|
|
76
|
+
reply = function(channel: any, who: any, call: number, status: number, value: any?)
|
|
77
|
+
outbound.reply(channel, who, call, status, value)
|
|
78
|
+
end,
|
|
79
|
+
|
|
80
|
+
spawn = task.spawn,
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
--[[ Read late, and through a function, because the protocol does not exist until every
|
|
84
|
+
namespace has been declared — see `ensure` below. ]]
|
|
85
|
+
local agreement = Protocol.new({
|
|
86
|
+
hash = function(): number
|
|
87
|
+
return Namespace.protocol().hash
|
|
88
|
+
end,
|
|
89
|
+
|
|
90
|
+
announce = function(who: any?)
|
|
91
|
+
local protocol = Namespace.protocol()
|
|
92
|
+
outbound.hello(if who == nil then Outbound.SERVER else who, protocol.hash, protocol.namespaces)
|
|
93
|
+
end,
|
|
94
|
+
|
|
95
|
+
--[[ Read late like `hash`, and for the same reason. ]]
|
|
96
|
+
namespaces = function()
|
|
97
|
+
return Namespace.protocol().namespaces
|
|
98
|
+
end,
|
|
99
|
+
|
|
100
|
+
isServer = isServer,
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
--[[
|
|
104
|
+
What the other end has, per replicated subject.
|
|
105
|
+
|
|
106
|
+
**Bounded on the server and not on the client**, and the asymmetry is the point. On the
|
|
107
|
+
server this is memory sized by how many people join, which is `PLAN-M3` D-6 territory and
|
|
108
|
+
what `baselinesPerClient` exists for. On the client it is what the server chose to send —
|
|
109
|
+
already bounded, by that limit, at the other end — and refusing to remember one here would
|
|
110
|
+
make the next change unreadable rather than saving anything.
|
|
111
|
+
]]
|
|
112
|
+
local baselines = Baseline.new(isServer)
|
|
113
|
+
|
|
114
|
+
local inbound = Inbound.new({
|
|
115
|
+
resolve = Namespace.channelById,
|
|
116
|
+
admit = budget.admit,
|
|
117
|
+
isServer = isServer,
|
|
118
|
+
queries = query,
|
|
119
|
+
agreement = agreement,
|
|
120
|
+
baselines = baselines,
|
|
121
|
+
|
|
122
|
+
--[[ A change this endpoint could not read means every change after it is unreadable, so it
|
|
123
|
+
asks for the channel again. Only a client can: the server is the sender, and a server
|
|
124
|
+
that lost track of what a client has simply sends everything next tick. ]]
|
|
125
|
+
desynced = if isServer
|
|
126
|
+
then nil
|
|
127
|
+
else function(channel: any)
|
|
128
|
+
outbound.resync(channel)
|
|
129
|
+
end,
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
--[[ Server-side only. A replicated channel is driven from the store, and the store is the
|
|
133
|
+
server's. ]]
|
|
134
|
+
local replicate = if isServer
|
|
135
|
+
then Tick.new({
|
|
136
|
+
outbound = outbound,
|
|
137
|
+
baselines = baselines,
|
|
138
|
+
roster = roster,
|
|
139
|
+
channels = Namespace.replicated,
|
|
140
|
+
})
|
|
141
|
+
else nil
|
|
142
|
+
|
|
143
|
+
--[[
|
|
144
|
+
Ids cannot be assigned until every namespace has been declared, because an id depends on
|
|
145
|
+
every other channel in the program (`docs/WIRE-FORMAT.md` §3). The first send is the latest
|
|
146
|
+
moment that can be deferred to, and one branch per send is what deferring costs.
|
|
147
|
+
|
|
148
|
+
The receive path seals through `Namespace.channelById` for the same reason.
|
|
149
|
+
]]
|
|
150
|
+
--[[
|
|
151
|
+
Asked of `Namespace` rather than remembered here. A flag of this driver's own stayed true
|
|
152
|
+
across `Namespace.reset()`, so a suite that had sent one packet through the real transport
|
|
153
|
+
left every namespace declared after it unsealed and unnumbered — found writing the Studio
|
|
154
|
+
half of the suite, where the benchmark declares its channels after the tests have run
|
|
155
|
+
(`PLAN-M4-BUG` phase 6).
|
|
156
|
+
]]
|
|
157
|
+
--[[
|
|
158
|
+
The protocol this client's hello carried (netweave-typescript patch 0001).
|
|
159
|
+
|
|
160
|
+
Compared with the protocol rather than kept as a flag, for the reason above: a reset replaces
|
|
161
|
+
the protocol, so the next one is greeted again. Without it a client that called
|
|
162
|
+
`nw.protocol()` or `nw.signature()` before its first packet found the numbering sealed,
|
|
163
|
+
returned before the hello, and a server on another build decoded that client's payloads as
|
|
164
|
+
the wrong channels instead of refusing them.
|
|
165
|
+
]]
|
|
166
|
+
local greeted: unknown = nil
|
|
167
|
+
|
|
168
|
+
local function ensure()
|
|
169
|
+
--[[ After the seal, and on the server only: every policy's server stage, once. Asked on
|
|
170
|
+
every send and receive because `Namespace.protocol()` seals too, from a game that
|
|
171
|
+
prints its hash before its first packet, and a seal that ran there must not leave the
|
|
172
|
+
stages unrun; the second and every later call is one boolean (`PLAN-M5` D-5). ]]
|
|
173
|
+
if Namespace.sealed() then
|
|
174
|
+
if isServer then
|
|
175
|
+
Namespace.runStages()
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
--[[ A client sealed by its own `nw.protocol()` has not said hello yet, and goes on to say
|
|
179
|
+
it; `Namespace.seal` below returns the protocol it already has. ]]
|
|
180
|
+
if isServer or greeted == Namespace.protocol() then
|
|
181
|
+
return
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
Namespace.seal()
|
|
186
|
+
|
|
187
|
+
if isServer then
|
|
188
|
+
Namespace.runStages()
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
--[[
|
|
192
|
+
The client's hello goes out here, the moment the protocol is real and before anything
|
|
193
|
+
else this peer will ever send.
|
|
194
|
+
|
|
195
|
+
`ensure` is the only place that knows when the numbering is fixed, and being first in the
|
|
196
|
+
buffer is what makes the hash the first packet of the first batch — so a server that
|
|
197
|
+
disagrees can refuse the rest of that same batch rather than accepting one batch of
|
|
198
|
+
payloads that decode as the wrong channels.
|
|
199
|
+
|
|
200
|
+
The server does not announce unprompted, because at install there is nobody to announce
|
|
201
|
+
to. It answers on a mismatch instead (`src/api/Protocol.luau`).
|
|
202
|
+
]]
|
|
203
|
+
if not isServer then
|
|
204
|
+
local protocol = Namespace.protocol()
|
|
205
|
+
greeted = protocol
|
|
206
|
+
outbound.hello(Outbound.SERVER, protocol.hash, protocol.namespaces)
|
|
207
|
+
end
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
--[[ Sealing on the way in as well as on the way out. A client that only listens never sends, so
|
|
211
|
+
`ensure` would never run and its hello would never leave — and a listen-only client on a
|
|
212
|
+
stale build is exactly the one whose mis-decodes are hardest to explain. ]]
|
|
213
|
+
link.receive(function(packed: buffer, instances: { unknown }, sender: any?)
|
|
214
|
+
ensure()
|
|
215
|
+
inbound.receive(packed, instances, sender)
|
|
216
|
+
end)
|
|
217
|
+
|
|
218
|
+
--[[
|
|
219
|
+
Once a frame, in this order.
|
|
220
|
+
|
|
221
|
+
Intents are delivered before the flush so that a handler's reply goes out in the same frame
|
|
222
|
+
it was provoked by, rather than waiting for the next one. Reversing them would add a frame
|
|
223
|
+
of latency to every response, invisibly.
|
|
224
|
+
]]
|
|
225
|
+
RunService.PostSimulation:Connect(function()
|
|
226
|
+
inbound.tick()
|
|
227
|
+
|
|
228
|
+
--[[ Between the dispatch and the flush, so that a call giving up this frame can retry in it
|
|
229
|
+
rather than waiting a frame to be sent. A caller resumed with a failure is game code
|
|
230
|
+
like any other and often does exactly that. ]]
|
|
231
|
+
query.tick()
|
|
232
|
+
|
|
233
|
+
--[[ After the handlers have run, so a change provoked by this frame's traffic goes out in
|
|
234
|
+
this frame's batch rather than the next one — and before the flush, for the same
|
|
235
|
+
reason. ]]
|
|
236
|
+
if replicate then
|
|
237
|
+
replicate()
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
--[[ Held intent values whose channel has a token again go out in this frame's batch. ]]
|
|
241
|
+
pacer.tick()
|
|
242
|
+
|
|
243
|
+
outbound.flush()
|
|
244
|
+
end)
|
|
245
|
+
|
|
246
|
+
if isServer then
|
|
247
|
+
Players.PlayerRemoving:Connect(function(player)
|
|
248
|
+
--[[ Six places hold something per player: an unsent batch, a pending intent, a context
|
|
249
|
+
record, a rate window, the call slots of any query still running for them, and
|
|
250
|
+
the replication baselines. None of them mean anything once the player is gone, and
|
|
251
|
+
every one would
|
|
252
|
+
otherwise grow with every join.
|
|
253
|
+
|
|
254
|
+
`query.forget` releases the accounting and nothing else — a handler already parked
|
|
255
|
+
inside a yield cannot be cancelled, because a yielded Luau coroutine cannot be. It
|
|
256
|
+
returns when it returns, finds its slot gone, and writes no reply. ]]
|
|
257
|
+
outbound.forget(player)
|
|
258
|
+
inbound.forget(player)
|
|
259
|
+
baselines.forget(player)
|
|
260
|
+
budget.forget(player)
|
|
261
|
+
query.forget(player)
|
|
262
|
+
agreement.forget(player)
|
|
263
|
+
end)
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
Transport.install({
|
|
267
|
+
send = function(channel: any, value: any)
|
|
268
|
+
ensure()
|
|
269
|
+
|
|
270
|
+
--[[ An intent is the newest value at the declared rate, and nothing else is: a signal
|
|
271
|
+
or a command sent past its rate is the server's refusal to make. ]]
|
|
272
|
+
if channel.class == "intent" then
|
|
273
|
+
pacer.send(channel, value)
|
|
274
|
+
else
|
|
275
|
+
outbound.push(channel, Outbound.SERVER, value)
|
|
276
|
+
end
|
|
277
|
+
end,
|
|
278
|
+
|
|
279
|
+
publish = function(channel: any, subject: any, value: any)
|
|
280
|
+
ensure()
|
|
281
|
+
outbound.publish(channel, subject, value)
|
|
282
|
+
end,
|
|
283
|
+
|
|
284
|
+
broadcast = function(channel: any, value: any)
|
|
285
|
+
ensure()
|
|
286
|
+
outbound.broadcast(channel, value)
|
|
287
|
+
end,
|
|
288
|
+
|
|
289
|
+
--[[ The timeout is the part that matters. Blink and Zap have none, so a peer that never
|
|
290
|
+
answers parks the caller's thread for the session (`RESEARCH §3.7-G`); `timeout` is a
|
|
291
|
+
required field on `nw.query` and there is no value of it meaning "wait forever". ]]
|
|
292
|
+
invoke = function(channel: any, args: any): (any?, string?)
|
|
293
|
+
ensure()
|
|
294
|
+
return query.invoke(channel, args)
|
|
295
|
+
end,
|
|
296
|
+
|
|
297
|
+
--[[ A handler has just been attached, so anything that arrived before it did is delivered
|
|
298
|
+
now rather than lost to the order the game happened to require its modules in. ]]
|
|
299
|
+
dispatch = function(channel: any)
|
|
300
|
+
inbound.drain(channel)
|
|
301
|
+
end,
|
|
302
|
+
})
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
return Driver
|