Skip to content
This repository has been archived by the owner on Mar 30, 2024. It is now read-only.

Commit

Permalink
rc9 0.9.4
Browse files Browse the repository at this point in the history
  • Loading branch information
imezx committed Sep 19, 2023
1 parent 4145438 commit 9ab5ca0
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 64 deletions.
6 changes: 6 additions & 0 deletions CHANGELOGS
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
[Refresh]
- Reupdate existing version

[RC9 - 0.9.4]
- Fixed Creating twice "Event" Instance
- Fixed requestId on Client/Server Pull (function)
- Add .uniqueNum (function) for requestId (Internal function)
- Minor performance improvement on :Fire (function) at Client-Side

[RC9 - 0.9.3]
- Using Luau Native Code Generation (--!native) for better optimized performance
- Changed serialize identifier from hash to string.pack (with lower byte cost)
Expand Down
Binary file modified FastNet2_Rewrite.rbxm
Binary file not shown.
2 changes: 1 addition & 1 deletion src/Client/Process.luau
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function ClientProcess.unreg(Identifier: string)
end

function ClientProcess.insert(Identifier: string, obj)
if not Debug.new(Collections[Identifier].OutgoingOrder < ClientProcess.rateLimit, "[FastNet2]: Unable to call :Fire (rate-limit reached)", 1) then return end
if Collections[Identifier].OutgoingOrder >= ClientProcess.rateLimit then return warn("[FastNet2]: Unable to call :Fire (rate-limit reached)") end
Collections[Identifier].OutgoingOrder += 1
Outgoing[Identifier][Collections[Identifier].OutgoingOrder] = obj
end
Expand Down
10 changes: 4 additions & 6 deletions src/Client/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -142,20 +142,18 @@ function Client:Pull(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(), tostring(#self.flag + 1)
local thread, requestId = coroutine.running(), string.pack("I5", Util.uniqueNum())
local session = task.delay(timeout, function()
-- session expire
-- removing and clear entire requests point
if self.flag[requestId] then
table.remove(self.flag, requestId)
end
if Process.verify(self.Identifier, requestId) then
table.remove(self.flag, requestId)
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)
-- set new flag as checkpoint
self.flag[requestId] = function(...)
self.flag[requestId] = nil
task.cancel(session)
task.spawn(thread, ...) -- resume thread to finish and return data as successed
end
Expand Down
10 changes: 4 additions & 6 deletions src/Server/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -150,20 +150,18 @@ function Server:Pull(timeout: number, player: Player, ...: any)
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(), tostring(#self.flag + 1)
local thread, requestId = coroutine.running(), string.pack("I5", Util.uniqueNum())
local session = task.delay(timeout, function()
-- session expire
-- removing and clear entire requests point
if self.flag[requestId] then
table.remove(self.flag, requestId)
end
if Process.verify(self.Identifier, player, requestId) then
table.remove(self.flag, requestId)
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)
-- set new flag as checkpoint
self.flag[requestId] = function(...)
self.flag[requestId] = nil
task.cancel(session)
task.spawn(thread, ...) -- resume thread to finish and return data as successed
end
Expand Down
14 changes: 12 additions & 2 deletions src/Util.luau
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function Util.CreateUUID(): string
return string.gsub(HttpService:GenerateGUID(false), "-", "") :: string
end

-- Create session semiliar with task.wait function (YIELD)
-- 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()
Expand All @@ -25,7 +25,16 @@ function Util.Session(timeout: number): number
return coroutine.yield()
end

-- Serialize Identifier (Beta) (https://www.lua.org/manual/5.3/manual.html#pdf-string.pack)
-- Generate a randomized unique number with 8 length num (Internal Function)
function Util.uniqueNum(): number
local num: string = ""
for i=1,8 do
num ..= math.random(0, 9)
end
return tonumber(num) :: number
end

-- Serialize Identifier (Beta) (Internal Function) (https://www.lua.org/manual/5.3/manual.html#pdf-string.pack)
function Util.ser(identifier: string): string
Debug.new(typeof(identifier) == "string", "[FastNet2]: Unable to serialize identifier (expected string got "..typeof(identifier)..", will convert to string)", 1)
identifier = tostring(identifier)
Expand All @@ -36,6 +45,7 @@ function Util.ser(identifier: string): string
end
else
while not Event:GetAttribute(identifier) do
print("e")
Util.Session(0.5)
end
end
Expand Down
13 changes: 10 additions & 3 deletions src/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
--!strict
-- FastNet2
-- Author: @EternityDev (Eternity_Devs)
-- version rc9 (0.9.3)
-- version rc9 (0.9.4)

local FastNet2 = {}
FastNet2.__index = FastNet2
Expand All @@ -11,8 +11,15 @@ FastNet2.version = 0.9
local IsServer: boolean = game:GetService("RunService"):IsServer()
local HttpService = game:GetService("HttpService")

if not script:FindFirstChild("Event") then
Instance.new("RemoteEvent", script).Name = "Event"
if IsServer then
if not script:FindFirstChild("Event") then
Instance.new("RemoteEvent", script).Name = "Event"
end
else
if not script:FindFirstChild("Event") then
warn("[FastNet2]: Waiting for initialize from server-side.")
repeat task.wait() until script:FindFirstChild("Event")
end
end

local Debug = require(script.Debug)
Expand Down
78 changes: 34 additions & 44 deletions src/oneSignal.luau
Original file line number Diff line number Diff line change
Expand Up @@ -31,48 +31,42 @@ function oneSignal.get(identifier: string)
end

function oneSignal:Connect(callback: (...any) -> ())
if self then
Debug.new(not self.Connected, string.format("[oneSignal]: %s already connected", self.Identifier), 0)
self.cb = callback
self.Connected = true
end
Debug.new(not self.Connected, string.format("[oneSignal]: %s already connected", self.Identifier), 0)
self.cb = callback
self.Connected = true
end

function oneSignal:Invoke(timeout: number, ...: any): any
if self then
Debug.new(typeof(timeout) == "number", string.format("[oneSignal]: timeout must be number, got %s", typeof(timeout)), 0)
Debug.new(#{...} > 0, "[oneSignal]: Unable to invoke without data", 0)
timeout = math.max(1, timeout)
local thread, packed, retrive = coroutine.running(), { ... }, nil
local session = task.delay(timeout, function()
task.cancel(retrive)
task.spawn(thread, nil)
packed = nil
end)
retrive = task.spawn(function()
task.wait(1/60)
task.cancel(session)
task.spawn(thread, self.cb(table.unpack(packed)))
packed = nil
end)
return coroutine.yield()
end
Debug.new(typeof(timeout) == "number", string.format("[oneSignal]: timeout must be number, got %s", typeof(timeout)), 0)
Debug.new(#{...} > 0, "[oneSignal]: Unable to invoke without data", 0)
timeout = math.max(1, timeout)
local thread, packed, retrive = coroutine.running(), { ... }, nil
local session = task.delay(timeout, function()
task.cancel(retrive)
task.spawn(thread, nil)
packed = nil
end)
retrive = task.spawn(function()
task.wait(1/60)
task.cancel(session)
task.spawn(thread, self.cb(table.unpack(packed)))
packed = nil
end)
return coroutine.yield()
end

function oneSignal:Wait(): number
if self then
local thread = coroutine.running()
local clock = os.clock()
self._ping = function(x)
self._ping = nil
task.spawn(thread, (x - clock))
end
return coroutine.yield()
local thread = coroutine.running()
local clock = os.clock()
self._ping = function(x)
self._ping = nil
task.spawn(thread, (x - clock))
end
return coroutine.yield()
end

function oneSignal:Once(callback: (...any) -> ())
if self and not self.Connected then
if not self.Connected then
self.cb = function(...)
self:Disconnect()
task.spawn(callback, ...)
Expand All @@ -82,28 +76,24 @@ function oneSignal:Once(callback: (...any) -> ())
end

function oneSignal:Disconnect()
if self and self.Connected then
if self.Connected then
self.Connected = false
self.cb = nil
end
end

function oneSignal:Destroy()
self:Disconnect()
if self then
Collections[self.Identifier] = nil
self = nil
end
Collections[self.Identifier] = nil
self = nil
end

function oneSignal:Fire(...: any)
if self then
if self.Connected then
task.spawn(self.cb, ...)
end
if self._ping then
task.spawn(self._ping, os.clock())
end
if self.Connected then
task.spawn(self.cb, ...)
end
if self._ping then
task.spawn(self._ping, os.clock())
end
end

Expand Down
2 changes: 1 addition & 1 deletion wally.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ registry = "test"

[[package]]
name = "imezx/fastnet2"
version = "0.9.3"
version = "0.9.4"
dependencies = []
2 changes: 1 addition & 1 deletion wally.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "imezx/fastnet2"
version = "0.9.3"
version = "0.9.4"
registry = "https://github.com/UpliftGames/wally-index"
realm = "shared"
license = "MIT"
Expand Down

0 comments on commit 9ab5ca0

Please sign in to comment.