OpenComputers Von Neumann Machine Programs
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.

104 lines
2.8 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. local computer = computer or require("computer")
  2. local event = event or require("event")
  3. local Queue = require("queue")
  4. local net = require("net")
  5. -- Sockets allow for simplier 2 way communicatation
  6. -- An event listener is created to recieve incoming messages and automatically queue them
  7. -- You should probably remember to close the socket when your done
  8. -- You don't _nessacrily_ have to create a socket on both sides
  9. local Socket = {}
  10. Socket.__index = Socket
  11. setmetatable(Socket, {
  12. __call = function (cls, ...)
  13. return cls.new(...)
  14. end,
  15. })
  16. function Socket.new(raddr, port)
  17. local self = setmetatable({}, Socket)
  18. self.raddr = net.checkAddr(raddr)
  19. self.port = port
  20. self._queue = Queue()
  21. -- name, local addr, remote addr, port, distance, msg
  22. listener = function(...)
  23. tmp = {...}
  24. for k, v in pairs(tmp) do print(k, v) end
  25. -- Check if port and raddr match
  26. if tmp[3] ~= self.raddr or tmp[4] ~= self.port then
  27. print("denied")
  28. return
  29. end
  30. print("accepted")
  31. pkt = {}
  32. pkt.laddr, pkt.raddr, pkt.port, pkt.distance, pkt.msg = tmp[2], tmp[3], tmp[4], tmp[5], tmp[6]
  33. self._queue:push(pkt)
  34. end
  35. net.open(port)
  36. -- Create modem_message listener
  37. self._listener = event.listen("modem_message", listener)
  38. -- Close socket on interrupt
  39. self._interrupter = event.listen("interrupted", function() self:close() end)
  40. return self
  41. end
  42. function Socket:close()
  43. net.close(self.port)
  44. event.cancel(self._listener)
  45. event.cancel(self._interrupter)
  46. -- Mark for garbage collection. Maybe this isn't needed?
  47. self._queue = nil
  48. self = nil
  49. end
  50. -- Send along msg, data
  51. function Socket:send(msg, ...)
  52. t = {...}
  53. if t ~= nil then
  54. print("Sending " .. self.raddr .. " " .. tostring(self.port) .. " " .. msg .. " ")
  55. else
  56. print("Sending " .. self.raddr .. " " .. tostring(self.port) .. " " .. msg .. " ")
  57. end
  58. net.send(self.raddr, self.port, msg, ...)
  59. end
  60. -- Pop off the queue
  61. -- blocking true if you want to block main execution until we recieve a message
  62. -- if blocking is true you can also optionally add a timeout
  63. function Socket:recieve(blocking, timeout)
  64. if blocking == nil then blocking = false end
  65. -- Get the result from the queue
  66. local result = self._queue:pop()
  67. -- If there is not a result and we're blocking then wait until we recieve one
  68. if result == nil and blocking then
  69. print("blocking")
  70. if timeout == nil then
  71. pkt, err = net.recv{self.port, self.raddr}
  72. else
  73. pkt, err = net.recv{self.port, self.raddr, timeout=timeout}
  74. end
  75. result = pkt
  76. print("Some reason we got : " .. pkt.laddr .. " " .. pkt.raddr)
  77. end
  78. return result
  79. end
  80. return Socket