Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require "component"
- local event = require "event"
- local socket = {}
- local octcp = {}
- octcp.modem = component.modem
- octcp.address = ""
- octcp.port = 0
- octcp.buffer = ""
- octcp.psize = 8192
- function octcp.listen(_,_,from,port,_,message)
- if from == octcp.address and port == octcp.port then
- octcp.buffer = octcp.buffer..message
- end
- end
- function octcp.rawread(n)
- local s = octcp.buffer:sub(1,n)
- if octcp.buffer:len() >= n then
- octcp.buffer=octcp.buffer:sub(n+1)
- else
- octcp.buffer=""
- end
- return s,n
- end
- function octcp.read(i)
- if type(i) == "string" then
- f=octcp.buffer:find(i)
- elseif type(i) == "number" then
- f=i
- else
- f=nil
- end
- if f ~= nil then
- return octcp.rawread(f)
- else
- return nil
- end
- end
- function octcp.write(s)
- local t = {}
- if s:len() > octcp.psize then
- repeat
- octcp.modem.send(octcp.address,octcp.port,s:sub(1,octcp.psize))
- s=s:sub(octcp.psize+1)
- until s:len() <= octcp.psize
- else
- octcp.modem.send(octcp.address,octcp.port,s)
- end
- end
- function octcp.isData()
- if octcp.buffer~="" then return true else return false end
- end
- function octcp.init()
- event.listen("modem_message",octcp.listen)
- end
- function octcp.close()
- event.ignore("modem_message",octcp.listen)
- end
- function socket.socket(port,address)
- t=octcp
- t.address = address
- t.port = port
- return t
- end
- return socket
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement