ZNZNCOOP

OpenNet

Apr 16th, 2015
318
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Библиотека OpenNet для WiFi-клиента
  2.  
  3. local component=require("component")
  4. local event = require("event")
  5. local ser = require("serialization").serialize
  6. local uns = require("serialization").unserialize
  7.  
  8. local opennet = {}
  9. local ONcard
  10. local LocalIP
  11. local Router
  12. local ONport=1
  13. local ONlong=2
  14. local maxPacketSize
  15.  
  16. function opennet.ver()
  17.   return "3.1"
  18. end
  19.  
  20. function opennet.getIP()
  21.   if not component.isAvailable("modem") then
  22.     ONcard=nil
  23.     return nil, "Сетевая карта не обнаружена"
  24.   end
  25.   ONcard=component.modem
  26.   ONcard.open(ONport)
  27.   ONcard.open(ONlong)
  28.   maxPacketSize=ONcard.maxPacketSize()-100
  29.   local ok,err=ONcard.broadcast(ONport,"", "", "getip")
  30.   if not ok then  return ok, err  end
  31.   local Dist=math.huge
  32.   LocalIP=nil
  33.   Router=nil
  34.   while true do
  35.     local ev, addr, rout, _, dist, locip, _, mess = event.pull(1,"modem_message")
  36.     if ev then
  37.       if dist<Dist and addr == ONcard.address and mess == "setip" then
  38.         LocalIP=locip Router=rout Dist=dist
  39.       end
  40.     else
  41.       if LocalIP then return LocalIP, Dist
  42.       else  return nil, "Нет ответа от WiFi роутера" end
  43.     end
  44.   end
  45. end
  46.  
  47. function opennet.send(recIP, ... )
  48.   if not ONcard or not Router then
  49.     return nil, "Сетевая карта не инициализирована"
  50.   end
  51.   return ONcard.send(Router, ONport, recIP, LocalIP, ...)
  52. end
  53.  
  54. function opennet.Lsend(recIP, ... )
  55.   if not ONcard or not Router then
  56.     return nil, "Сетевая карта не инициализирована"
  57.   end
  58.   local data=ser({...})
  59.   repeat
  60.     ONcard.send(Router, ONlong, recIP, LocalIP, data:sub(1,maxPacketSize), #data<=maxPacketSize)
  61.     data=data:sub(maxPacketSize+1)
  62.   until #data==0
  63. end
  64.  
  65. function opennet.receive(timeout)
  66.   local ev
  67.   repeat
  68.     ev = {event.pull(timeout,"modem_message")}
  69.     if not ev[1] then return nil end
  70.   until ev[2] == ONcard.address
  71.   return table.unpack(ev,7)
  72. end
  73.  
  74. function opennet.Lreceive(timeout)
  75.   local ev
  76.   local data=""
  77.   repeat
  78.     ev = {event.pull(timeout,"modem_message")}
  79.     localAdr, senderAdr, port, dist, recIP, sendIP, dat, finish= ev[2], ev[3], ev[4], ev[5], ev[6], ev[7], ev[8], ev[9]
  80.     if not ev[1] then return nil end
  81.   until ev[2] == ONcard.address
  82.   return table.unpack(ev,7)
  83. end
  84.  
  85. function opennet.sendrec(recIP, ... )
  86.   local ok,err=opennet.send(recIP, ... )
  87.   if ok then
  88.     return opennet.receive(3)
  89.   else
  90.     return ok,err
  91.   end
  92. end
  93.  
  94. function opennet.Lsendrec(recIP, ... )
  95.   local ok,err=opennet.send(recIP, ... )
  96.   if ok then
  97.     return opennet.Lreceive(3)
  98.   else
  99.     return ok,err
  100.   end
  101. end
  102. -----------------------------------------------------------------------
  103. return opennet
RAW Paste Data