@rbxts/netweave 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +149 -2
- package/index.d.ts +1272 -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,162 @@
|
|
|
1
|
+
--!strict
|
|
2
|
+
--!optimize 2
|
|
3
|
+
|
|
4
|
+
--[=[
|
|
5
|
+
@class Link
|
|
6
|
+
|
|
7
|
+
The two remotes, behind an interface.
|
|
8
|
+
|
|
9
|
+
Everything above this line is plain Luau and runs under lune. `RemoteEvent`, `Players` and the
|
|
10
|
+
rest of Roblox live below it, which is what lets the batch envelope, the fan-out and the
|
|
11
|
+
dispatch be tested in-process instead of through a Studio round trip.
|
|
12
|
+
|
|
13
|
+
Internal.
|
|
14
|
+
]=]
|
|
15
|
+
|
|
16
|
+
--[[
|
|
17
|
+
Two remotes per side, one reliable and one unreliable, as every batching library surveyed does
|
|
18
|
+
(`RESEARCH §3.7-E`). Not one per event: Zap creates a `RemoteEvent` per unreliable event and
|
|
19
|
+
pays an instance for each, which buys nothing once packets carry a channel id anyway.
|
|
20
|
+
]]
|
|
21
|
+
|
|
22
|
+
--[[
|
|
23
|
+
Services are looked up inside `roblox()` rather than at the top of the file. A module that
|
|
24
|
+
reaches for `game` while loading cannot be *required* under lune at all, and `Outbound` needs
|
|
25
|
+
this module for its type — so a top-level lookup here would take the whole transport out of the
|
|
26
|
+
lune test suite to save one line.
|
|
27
|
+
]]
|
|
28
|
+
local Link = {}
|
|
29
|
+
|
|
30
|
+
local FOLDER_NAME = "NETWEAVE"
|
|
31
|
+
local RELIABLE_NAME = "NETWEAVE_RELIABLE"
|
|
32
|
+
local UNRELIABLE_NAME = "NETWEAVE_UNRELIABLE"
|
|
33
|
+
local WAIT_TIMEOUT = 30
|
|
34
|
+
|
|
35
|
+
--[=[
|
|
36
|
+
@interface Link
|
|
37
|
+
@within Link
|
|
38
|
+
.send (reliable: boolean, destination: any, packed: buffer, instances: { Instance }) -> ()
|
|
39
|
+
.sendAll (reliable: boolean, packed: buffer, instances: { Instance }) -> ()
|
|
40
|
+
.receive ((packed: buffer, instances: { unknown }, sender: any?) -> ()) -> () -- wire data on the way in
|
|
41
|
+
|
|
42
|
+
`destination` is the recipient on the server and ignored on the client, where there is only one
|
|
43
|
+
place to send. `sendAll` is server-only; on the client it is the same as `send`.
|
|
44
|
+
|
|
45
|
+
`sender` is the player on the server and nil on the client — the server does not need to be
|
|
46
|
+
identified, and pretending it can be would invite a check that means nothing.
|
|
47
|
+
]=]
|
|
48
|
+
export type Link = {
|
|
49
|
+
send: (reliable: boolean, destination: any, packed: buffer, instances: { Instance }) -> (),
|
|
50
|
+
sendAll: (reliable: boolean, packed: buffer, instances: { Instance }) -> (),
|
|
51
|
+
receive: ((packed: buffer, instances: { unknown }, sender: any?) -> ()) -> (),
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
local function remoteFolder(): Folder
|
|
55
|
+
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
|
56
|
+
|
|
57
|
+
if game:GetService("RunService"):IsServer() then
|
|
58
|
+
local existing = ReplicatedStorage:FindFirstChild(FOLDER_NAME)
|
|
59
|
+
|
|
60
|
+
if existing then
|
|
61
|
+
return existing :: Folder
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
local folder = Instance.new("Folder")
|
|
65
|
+
folder.Name = FOLDER_NAME
|
|
66
|
+
folder.Parent = ReplicatedStorage
|
|
67
|
+
|
|
68
|
+
return folder
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
local folder = ReplicatedStorage:WaitForChild(FOLDER_NAME, WAIT_TIMEOUT) :: Folder
|
|
72
|
+
assert(folder, "netweave remotes never replicated to the client")
|
|
73
|
+
|
|
74
|
+
return folder
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
local function remote(folder: Folder, name: string, unreliable: boolean): any
|
|
78
|
+
if game:GetService("RunService"):IsServer() then
|
|
79
|
+
local existing = folder:FindFirstChild(name)
|
|
80
|
+
|
|
81
|
+
if existing then
|
|
82
|
+
return existing
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
local created = Instance.new(if unreliable then "UnreliableRemoteEvent" else "RemoteEvent")
|
|
86
|
+
created.Name = name
|
|
87
|
+
created.Parent = folder
|
|
88
|
+
|
|
89
|
+
return created
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
local found = folder:WaitForChild(name, WAIT_TIMEOUT)
|
|
93
|
+
assert(found, `netweave remote "{name}" never replicated`)
|
|
94
|
+
|
|
95
|
+
return found
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
--[=[
|
|
99
|
+
@within Link
|
|
100
|
+
@function roblox
|
|
101
|
+
@return Link
|
|
102
|
+
|
|
103
|
+
Builds the real link. Creates the remotes on the server and waits for them on the client.
|
|
104
|
+
|
|
105
|
+
Calling this outside Roblox fails, which is correct — a test wants a loopback link, not a
|
|
106
|
+
pretend `RemoteEvent`.
|
|
107
|
+
]=]
|
|
108
|
+
function Link.roblox(): Link
|
|
109
|
+
local folder = remoteFolder()
|
|
110
|
+
local reliable = remote(folder, RELIABLE_NAME, false)
|
|
111
|
+
local unreliable = remote(folder, UNRELIABLE_NAME, true)
|
|
112
|
+
local isServer = game:GetService("RunService"):IsServer()
|
|
113
|
+
|
|
114
|
+
return {
|
|
115
|
+
send = function(useReliable: boolean, destination: any, packed: buffer, instances: { Instance })
|
|
116
|
+
local channel = if useReliable then reliable else unreliable
|
|
117
|
+
|
|
118
|
+
if isServer then
|
|
119
|
+
channel:FireClient(destination, packed, instances)
|
|
120
|
+
else
|
|
121
|
+
channel:FireServer(packed, instances)
|
|
122
|
+
end
|
|
123
|
+
end,
|
|
124
|
+
|
|
125
|
+
sendAll = function(useReliable: boolean, packed: buffer, instances: { Instance })
|
|
126
|
+
local channel = if useReliable then reliable else unreliable
|
|
127
|
+
|
|
128
|
+
if isServer then
|
|
129
|
+
channel:FireAllClients(packed, instances)
|
|
130
|
+
else
|
|
131
|
+
channel:FireServer(packed, instances)
|
|
132
|
+
end
|
|
133
|
+
end,
|
|
134
|
+
|
|
135
|
+
receive = function(handler: (packed: buffer, instances: { unknown }, sender: any?) -> ())
|
|
136
|
+
if isServer then
|
|
137
|
+
local function onServer(player: Player, packed: any, instances: any)
|
|
138
|
+
--[[ Both arguments come from a client and are whatever it chose to send. A
|
|
139
|
+
wrong type here is a hostile peer, not a bug, so it is dropped rather
|
|
140
|
+
than passed on to be discovered inside the decoder. ]]
|
|
141
|
+
if type(packed) == "buffer" then
|
|
142
|
+
handler(packed, if type(instances) == "table" then instances else {}, player)
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
reliable.OnServerEvent:Connect(onServer)
|
|
147
|
+
unreliable.OnServerEvent:Connect(onServer)
|
|
148
|
+
else
|
|
149
|
+
local function onClient(packed: any, instances: any)
|
|
150
|
+
if type(packed) == "buffer" then
|
|
151
|
+
handler(packed, if type(instances) == "table" then instances else {}, nil)
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
reliable.OnClientEvent:Connect(onClient)
|
|
156
|
+
unreliable.OnClientEvent:Connect(onClient)
|
|
157
|
+
end
|
|
158
|
+
end,
|
|
159
|
+
}
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
return Link
|
|
@@ -0,0 +1,523 @@
|
|
|
1
|
+
--!strict
|
|
2
|
+
--!optimize 2
|
|
3
|
+
|
|
4
|
+
--[=[
|
|
5
|
+
@class Outbound
|
|
6
|
+
|
|
7
|
+
The send half. Accumulates a frame's packets per recipient and flushes them as one call each.
|
|
8
|
+
|
|
9
|
+
Internal.
|
|
10
|
+
]=]
|
|
11
|
+
|
|
12
|
+
--[[
|
|
13
|
+
Three decisions here are the M2 roadmap's, and each is a competitor's behaviour adopted or
|
|
14
|
+
refused on purpose.
|
|
15
|
+
|
|
16
|
+
**Serialize once, memcpy N times** (`RESEARCH §3.7-E`). Blink's `RELIABLE_BODY` and Zap's
|
|
17
|
+
`push_return_fire_all` both save the buffer, serialise once into a scratch, then per player
|
|
18
|
+
load, allocate and copy. It is the universal idiom and netweave takes it. The M1 benchmark's
|
|
19
|
+
stand-in re-encoded per recipient, so its fan-out numbers are a floor rather than a forecast.
|
|
20
|
+
|
|
21
|
+
**One `FireAllClients` where the audience is everyone** (`RESEARCH §3.7-E`). This is the single
|
|
22
|
+
axis where ByteNet beats both code generators: Blink and Zap issue 100 memcpys and 100 remote
|
|
23
|
+
calls for 100 players; ByteNet issues one. netweave already knows which channels qualify,
|
|
24
|
+
because `nw.audience.everyone` is a singleton in the type and the same tag decides whether
|
|
25
|
+
`broadcast` exists at all.
|
|
26
|
+
|
|
27
|
+
**Unreliable is not batched** (`RESEARCH §3.7-E`). Batching unreliable traffic is a semantic
|
|
28
|
+
error rather than an optimisation: one lost datagram becomes N lost events instead of one.
|
|
29
|
+
Blink not batching unreliable looks like a choice, not an omission, and netweave makes the same
|
|
30
|
+
one — an unreliable packet is its own batch and goes out immediately.
|
|
31
|
+
]]
|
|
32
|
+
|
|
33
|
+
local Batch = require("./Batch")
|
|
34
|
+
local Buffer = require("../codec/Buffer")
|
|
35
|
+
local Link = require("./Link")
|
|
36
|
+
local Config = require("../api/Config")
|
|
37
|
+
local Observer = require("../api/Observer")
|
|
38
|
+
local Recipients = require("./Recipients")
|
|
39
|
+
|
|
40
|
+
--[[
|
|
41
|
+
`pcall` of a function whose return pack is empty is typed as returning `boolean` alone, so the
|
|
42
|
+
error cannot be destructured out of it. The alias widens the pack without changing what runs.
|
|
43
|
+
]]
|
|
44
|
+
local attempt: (...any) -> ...any = Batch.writeBody
|
|
45
|
+
local attemptPacket: (...any) -> ...any = Batch.writePacket
|
|
46
|
+
local attemptRequest: (...any) -> ...any = Batch.writeRequest
|
|
47
|
+
local attemptReply: (...any) -> ...any = Batch.writeReply
|
|
48
|
+
local attemptHello: (...any) -> ...any = Batch.writeHello
|
|
49
|
+
local attemptDigest: (...any) -> ...any = Batch.writeDigest
|
|
50
|
+
local attemptChange: (...any) -> ...any = Batch.writeChange
|
|
51
|
+
local attemptResync: (...any) -> ...any = Batch.writeResync
|
|
52
|
+
|
|
53
|
+
local Outbound = {}
|
|
54
|
+
|
|
55
|
+
--[[
|
|
56
|
+
Roblox drops an unreliable payload past this size **silently** — no error, no warning, nothing
|
|
57
|
+
on either side. `grep` for `900|908|buffer.len` across `blink/src` and `zap/src/output/luau`
|
|
58
|
+
returns nothing, so all four surveyed libraries are exposed to it (`RESEARCH §3.7-F`).
|
|
59
|
+
|
|
60
|
+
908 is the documented limit for an `UnreliableRemoteEvent`'s payload. The instance sidecar adds
|
|
61
|
+
overhead this number does not model, so the check is a floor rather than an exact boundary; a
|
|
62
|
+
packet that passes it can still be dropped if it carries many instances, and that is stated
|
|
63
|
+
rather than papered over.
|
|
64
|
+
]]
|
|
65
|
+
--[[ Roblox refuses an unreliable payload over 908 bytes, so this is a ceiling rather than a
|
|
66
|
+
preference: `nw.configure` can only lower it. `RESEARCH §3.7-F` records that none of the four
|
|
67
|
+
surveyed libraries checks it at all. ]]
|
|
68
|
+
local UNRELIABLE_CEILING = 908
|
|
69
|
+
|
|
70
|
+
--[=[
|
|
71
|
+
@prop EVERYONE any
|
|
72
|
+
@within Outbound
|
|
73
|
+
|
|
74
|
+
The destination a broadcast is parked under.
|
|
75
|
+
]=]
|
|
76
|
+
--[[
|
|
77
|
+
Two sentinels, and they have to be values a `Player` can never be — `false` and `0` are both
|
|
78
|
+
safe as table keys and neither can collide with a real recipient.
|
|
79
|
+
|
|
80
|
+
`nil` would not do for either. A destination of `nil` is indistinguishable from "no destination
|
|
81
|
+
loaded", so the swap would think it was already there and write the packet into whichever
|
|
82
|
+
buffer happened to be current — a client's traffic landing in another player's batch, silently.
|
|
83
|
+
]]
|
|
84
|
+
local EVERYONE = false
|
|
85
|
+
|
|
86
|
+
--[=[
|
|
87
|
+
@prop SERVER any
|
|
88
|
+
@within Outbound
|
|
89
|
+
|
|
90
|
+
The destination a client sends to. There is only one, and it is not addressable by name.
|
|
91
|
+
]=]
|
|
92
|
+
local SERVER = 0
|
|
93
|
+
|
|
94
|
+
Outbound.EVERYONE = EVERYONE
|
|
95
|
+
Outbound.SERVER = SERVER
|
|
96
|
+
|
|
97
|
+
export type Outbound = {
|
|
98
|
+
push: (channel: any, destination: any, value: any) -> (),
|
|
99
|
+
publish: (channel: any, subject: any, value: any) -> (),
|
|
100
|
+
--[[ One replicated subject's change, for one recipient. Answers whether anything was written,
|
|
101
|
+
because a recipient that is already up to date costs nothing and has to be distinguishable
|
|
102
|
+
from one that was sent something. ]]
|
|
103
|
+
change: (channel: any, destination: any, subject: any, old: any, new: any) -> (boolean, string?),
|
|
104
|
+
resync: (channel: any) -> (),
|
|
105
|
+
broadcast: (channel: any, value: any) -> (),
|
|
106
|
+
request: (channel: any, call: number, value: any) -> (),
|
|
107
|
+
reply: (channel: any, destination: any, call: number, status: number, value: any?) -> (),
|
|
108
|
+
hello: (destination: any, hash: number, namespaces: { Batch.NamespaceDigest }?) -> (),
|
|
109
|
+
flush: () -> (),
|
|
110
|
+
forget: (destination: any) -> (),
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
--[=[
|
|
114
|
+
@within Outbound
|
|
115
|
+
@function new
|
|
116
|
+
@param link Link
|
|
117
|
+
@param roster Roster
|
|
118
|
+
@return Outbound
|
|
119
|
+
]=]
|
|
120
|
+
function Outbound.new(link: Link.Link, roster: Recipients.Roster): Outbound
|
|
121
|
+
--[[
|
|
122
|
+
One parked buffer per destination, swapped rather than reallocated (`PLAN-M1` D-4). The
|
|
123
|
+
outgoing buffer is global and the swap is how per-player multiplexing works; a second buffer
|
|
124
|
+
implementation here would be measuring something netweave does not do.
|
|
125
|
+
|
|
126
|
+
The records are owned here and refilled with `Buffer.saveInto`, never rebuilt. One table per
|
|
127
|
+
destination for the life of the connection, rather than one per packet — M1's benchmark
|
|
128
|
+
measured what the other way costs and it was the largest single number in the run.
|
|
129
|
+
]]
|
|
130
|
+
local parked: { [any]: Buffer.Save } = {}
|
|
131
|
+
|
|
132
|
+
--[[
|
|
133
|
+
Which destination is currently loaded, and its record.
|
|
134
|
+
|
|
135
|
+
Consecutive sends to one destination are the common case — a fan-out loop, a player's own
|
|
136
|
+
batch — so tracking this removes the swap from the path entirely rather than paying for one
|
|
137
|
+
per packet.
|
|
138
|
+
]]
|
|
139
|
+
local loaded: any = nil
|
|
140
|
+
local loadedRecord: Buffer.Save? = nil
|
|
141
|
+
|
|
142
|
+
--[[ Filled per publish by `Recipients.of` and reused, so an audience costs no allocation. ]]
|
|
143
|
+
local recipients: { any } = {}
|
|
144
|
+
|
|
145
|
+
--[[ Where a fan-out serialises once before copying. Owned here for the same reason as the
|
|
146
|
+
parked records: it is touched once per publish. ]]
|
|
147
|
+
local scratch: Buffer.Save = Buffer.emptySave()
|
|
148
|
+
|
|
149
|
+
--[[ Where an unreliable packet is written, a batch of one. Its own record rather than
|
|
150
|
+
`Buffer.load(nil)`, which was a fresh 64-byte buffer per packet and a regrowth for any
|
|
151
|
+
packet larger — on the path a `nearby` marker takes thousands of times a second
|
|
152
|
+
(`live/`, two clients: 23,000 a second each). Same finding as the parked records
|
|
153
|
+
(SECURITY-REPORT-M4 76; `bench/residue` rung 76). ]]
|
|
154
|
+
local unreliable: Buffer.Save = Buffer.emptySave()
|
|
155
|
+
|
|
156
|
+
local function park()
|
|
157
|
+
if loadedRecord then
|
|
158
|
+
Buffer.saveInto(loadedRecord)
|
|
159
|
+
loaded = nil
|
|
160
|
+
loadedRecord = nil
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
local function switchTo(destination: any)
|
|
165
|
+
if loaded == destination then
|
|
166
|
+
return
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
park()
|
|
170
|
+
|
|
171
|
+
local record = parked[destination]
|
|
172
|
+
|
|
173
|
+
if record then
|
|
174
|
+
Buffer.load(record)
|
|
175
|
+
else
|
|
176
|
+
Buffer.load(nil)
|
|
177
|
+
|
|
178
|
+
record = Buffer.emptySave()
|
|
179
|
+
Buffer.saveInto(record)
|
|
180
|
+
parked[destination] = record
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
loaded = destination
|
|
184
|
+
loadedRecord = record
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
--[[
|
|
188
|
+
A packet whose value the schema forbids raises, and that is the encode-side posture. What it
|
|
189
|
+
must not do is leave the half-written packet in the batch: the bytes behind it are another
|
|
190
|
+
player's real traffic, and a partial packet in front of them makes the whole batch
|
|
191
|
+
undecodable — the framing after it would be read from the wrong offsets.
|
|
192
|
+
|
|
193
|
+
The rewind is two numbers rather than a saved record, so the guard costs no allocation.
|
|
194
|
+
]]
|
|
195
|
+
local function guarded(writer: (...any) -> ...any, ...: any)
|
|
196
|
+
local used, instances = Buffer.mark()
|
|
197
|
+
local ok, failure = pcall(writer, ...)
|
|
198
|
+
|
|
199
|
+
if not ok then
|
|
200
|
+
Buffer.rollback(used, instances)
|
|
201
|
+
|
|
202
|
+
--[[ Re-raised rather than swallowed: this is game code passing a value game code
|
|
203
|
+
produced, and dropping it silently would hide the bug that produced it. The batch
|
|
204
|
+
is intact by the time this leaves. ]]
|
|
205
|
+
error(failure, 0)
|
|
206
|
+
end
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
local function isUnreliable(channel: any): boolean
|
|
210
|
+
return channel.unreliable == true
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
--[[ The three destinations behind one call, so `flush` can put a `pcall` around the send without
|
|
214
|
+
one per branch. ]]
|
|
215
|
+
local function deliver(destination: any, packed: buffer, instances: { Instance })
|
|
216
|
+
if destination == EVERYONE then
|
|
217
|
+
link.sendAll(true, packed, instances)
|
|
218
|
+
elseif destination == SERVER then
|
|
219
|
+
link.send(true, nil, packed, instances)
|
|
220
|
+
else
|
|
221
|
+
link.send(true, destination, packed, instances)
|
|
222
|
+
end
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
--[[
|
|
226
|
+
An unreliable packet is a batch of one, written into a buffer nobody else is using so that
|
|
227
|
+
taking it cannot disturb a reliable batch mid-accumulation.
|
|
228
|
+
]]
|
|
229
|
+
local function pushUnreliable(channel: any, destination: any, value: any, all: boolean)
|
|
230
|
+
park()
|
|
231
|
+
Buffer.load(unreliable)
|
|
232
|
+
|
|
233
|
+
local ok, failure = pcall(attemptPacket, channel, value)
|
|
234
|
+
|
|
235
|
+
if not ok then
|
|
236
|
+
--[[ Taken and saved so the record is empty again, then re-raised for the reason
|
|
237
|
+
`guarded` gives. ]]
|
|
238
|
+
Buffer.take()
|
|
239
|
+
Buffer.saveInto(unreliable)
|
|
240
|
+
error(failure, 0)
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
local packed, instances = Buffer.take()
|
|
244
|
+
Buffer.saveInto(unreliable)
|
|
245
|
+
local size = buffer.len(packed)
|
|
246
|
+
|
|
247
|
+
local ceiling = math.min(Config.limitOf("unreliableBytes") or UNRELIABLE_CEILING, UNRELIABLE_CEILING)
|
|
248
|
+
|
|
249
|
+
if size > ceiling then
|
|
250
|
+
--[[ Refused here rather than dropped by Roblox there. A payload over the limit
|
|
251
|
+
disappears silently on the wire, and none of the three surveyed libraries checks
|
|
252
|
+
for it (`RESEARCH §3.7-F`). ]]
|
|
253
|
+
Observer.emit(
|
|
254
|
+
channel.qualified,
|
|
255
|
+
if all or destination == SERVER then nil else destination,
|
|
256
|
+
"send",
|
|
257
|
+
`unreliable payload is {size} bytes, over the {ceiling} byte limit`,
|
|
258
|
+
size
|
|
259
|
+
)
|
|
260
|
+
return
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
if all then
|
|
264
|
+
link.sendAll(false, packed, instances)
|
|
265
|
+
elseif destination == SERVER then
|
|
266
|
+
link.send(false, nil, packed, instances)
|
|
267
|
+
else
|
|
268
|
+
link.send(false, destination, packed, instances)
|
|
269
|
+
end
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
local self: Outbound
|
|
273
|
+
|
|
274
|
+
self = {
|
|
275
|
+
--[[ One recipient, named. A client's `send` and a single-recipient `publish` both land
|
|
276
|
+
here. ]]
|
|
277
|
+
push = function(channel: any, destination: any, value: any)
|
|
278
|
+
if isUnreliable(channel) then
|
|
279
|
+
pushUnreliable(channel, destination, value, false)
|
|
280
|
+
return
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
switchTo(destination)
|
|
284
|
+
guarded(attemptPacket, channel, value)
|
|
285
|
+
end,
|
|
286
|
+
|
|
287
|
+
--[[
|
|
288
|
+
One replicated subject's change, for one recipient.
|
|
289
|
+
|
|
290
|
+
**A raise here is reported rather than re-raised**, which is the opposite of `push`, and
|
|
291
|
+
the difference is whose call stack it is on. `push` runs inside the game's own
|
|
292
|
+
`:send` or `:publish`, so a value the schema forbids belongs back at that call site.
|
|
293
|
+
This runs on netweave's tick, over a store the game filled at some other time, and
|
|
294
|
+
re-raising would take down every other channel in that tick to report one bad subject.
|
|
295
|
+
|
|
296
|
+
The subject is skipped and the recipient keeps the baseline it had, so the next tick
|
|
297
|
+
tries again — which is right, because the store may have been mid-update.
|
|
298
|
+
]]
|
|
299
|
+
change = function(
|
|
300
|
+
channel: any,
|
|
301
|
+
destination: any,
|
|
302
|
+
subject: any,
|
|
303
|
+
old: any,
|
|
304
|
+
new: any
|
|
305
|
+
): (boolean, string?)
|
|
306
|
+
switchTo(destination)
|
|
307
|
+
|
|
308
|
+
local used, instances = Buffer.mark()
|
|
309
|
+
local ok, result = pcall(attemptChange, channel, subject, old, new)
|
|
310
|
+
|
|
311
|
+
--[[ The reason travels back as well as being reported, because "wrote nothing" means
|
|
312
|
+
two things: the recipient is up to date, or the change cannot be written at all —
|
|
313
|
+
past the two-byte length, an encoder raise. The tick treats the second as a subject
|
|
314
|
+
to leave alone until it moves (M4 report, finding 24). ]]
|
|
315
|
+
if not ok then
|
|
316
|
+
Buffer.rollback(used, instances)
|
|
317
|
+
Observer.emit(channel.qualified, destination, "replicate", tostring(result), 0)
|
|
318
|
+
return false, tostring(result)
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
return result, nil
|
|
322
|
+
end,
|
|
323
|
+
|
|
324
|
+
--[[ This endpoint has lost track of a replicated channel and is asking for all of it again.
|
|
325
|
+
Rides the outgoing batch like anything else, on the reserved control id, so it costs a
|
|
326
|
+
packet rather than a round trip of its own. ]]
|
|
327
|
+
resync = function(channel: any)
|
|
328
|
+
switchTo(SERVER)
|
|
329
|
+
guarded(attemptResync, channel.id)
|
|
330
|
+
end,
|
|
331
|
+
|
|
332
|
+
--[[ A publish resolves its audience and fans out. `everyone` takes the one-call path;
|
|
333
|
+
everything else is serialised once and copied per recipient. ]]
|
|
334
|
+
publish = function(channel: any, subject: any, value: any)
|
|
335
|
+
local count = Recipients.of(channel.audience, subject, roster, recipients, channel)
|
|
336
|
+
|
|
337
|
+
if count == -1 then
|
|
338
|
+
self.broadcast(channel, value)
|
|
339
|
+
return
|
|
340
|
+
end
|
|
341
|
+
|
|
342
|
+
if count == 0 then
|
|
343
|
+
return
|
|
344
|
+
end
|
|
345
|
+
|
|
346
|
+
if count == 1 then
|
|
347
|
+
--[[ Serialising and copying to reach one buffer is strictly more work than writing
|
|
348
|
+
into it. ]]
|
|
349
|
+
self.push(channel, recipients[1], value)
|
|
350
|
+
return
|
|
351
|
+
end
|
|
352
|
+
|
|
353
|
+
if isUnreliable(channel) then
|
|
354
|
+
for index = 1, count do
|
|
355
|
+
pushUnreliable(channel, recipients[index], value, false)
|
|
356
|
+
end
|
|
357
|
+
|
|
358
|
+
return
|
|
359
|
+
end
|
|
360
|
+
|
|
361
|
+
--[[
|
|
362
|
+
Serialise once into a buffer of its own, then copy the bytes into each recipient's
|
|
363
|
+
batch (`RESEARCH §3.7-E`). `writeBody`, not `writePacket`: the version byte belongs
|
|
364
|
+
to the recipient's buffer, which may already have one, and a scratch carrying its own
|
|
365
|
+
would have to be copied from an offset — an offset to get wrong on every broadcast.
|
|
366
|
+
]]
|
|
367
|
+
park()
|
|
368
|
+
Buffer.load(scratch)
|
|
369
|
+
Buffer.rollback(0, 0)
|
|
370
|
+
|
|
371
|
+
local ok, failure = pcall(attempt, channel, value)
|
|
372
|
+
|
|
373
|
+
Buffer.saveInto(scratch)
|
|
374
|
+
|
|
375
|
+
if not ok then
|
|
376
|
+
error(failure, 0)
|
|
377
|
+
end
|
|
378
|
+
|
|
379
|
+
local source = scratch.buffer
|
|
380
|
+
local body = scratch.used
|
|
381
|
+
local carried = scratch.instances
|
|
382
|
+
local instanceCount = scratch.instanceCount
|
|
383
|
+
|
|
384
|
+
for index = 1, count do
|
|
385
|
+
switchTo(recipients[index])
|
|
386
|
+
Batch.begin()
|
|
387
|
+
Buffer.writeBuffer(source, body)
|
|
388
|
+
|
|
389
|
+
for entry = 1, instanceCount do
|
|
390
|
+
Buffer.writeInstance(carried[entry])
|
|
391
|
+
end
|
|
392
|
+
end
|
|
393
|
+
end,
|
|
394
|
+
|
|
395
|
+
--[[ Every player, in one call. Parked like any other destination so it batches with the
|
|
396
|
+
rest of the frame rather than going out alone. ]]
|
|
397
|
+
broadcast = function(channel: any, value: any)
|
|
398
|
+
if isUnreliable(channel) then
|
|
399
|
+
pushUnreliable(channel, nil, value, true)
|
|
400
|
+
return
|
|
401
|
+
end
|
|
402
|
+
|
|
403
|
+
switchTo(EVERYONE)
|
|
404
|
+
guarded(attemptPacket, channel, value)
|
|
405
|
+
end,
|
|
406
|
+
|
|
407
|
+
--[[
|
|
408
|
+
A query going out from the client. There is one destination and it is not addressable, so
|
|
409
|
+
this is `push` with a call id — batched with the frame's other traffic rather than sent
|
|
410
|
+
alone, because a request that leaves early still waits a frame for the answer.
|
|
411
|
+
]]
|
|
412
|
+
request = function(channel: any, call: number, value: any)
|
|
413
|
+
switchTo(SERVER)
|
|
414
|
+
guarded(attemptRequest, channel, call, value)
|
|
415
|
+
end,
|
|
416
|
+
|
|
417
|
+
--[[
|
|
418
|
+
The answer, back to the one player who asked.
|
|
419
|
+
|
|
420
|
+
It is not budgeted, and that is an argument rather than an omission: a reply is
|
|
421
|
+
one-for-one with a request, and the request already spent that player's rate budget on
|
|
422
|
+
the way in. A second budget here would police the server's own answers to questions it
|
|
423
|
+
had already agreed to answer, and the only way to overrun it would be to have accepted
|
|
424
|
+
too many requests — which is what `rate` is for.
|
|
425
|
+
|
|
426
|
+
**A request the server *refused* is answered too**, and that half the argument above does
|
|
427
|
+
not cover was found by an external review (M3 phase 9). It is deliberate and it is
|
|
428
|
+
measured. Three hundred requests a rate budget refused, in one batch:
|
|
429
|
+
|
|
430
|
+
* refused: 1,074 bytes in, **1,074 bytes out**, 0.71 ms
|
|
431
|
+
* the same batch admitted and answered: 1,074 in, **2,274 out**, 1.60 ms
|
|
432
|
+
|
|
433
|
+
So `Batch.read`'s premise — refusing has to stay cheaper than accepting — holds for this
|
|
434
|
+
class as well, by a factor of two in both bytes and time, and the outbound never exceeds
|
|
435
|
+
what the peer spent to provoke it. What the alternative buys is worse than what it costs:
|
|
436
|
+
dropping the refusal turns a client one packet over its burst into a caller parked for
|
|
437
|
+
the full `timeout`, which is the failure `D-12` and `Query.abandon` exist to prevent.
|
|
438
|
+
]]
|
|
439
|
+
reply = function(channel: any, destination: any, call: number, status: number, value: any?)
|
|
440
|
+
switchTo(destination)
|
|
441
|
+
guarded(attemptReply, channel, call, status, value)
|
|
442
|
+
end,
|
|
443
|
+
|
|
444
|
+
--[[
|
|
445
|
+
The handshake, on reserved id 0. Parked like any other packet rather than sent alone, so
|
|
446
|
+
it rides out in front of whatever provoked it — which is what makes the peer's *first*
|
|
447
|
+
batch carry the hash, and what lets the packets behind it in that same batch be refused
|
|
448
|
+
if the hash does not match.
|
|
449
|
+
]]
|
|
450
|
+
hello = function(destination: any, hash: number, namespaces: { Batch.NamespaceDigest }?)
|
|
451
|
+
switchTo(destination)
|
|
452
|
+
guarded(attemptHello, hash)
|
|
453
|
+
|
|
454
|
+
--[[ Beside the hello, never alone: the digest is what lets the peer name a mismatch, and
|
|
455
|
+
a mismatch is decided by the hello. Optional so a rig that hands in no namespaces
|
|
456
|
+
keeps its hello the size it was. ]]
|
|
457
|
+
if namespaces then
|
|
458
|
+
guarded(attemptDigest, namespaces)
|
|
459
|
+
end
|
|
460
|
+
end,
|
|
461
|
+
|
|
462
|
+
--[[ Once a frame. Everything accumulated goes out as one call per destination, which is the
|
|
463
|
+
difference between a batching library's ~126 remote invocations a second and raw's
|
|
464
|
+
~6,400 (`bench/RESULTS.md`). ]]
|
|
465
|
+
flush = function()
|
|
466
|
+
park()
|
|
467
|
+
|
|
468
|
+
for destination, record in parked do
|
|
469
|
+
if record.used > 0 then
|
|
470
|
+
Buffer.load(record)
|
|
471
|
+
|
|
472
|
+
local packed, instances = Buffer.take()
|
|
473
|
+
|
|
474
|
+
--[[
|
|
475
|
+
`take` left the buffer empty at the size it had grown to; the record keeps
|
|
476
|
+
it so the destination does not regrow it next frame — and it happens
|
|
477
|
+
**before** the send, not after.
|
|
478
|
+
|
|
479
|
+
After the send it happened only when the send returned. A `link.send` that
|
|
480
|
+
raised left the record holding the same bytes and the same `used`, so the
|
|
481
|
+
next frame loaded them, sent them, raised again, and the loop never reached
|
|
482
|
+
the destinations iterated after it: one bad recipient stopped delivery to a
|
|
483
|
+
subset of players permanently, with an error every frame. `nw.audience.select`
|
|
484
|
+
hands back whatever the game returned, so a single non-Player entry was
|
|
485
|
+
enough (`PLAN-M3` phase 9).
|
|
486
|
+
]]
|
|
487
|
+
Buffer.saveInto(record)
|
|
488
|
+
|
|
489
|
+
local ok, failure = pcall(deliver, destination, packed, instances)
|
|
490
|
+
|
|
491
|
+
if not ok then
|
|
492
|
+
--[[ Reported rather than raised: this is one frame's traffic to one
|
|
493
|
+
destination, and taking the flush down with it would cost every other
|
|
494
|
+
player theirs. There is no channel to name — a batch is many. ]]
|
|
495
|
+
Observer.emit(
|
|
496
|
+
"?",
|
|
497
|
+
if destination == EVERYONE or destination == SERVER then nil else destination,
|
|
498
|
+
"send",
|
|
499
|
+
`the batch did not go out: {tostring(failure)}`,
|
|
500
|
+
buffer.len(packed)
|
|
501
|
+
)
|
|
502
|
+
end
|
|
503
|
+
end
|
|
504
|
+
end
|
|
505
|
+
end,
|
|
506
|
+
|
|
507
|
+
--[[ A player who left. Their parked batch is bytes nobody will receive, and holding it
|
|
508
|
+
holds the buffer behind it. ]]
|
|
509
|
+
forget = function(destination: any)
|
|
510
|
+
if loaded == destination then
|
|
511
|
+
loaded = nil
|
|
512
|
+
loadedRecord = nil
|
|
513
|
+
Buffer.load(nil)
|
|
514
|
+
end
|
|
515
|
+
|
|
516
|
+
parked[destination] = nil
|
|
517
|
+
end,
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
return self
|
|
521
|
+
end
|
|
522
|
+
|
|
523
|
+
return Outbound
|