Operating system for OpenComputers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

microtel-3.lua 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. os.spawn(function()
  2. _G.net={}
  3. do
  4. local modems,packetQueue,packetCache,routeCache,C,Y = {},{},{},{},COMPUTER,UNPACK
  5. net.port,net.hostname,net.route,net.hook,U=4096,computer.address():sub(1,8),true,{},UPTIME
  6. for a in component.list("modem") do
  7. modems[a] = component.proxy(a)
  8. modems[a].open(net.port)
  9. end
  10. local function genPacketID()
  11. local packetID = ""
  12. for i = 1, 16 do
  13. packetID = packetID .. string.char(math.random(32,126))
  14. end
  15. return packetID
  16. end
  17. local function rawSendPacket(packetID,packetType,to,from,vport,data)
  18. if routeCache[to] then
  19. modems[routeCache[to][1]].send(routeCache[to][2],net.port,packetID,packetType,to,from,vport,data)
  20. else
  21. for k,v in pairs(modems) do
  22. v.broadcast(net.port,packetID,packetType,to,from,vport,data)
  23. end
  24. end
  25. end
  26. local function sendPacket(packetID,packetType,to,vport,data)
  27. packetCache[packetID] = computer.uptime()
  28. rawSendPacket(packetID,packetType,to,net.hostname,vport,data)
  29. end
  30. function net.send(to,vport,data,packetType,packetID)
  31. packetType,packetID = packetType or 1, packetID or genPacketID()
  32. packetQueue[packetID] = {packetType,to,vport,data,0}
  33. sendPacket(packetID,packetType,to,vport,data)
  34. end
  35. local function checkCache(packetID)
  36. for k,v in pairs(packetCache) do
  37. if k == packetID then
  38. return false
  39. end
  40. end
  41. return true
  42. end
  43. while true do
  44. local eventTab = {coroutine.yield()}
  45. if eventTab[1] == "modem_message" and (eventTab[4] == net.port or eventTab[4] == 0) and checkCache(eventTab[6]) then
  46. for k,v in pairs(packetCache) do
  47. if computer.uptime() > v+30 then
  48. packetCache[k] = nil
  49. end
  50. end
  51. for k,v in pairs(routeCache) do
  52. if computer.uptime() > v[3]+30 then
  53. routeCache[k] = nil
  54. end
  55. end
  56. routeCache[eventTab[9]] = {eventTab[2],eventTab[3],computer.uptime()}
  57. if eventTab[8] == net.hostname then
  58. if eventTab[7] ~= 2 then
  59. computer.pushSignal("net_msg",eventTab[9],eventTab[10],eventTab[11])
  60. if eventTab[7] == 1 then
  61. sendPacket(genPacketID(),2,eventTab[9],eventTab[10],eventTab[6])
  62. end
  63. else
  64. packetQueue[eventTab[11]] = nil
  65. end
  66. elseif net.route and checkCache(eventTab[6]) then
  67. rawSendPacket(eventTab[6],eventTab[7],eventTab[8],eventTab[9],eventTab[10],eventTab[11])
  68. end
  69. packetCache[eventTab[6]] = computer.uptime()
  70. end
  71. for k,v in pairs(packetQueue) do
  72. if computer.uptime() > v[5] then
  73. sendPacket(k,table.unpack(v))
  74. v[5]=computer.uptime()+30
  75. end
  76. end
  77. end
  78. end
  79. end,"microtel-3.lua")