mirror of
https://github.com/rayaman/multi.git
synced 2026-09-05 07:27:35 -04:00
almost done, just a few more tests
This commit is contained in:
+82
-3
@@ -6,7 +6,7 @@ Copyright (c) 2017 Ryan Ward
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
to use, copy, modify, merge, publish, distribute, sub-license, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
@@ -750,15 +750,83 @@ function multi:newCondition(func)
|
||||
return c
|
||||
end
|
||||
multi.NewCondition=multi.newCondition
|
||||
function multi:threadloop(settings)
|
||||
multi.scheduler:Destroy() -- destroy is an interesting thing... if you dont set references to nil, then you only remove it from the mainloop
|
||||
local Threads=multi:linkDomain("Threads")
|
||||
local Globals=multi:linkDomain("Globals")
|
||||
local counter=0
|
||||
local tick = 0
|
||||
while true do
|
||||
tick = tick + 1
|
||||
if tick == 1024 then
|
||||
tick = 0
|
||||
multi:uManager(settings)
|
||||
end
|
||||
counter=counter+1
|
||||
for i=#Threads,1,-1 do
|
||||
ret={}
|
||||
if coroutine.status(Threads[i].thread)=="dead" then
|
||||
table.remove(Threads,i)
|
||||
else
|
||||
if Threads[i].timer:Get()>=Threads[i].sleep then
|
||||
if Threads[i].firstRunDone==false then
|
||||
Threads[i].firstRunDone=true
|
||||
Threads[i].timer:Start()
|
||||
_,ret=coroutine.resume(Threads[i].thread,Threads[i].ref)
|
||||
else
|
||||
_,ret=coroutine.resume(Threads[i].thread,Globals)
|
||||
end
|
||||
if _==false then
|
||||
self.Parent.OnError:Fire(Threads[i],"Error in thread: <"..Threads[i].Name.."> "..ret)
|
||||
end
|
||||
if ret==true or ret==false then
|
||||
ret={}
|
||||
end
|
||||
end
|
||||
if ret then
|
||||
if ret[1]=="_kill_" then
|
||||
table.remove(Threads,i)
|
||||
elseif ret[1]=="_sleep_" then
|
||||
Threads[i].timer:Reset()
|
||||
Threads[i].sleep=ret[2]
|
||||
elseif ret[1]=="_skip_" then
|
||||
Threads[i].timer:Reset()
|
||||
Threads[i].sleep=math.huge
|
||||
local event=multi:newEvent(function(evnt) return counter>=evnt.counter end)
|
||||
event.link=Threads[i]
|
||||
event.counter=counter+ret[2]
|
||||
event:OnEvent(function(evnt)
|
||||
evnt.link.sleep=0
|
||||
end)
|
||||
elseif ret[1]=="_hold_" then
|
||||
Threads[i].timer:Reset()
|
||||
Threads[i].sleep=math.huge
|
||||
local event=multi:newEvent(ret[2])
|
||||
event.link=Threads[i]
|
||||
event:OnEvent(function(evnt)
|
||||
evnt.link.sleep=0
|
||||
end)
|
||||
elseif ret.Name then
|
||||
Globals[ret.Name]=ret.Value
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:mainloop(settings)
|
||||
multi.defaultSettings = settings or multi.defaultSettings
|
||||
if not multi.isRunning then
|
||||
local protect = false
|
||||
local priority = false
|
||||
local stopOnError = true
|
||||
if settings then
|
||||
if settings.preLoop then
|
||||
settings.preLoop(self)
|
||||
end
|
||||
if settings.stopOnError then
|
||||
stopOnError = settings.stopOnError
|
||||
end
|
||||
protect = settings.protect
|
||||
priority = settings.priority
|
||||
end
|
||||
@@ -781,6 +849,9 @@ function multi:mainloop(settings)
|
||||
if err then
|
||||
Loop[_D].error=err
|
||||
self.OnError:Fire(Loop[_D],err)
|
||||
if stopOnError then
|
||||
Loop[_D]:Destroy()
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -803,6 +874,9 @@ function multi:mainloop(settings)
|
||||
if err then
|
||||
Loop[_D].error=err
|
||||
self.OnError:Fire(Loop[_D],err)
|
||||
if stopOnError then
|
||||
Loop[_D]:Destroy()
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -826,6 +900,9 @@ function multi:mainloop(settings)
|
||||
if err then
|
||||
Loop[_D].error=err
|
||||
self.OnError:Fire(Loop[_D],err)
|
||||
if stopOnError then
|
||||
Loop[_D]:Destroy()
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1386,6 +1463,9 @@ end
|
||||
function thread.yeild()
|
||||
coroutine.yield({"_sleep_",0})
|
||||
end
|
||||
function thread.isThread()
|
||||
return coroutine.running()
|
||||
end
|
||||
function thread.getCores()
|
||||
return thread.__CORES
|
||||
end
|
||||
@@ -1488,8 +1568,7 @@ multi.scheduler:OnLoop(function(self)
|
||||
_,ret=coroutine.resume(self.Threads[i].thread,self.Globals)
|
||||
end
|
||||
if _==false then
|
||||
self.Parent.OnError:Fire(self.Threads[i],ret)
|
||||
print("Error in thread: <"..self.Threads[i].Name.."> "..ret)
|
||||
self.Parent.OnError:Fire(Threads[i],"Error in thread: <"..Threads[i].Name.."> "..ret)
|
||||
end
|
||||
if ret==true or ret==false then
|
||||
print("Thread Ended!!!")
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
-- CURRENT TASK: newNetThread()
|
||||
-- CURRENT TASK:
|
||||
|
||||
local multi = require("multi")
|
||||
local net = require("net")
|
||||
@@ -158,7 +158,7 @@ function multi:newNode(settings)
|
||||
local node = {}
|
||||
node.name = name
|
||||
multi.OnError(function(i,error)
|
||||
node.OnError(node,error,node.server)
|
||||
node.OnError:Fire(node,error,node.server)
|
||||
end)
|
||||
node.server = net:newUDPServer(0) -- hosts the node using the default port
|
||||
_, node.port = node.server.udp:getsockname()
|
||||
@@ -168,12 +168,16 @@ function multi:newNode(settings)
|
||||
node.hasFuncs = {}
|
||||
node.OnError = multi:newConnection()
|
||||
node.OnError(function(node,err,master)
|
||||
print("ERROR",err,node.name)
|
||||
local temp = bin.new()
|
||||
temp:addBlock(#node.name)
|
||||
temp:addBlock(#node.name,2)
|
||||
temp:addBlock(node.name)
|
||||
temp:addBlock(#err)
|
||||
temp:addBlock(#err,2)
|
||||
temp:addBlock(err)
|
||||
node.server:send(char(CMD_ERROR..temp))
|
||||
for i,v in pairs(node.connections) do
|
||||
print(i)
|
||||
v[1]:send(v[2],char(CMD_ERROR)..temp.data,v[3])
|
||||
end
|
||||
end)
|
||||
if settings.managerDetails then
|
||||
local c = net:newTCPClient(settings.managerDetails[1],settings.managerDetails[2])
|
||||
@@ -216,14 +220,17 @@ function multi:newNode(settings)
|
||||
end
|
||||
function node:getConsole()
|
||||
local c = {}
|
||||
c.connections = node.connections
|
||||
function c:print(...)
|
||||
local conn = node.connections
|
||||
function c.print(...)
|
||||
local data = char(CMD_CONSOLE)..packData({...})
|
||||
for i,v in pairs(self.connections) do
|
||||
for i,v in pairs(conn) do
|
||||
--print(i)
|
||||
v[1]:send(v[2],data,v[3])
|
||||
end
|
||||
print("sent message")
|
||||
end
|
||||
-- function c:printTo()
|
||||
|
||||
-- end
|
||||
return c
|
||||
end
|
||||
node.loadRate=1
|
||||
@@ -266,7 +273,10 @@ function multi:newNode(settings)
|
||||
local func = holder:getBlock("s",len2)
|
||||
args = resolveData(args)
|
||||
func = resolveData(func)
|
||||
func(unpack(args))
|
||||
status, err = pcall(func,node,unpack(args))
|
||||
if not status then
|
||||
node.OnError:Fire(node,err,server)
|
||||
end
|
||||
elseif cmd == CMD_INITNODE then
|
||||
print("Connected with another node!")
|
||||
node.connections[dat]={server,ip,port}
|
||||
@@ -427,7 +437,7 @@ function multi:newMaster(settings) -- You will be able to have more than one mas
|
||||
multi:newTLoop(function(loop)
|
||||
if self.connections[name]~=nil then
|
||||
self.connections[name]:send(data)
|
||||
loop:Desrtoy()
|
||||
loop:Destroy()
|
||||
end
|
||||
end,.1)
|
||||
else
|
||||
@@ -486,7 +496,7 @@ function multi:newMaster(settings) -- You will be able to have more than one mas
|
||||
local node = temp:getBlock("s",len)
|
||||
len = temp:getBlock("n",2)
|
||||
local err = temp:getBlock("s",len)
|
||||
master.OnError(name,err)
|
||||
master.OnError:Fire(name,err)
|
||||
elseif cmd == CMD_CONSOLE then
|
||||
print(unpack(resolveData(dat)))
|
||||
elseif cmd == CMD_PONG then
|
||||
|
||||
Reference in New Issue
Block a user