mirror of
https://github.com/imezx/Warp.git
synced 2026-06-02 12:18:32 +00:00
feat: hash-map for listeners
This commit is contained in:
parent
050969ab5b
commit
94b0c5a919
4 changed files with 68 additions and 69 deletions
|
|
@ -23,7 +23,7 @@ type Event = {
|
||||||
|
|
||||||
local queueEvent: { { any } } = {}
|
local queueEvent: { { any } } = {}
|
||||||
local queueUnreliableEvent: { { any } } = {}
|
local queueUnreliableEvent: { { any } } = {}
|
||||||
local eventListeners: { Event } = {}
|
local eventListeners: { [number]: { Event } } = {}
|
||||||
local eventSchemas: { [number]: Buffer.SchemaType } = {}
|
local eventSchemas: { [number]: Buffer.SchemaType } = {}
|
||||||
|
|
||||||
local pendingInvokes: { [string]: thread } = {}
|
local pendingInvokes: { [string]: thread } = {}
|
||||||
|
|
@ -52,13 +52,21 @@ Client.Connect = function(remoteName: string, fn: (Player, ...any?) -> ...any?):
|
||||||
local id = Replication.get_id[remoteName]
|
local id = Replication.get_id[remoteName]
|
||||||
if not id then
|
if not id then
|
||||||
warn(`[Warp]: ".Connect"::"{remoteName}" does not exist, likely its not registered on the server yet.`)
|
warn(`[Warp]: ".Connect"::"{remoteName}" does not exist, likely its not registered on the server yet.`)
|
||||||
return { Connected = false, Disconnect = function() return end } :: Connection
|
return {
|
||||||
|
Connected = false,
|
||||||
|
Disconnect = function()
|
||||||
|
return
|
||||||
|
end,
|
||||||
|
} :: Connection
|
||||||
end
|
end
|
||||||
local detail = {
|
local detail = {
|
||||||
i = id,
|
i = id,
|
||||||
c = fn,
|
c = fn,
|
||||||
}
|
}
|
||||||
table.insert(eventListeners, detail)
|
if not eventListeners[id] then
|
||||||
|
eventListeners[id] = {}
|
||||||
|
end
|
||||||
|
table.insert(eventListeners[id], detail)
|
||||||
return {
|
return {
|
||||||
Connected = true,
|
Connected = true,
|
||||||
Disconnect = function(self: Connection)
|
Disconnect = function(self: Connection)
|
||||||
|
|
@ -66,9 +74,12 @@ Client.Connect = function(remoteName: string, fn: (Player, ...any?) -> ...any?):
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
self.Connected = false
|
self.Connected = false
|
||||||
local idx = table.find(eventListeners, detail)
|
local bucket = eventListeners[detail.i]
|
||||||
|
if bucket then
|
||||||
|
local idx = table.find(bucket, detail)
|
||||||
if idx then
|
if idx then
|
||||||
table.remove(eventListeners, idx)
|
table.remove(bucket, idx)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
} :: Connection
|
} :: Connection
|
||||||
|
|
@ -105,11 +116,7 @@ Client.DisconnectAll = function(remoteName: string)
|
||||||
if not id then
|
if not id then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
for idx = #eventListeners, 1, -1 do
|
eventListeners[id] = nil
|
||||||
if eventListeners[idx].i == id then
|
|
||||||
table.remove(eventListeners, idx)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--@remoteName string
|
--@remoteName string
|
||||||
|
|
@ -177,38 +184,30 @@ if RunService:IsClient() then
|
||||||
continue
|
continue
|
||||||
end
|
end
|
||||||
if remote == 1 then
|
if remote == 1 then
|
||||||
if #eventListeners == 0 then
|
|
||||||
continue
|
|
||||||
end
|
|
||||||
local remoteName = content[1]
|
local remoteName = content[1]
|
||||||
local id = content[2]
|
local id = content[2]
|
||||||
local args = content[3]
|
local args = content[3]
|
||||||
for _, connection in eventListeners do
|
local connections = eventListeners[remoteName]
|
||||||
if connection.i == remoteName then
|
if connections and #connections > 0 then
|
||||||
Thread(function()
|
Thread(function()
|
||||||
local results = { connection.c(table.unpack(args)) }
|
local results = { connections[1].c(table.unpack(args)) }
|
||||||
table.insert(queueEvent, {
|
table.insert(queueEvent, {
|
||||||
1,
|
1,
|
||||||
{ id, results } :: any,
|
{ id, results } :: any,
|
||||||
})
|
})
|
||||||
end)
|
end)
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
continue
|
continue
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if #eventListeners == 0 then
|
local connections = eventListeners[remote]
|
||||||
continue
|
if connections then
|
||||||
end
|
for _, connection in connections do
|
||||||
for _, connection in eventListeners do
|
|
||||||
if connection.i ~= remote then
|
|
||||||
continue
|
|
||||||
end
|
|
||||||
Thread(connection.c, table.unpack(content))
|
Thread(connection.c, table.unpack(content))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
Event.OnClientEvent:Connect(function(b: buffer, ref: { Instance }?)
|
Event.OnClientEvent:Connect(function(b: buffer, ref: { Instance }?)
|
||||||
processIncoming(b, ref, true)
|
processIncoming(b, ref, true)
|
||||||
|
|
|
||||||
|
|
@ -124,7 +124,9 @@ else
|
||||||
end)
|
end)
|
||||||
|
|
||||||
Replication.remove = function(player: Player)
|
Replication.remove = function(player: Player)
|
||||||
table.remove(replication_ready, table.find(replication_ready, player))
|
local idx = table.find(replication_ready, player)
|
||||||
|
if not idx then return end
|
||||||
|
table.remove(replication_ready, idx)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ local queueEvent: {
|
||||||
local queueUnreliableEvent: {
|
local queueUnreliableEvent: {
|
||||||
[Player]: { { any } },
|
[Player]: { { any } },
|
||||||
} = {}
|
} = {}
|
||||||
local eventListeners: { Event } = {}
|
local eventListeners: { [number]: { Event } } = {}
|
||||||
local eventSchemas: { [number]: Buffer.SchemaType } = {}
|
local eventSchemas: { [number]: Buffer.SchemaType } = {}
|
||||||
local players_ready: { Player }, player_bytes: { [Player]: number } = {}, {}
|
local players_ready: { Player }, player_bytes: { [Player]: number } = {}, {}
|
||||||
|
|
||||||
|
|
@ -61,7 +61,10 @@ Server.Connect = function(remoteName: string, fn: (Player, ...any?) -> ...any?):
|
||||||
i = Identifier.get_id(remoteName),
|
i = Identifier.get_id(remoteName),
|
||||||
c = fn,
|
c = fn,
|
||||||
}
|
}
|
||||||
table.insert(eventListeners, detail)
|
if not eventListeners[detail.i] then
|
||||||
|
eventListeners[detail.i] = {}
|
||||||
|
end
|
||||||
|
table.insert(eventListeners[detail.i], detail)
|
||||||
return {
|
return {
|
||||||
Connected = true,
|
Connected = true,
|
||||||
Disconnect = function(self: Connection)
|
Disconnect = function(self: Connection)
|
||||||
|
|
@ -69,9 +72,12 @@ Server.Connect = function(remoteName: string, fn: (Player, ...any?) -> ...any?):
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
self.Connected = false
|
self.Connected = false
|
||||||
local idx = table.find(eventListeners, detail)
|
local bucket = eventListeners[detail.i]
|
||||||
|
if bucket then
|
||||||
|
local idx = table.find(bucket, detail)
|
||||||
if idx then
|
if idx then
|
||||||
table.remove(eventListeners, idx)
|
table.remove(bucket, idx)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
} :: Connection
|
} :: Connection
|
||||||
|
|
@ -108,11 +114,7 @@ Server.DisconnectAll = function(remoteName: string)
|
||||||
if not id then
|
if not id then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
for idx = #eventListeners, 1, -1 do
|
eventListeners[id] = nil
|
||||||
if eventListeners[idx].i == id then
|
|
||||||
table.remove(eventListeners, idx)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--@remoteName string
|
--@remoteName string
|
||||||
|
|
@ -149,7 +151,9 @@ end
|
||||||
-- Fire an event to all players except specified ones.
|
-- Fire an event to all players except specified ones.
|
||||||
Server.FireExcept = function(remoteName: string, reliable: boolean, except: { Player }, ...: any?)
|
Server.FireExcept = function(remoteName: string, reliable: boolean, except: { Player }, ...: any?)
|
||||||
for _, player: Player in players_ready do
|
for _, player: Player in players_ready do
|
||||||
if table.find(except, player) then continue end
|
if table.find(except, player) then
|
||||||
|
continue
|
||||||
|
end
|
||||||
Server.Fire(remoteName, reliable, player, ...)
|
Server.Fire(remoteName, reliable, player, ...)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -188,7 +192,9 @@ if RunService:IsServer() then
|
||||||
end
|
end
|
||||||
if not RunService:IsStudio() then
|
if not RunService:IsStudio() then
|
||||||
local bytes: number = (player_bytes[player] or 0) + math.max(buffer.len(b), 800)
|
local bytes: number = (player_bytes[player] or 0) + math.max(buffer.len(b), 800)
|
||||||
if bytes > 8e3 then return end
|
if bytes > 8e3 then
|
||||||
|
return
|
||||||
|
end
|
||||||
player_bytes[player] = bytes
|
player_bytes[player] = bytes
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -214,10 +220,10 @@ if RunService:IsServer() then
|
||||||
local remoteName = content[1]
|
local remoteName = content[1]
|
||||||
local id = content[2]
|
local id = content[2]
|
||||||
local args = content[3]
|
local args = content[3]
|
||||||
for _, connection in eventListeners do
|
local connections = eventListeners[remoteName]
|
||||||
if connection.i == remoteName then
|
if connections and #connections > 0 then
|
||||||
Thread(function()
|
Thread(function()
|
||||||
local results = { connection.c(player, table.unpack(args)) }
|
local results = { connections[1].c(player, table.unpack(args)) }
|
||||||
if not queueEvent[player] then
|
if not queueEvent[player] then
|
||||||
queueEvent[player] = {} :: any
|
queueEvent[player] = {} :: any
|
||||||
end
|
end
|
||||||
|
|
@ -226,23 +232,18 @@ if RunService:IsServer() then
|
||||||
{ id, results } :: any,
|
{ id, results } :: any,
|
||||||
})
|
})
|
||||||
end)
|
end)
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
continue
|
continue
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if #eventListeners == 0 then
|
local connections = eventListeners[remote]
|
||||||
continue
|
if connections then
|
||||||
end
|
for _, connection in connections do
|
||||||
for _, connection in eventListeners do
|
|
||||||
if connection.i ~= remote then
|
|
||||||
continue
|
|
||||||
end
|
|
||||||
Thread(connection.c, player, table.unpack(content))
|
Thread(connection.c, player, table.unpack(content))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
Event.OnServerEvent:Connect(function(player: Player, b: buffer, ref: { Instance }?)
|
Event.OnServerEvent:Connect(function(player: Player, b: buffer, ref: { Instance }?)
|
||||||
processIncoming(player, b, ref, true)
|
processIncoming(player, b, ref, true)
|
||||||
|
|
|
||||||
|
|
@ -12,9 +12,6 @@ if game.RunService:IsServer() then
|
||||||
if not script:FindFirstChild("UnreliableEvent") then
|
if not script:FindFirstChild("UnreliableEvent") then
|
||||||
Instance.new("UnreliableRemoteEvent", script).Name = "UnreliableEvent"
|
Instance.new("UnreliableRemoteEvent", script).Name = "UnreliableEvent"
|
||||||
end
|
end
|
||||||
if not script:FindFirstChild("Function") then
|
|
||||||
Instance.new("RemoteFunction", script).Name = "Function"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local Client = require("@self/Client")
|
local Client = require("@self/Client")
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue