Skip to content

Commit

Permalink
Give half second grace for handshake
Browse files Browse the repository at this point in the history
  • Loading branch information
boatbomber committed Oct 9, 2023
1 parent 9d5a532 commit e9bc10e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@boatbomber/encryptednet",
"version": "1.0.3",
"version": "1.0.4",
"license": "MIT",
"repository": {
"type": "git",
Expand Down
31 changes: 24 additions & 7 deletions src/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,31 @@ Players.PlayerRemoving:Connect(function(Player)
PlayerData[Player] = nil
end)

local function getPlayerData(Player, shouldYield)
local playerData = PlayerData[Player]
if shouldYield and playerData == nil then
-- Perhaps they're in middle of the handshake?
-- Poll for half a second
for _=1, 15 do
task.wait(1/30)
playerData = PlayerData[Player]
if playerData then
break
end
end
end

return playerData
end

return function(Remote)
local Wrapper = setmetatable({}, { __index = Remote })

-- Event

function Wrapper:Connect(callback)
Remote:Connect(function(Player, encryptedData, signature)
local playerData = PlayerData[Player]
local playerData = getPlayerData(Player, false) -- No yield since they can't have sent valid data without a handshake first
if not playerData then
return
end
Expand All @@ -59,7 +76,7 @@ return function(Remote)
end

function Wrapper:SendToPlayer(Player, ...)
local playerData = PlayerData[Player]
local playerData = getPlayerData(Player, true) -- Yield in case we're sending before they're ready
if not playerData then
return
end
Expand All @@ -81,13 +98,13 @@ return function(Remote)
continue
end

Wrapper:SendToPlayer(Player, ...)
task.spawn(Wrapper.SendToPlayer, Wrapper, Player, ...)
end
end

function Wrapper:SendToAllPlayers(...)
for _, Player in ipairs(Players:GetPlayers()) do
Wrapper:SendToPlayer(Player, ...)
task.spawn(Wrapper.SendToPlayer, Wrapper, Player, ...)
end
end

Expand All @@ -100,14 +117,14 @@ return function(Remote)
continue
end

Wrapper:SendToPlayer(Player, ...)
task.spawn(Wrapper.SendToPlayer, Wrapper, Player, ...)
end
end

-- AsyncFunction

function Wrapper:CallPlayerAsync(Player, ...)
local playerData = PlayerData[Player]
local playerData = getPlayerData(Player, true) -- Yield in case we're calling before they're ready
if not playerData then
return
end
Expand All @@ -125,7 +142,7 @@ return function(Remote)

function Wrapper:SetCallback(callback)
Remote:SetCallback(function(Player, encryptedData, signature)
local playerData = PlayerData[Player]
local playerData = getPlayerData(Player, false) -- No yield since they can't have sent valid data without a handshake first
if not playerData then
return
end
Expand Down
2 changes: 1 addition & 1 deletion wally.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ dependencies = []

[[package]]
name = "boatbomber/encryptednet"
version = "1.0.3"
version = "1.0.4"
dependencies = []
2 changes: 1 addition & 1 deletion wally.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "boatbomber/encryptednet"
description = "Authenticated encryption of Roblox networking with ECDH key exchanges and ChaCha20 ciphering."
version = "1.0.3"
version = "1.0.4"
license = "MIT"
authors = ["boatbomber (https://boatbomber.com)"]
registry = "https://github.com/upliftgames/wally-index"
Expand Down

0 comments on commit e9bc10e

Please sign in to comment.