Advertisement
Guest User

octcp.lua

a guest
Jun 23rd, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.96 KB | None | 0 0
  1. local component = require "component"
  2. local event = require "event"
  3.  
  4. local octcp = {}
  5.  
  6. octcp.modem = component.modem
  7. octcp.address = ""
  8. octcp.port = 0
  9. octcp.buffer = ""
  10. octcp.psize = 8192
  11.  
  12. function octcp.listen(_,_,from,port,_,message)
  13.  if from == octcp.address and port == octcp.port then
  14.   octcp.buffer = octcp.buffer..message
  15.  end
  16. end
  17.  
  18. function octcp.read(n)
  19.  local s = octcp.buffer:sub(1,n)
  20.  if octcp.buffer:len() >=n then
  21.   octcp.buffer:sub(n+1)
  22.  else
  23.   octcp.buffer=""
  24.  end
  25.  return s
  26. end
  27.  
  28. function octcp.write(s)
  29.  local t = {}
  30.  if s:len() > octcp.psize then
  31.   repeat
  32.    octcp.modem.send(octcp.address,octcp.port,s:sub(1,octcp.psize))
  33.    s=s:sub(octcp.psize+1)
  34.   until s:len() <= octcp.psize
  35.  else
  36.   octcp.modem.send(octcp.address,octcp.port,s)
  37.  end
  38. end
  39.  
  40. function octcp.isData()
  41.  if octcp.buffer~="" then return true else return false end
  42. end
  43.  
  44. function octcp.init(port,address)
  45.  octcp.port=port
  46.  octcp.address=address
  47.  event.listen("modem_message",octcp.listen)
  48. end
  49.  
  50. return octcp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement