diff --git a/CHANGELOGS b/CHANGELOGS index 408dfff..57e43d3 100644 --- a/CHANGELOGS +++ b/CHANGELOGS @@ -3,6 +3,13 @@ [Refresh] - Reupdate existing version +[RC9 - 0.9.6] +- Changed :Pull to :Invoke +- Improved Invoking on bandwidth saving (only affect to multiple-events) +- Fix :Once +- Changed entire :Session works +- :Invoke now use :Session for it internal system + [RC9 - 0.9.5] - Fixed Wait-Event Connected (built-in feature) - Support for using multiple :Wait diff --git a/FastNet2_Rewrite.rbxm b/FastNet2_Rewrite.rbxm index 0d8ec38..f1efd76 100644 Binary files a/FastNet2_Rewrite.rbxm and b/FastNet2_Rewrite.rbxm differ diff --git a/docs/api/1.0/client.md b/docs/api/1.0/client.md index a2f2a2e..4f16e95 100644 --- a/docs/api/1.0/client.md +++ b/docs/api/1.0/client.md @@ -93,9 +93,9 @@ Remote:Fire("Hello World!") This function have rate limiting to prevent spamming ::: -## `:Pull` +## `:Invoke` -Pull is a function that invoke to server. +Semiliar with :InvokeServer, its for doing Invoke to a Server. ::: code-group ```lua [main] @@ -106,7 +106,7 @@ Pull is a function that invoke to server. ``` ```lua [Example] -local Request = Remote:Pull(2, "Hello World!") +local Request = Remote:Invoke(2, "Hello World!") ``` ::: diff --git a/docs/api/1.0/server.md b/docs/api/1.0/server.md index 7584f43..e9f8880 100644 --- a/docs/api/1.0/server.md +++ b/docs/api/1.0/server.md @@ -106,9 +106,9 @@ Remote:Fires("Hello World!") ``` ::: -## `:Pull` +## `:Invoke` -Pull is a function that invoke to client. +Semiliar with `:InvokeClient`, its for doing Invoke to spesific Client. ::: code-group ```lua [main] @@ -120,7 +120,7 @@ Pull is a function that invoke to client. ``` ```lua [Example] -local Request = Remote:Pull(2, player, "Hello World!") +local Request = Remote:Invoke(2, player, "Hello World!") ``` ::: diff --git a/src/Client/init.luau b/src/Client/init.luau index b7efac8..47e0c46 100644 --- a/src/Client/init.luau +++ b/src/Client/init.luau @@ -11,7 +11,7 @@ local FastSpawn = require(script.Parent.FastSpawn) local Debug = require(script.Parent.Debug) local Util = require(script.Parent.Util) -local Event = script.Parent:WaitForChild("Event") +local Event: RemoteEvent = script.Parent:WaitForChild("Event") local Collections = {} @@ -92,6 +92,7 @@ function Client:Once(callback: (...any) -> ()) self:Disconnect() task.spawn(callback, ...) end + Process.reg_pre_f(self.Identifier, callback) self.Connected = true end end @@ -139,23 +140,24 @@ end @params ... any parameters to sent **--]] -function Client:Pull(timeout: number, ...: any) +function Client:Invoke(timeout: number, ...: any) Debug.new(typeof(timeout) == "number", "[FastNet2]: expire argument must be a number.", 0) Debug.new(timeout > 1, "[FastNet2]: expire argument must be minimum 2.", 0) Debug.new(#{...} > 0, "[FastNet2]: Unable to invoke without data", 0) local thread, requestId = coroutine.running(), string.pack("I5", Util.uniqueNum()) - local session = task.delay(timeout, function() + local session = Util.Session(timeout) + :After(function(e) -- session expire -- removing and clear entire requests point if Process.verify(self.Identifier, requestId) or self.flag[requestId] then self.flag[requestId] = nil end - task.spawn(thread, nil) -- resume thread to finish and return nil as failed to retreive due over-session (expired) - end) + task.spawn(thread, e) -- resume thread to finish and return nil as failed to retreive due over-session (expired) + end, nil) -- set new flag as checkpoint self.flag[requestId] = function(...) self.flag[requestId] = nil - task.cancel(session) + session:Cancel() task.spawn(thread, ...) -- resume thread to finish and return data as successed end -- push new request diff --git a/src/Server/Process.luau b/src/Server/Process.luau index c097023..84314c9 100644 --- a/src/Server/Process.luau +++ b/src/Server/Process.luau @@ -16,7 +16,7 @@ function ServerProcess.reg(Identifier: string) Identifier = Identifier, _ping = {}, returnRequest = {}, - re_req_i = 0, + re_req_i = {}, requests = {}, req_i = 0, packets = {}, @@ -60,7 +60,7 @@ function ServerProcess.pushback(Identifier: string, player: string, id: number, Collections[Identifier].returnRequest[player] = {} end Collections[Identifier].returnRequest[player][id] = obj - Collections[Identifier].re_req_i += 1 + Collections[Identifier].re_req_i[player] = true end function ServerProcess._ping(Identifier: string): number @@ -100,6 +100,7 @@ end function ServerProcess.__start() local outQueue: { [string]: {any} }, outIndex: number = {}, 0 local outSQueue: { [string]: {any} }, outSIndex: number = {}, 0 + local outReq: { [string]: {any} }, outRIndex: { [string]: boolean } = {}, {} RunService.PostSimulation:Connect(function() -- Sent all data to client (Multiple Players) if outIndex > 0 then @@ -116,6 +117,13 @@ function ServerProcess.__start() end end end + for i, player: Player in Players:GetPlayers() do + if outReq[player.Name] and outRIndex[player.Name] == true then + outRIndex[player.Name] = false + Event:FireClient(player, "1", outReq[player.Name]) + table.clear(outReq[player.Name]) + end + end debug.profilebegin("FastNet2.process") -- Connections Dataset from Collections for Identifier: string, net in Collections do @@ -164,13 +172,19 @@ function ServerProcess.__start() end end -- Return Requests - if net.re_req_i > 0 then - for player, data in net.returnRequest do - if Players:FindFirstChild(player) and Event:GetAttribute(player) == true then - net.re_req_i -= 1 - Event:FireClient(Players[player], "1", {[net.Identifier] = data}) - table.clear(data) + for player, data in net.returnRequest do + if player and Players:FindFirstChild(player) and net.re_req_i[player] == true and Event:GetAttribute(player) == true then + net.re_req_i[player] = false + if not outReq[player] then + outReq[player] = {} + end + if not outReq[player][net.Identifier] then + outReq[player][net.Identifier] = {} end + outReq[player][net.Identifier] = table.clone(data) + outRIndex[player] = true + --Event:FireClient(Players[player], "1", {[net.Identifier] = data}) + table.clear(data) end end -- Single Players to Queue diff --git a/src/Server/init.luau b/src/Server/init.luau index 3b9b872..d118e9c 100644 --- a/src/Server/init.luau +++ b/src/Server/init.luau @@ -10,7 +10,7 @@ local Debug = require(script.Parent.Debug) local Util = require(script.Parent.Util) local Players = game:GetService("Players") -local Event = script.Parent:WaitForChild("Event") +local Event:RemoteEvent = script.Parent:WaitForChild("Event") local Collections = {} @@ -52,6 +52,7 @@ end --[[** Connect a connection (Luau Parralel) + This only works with Actor Instance @param callback this must be a function with any arguments **--]] @@ -92,6 +93,7 @@ function Server:Once(callback: (...any) -> ()) self:Disconnect() task.spawn(callback, ...) end + Process.reg_pre_f(self.Identifier, callback) self.Connected = true end end @@ -148,24 +150,25 @@ end @params ... any parameters to sent **--]] -function Server:Pull(timeout: number, player: Player, ...: any) +function Server:Invoke(timeout: number, player: Player, ...: any) Debug.new(typeof(timeout) == "number", "[FastNet2]: expire argument must be a number.", 0) Debug.new(timeout > 1, "[FastNet2]: expire argument must be minimum 2.", 0) Debug.new(player:IsA("Player"), "[FastNet2]: player argument must be a Player.", 0) Debug.new(#{...} > 0, "[FastNet2]: Unable to invoke without data", 0) local thread, requestId = coroutine.running(), string.pack("I5", Util.uniqueNum()) - local session = task.delay(timeout, function() + local session = Util.Session(timeout) + :After(function(e) -- session expire -- removing and clear entire requests point if Process.verify(self.Identifier, player, requestId) or self.flag[requestId] then self.flag[requestId] = nil end - task.spawn(thread, nil) -- resume thread to finish and return nil as failed to retreive due over-session (expired) - end) + task.spawn(thread, e) -- resume thread to finish and return nil as failed to retreive due over-session (expired) + end, nil) -- set new flag as checkpoint self.flag[requestId] = function(...) self.flag[requestId] = nil - task.cancel(session) + session:Cancel() task.spawn(thread, ...) -- resume thread to finish and return data as successed end -- push new request diff --git a/src/Util.luau b/src/Util.luau index 5c3fd72..cc475ab 100644 --- a/src/Util.luau +++ b/src/Util.luau @@ -16,13 +16,23 @@ function Util.CreateUUID(): string return string.gsub(HttpService:GenerateGUID(false), "-", "") :: string end --- Create session semiliar with task.wait function (YIELD) (Internal Function) -function Util.Session(timeout: number): number - local thread, n = coroutine.running(), tick() - task.delay(math.max(0.25, timeout), function() - task.spawn(thread, tick() - n) +-- Create session semiliar with task.wait function (Internal Function) +function Util.Session(timeout: number) + local afterSession, arguments, private = nil, {}, {} + local session = task.delay(math.max(0.5, timeout), function() + if afterSession and typeof(afterSession) == "function" then + task.spawn(afterSession, table.unpack(arguments)) + end end) - return coroutine.yield() + function private:After(func: (...any) -> (), ...: any) + afterSession = func + arguments = { ... } + return private + end + function private:Cancel() + task.cancel(session) + end + return private end -- Generate a randomized unique number with 8 length num (Internal Function) @@ -45,7 +55,7 @@ function Util.ser(identifier: string): string end else while not Event:GetAttribute(identifier) do - Util.Session(0.5) + task.wait(0.5) end end return Event:GetAttribute(identifier) diff --git a/src/init.luau b/src/init.luau index 10742a4..a84bdb9 100644 --- a/src/init.luau +++ b/src/init.luau @@ -2,7 +2,7 @@ --!strict -- FastNet2 -- Author: @EternityDev (Eternity_Devs) --- version rc9 (0.9.5) +-- version rc9 (0.9.6) local FastNet2 = {} FastNet2.__index = FastNet2 @@ -86,7 +86,7 @@ end **--]] -function FastNet2.debugLogs() +function FastNet2.debugLogs(): {any} return Debug.receiveAll() end diff --git a/wally.lock b/wally.lock index df2bcb8..fcbaa97 100644 --- a/wally.lock +++ b/wally.lock @@ -4,5 +4,5 @@ registry = "test" [[package]] name = "imezx/fastnet2" -version = "0.9.5" +version = "0.9.6" dependencies = [] diff --git a/wally.toml b/wally.toml index 006305c..3930d28 100644 --- a/wally.toml +++ b/wally.toml @@ -1,6 +1,6 @@ [package] name = "imezx/fastnet2" -version = "0.9.5" +version = "0.9.6" registry = "https://github.com/UpliftGames/wally-index" realm = "shared" license = "MIT"