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 event = require("event")
|
||||
local serialization = require("serialization")
|
||||
require("util")
|
||||
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_client_port = 9998
|
||||
|
||||
local internal_port={}
|
||||
|
||||
--- Auto Configure
|
||||
local modem=proxy("modem")
|
||||
local tunnel=proxy("tunnel")
|
||||
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,...)
|
||||
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
|
||||
|
||||
function BroadcastData(port,...)
|
||||
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
|
||||
|
||||
|
@ -65,20 +100,34 @@ function routerMain()
|
|||
|
||||
if(e.receiverAddress==tunnel.address) then
|
||||
if(e.data[1]=="NetBoxAir") then
|
||||
if(e.data[2]=="Direct") then
|
||||
modem.send(e.data[4],netbox_client_port,"NetBox",e.data[3],table.unpack(e.data,5))
|
||||
elseif(e.data[2]=="Broadcast") then
|
||||
modem.broadcast(netbox_client_port,"NetBox",e.data[3],table.unpack(e.data,4))
|
||||
local port=e.data[2]
|
||||
local tt=decompress(e.data[3])
|
||||
|
||||
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
|
||||
elseif(e.receiverAddress==modem.address) then
|
||||
if(e.port==netbox_router_port) then
|
||||
if(e.data[1]=="NetBox") then
|
||||
if(e.data[2]=="Direct") then
|
||||
tunnel.send("NetBoxAir","Direct",e.senderAddress,e.data[3],table.unpack(e.data,4))
|
||||
elseif(e.data[2]=="Broadcast") then
|
||||
tunnel.send("NetBoxAir","Broadcast",e.senderAddress,e.data[3],table.unpack(e.data,4))
|
||||
end
|
||||
if(e.port==netbox_router_port and e.data[1]=="NetBox") then
|
||||
local port=e.data[2]
|
||||
local dt=decompress(e.data[3])
|
||||
|
||||
local tt={}
|
||||
tt.data=dt.data
|
||||
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
|
||||
|
@ -91,14 +140,36 @@ function routerMain()
|
|||
print("Router Stopped.")
|
||||
end
|
||||
|
||||
function clientServiceStart(redirect)
|
||||
local _clientService_hand=-1
|
||||
|
||||
function NetBoxInit(redirect)
|
||||
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
|
||||
|
||||
AddEventListener("modem_message",function(ev)
|
||||
if(ev.data[1]=="NetBox" and internal_port[ev.data[3]]~=nil ) then
|
||||
event.push("net_message",ev.receiverAddress,ev.data[2],ev.data[3],table.unpack(ev.data,4))
|
||||
end
|
||||
end)
|
||||
if(_clientService_hand<0) then
|
||||
_clientService_hand=AddEventListener("modem_message",function(ev)
|
||||
if(ev.data[1]=="NetBox" and internal_port[ev.data[2]]~=nil) then
|
||||
local dt=decompress(ev.data[3])
|
||||
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
|
||||
|
||||
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