mirror of
https://github.com/Kiritow/OpenComputerScripts.git
synced 2024-03-22 13:10:46 +08:00
Update libnetbox.
Meet "packet has too many parts" error. Now we are using serialization library.
This commit is contained in:
parent
1f2491ca83
commit
64e657bd14
115
libnetbox.lua
115
libnetbox.lua
|
@ -1,29 +1,64 @@
|
||||||
local component = require("component")
|
local component = require("component")
|
||||||
local event = require("event")
|
local event = require("event")
|
||||||
|
local serialization = require("serialization")
|
||||||
require("util")
|
require("util")
|
||||||
require("libevent")
|
require("libevent")
|
||||||
|
require("checkarg")
|
||||||
|
|
||||||
|
local function compress(t)
|
||||||
|
checktable(t)
|
||||||
|
return serialization.serialize(t)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function decompress(x)
|
||||||
|
checkstring(x)
|
||||||
|
return serialization.unserialize(x)
|
||||||
|
end
|
||||||
|
|
||||||
local netbox_router_port = 9999
|
local netbox_router_port = 9999
|
||||||
local netbox_client_port = 9998
|
local netbox_client_port = 9998
|
||||||
|
|
||||||
|
local internal_port={}
|
||||||
|
|
||||||
--- Auto Configure
|
--- Auto Configure
|
||||||
local modem=proxy("modem")
|
local modem=proxy("modem")
|
||||||
local tunnel=proxy("tunnel")
|
local tunnel=proxy("tunnel")
|
||||||
local is_router=false
|
local is_router=false
|
||||||
|
|
||||||
-- APIs
|
-- Hardware Check
|
||||||
|
|
||||||
local internal_port={}
|
local function doHardwareCheck()
|
||||||
|
if(modem==nil) then
|
||||||
|
error("Libnetbox[Warning]: modem not avaliable.")
|
||||||
|
end
|
||||||
|
|
||||||
|
if(tunnel==nil) then
|
||||||
|
is_router=false
|
||||||
|
else
|
||||||
|
is_router=true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- APIs
|
||||||
|
|
||||||
function SendData(target_address,port,...)
|
function SendData(target_address,port,...)
|
||||||
if(not is_router) then
|
if(not is_router) then
|
||||||
modem.broadcast(netbox_router_port,"NetBox","Direct",target_address,port,...)
|
local dt={}
|
||||||
|
dt["target"]=target_address
|
||||||
|
dt["method"]="Direct"
|
||||||
|
dt["data"]=compress(table.pack(...))
|
||||||
|
|
||||||
|
modem.broadcast(netbox_router_port,"NetBox",port,compress(dt))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function BroadcastData(port,...)
|
function BroadcastData(port,...)
|
||||||
if(not is_router) then
|
if(not is_router) then
|
||||||
modem.broadcast(netbox_router_port,"NetBox","Broadcast",port,...)
|
local dt={}
|
||||||
|
dt["method"]="Broadcast"
|
||||||
|
dt["data"]=compress(table.pack(...))
|
||||||
|
|
||||||
|
modem.broadcast(netbox_router_port,"NetBox",port,compress(dt))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -65,20 +100,34 @@ function routerMain()
|
||||||
|
|
||||||
if(e.receiverAddress==tunnel.address) then
|
if(e.receiverAddress==tunnel.address) then
|
||||||
if(e.data[1]=="NetBoxAir") then
|
if(e.data[1]=="NetBoxAir") then
|
||||||
if(e.data[2]=="Direct") then
|
local port=e.data[2]
|
||||||
modem.send(e.data[4],netbox_client_port,"NetBox",e.data[3],table.unpack(e.data,5))
|
local tt=decompress(e.data[3])
|
||||||
elseif(e.data[2]=="Broadcast") then
|
|
||||||
modem.broadcast(netbox_client_port,"NetBox",e.data[3],table.unpack(e.data,4))
|
local dt={}
|
||||||
|
dt.senderAddress=tt.senderAddress
|
||||||
|
dt.data=tt.data
|
||||||
|
|
||||||
|
if(tt.method=="Direct") then
|
||||||
|
modem.send(tt.target,netbox_client_port,"NetBox",port,compress(dt))
|
||||||
|
elseif(tt.method=="Broadcast") then
|
||||||
|
modem.broadcast(netbox_client_port,"NetBox",port,compress(dt))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
elseif(e.receiverAddress==modem.address) then
|
elseif(e.receiverAddress==modem.address) then
|
||||||
if(e.port==netbox_router_port) then
|
if(e.port==netbox_router_port and e.data[1]=="NetBox") then
|
||||||
if(e.data[1]=="NetBox") then
|
local port=e.data[2]
|
||||||
if(e.data[2]=="Direct") then
|
local dt=decompress(e.data[3])
|
||||||
tunnel.send("NetBoxAir","Direct",e.senderAddress,e.data[3],table.unpack(e.data,4))
|
|
||||||
elseif(e.data[2]=="Broadcast") then
|
local tt={}
|
||||||
tunnel.send("NetBoxAir","Broadcast",e.senderAddress,e.data[3],table.unpack(e.data,4))
|
tt.data=dt.data
|
||||||
end
|
tt.method=dt.method
|
||||||
|
tt.senderAddress=e.senderAddress
|
||||||
|
|
||||||
|
if(dt.method=="Direct") then
|
||||||
|
tt.target=dt.target
|
||||||
|
tunnel.send("NetBoxAir",port,compress(tt))
|
||||||
|
elseif(dt.method=="Broadcast") then
|
||||||
|
tunnel.send("NetBoxAir",port,compress(tt))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -91,14 +140,36 @@ function routerMain()
|
||||||
print("Router Stopped.")
|
print("Router Stopped.")
|
||||||
end
|
end
|
||||||
|
|
||||||
function clientServiceStart(redirect)
|
local _clientService_hand=-1
|
||||||
|
|
||||||
|
function NetBoxInit(redirect)
|
||||||
if(modem.open(netbox_client_port)==false) then
|
if(modem.open(netbox_client_port)==false) then
|
||||||
error("Failed to start client service. Real modem port can not be opened.")
|
if(modem.isOpen(netbox_client_port)==false) then
|
||||||
|
error("Failed to start client service. Real modem port can not be opened.")
|
||||||
|
else
|
||||||
|
print("libnetbox: Modem port has been opened. This may cause something wrong...")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
AddEventListener("modem_message",function(ev)
|
if(_clientService_hand<0) then
|
||||||
if(ev.data[1]=="NetBox" and internal_port[ev.data[3]]~=nil ) then
|
_clientService_hand=AddEventListener("modem_message",function(ev)
|
||||||
event.push("net_message",ev.receiverAddress,ev.data[2],ev.data[3],table.unpack(ev.data,4))
|
if(ev.data[1]=="NetBox" and internal_port[ev.data[2]]~=nil) then
|
||||||
end
|
local dt=decompress(ev.data[3])
|
||||||
end)
|
event.push("net_message",ev.receiverAddress,dt.senderAddress,ev.data[2],table.unpack(decompress(dt.data)))
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
else
|
||||||
|
print("libnetbox: Service has been started before. This may cause something wrong...")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function NetBoxCleanUp()
|
||||||
|
if(_clientService_hand>=0) then
|
||||||
|
RemoveEventListener(_clientService_hand)
|
||||||
|
_clientService_hand=-1
|
||||||
|
end
|
||||||
|
modem.close(netbox_client_port)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Do hardware check
|
||||||
|
doHardwareCheck()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user