2024-01-05 12:14:38 +00:00
|
|
|
--!strict
|
2024-03-02 16:43:36 +00:00
|
|
|
--!native
|
2024-03-14 04:58:08 +00:00
|
|
|
--!optimize 2
|
2024-01-05 12:14:38 +00:00
|
|
|
local Client = {}
|
|
|
|
Client.__index = Client
|
|
|
|
|
|
|
|
local Players = game:GetService("Players")
|
|
|
|
local Util = script.Parent.Parent.Util
|
|
|
|
|
|
|
|
local ClientProcess = require(script.Parent.ClientProcess)
|
|
|
|
local Assert = require(Util.Assert)
|
|
|
|
local Key = require(Util.Key)
|
|
|
|
local Serdes = require(Util.Serdes)
|
2024-03-14 04:58:08 +00:00
|
|
|
local Buffer = require(Util.Buffer)
|
2024-01-05 12:14:38 +00:00
|
|
|
|
|
|
|
function Client.new(Identifier: string)
|
|
|
|
local self = setmetatable({}, Client)
|
2024-03-14 04:58:08 +00:00
|
|
|
self._buffer = Buffer.new()
|
|
|
|
self._buffer:writeu8(Serdes(Identifier))
|
|
|
|
self.id = Buffer.convert(self._buffer:build())
|
2024-01-05 12:14:38 +00:00
|
|
|
self.fn = {}
|
2024-03-14 04:58:08 +00:00
|
|
|
self.IsConnected = false
|
2024-01-05 12:14:38 +00:00
|
|
|
ClientProcess.add(self.id, Identifier)
|
2024-03-14 04:58:08 +00:00
|
|
|
self._buffer:remove()
|
2024-01-05 12:14:38 +00:00
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
|
|
|
function Client:Fire(reliable: boolean,...: any)
|
|
|
|
ClientProcess.insertQueue(self.id, reliable, ...)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Client:Invoke(timeout: number, ...: any): any
|
|
|
|
return ClientProcess.insertRequest(self.id, timeout, ...)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Client:Connect(callback: (args: any) -> ()): string
|
|
|
|
local key = tostring(Key())
|
|
|
|
table.insert(self.fn, key)
|
2024-03-14 04:58:08 +00:00
|
|
|
self.IsConnected = #self.fn > 0
|
2024-01-05 12:14:38 +00:00
|
|
|
ClientProcess.addCallback(self.id, key, callback)
|
|
|
|
return key
|
|
|
|
end
|
|
|
|
|
|
|
|
function Client:Once(callback: (args: any) -> ()): string
|
|
|
|
local key = tostring(Key())
|
|
|
|
table.insert(self.fn, key)
|
2024-03-14 04:58:08 +00:00
|
|
|
self.IsConnected = #self.fn > 0
|
2024-01-05 12:14:38 +00:00
|
|
|
ClientProcess.addCallback(self.id, key, function(...)
|
|
|
|
self:Disconnect(key)
|
|
|
|
task.spawn(callback, ...)
|
|
|
|
end)
|
|
|
|
return key
|
|
|
|
end
|
|
|
|
|
|
|
|
function Client:Wait()
|
|
|
|
local thread: thread, t = coroutine.running(), os.clock()
|
|
|
|
self:Once(function()
|
|
|
|
task.spawn(thread, os.clock()-t)
|
|
|
|
end)
|
|
|
|
return coroutine.yield()
|
|
|
|
end
|
|
|
|
|
|
|
|
function Client:DisconnectAll()
|
2024-03-14 04:58:08 +00:00
|
|
|
for _, key: string in self.fn do
|
|
|
|
self:Disconnect(key)
|
2024-01-05 12:14:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-03-14 04:58:08 +00:00
|
|
|
function Client:Disconnect(key: string): boolean
|
2024-01-05 12:14:38 +00:00
|
|
|
Assert(typeof(key) == "string", "Key must be a string type.")
|
|
|
|
ClientProcess.removeCallback(self.id, key)
|
2024-03-14 04:58:08 +00:00
|
|
|
table.remove(self.fn, table.find(self.fn, key))
|
|
|
|
self.IsConnected = #self.fn > 0
|
|
|
|
return table.find(self.fn, key) == nil
|
2024-01-05 12:14:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Client:Destroy()
|
|
|
|
self:DisconnectAll()
|
|
|
|
setmetatable(self, nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
return Client.new
|