Skip to content

Commit 4d61c0c

Browse files
committed
ye
1 parent eed00db commit 4d61c0c

11 files changed

Lines changed: 56 additions & 35 deletions

File tree

src/api/Packet.luau

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,6 @@ function PacketModule.define(name: string, config: PacketConfig<any>): Packet
214214
timestampMode
215215
)
216216

217-
-- Resolve batch opener at define time: zero branching on hot path (#2)
218-
reg._openFn = Channel.resolveOpenFn(reg.timestampMode)
219-
220217
local isDelta = (config.value :: InternalCodec<any>)._isDelta == true
221218

222219
local fields: PacketFields = {

src/api/Query.luau

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,6 @@ function QueryModule.define(name: string, config: QueryConfig<any, any>): Query
318318

319319
local rateLimit = config.rateLimit or { maxPerSecond = 30 }
320320

321-
local Channel = require(script.Parent.Parent.internal.Channel)
322-
323321
local reqReg, respReg = Registry.registerQueryPair(
324322
name,
325323
config.request,
@@ -330,11 +328,6 @@ function QueryModule.define(name: string, config: QueryConfig<any, any>): Query
330328
config.validate
331329
)
332330

333-
-- Queries use writeQuery, not writeBatch, but _openFn must be set
334-
local openFn = Channel.resolveOpenFn(0)
335-
reqReg._openFn = openFn
336-
respReg._openFn = openFn
337-
338331
respSignal:connect(function(resp: any, source: Player?, correlation: number): ()
339332
completeQuery(correlation, resp, source)
340333
end)

src/internal/Channel.luau

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,22 @@ function Channel.reset(ch: ChannelState): ()
346346
end
347347
end
348348

349+
-- Test helper: builds a minimal reg-like object for writeBatch.
350+
function Channel.mockReg(id: number, name: string, codec: any): any
351+
return {
352+
id = id,
353+
name = name,
354+
codec = codec,
355+
timestampMode = 0,
356+
_openFn = openBatchPlain,
357+
drops = 0,
358+
bytesSent = 0,
359+
bytesReceived = 0,
360+
fires = 0,
361+
recvFires = 0,
362+
}
363+
end
364+
349365
Channel.TS_NONE = TS_NONE
350366
Channel.TS_FRAME = TS_FRAME
351367
Channel.TS_OFFSET = TS_OFFSET

src/internal/Registry.luau

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
--!optimize 2
33
-- Deterministic ID assignment for packets and queries.
44

5+
local Channel = require(script.Parent.Channel)
56
local Types = require(script.Parent.Parent.Types)
67

78
type Codec<T> = Types.Codec<T>
@@ -72,7 +73,7 @@ local function assign(
7273
needsGate = rateLimit ~= nil or validate ~= nil,
7374
maxPayloadBytes = maxPayloadBytes,
7475
timestampMode = timestampMode or 0,
75-
_openFn = nil :: any, -- set by Packet.define via Channel.resolveOpenFn
76+
_openFn = Channel.resolveOpenFn(timestampMode or 0),
7677
bytesSent = 0,
7778
bytesReceived = 0,
7879
fires = 0,

src/transport/Gate.luau

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,17 @@ end
162162

163163
function Gate.check(value: any, reg: Registration, player: Player): boolean
164164
if not checkRate(player, reg) then
165-
reg.drops += 1
165+
if reg.drops then
166+
reg.drops += 1
167+
end
166168
fireDrop(player, "rate", reg.name, nil)
167169
return false
168170
end
169171

170172
if not isClean(value, 1) then
171-
reg.drops += 1
173+
if reg.drops then
174+
reg.drops += 1
175+
end
172176
fireDrop(player, "nan", reg.name, value)
173177
return false
174178
end
@@ -177,7 +181,9 @@ function Gate.check(value: any, reg: Registration, player: Player): boolean
177181
if validate then
178182
local ok, reason = validate(value, player)
179183
if not ok then
180-
reg.drops += 1
184+
if reg.drops then
185+
reg.drops += 1
186+
end
181187
fireDrop(player, reason or "validate", reg.name, value)
182188
return false
183189
end

test/codec/Struct.test.luau

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,12 @@ describe("deltaStruct", function(): ()
107107
Baseline.setReadKey(false)
108108
local c = Lync.deltaStruct({ hp = Lync.u8, mp = Lync.u8 })
109109
local ch = Channel.create()
110+
-- Write and read full frame to populate baseline
110111
c.write(ch, { hp = 100, mp = 50 })
112+
local fullBuf = buffer.create(ch.cursor)
113+
buffer.copy(fullBuf, 0, ch.buff, 0, ch.cursor)
114+
c.read(fullBuf, 0, nil)
115+
-- Write delta frame
111116
ch.cursor = 0
112117
c.write(ch, { hp = 80, mp = 50 })
113118
local buf = buffer.create(ch.cursor)

test/internal/Channel.test.luau

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ local Lync = require(script.Parent.Parent.Parent)
55
local Runner = require(script.Parent.Parent.Runner)
66
local describe, it, expect = Runner.describe, Runner.it, Runner.expect
77

8+
local mockReg = Channel.mockReg
9+
810
describe("Channel.create", function(): ()
911
it("size 1024", function(): ()
1012
expect(Channel.create().size):toBe(1024)
@@ -59,13 +61,14 @@ end)
5961
describe("Channel.writeBatch", function(): ()
6062
it("advances cursor", function(): ()
6163
local ch = Channel.create()
62-
Channel.writeBatch(ch, 1, "t", Lync.u8, 42)
64+
Channel.writeBatch(ch, mockReg(1, "t", Lync.u8), 42)
6365
expect(ch.cursor):toBeGreaterThan(0)
6466
end)
6567
it("same ID increments itemCount", function(): ()
6668
local ch = Channel.create()
67-
Channel.writeBatch(ch, 1, "a", Lync.u8, 10)
68-
Channel.writeBatch(ch, 1, "a", Lync.u8, 20)
69+
local reg = mockReg(1, "a", Lync.u8)
70+
Channel.writeBatch(ch, reg, 10)
71+
Channel.writeBatch(ch, reg, 20)
6972
expect(ch.itemCount):toBe(2)
7073
end)
7174
end)
@@ -78,7 +81,7 @@ describe("Channel.sealAndDump", function(): ()
7881
end)
7982
it("non-empty is positive length", function(): ()
8083
local ch = Channel.create()
81-
Channel.writeBatch(ch, 1, "t", Lync.u8, 255)
84+
Channel.writeBatch(ch, mockReg(1, "t", Lync.u8), 255)
8285
local raw, _ = Channel.sealAndDump(ch)
8386
expect(buffer.len(raw)):toBeGreaterThan(0)
8487
end)
@@ -115,7 +118,7 @@ end)
115118
describe("Channel.reset", function(): ()
116119
it("zeroes all state", function(): ()
117120
local ch = Channel.create()
118-
Channel.writeBatch(ch, 1, "t", Lync.u8, 1)
121+
Channel.writeBatch(ch, mockReg(1, "t", Lync.u8), 1)
119122
table.insert(ch.refs, Instance.new("Part"))
120123
Channel.reset(ch)
121124
expect(ch.cursor):toBe(0)

test/pipeline/Batch.test.luau

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe("batch: 100 values same packet", function(): ()
1515
it("all 100 arrive in order", function(): ()
1616
local ch = Channel.create()
1717
for i = 1, 100 do
18-
Channel.writeBatch(ch, reg.id, reg.name, Lync.u16, i)
18+
Channel.writeBatch(ch, reg, i)
1919
end
2020
local raw, refs = Channel.sealAndDump(ch)
2121

@@ -40,10 +40,10 @@ describe("batch: interleaved packets", function(): ()
4040

4141
it("values arrive to correct signals", function(): ()
4242
local ch = Channel.create()
43-
Channel.writeBatch(ch, regA.id, regA.name, Lync.u8, 1)
44-
Channel.writeBatch(ch, regA.id, regA.name, Lync.u8, 2)
45-
Channel.writeBatch(ch, regB.id, regB.name, Lync.string, "hello")
46-
Channel.writeBatch(ch, regA.id, regA.name, Lync.u8, 3)
43+
Channel.writeBatch(ch, regA, 1)
44+
Channel.writeBatch(ch, regA, 2)
45+
Channel.writeBatch(ch, regB, "hello")
46+
Channel.writeBatch(ch, regA, 3)
4747
local raw, refs = Channel.sealAndDump(ch)
4848

4949
local numsA = {} :: { number }

test/pipeline/Fidelity.test.luau

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe("u8 batch write-seal-read", function(): ()
1616

1717
it("write+seal+read recovers value", function(): ()
1818
local ch = Channel.create()
19-
Channel.writeBatch(ch, reg.id, reg.name, Lync.u8, 42)
19+
Channel.writeBatch(ch, reg, 42)
2020
local raw, refs = Channel.sealAndDump(ch)
2121

2222
local got: any
@@ -38,7 +38,7 @@ describe("struct batch write-seal-read", function(): ()
3838

3939
it("round-trips struct", function(): ()
4040
local ch = Channel.create()
41-
Channel.writeBatch(ch, reg.id, reg.name, codec, { hp = 100, name = "hero" })
41+
Channel.writeBatch(ch, reg, { hp = 100, name = "hero" })
4242
local raw, refs = Channel.sealAndDump(ch)
4343

4444
local got: any
@@ -62,8 +62,8 @@ describe("multiple packets in one frame", function(): ()
6262

6363
it("both values arrive", function(): ()
6464
local ch = Channel.create()
65-
Channel.writeBatch(ch, regA.id, regA.name, Lync.u8, 255)
66-
Channel.writeBatch(ch, regB.id, regB.name, Lync.string, "hello")
65+
Channel.writeBatch(ch, regA, 255)
66+
Channel.writeBatch(ch, regB, "hello")
6767
local raw, refs = Channel.sealAndDump(ch)
6868

6969
local gotA, gotB: any

test/pipeline/GateContinue.test.luau

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ describe("gate drop continues next packet", function(): ()
3131
Gate.onPlayerAdded(player)
3232

3333
local ch = Channel.create()
34-
Channel.writeBatch(ch, regA.id, "__gc_a", Lync.f32, 3.14)
35-
Channel.writeBatch(ch, regB.id, "__gc_b", Lync.u8, 42)
34+
Channel.writeBatch(ch, regA, 3.14)
35+
Channel.writeBatch(ch, regB, 42)
3636
local raw, refs = Channel.sealAndDump(ch)
3737

3838
local gotA: any = "NOT_FIRED"

0 commit comments

Comments
 (0)