Repository navigation
Expand file tree
/
Copy pathclient.lua
More file actions
305 lines (272 loc) · 8.6 KB
/
Copy pathclient.lua
File metadata and controls
305 lines (272 loc) · 8.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
local runService = game:GetService('RunService')
local codec = require(script.Parent.Modules.Codec)
local bufferWriter = require(script.Parent.Modules.Writer)
local bufferReader = require(script.Parent.Modules.Reader)
local signalModule = require(script.Parent.Modules.Signal)
local net = {}
local registeredEvents = {}
local nameById = {}
local idByName = {}
local nextRequestId = 1
local pendingRequests = {}
local pendingQueue = {}
local reliableQueue = {}
local unreliableQueue = {}
local statsSent = { bytes = 0, messages = 0 }
local statsReceived = { bytes = 0, messages = 0 }
local kindEvent = 0
local kindRequest = 1
local kindResponse = 2
local reliableFlushThreshold = 24
local unreliableFlushThreshold = 32
local maxQueuedMessages = 512
local masterRemote = script.Parent.__Master
local masterUnreliableRemote = script.Parent.__MasterUnreliable
local function writeMessageWithId(writer, message)
writer:writeUInt8(message.kind)
if message.kind == kindResponse then
writer:writeVarUInt(message.requestId)
writer:writeUInt8(message.success and 1 or 0)
writer:writeBuffer(message.payload)
elseif message.kind == kindRequest then
writer:writeVarUInt(message.id)
writer:writeVarUInt(message.requestId)
writer:writeBuffer(message.payload)
else
writer:writeVarUInt(message.id)
writer:writeBuffer(message.payload)
end
end
local function flushReliable()
if #reliableQueue == 0 then
return
end
local writer = bufferWriter.new()
writer:writeVarUInt(#reliableQueue)
for _, message in ipairs(reliableQueue) do
writeMessageWithId(writer, message)
end
statsSent.messages += #reliableQueue
table.clear(reliableQueue)
local outgoingBuffer = writer:toBuffer()
statsSent.bytes += buffer.len(outgoingBuffer)
masterRemote:FireServer(outgoingBuffer)
end
local function flushUnreliable()
if #unreliableQueue == 0 then
return
end
local writer = bufferWriter.new()
writer:writeVarUInt(#unreliableQueue)
for _, message in ipairs(unreliableQueue) do
writeMessageWithId(writer, message)
end
statsSent.messages += #unreliableQueue
table.clear(unreliableQueue)
local outgoingBuffer = writer:toBuffer()
statsSent.bytes += buffer.len(outgoingBuffer)
masterUnreliableRemote:FireServer(outgoingBuffer)
end
local function queueReliable(message)
if #reliableQueue >= maxQueuedMessages then
warn('network reliable queue overflow, forcing flush')
flushReliable()
end
table.insert(reliableQueue, message)
if #reliableQueue >= reliableFlushThreshold then
flushReliable()
end
end
local function queueUnreliable(message)
if #unreliableQueue >= maxQueuedMessages then
warn('network unreliable queue overflow, dropping oldest')
table.remove(unreliableQueue, 1)
end
table.insert(unreliableQueue, message)
if #unreliableQueue >= unreliableFlushThreshold then
flushUnreliable()
end
end
local function dispatchMessage(kind, eventName, payload, unreliable, requestId)
local eventId = idByName[eventName]
if not eventId then
table.insert(pendingQueue, {
kind = kind,
name = eventName,
payload = payload,
unreliable = unreliable,
requestId = requestId,
})
return
end
local message = { kind = kind, id = eventId, requestId = requestId, payload = payload }
if unreliable then
queueUnreliable(message)
else
queueReliable(message)
end
end
local function resolvePending()
if #pendingQueue == 0 then
return
end
local stillPending = {}
for _, entry in ipairs(pendingQueue) do
local eventId = idByName[entry.name]
if eventId then
local message = { kind = entry.kind, id = eventId, requestId = entry.requestId, payload = entry.payload }
if entry.unreliable then
queueUnreliable(message)
else
queueReliable(message)
end
else
table.insert(stillPending, entry)
end
end
pendingQueue = stillPending
end
runService.Heartbeat:Connect(function()
resolvePending()
flushReliable()
flushUnreliable()
end)
local function processIncoming(packedBuffer)
local reader = bufferReader.new(packedBuffer)
statsReceived.bytes += buffer.len(packedBuffer)
local idSyncCount = reader:readVarUInt()
for _ = 1, idSyncCount do
local eventId = reader:readVarUInt()
local eventName = reader:readString()
nameById[eventId] = eventName
idByName[eventName] = eventId
end
local messageCount = reader:readVarUInt()
statsReceived.messages += messageCount
for _ = 1, messageCount do
local kind = reader:readUInt8()
if kind == kindEvent then
local eventId = reader:readVarUInt()
local payload = reader:readBuffer()
local eventName = nameById[eventId]
local eventObject = eventName and registeredEvents[eventName]
if eventObject and eventObject.onClientFire then
local unpackedPayload = codec.unpack(payload)
eventObject.onClientFire:fire(table.unpack(unpackedPayload))
end
elseif kind == kindRequest then
local eventId = reader:readVarUInt()
local requestId = reader:readVarUInt()
local payload = reader:readBuffer()
local eventName = nameById[eventId]
local eventObject = eventName and registeredEvents[eventName]
if eventObject and eventObject.onClientInvoke then
task.spawn(function()
local success, result = pcall(eventObject.onClientInvoke, codec.unpack(payload))
queueReliable({ kind = kindResponse, requestId = requestId, success = success, payload = codec.pack(result) })
flushReliable()
end)
end
elseif kind == kindResponse then
local requestId = reader:readVarUInt()
local success = reader:readUInt8() == 1
local payload = reader:readBuffer()
local thread = pendingRequests[requestId]
if thread then
pendingRequests[requestId] = nil
local unpackedPayload = codec.unpack(payload)
task.spawn(thread, success, table.unpack(unpackedPayload))
end
end
end
end
local function processIncomingUnreliable(packedBuffer)
local reader = bufferReader.new(packedBuffer)
statsReceived.bytes += buffer.len(packedBuffer)
local messageCount = reader:readVarUInt()
statsReceived.messages += messageCount
for _ = 1, messageCount do
local kind = reader:readUInt8()
local eventId = reader:readVarUInt()
local payload = reader:readBuffer()
if kind == kindEvent then
local eventName = nameById[eventId]
local eventObject = eventName and registeredEvents[eventName]
if eventObject and eventObject.onClientFire then
local unpackedPayload = codec.unpack(payload)
eventObject.onClientFire:fire(table.unpack(unpackedPayload))
end
end
end
end
export type ClientEventApi = {
FireServer: (self: ClientEventApi, data: any) -> (),
Connect: (self: ClientEventApi, callback: (any) -> ()) -> any,
Once: (self: ClientEventApi, callback: (any) -> ()) -> any,
}
function net.loadEvent(eventName: string, options: { unreliable: boolean? }?): ClientEventApi
if registeredEvents[eventName] then
return registeredEvents[eventName]
end
local unreliable = options and options.unreliable or false
local eventApi = {} :: any
eventApi.onClientFire = signalModule.new()
function eventApi:FireServer(...)
local data = {...}
dispatchMessage(kindEvent, eventName, codec.pack(data), unreliable, nil)
end
function eventApi:Connect(callback)
return eventApi.onClientFire:connect(callback)
end
function eventApi:Once(callback)
return eventApi.onClientFire:once(callback)
end
registeredEvents[eventName] = eventApi
return eventApi
end
export type ClientFunctionApi = {
OnInvoke: (self: ClientFunctionApi, callback: (any) -> any) -> (),
InvokeServer: (self: ClientFunctionApi, data: any) -> (boolean, any),
}
function net.loadFunction(eventName: string): ClientFunctionApi
if registeredEvents[eventName] then
return registeredEvents[eventName]
end
local eventApi = {} :: any
function eventApi:OnInvoke(callback)
eventApi.onClientInvoke = callback
end
function eventApi:InvokeServer(...)
local data = {...}
local timeoutSeconds = 3
local requestId = nextRequestId
nextRequestId += 1
local thread = coroutine.running()
pendingRequests[requestId] = thread
dispatchMessage(kindRequest, eventName, codec.pack(data), false, requestId)
resolvePending()
flushReliable()
local timeoutHandle = task.delay(timeoutSeconds, function()
if pendingRequests[requestId] then
pendingRequests[requestId] = nil
task.spawn(thread, false, 'timeout')
end
end)
local success, result = coroutine.yield()
task.cancel(timeoutHandle)
return success, result
end
registeredEvents[eventName] = eventApi
return eventApi
end
function net.getStats()
return {
sentBytes = statsSent.bytes,
sentMessages = statsSent.messages,
receivedBytes = statsReceived.bytes,
receivedMessages = statsReceived.messages,
}
end
masterRemote.OnClientEvent:Connect(processIncoming)
masterUnreliableRemote.OnClientEvent:Connect(processIncomingUnreliable)
return net