working on systemthreadedconnections

This commit is contained in:
Ryan Ward 2022-09-18 13:56:00 -04:00
parent 1546076c80
commit bb7fab0857
3 changed files with 33 additions and 4 deletions

View File

@ -17,6 +17,10 @@ Added
Sets the helper function that the connection object uses when creating connection links. Sets the helper function that the connection object uses when creating connection links.
- `multi.ForEach(table, callback_function)`
Loops through the table and calls callback_function with each element of the array.
Changed Changed
--- ---
- `Connection:[connect, hasConnections, getConnection]` changed to be `Connection:[Connect, HasConnections, getConnections]`. This was done in an attempt to follow a consistent naming scheme. The old methods still will work to prevent old code breaking. - `Connection:[connect, hasConnections, getConnection]` changed to be `Connection:[Connect, HasConnections, getConnections]`. This was done in an attempt to follow a consistent naming scheme. The old methods still will work to prevent old code breaking.

View File

@ -109,6 +109,12 @@ function multi:getStats()
end end
--Helpers --Helpers
function multi.ForEach(tab,func)
for i=1,#tab do
func(tab[i])
end
end
local ignoreconn = true local ignoreconn = true
function multi:newConnection(protect,func,kill) function multi:newConnection(protect,func,kill)
local c={} local c={}
@ -198,6 +204,7 @@ function multi:newConnection(protect,func,kill)
end end
else else
function c:Fire(...) function c:Fire(...)
if lock then return end
for i=#call_funcs,1,-1 do for i=#call_funcs,1,-1 do
call_funcs[i](...) call_funcs[i](...)
if kill then if kill then
@ -272,7 +279,6 @@ function multi:newConnection(protect,func,kill)
}) })
function temp:Fire(...) function temp:Fire(...)
if lock then return end
return call_funcs(...) return call_funcs(...)
end end

View File

@ -3,24 +3,34 @@ package.cpath = "lua5.4/lib/lua/?/core.dll;"..package.cpath
multi, thread = require("multi"):init{print=true} multi, thread = require("multi"):init{print=true}
GLOBAL, THREAD = require("multi.integration.lanesManager"):init() GLOBAL, THREAD = require("multi.integration.lanesManager"):init()
function multi:newSystemThreadedConnection(name,...) function multi:newSystemThreadedConnection(name,...)
local c = {} local c = {}
c.CONN = 0x00
c.TRIG = 0x01
c.PING = 0x02
c.PONG = 0x03
local proxy_conn = multi:newConnection(...) local proxy_conn = multi:newConnection(...)
local name = name or multi.randomString(16) local name = name or multi.randomString(16)
local connections = {} -- All triggers sent from main connection. When a connection is triggered on another thread, they speak to the main then send stuff out. local connections = {} -- All triggers sent from main connection. When a connection is triggered on another thread, they speak to the main then send stuff out.
local funcs = {}
setmetatable(c,master_conn) -- A different approach will be taken for the non main connection objects setmetatable(c,master_conn) -- A different approach will be taken for the non main connection objects
c.subscribe = multi:newSystemThreadedQueue("Subscribe_"..name):init() -- Incoming subscriptions c.subscribe = multi:newSystemThreadedQueue("Subscribe_"..name):init() -- Incoming subscriptions
multi:newThread("STC_"..name,function() multi:newThread("STC_"..name,function()
while true do while true do
thread.yield()
local item = c.subscribe:pop() local item = c.subscribe:pop()
-- We need to check on broken connections -- We need to check on broken connections
-- c:Ping() -- c:Ping()
-- --
if item ~= nil then if item ~= nil then
connections[#connections+1] = item connections[#connections+1] = item
multi.ForEach(funcs, function(link) -- Sync new connections
item:push{c.CONN, link}
end)
thread.skip(multi.Priority_Normal) -- Usually a bunch of threads subscribe close to the same time. Process those by ensuring that they come alive around the same time thread.skip(multi.Priority_Normal) -- Usually a bunch of threads subscribe close to the same time. Process those by ensuring that they come alive around the same time
else -- I'm using these "Constants" since they may change with other releases and this should allow these functions to adjust with them. -- I'm using these "Constants" since they may change with other releases and this should allow these functions to adjust with them.
thread.skip(multi.Priority_Idle)
end end
end end
end) end)
@ -30,9 +40,18 @@ function multi:newSystemThreadedConnection(name,...)
function c:Fire(...) function c:Fire(...)
-- --
end end
function c:Connect(...) function c:Sync(link)
-- --
end end
function c:Connect(func)
local conn_func = func
proxy_conn(function()
funcs[#funcs+1] = func -- Used for syncing new connections to this connection later on
multi.ForEach(c.connections, function(link)
link:push{CONN, func}
end)
end)
end
function c:init() function c:init()
return self return self
end end