-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
62a7571
commit bd50c5a
Showing
5 changed files
with
271 additions
and
244 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
local ReplicatedStorage = game:GetService("ReplicatedStorage") | ||
local RunService = game:GetService("RunService") | ||
|
||
local ReliableRemote = ReplicatedStorage:WaitForChild("ReliableRedEvent") :: RemoteEvent | ||
local UnreliableRemote = ReplicatedStorage:WaitForChild("UnreliableRedEvent") :: RemoteEvent | ||
|
||
local ListenerMap: { [string]: ({ any }) -> () } = {} | ||
local CallMap: { [string]: thread } = {} | ||
local Outgoing: { | ||
Reliable: { [string]: { { any } } }, | ||
Unreliable: { [string]: { { any } } }, | ||
Call: { [string]: { { any } } }, | ||
} = | ||
{ | ||
Reliable = {}, | ||
Unreliable = {}, | ||
Call = {}, | ||
} | ||
|
||
local function SendReliableEvent(EventId: string, CallList: { any }) | ||
if not Outgoing.Reliable[EventId] then | ||
Outgoing.Reliable[EventId] = {} | ||
end | ||
|
||
table.insert(Outgoing.Reliable[EventId], CallList) | ||
end | ||
|
||
local function SendUnreliableEvent(EventId: string, CallList: { any }) | ||
if not Outgoing.Unreliable[EventId] then | ||
Outgoing.Unreliable[EventId] = {} | ||
end | ||
|
||
table.insert(Outgoing.Unreliable[EventId], CallList) | ||
end | ||
|
||
local function SetListener(EventId: string, Callback: ({ any }) -> ()) | ||
ListenerMap[EventId] = Callback | ||
end | ||
|
||
local function CallAsync(EventId: string, CallList: { any }): { any } | ||
if not Outgoing.Call[EventId] then | ||
Outgoing.Call[EventId] = {} | ||
end | ||
|
||
table.insert(Outgoing.Call[EventId], CallList) | ||
CallMap[CallList[1]] = coroutine.running() | ||
|
||
return coroutine.yield() | ||
end | ||
|
||
local function Start() | ||
ReliableRemote.OnClientEvent:Connect(function(IncomingFireSection, IncomingCallSection) | ||
if IncomingFireSection then | ||
for EventId, CallList in IncomingFireSection do | ||
local Listener = ListenerMap[EventId] | ||
|
||
if Listener then | ||
for _, Call in CallList do | ||
Listener(Call) | ||
end | ||
end | ||
end | ||
end | ||
|
||
if IncomingCallSection then | ||
for CallId, CallList in IncomingCallSection do | ||
local CallThread = CallMap[CallId] | ||
|
||
if CallThread then | ||
coroutine.resume(CallThread, table.unpack(CallList)) | ||
end | ||
end | ||
end | ||
end) | ||
|
||
UnreliableRemote.OnClientEvent:Connect(function(IncomingFireSection, IncomingCallSection) | ||
if IncomingFireSection then | ||
for EventId, CallList in IncomingFireSection do | ||
local Listener = ListenerMap[EventId] | ||
|
||
if Listener then | ||
for _, Call in CallList do | ||
Listener(Call) | ||
end | ||
end | ||
end | ||
end | ||
|
||
if IncomingCallSection then | ||
for CallId, CallList in IncomingCallSection do | ||
local CallThread = CallMap[CallId] | ||
|
||
if CallThread then | ||
coroutine.resume(CallThread, table.unpack(CallList)) | ||
CallMap[CallId] = nil | ||
end | ||
end | ||
end | ||
end) | ||
|
||
RunService.Heartbeat:Connect(function() | ||
if next(Outgoing.Reliable) or next(Outgoing.Call) then | ||
ReliableRemote:FireServer(Outgoing.Reliable, Outgoing.Call) | ||
|
||
Outgoing.Reliable = {} | ||
Outgoing.Call = {} | ||
end | ||
|
||
if next(Outgoing.Unreliable) then | ||
UnreliableRemote:FireServer(Outgoing.Unreliable) | ||
|
||
Outgoing.Unreliable = {} | ||
end | ||
end) | ||
end | ||
|
||
return { | ||
SendReliableEvent = SendReliableEvent, | ||
SendUnreliableEvent = SendUnreliableEvent, | ||
SetListener = SetListener, | ||
CallAsync = CallAsync, | ||
Start = Start, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
local ReplicatedStorage = game:GetService("ReplicatedStorage") | ||
local RunService = game:GetService("RunService") | ||
|
||
local Guard = require(script.Parent.Parent.Parent.Guard) | ||
|
||
local ReliableRemote = ReplicatedStorage:WaitForChild("ReliableRedEvent") :: RemoteEvent | ||
local UnreliableRemote = ReplicatedStorage:WaitForChild("UnreliableRedEvent") :: RemoteEvent | ||
|
||
local FireSectionCheck = Guard.Check(Guard.Map(Guard.String, Guard.List(Guard.Any))) | ||
local CallSectionCheck = Guard.Check(Guard.Map(Guard.String, Guard.List(Guard.Any))) | ||
|
||
local ListenerMap: { [string]: (Player, { any }) -> () } = {} | ||
local OutgoingMap: { | ||
[Player]: { | ||
Reliable: { [string]: { { any } } }, | ||
Unreliable: { [string]: { { any } } }, | ||
CallReturn: { [string]: { any } }, | ||
}, | ||
} = | ||
{} | ||
|
||
local function SendReliableEvent(Player: Player, EventId: string, Args: { any }) | ||
if not OutgoingMap[Player] then | ||
OutgoingMap[Player] = { | ||
Reliable = {}, | ||
Unreliable = {}, | ||
CallReturn = {}, | ||
} | ||
end | ||
|
||
if not OutgoingMap[Player].Reliable[EventId] then | ||
OutgoingMap[Player].Reliable[EventId] = {} | ||
end | ||
|
||
table.insert(OutgoingMap[Player].Reliable[EventId], Args) | ||
end | ||
|
||
local function SendUnreliableEvent(Player: Player, EventId: string, Args: { any }) | ||
if not OutgoingMap[Player] then | ||
OutgoingMap[Player] = { | ||
Reliable = {}, | ||
Unreliable = {}, | ||
CallReturn = {}, | ||
} | ||
end | ||
|
||
if not OutgoingMap[Player].Unreliable[EventId] then | ||
OutgoingMap[Player].Unreliable[EventId] = {} | ||
end | ||
|
||
table.insert(OutgoingMap[Player].Unreliable[EventId], Args) | ||
end | ||
|
||
local function SetListener(EventId: string, Callback: (Player, { any }) -> ()) | ||
ListenerMap[EventId] = Callback | ||
end | ||
|
||
local function SendCallReturn(Player: Player, CallId: string, Args: { any }) | ||
if not OutgoingMap[Player] then | ||
OutgoingMap[Player] = { | ||
Reliable = {}, | ||
Unreliable = {}, | ||
CallReturn = {}, | ||
} | ||
end | ||
|
||
OutgoingMap[Player].CallReturn[CallId] = Args | ||
end | ||
|
||
local function Start() | ||
ReliableRemote.OnServerEvent:Connect(function(Player, IncomingFireSection, IncomingCallSection) | ||
local FireSectionValid, FireSection = FireSectionCheck(IncomingFireSection) | ||
|
||
if FireSectionValid then | ||
for EventId, CallList in FireSection do | ||
local Callback = ListenerMap[EventId] | ||
|
||
if Callback then | ||
for _, Call in CallList do | ||
Callback(Player, Call) | ||
end | ||
end | ||
end | ||
end | ||
|
||
local CallSectionValid, CallSection = CallSectionCheck(IncomingCallSection) | ||
|
||
if CallSectionValid then | ||
for EventId, CallList in CallSection do | ||
local Callback = ListenerMap[EventId] | ||
|
||
if Callback then | ||
for _, Call in CallList do | ||
Callback(Player, Call) | ||
end | ||
else | ||
for _, Call in CallList do | ||
local CallId = Call[1] | ||
|
||
SendCallReturn(Player, CallId, { false, "Event has no listener." }) | ||
end | ||
end | ||
end | ||
end | ||
end) | ||
|
||
UnreliableRemote.OnServerEvent:Connect(function(Player, IncomingFireSection, IncomingCallSection) | ||
local FireSectionValid, FireSection = FireSectionCheck(IncomingFireSection) | ||
|
||
if FireSectionValid then | ||
for EventId, CallList in FireSection do | ||
local Callback = ListenerMap[EventId] | ||
|
||
if Callback then | ||
for _, Call in CallList do | ||
Callback(Player, Call) | ||
end | ||
end | ||
end | ||
end | ||
end) | ||
|
||
RunService.Heartbeat:Connect(function() | ||
for Player, Outgoing in OutgoingMap do | ||
if next(Outgoing.Reliable) or next(Outgoing.CallReturn) then | ||
ReliableRemote:FireClient(Player, Outgoing.Reliable, Outgoing.CallReturn) | ||
end | ||
|
||
if next(Outgoing.Unreliable) then | ||
UnreliableRemote:FireClient(Player, Outgoing.Unreliable) | ||
end | ||
|
||
OutgoingMap[Player] = nil | ||
end | ||
end) | ||
end | ||
|
||
return { | ||
SendReliableEvent = SendReliableEvent, | ||
SendUnreliableEvent = SendUnreliableEvent, | ||
SetListener = SetListener, | ||
SendCallReturn = SendCallReturn, | ||
Start = Start, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
return { | ||
Server = require(script.Server), | ||
Client = require(script.Client), | ||
} |
Oops, something went wrong.