Improve client connection and packet forwarding logic

Enhanced packet handling and client management in mesh router.
This commit is contained in:
CadenCoaster
2025-10-19 13:25:17 -07:00
committed by GitHub
parent 0f2dca23a7
commit f17377192c

View File

@@ -20,6 +20,7 @@ local function entrypoint_recieving(channel, msg)
if msg.protocol == "entrypoint_connect" then
if not clients[msg.sender] and msg.target == os.getComputerID() then
clients[msg.sender] = true
distancemap[msg.sender] = {dist = 1, sender = msg.sender}
print("New client connected:",msg.sender)
queue_message({protocol="route",destination=msg.sender,distance=1})
modem.transmit(15125,15125,{protocol="entrypoint_acknowledge",sender=os.getComputerID()})
@@ -35,9 +36,12 @@ local function entrypoint_recieving(channel, msg)
heartbeat_entrypoint = true
end
elseif msg.protocol == "packet" then
if clients[msg.sender] then
print("Packet received from client",tostring(msg.sender)..":",msg.content,"to",msg.destination)
if distancemap[msg.destination] ~= nil then
if clients[msg.sender] and (msg.hops or 0 ) == 0 then
print("Packet received from client",tostring(msg.sender),"to",msg.destination)
if clients[msg.destination] then
msg.hops = (msg.hops or 0) + 1
modem.transmit(15125,15125,msg)
elseif distancemap[msg.destination] ~= nil then
msg.hops = (msg.hops or 0) + 1
queue_message(msg, distancemap[msg.destination].sender)
else
@@ -135,24 +139,32 @@ while true do
end
elseif msg.protocol == "route_erase" then
if distancemap[msg.destination] ~= nil and msg.destination ~= os.getComputerID() and not is_in_table(msg.visited,os.getComputerID()) then
distancemap[msg.destination] = nil
if (not msg.visited) then msg.visited = {} end
msg.visited[#msg.visited+1] = os.getComputerID()
print("route to",msg.destination,"erased!")
queue_message(msg)
if clients[msg.destination] then
queue_message({protocol="route",destination=msg.destination,distance=1})
else
distancemap[msg.destination] = nil
if (not msg.visited) then msg.visited = {} end
msg.visited[#msg.visited+1] = os.getComputerID()
print("route to",msg.destination,"erased!")
queue_message(msg)
end
end
elseif msg.protocol == "packet" then
if msg.destination == os.getComputerID() then
print("Packet received from",tostring(msg.sender)..":",msg.content)
else
if distancemap[msg.destination] ~= nil then
msg.hops = msg.hops + 1
queue_message(msg, distancemap[msg.destination].sender)
elseif clients[msg.destination] then
msg.hops = msg.hops + 1
modem.transmit(15125,15125,msg)
else
print("No route to",msg.destination)
if msg._target then
if clients[msg.destination] then
print("Forwarding packet to client",msg.destination)
msg.hops = msg.hops + 1
modem.transmit(15125,15125,msg)
elseif distancemap[msg.destination] ~= nil then
print("Forwarding packet to node",msg.destination)
msg.hops = msg.hops + 1
queue_message(msg, distancemap[msg.destination].sender)
else
print("No route to",msg.destination)
end
end
end
elseif msg.protocol == "heartbeat" then
@@ -164,7 +176,7 @@ while true do
queue_message(response, msg._sender)
elseif msg.protocol == "getroutes_response" then
for k,v in pairs(msg.routes) do
if v.dist+1 < (distancemap[k] or {dist = math.huge}).dist and k ~= os.getComputerID() then
if v.dist+1 < (distancemap[k] or {dist = math.huge}).dist and k ~= os.getComputerID() and v.sender ~= os.getComputerID() then
v.sender = msg._sender
v.dist = v.dist + 1
distancemap[k] = v
@@ -196,7 +208,7 @@ end
local function heartbeat_f()
while true do
for k,v in pairs(distancemap) do
if k ~= os.getComputerID() then
if k ~= os.getComputerID() and not clients[v.sender] then
heartbeat = false
interactions.send({protocol="heartbeat"}, v.sender)
parallel.waitForAny(function()
@@ -230,7 +242,7 @@ local function healthcheck()
sleep(0.1)
end, function()
while not heartbeat_entrypoint do
modem.transmit(15125,15125,{protocol="heartbeat"})
modem.transmit(15125,15125,{protocol="heartbeat",sender=os.getComputerID(),target=client})
sleep()
end
end)