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.

54 lines
1.0 KiB

local computer = computer or require("computer")
local event = event or require("event")
local Queue = require("queue")
local net = require("net")
local Socket = {}
Socket.__index = Socket
setmetatable(Socket, {
__call = function (cls, ...)
return cls.new(...)
end,
})
function Socket.new(addr, port)
local self = setmetatable({}, Socket)
self.addr = addr
self.port = port
self.q = Queue()
-- local addr, remote addr, port, distance, msg
self.listener = function(name, ...)
t = {...}
if t[2] ~= self.addr or t[3] ~= self.port then return end
self.q:push(t)
end
net.open(port)
self.id = event.listen("modem_message", self.listener)
return self
end
function Socket:close()
net.close(self.port)
self.queue = nil
event.cancel(self.id)
end
-- Send along msg, data
function Socket:send(msg, ...)
net.send(self.addr, self.port, msg, ...)
end
-- Pop results of queue
function Socket:recieve()
return self.q:pop()
end
return Socket