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. _G.net={}
  2. do
  3. local modems,packetQueue,packetCache,routeCache,C,Y = {},{},{},{},COMPUTER,UNPACK
  4. net.port,net.hostname,net.route,net.hook,U=4096,computer.address():sub(1,8),true,{},UPTIME
  5. for a in component.list("modem") do
  6. modems[a] = component.proxy(a)
  7. modems[a].open(net.port)
  8. end
  9. local function genPacketID()
  10. local packetID = ""
  11. for i = 1, 16 do
  12. packetID = packetID .. string.char(math.random(32,126))
  13. end
  14. return packetID
  15. end
  16. local function rawSendPacket(packetID,packetType,to,from,vport,data)
  17. if routeCache[to] then
  18. modems[routeCache[to][1]].send(routeCache[to][2],net.port,packetID,packetType,to,from,vport,data)
  19. else
  20. for k,v in pairs(modems) do
  21. v.broadcast(net.port,packetID,packetType,to,from,vport,data)
  22. end
  23. end
  24. end
  25. local function sendPacket(packetID,packetType,to,vport,data)
  26. packetCache[packetID] = computer.uptime()
  27. rawSendPacket(packetID,packetType,to,net.hostname,vport,data)
  28. end
  29. function net.send(to,vport,data,packetType,packetID)
  30. packetType,packetID = packetType or 1, packetID or genPacketID()
  31. packetQueue[packetID] = {packetType,to,vport,data,0}
  32. sendPacket(packetID,packetType,to,vport,data)
  33. end
  34. local function checkCache(packetID)
  35. for k,v in pairs(packetCache) do
  36. if k == packetID then
  37. return false
  38. end
  39. end
  40. return true
  41. end
  42. os.spawn(function()
  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,"minitel.3")
  79. end