Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for UnreliableRemoteEvents #174

Merged
merged 5 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions modules/comm/Client/ClientRemoteSignal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ ClientRemoteSignal.__index = ClientRemoteSignal
@within ClientRemoteSignal
@interface Connection
.Disconnect () -> ()

Represents a connection.
]=]

function ClientRemoteSignal.new(
re: RemoteEvent,
re: RemoteEvent | UnreliableRemoteEvent,
inboundMiddleware: Types.ClientMiddleware?,
outboudMiddleware: Types.ClientMiddleware?
)
Expand All @@ -37,7 +39,7 @@ function ClientRemoteSignal.new(
self._signal = Signal.new()
self._reConn = self._re.OnClientEvent:Connect(function(...)
local args = table.pack(...)
for _, middlewareFunc in ipairs(inboundMiddleware) do
for _, middlewareFunc in inboundMiddleware do
local middlewareResult = table.pack(middlewareFunc(args))
if not middlewareResult[1] then
return
Expand All @@ -54,7 +56,7 @@ end

function ClientRemoteSignal:_processOutboundMiddleware(...: any)
local args = table.pack(...)
for _, middlewareFunc in ipairs(self._outbound) do
for _, middlewareFunc in self._outbound do
local middlewareResult = table.pack(middlewareFunc(args))
if not middlewareResult[1] then
return table.unpack(middlewareResult, 2, middlewareResult.n)
Expand Down
22 changes: 17 additions & 5 deletions modules/comm/Server/RemoteSignal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@ RemoteSignal.__index = RemoteSignal
@interface Connection
.Disconnect () -> nil
.Connected boolean

Represents a connection.
]=]

function RemoteSignal.new(
parent: Instance,
name: string,
unreliable: boolean?,
inboundMiddleware: Types.ServerMiddleware?,
outboundMiddleware: Types.ServerMiddleware?
)
local self = setmetatable({}, RemoteSignal)
self._re = Instance.new("RemoteEvent")
self._re = if unreliable == true then Instance.new("UnreliableRemoteEvent") else Instance.new("RemoteEvent")
self._re.Name = name
self._re.Parent = parent
if outboundMiddleware and #outboundMiddleware > 0 then
Expand All @@ -43,7 +46,7 @@ function RemoteSignal.new(
self._signal = Signal.new()
self._re.OnServerEvent:Connect(function(player, ...)
local args = table.pack(...)
for _, middlewareFunc in ipairs(inboundMiddleware) do
for _, middlewareFunc in inboundMiddleware do
local middlewareResult = table.pack(middlewareFunc(player, args))
if not middlewareResult[1] then
return
Expand All @@ -58,6 +61,15 @@ function RemoteSignal.new(
return self
end

--[=[
@return boolean
Returns `true` if the underlying RemoteSignal is bound to an
UnreliableRemoteEvent object.
]=]
function RemoteSignal:IsUnreliable(): boolean
return self._re:IsA("UnreliableRemoteEvent")
end

--[=[
@param fn (player: Player, ...: any) -> nil -- The function to connect
@return Connection
Expand All @@ -78,7 +90,7 @@ function RemoteSignal:_processOutboundMiddleware(player: Player?, ...: any)
return ...
end
local args = table.pack(...)
for _, middlewareFunc in ipairs(self._outbound) do
for _, middlewareFunc in self._outbound do
local middlewareResult = table.pack(middlewareFunc(player, args))
if not middlewareResult[1] then
return table.unpack(middlewareResult, 2, middlewareResult.n)
Expand Down Expand Up @@ -157,7 +169,7 @@ end
```
]=]
function RemoteSignal:FireFilter(predicate: (Player, ...any) -> boolean, ...: any)
for _, player in ipairs(Players:GetPlayers()) do
for _, player in Players:GetPlayers() do
if predicate(player, ...) then
self._re:FireClient(player, self:_processOutboundMiddleware(nil, ...))
end
Expand All @@ -181,7 +193,7 @@ end
```
]=]
function RemoteSignal:FireFor(players: { Player }, ...: any)
for _, player in ipairs(players) do
for _, player in players do
self._re:FireClient(player, self:_processOutboundMiddleware(nil, ...))
end
end
Expand Down
10 changes: 8 additions & 2 deletions modules/comm/Server/ServerComm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
-- December 20, 2021

local Comm = require(script.Parent)
local Util = require(script.Parent.Parent.Util)
local Types = require(script.Parent.Parent.Types)
local Util = require(script.Parent.Parent.Util)

--[=[
@class ServerComm
Expand Down Expand Up @@ -116,13 +116,18 @@ end

--[=[
@param name string
@param unreliable boolean?
@param inboundMiddleware ServerMiddleware?
@param outboundMiddleware ServerMiddleware?
@return RemoteSignal

Creates a signal that can be used to fire data to the clients
or receive data from the clients.

By default, signals use RemoteEvents internally. However, if
the `unreliable` argument is set to `true`, then an
UnreliableRemoteEvent will be used instead.

```lua
local mySignal = serverComm:CreateSignal("MySignal")

Expand All @@ -140,10 +145,11 @@ end
]=]
function ServerComm:CreateSignal(
name: string,
unreliable: boolean?,
inboundMiddleware: Types.ServerMiddleware?,
outboundMiddleware: Types.ServerMiddleware?
)
return Comm.CreateSignal(self._instancesFolder, name, inboundMiddleware, outboundMiddleware)
return Comm.CreateSignal(self._instancesFolder, name, unreliable, inboundMiddleware, outboundMiddleware)
end

--[=[
Expand Down
7 changes: 4 additions & 3 deletions modules/comm/Server/init.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local Util = require(script.Parent.Util)
local RemoteSignal = require(script.RemoteSignal)
local RemoteProperty = require(script.RemoteProperty)
local RemoteSignal = require(script.RemoteSignal)
local Types = require(script.Parent.Types)
local Util = require(script.Parent.Util)

local Server = {}

Expand Down Expand Up @@ -114,12 +114,13 @@ end
function Server.CreateSignal(
parent: Instance,
name: string,
reliable: boolean?,
inboundMiddleware: Types.ServerMiddleware?,
outboundMiddleware: Types.ServerMiddleware?
)
assert(Util.IsServer, "CreateSignal must be called from the server")
local folder = Util.GetCommSubFolder(parent, "RE"):Expect("Failed to get Comm RE folder")
local rs = RemoteSignal.new(folder, name, inboundMiddleware, outboundMiddleware)
local rs = RemoteSignal.new(folder, name, reliable, inboundMiddleware, outboundMiddleware)
return rs
end

Expand Down
4 changes: 2 additions & 2 deletions modules/comm/wally.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[package]
name = "sleitnick/comm"
description = "Comm library for remote communication"
version = "0.3.2"
version = "1.0.0"
license = "MIT"
authors = ["Stephen Leitnick"]
registry = "https://github.com/UpliftGames/wally-index"
realm = "shared"

[dependencies]
Signal = "sleitnick/signal@1"
Signal = "sleitnick/signal@2"
Option = "sleitnick/option@1"
Promise = "evaera/promise@4"
55 changes: 55 additions & 0 deletions modules/net/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,40 @@ function Net:RemoteEvent(name: string): RemoteEvent
end
end

--[=[
Gets an UnreliableRemoteEvent with the given name.

On the server, if the UnreliableRemoteEvent does not
exist, then it will be created with the given name.

On the client, if the UnreliableRemoteEvent does not
exist, then it will wait until it exists for at least
10 seconds. If the UnreliableRemoteEvent does not exist
after 10 seconds, an error will be thrown.

```lua
local unreliableRemoteEvent = Net:UnreliableRemoteEvent("PositionChanged")
```
]=]
function Net:UnreliableRemoteEvent(name: string): UnreliableRemoteEvent
name = "URE/" .. name
if RunService:IsServer() then
local r = script:FindFirstChild(name)
if not r then
r = Instance.new("UnreliableRemoteEvent")
r.Name = name
r.Parent = script
end
return r
else
local r = script:WaitForChild(name, 10)
if not r then
error("Failed to find UnreliableRemoteEvent: " .. name, 2)
end
return r
end
end

--[=[
Connects a handler function to the given RemoteEvent.

Expand All @@ -62,6 +96,27 @@ function Net:Connect(name: string, handler: (...any) -> ()): RBXScriptConnection
end
end

--[=[
Connects a handler function to the given UnreliableRemoteEvent.

```lua
-- Client
Net:ConnectUnreliable("PositionChanged", function(position)
print("Position", position)
end)

-- Server
Net:ConnectUnreliable("SomeEvent", function(player, ...) end)
```
]=]
function Net:ConnectUnreliable(name: string, handler: (...any) -> ()): RBXScriptConnection
if RunService:IsServer() then
return self:UnreliableRemoteEvent(name).OnServerEvent:Connect(handler)
else
return self:UnreliableRemoteEvent(name).OnClientEvent:Connect(handler)
end
end

--[=[
Gets a RemoteFunction with the given name.

Expand Down
2 changes: 1 addition & 1 deletion modules/net/wally.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "sleitnick/net"
description = "Static networking module"
version = "0.1.0"
version = "0.2.0"
license = "MIT"
authors = ["Stephen Leitnick"]
registry = "https://github.com/UpliftGames/wally-index"
Expand Down