#51 network file cleanup

This commit is contained in:
Mikayla Fischler 2023-06-25 13:06:03 -04:00
parent 336662de62
commit f469754bb7

View File

@ -73,7 +73,7 @@ end
---@param modem table modem to use ---@param modem table modem to use
function network.nic(modem) function network.nic(modem)
local self = { local self = {
connected = true, connected = true, -- used to avoid costly MAC calculations if modem isn't even present
channels = {} channels = {}
} }
@ -128,43 +128,39 @@ function network.nic(modem)
-- if disconnected *after* opening, previousy opened channels will be re-opened on reconnection -- if disconnected *after* opening, previousy opened channels will be re-opened on reconnection
---@param channel integer ---@param channel integer
function public.open(channel) function public.open(channel)
if self.connected then modem.open(channel)
modem.open(channel)
local already_open = false local already_open = false
for i = 1, #self.channels do for i = 1, #self.channels do
if self.channels[i] == channel then if self.channels[i] == channel then
already_open = true already_open = true
break break
end
end end
end
if not already_open then if not already_open then
table.insert(self.channels, channel) table.insert(self.channels, channel)
end
end end
end end
-- close a channel on the modem -- close a channel on the modem
---@param channel integer ---@param channel integer
function public.close(channel) function public.close(channel)
if self.connected then modem.close(channel)
modem.close(channel)
for i = 1, #self.channels do for i = 1, #self.channels do
if self.channels[i] == channel then if self.channels[i] == channel then
table.remove(self.channels, i) table.remove(self.channels, i)
return return
end
end end
end end
end end
-- close all channels on the modem -- close all channels on the modem
function public.closeAll() function public.closeAll()
if self.connected then modem.closeAll()
modem.closeAll()
self.channels = {} self.channels = {}
end
end end
-- send a packet, with message authentication if configured -- send a packet, with message authentication if configured