Skip to content

Commit

Permalink
Replace Reliable[Un]ordered Channels
Browse files Browse the repository at this point in the history
The `ReliableUnordered` channel is already ordered
  • Loading branch information
YetAnotherClown committed Sep 20, 2023
1 parent a89e4a0 commit f2d3fd8
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 41 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ For more help, check out [the Rojo documentation](https://rojo.space/docs).
- [x] Filter useless returns in query results (e.g. `player` when doing ``Net:query(Player)``, `sender` when doing ``Net:query(Net.Server)``, `identifier` when doing ``Net:query(identifier)``.) We already know what these values are, as we are querying for those specific items.
- [x] Stable Core API
- [x] Compress `Identifier` strings
- [X] ReliableUnordered Channel
- [ ] Rate limiting for `Net` or `Identifier`
- [ ] Middleware
- [ ] Debugger
Expand Down
2 changes: 1 addition & 1 deletion docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ local Net = require(Net)

-- Create a new Net Server/Client
local net = Net.new({
Channel: "UnreliableOrdered"
Channel: "Reliable"
Event: "default",
})

Expand Down
2 changes: 1 addition & 1 deletion example/matter/src/shared/start.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ local function start(containers)
-- Set up Networking

local net = Net.new({
Channel = "UnreliableOrdered",
Channel = "Reliable",
Event = "default",
})

Expand Down
35 changes: 4 additions & 31 deletions lib/Bridge.luau
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ Bridge._outgoingQueue = {} :: OutgoingQueue
It is structured in a way to minimize overhead when calling RemoteEvents.
By default, each queue will be ordered. This is a design choice in order to promote
predictable and data-driven networking. To make the queue unordered, set the ``Channel``
to ``ReliableUnordered`` in the Configuration.
As of v0.1.0, the ``ReliableUnordered`` Channel is not implemented.
predictable and data-driven networking.
]=]

-- Packages a Packet to go into a Server Payload
Expand All @@ -74,26 +71,14 @@ function Bridge:_addPacketToPayload(
identifier: string,
recipient: { Player } | Player | "NET_SERVER",
data: { any },
},
pos
}
)
if not payload[packet.identifier] then
payload[packet.identifier] = {}
end

local data = {}

-- Implement ReliableOrdered Channel for precise ordering
if self._configuration and self._configuration.Channel == "ReliableOrdered" then
-- Prevent unsigned overflow
if pos < 0 or pos > 65535 then
pos = 0
end

-- Store the queue position
table.insert(data, string.pack("H", pos)) -- Limit: 65535
end

for _, value in packet.data do
table.insert(data, value)
end
Expand Down Expand Up @@ -176,20 +161,8 @@ function Bridge:_processIncoming(
)
-- Unpack each packet and add it to the queue
for identifier, packets in payload do
for _, rawData in packets do
for _, data in packets do
local pos = #self._incomingQueue + 1
local data = {}

-- Implement ReliableOrdered Channel
if self._configuration and self._configuration.Channel == "ReliableOrdered" then
pos = string.unpack("H", rawData[1]) -- Limit: 65535

for i = 2, #rawData do
table.insert(data, rawData[i])
end
else
data = rawData -- No extra arguments stored
end

local packet = {
identifier = identifier,
Expand Down Expand Up @@ -277,7 +250,7 @@ function Bridge.new(configuration: { any }?)

self._configuration = configuration or {}

local channel = self._configuration.Channel or "ReliableUnordered"
local channel = self._configuration.Channel or "Reliable"
local event = self._configuration.Event or "default"

local remoteKey = channel .. "@" .. event
Expand Down
10 changes: 3 additions & 7 deletions lib/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type Identifier = Identifier.Identifier
--[=[
@interface Configuration
@within Net
@field Channel "ReliableOrdered" | "ReliableUnordered" | "Unreliable" -- Default: ReliableUnordered
@field Channel "Reliable" | "Unreliable" -- Default: Reliable
@field DebugMode boolean -- Enable Debug Features?
@field DebugKey KeyCode -- The key to open the Debug Panel
@field Event string -- The event to schedule Net on in your Matter Loop -- Default: "default"
Expand All @@ -24,16 +24,12 @@ export type Identifier = Identifier.Identifier
### Channel
**ReliableOrdered**: Ensures the order in which Packets were sent at the cost of a few bytes. It is not recommended
that this is used. In most cases, ``ReliableUnordered`` should work.
**ReliableUnordered**: Does not ensure the order in which Packets are sent. However, Packets will be ordered properly
per Identifier only. This should work in most cases and does not have any overhead.
**Reliable**: All packets will be sent and received per frame in order.
**Unreliable**: Anticipated feature that Roblox may add, this is here for planning and is not yet available.
]=]
type Configuration = {
Channel: ("ReliableOrdered" | "ReliableUnordered" | "Unreliable")?,
Channel: ("Reliable" | "Unreliable")?,
DebugMode: boolean?,
DebugKey: Enum.KeyCode?,
Event: string?,
Expand Down

0 comments on commit f2d3fd8

Please sign in to comment.