use gamax style code more with nil check and type strings

This commit is contained in:
payonel 2015-09-02 17:46:17 -07:00
parent 03f5c34607
commit c1e8835b82

View File

@ -24,14 +24,14 @@ modem_host.clients = {}
-- [port_number] = true when open -- [port_number] = true when open
modem_host.open_ports = {} modem_host.open_ports = {}
function modem_host.createPacketArray(t, address, port, ...) function modem_host.createPacketArray(packetType, address, port, ...)
compCheckArg(1,t,type("")) compCheckArg(1,packetType,"string")
compCheckArg(2,address,type(""),type(0)) compCheckArg(2,address,"string","number")
compCheckArg(3,port,type(0)) compCheckArg(3,port,"number")
local packed = local packed =
{ {
t, packetType,
address, address,
modem_host.id, modem_host.id,
port, port,
@ -43,7 +43,7 @@ function modem_host.createPacketArray(t, address, port, ...)
end end
function modem_host.packetArrayToPacket(packed) function modem_host.packetArrayToPacket(packed)
compCheckArg(1,packed,type({})) compCheckArg(1,packed,"table")
assert(#packed >= 5) assert(#packed >= 5)
local packet = {} local packet = {}
@ -62,7 +62,7 @@ function modem_host.packetArrayToPacket(packed)
end end
function modem_host.packetArrayToDatagram(packed) function modem_host.packetArrayToDatagram(packed)
compCheckArg(1,packed,type({})) compCheckArg(1,packed,"table")
local datagram = ser.serialize(packed) local datagram = ser.serialize(packed)
return datagram .. '\n' return datagram .. '\n'
@ -81,7 +81,7 @@ function modem_host.packetToPacketArray(packet)
end end
function modem_host.datagramToPacketArray(datagram) function modem_host.datagramToPacketArray(datagram)
compCheckArg(1,datagram,type("")) compCheckArg(1,datagram,"string")
return ser.unserialize(datagram) return ser.unserialize(datagram)
end end
@ -101,13 +101,13 @@ end
function modem_host.readPacketArray(client) -- client:receive() function modem_host.readPacketArray(client) -- client:receive()
local datagram, err = modem_host.readDatagram(client) local datagram, err = modem_host.readDatagram(client)
if not datagram then return nil, err end if datagram == nil then return nil, err end
return modem_host.datagramToPacketArray(datagram) return modem_host.datagramToPacketArray(datagram)
end end
function modem_host.readPacket(client) -- client:receive() function modem_host.readPacket(client) -- client:receive()
local packed, err = modem_host.readPacketArray(client) local packed, err = modem_host.readPacketArray(client)
if not packed then return nil, err end if packed == nil then return nil, err end
return modem_host.packetArrayToPacket(packed) return modem_host.packetArrayToPacket(packed)
end end
@ -219,12 +219,12 @@ function modem_host.recvPendingMessages()
if modem_host.hosting then if modem_host.hosting then
while true do while true do
local client = modem_host.socket:accept() local client = modem_host.socket:accept()
if not client then if client == nil then
break; break;
end end
local handshake, err = modem_host.readPacket(client) -- client:receive() local handshake, err = modem_host.readPacket(client) -- client:receive()
if not handshake then if handshake == nil then
client:close() client:close()
else else
@ -331,7 +331,7 @@ function modem_host.connectMessageBoard()
modem_host.messages = {} modem_host.messages = {}
-- computer address seems to be applied late -- computer address seems to be applied late
if not modem_host.id then if modem_host.id == nil then
modem_host.id = component.list("computer",true)() modem_host.id = component.list("computer",true)()
assert(modem_host.id) assert(modem_host.id)
end end
@ -382,7 +382,7 @@ function obj.close(port) -- Closes the specified port (default: all ports). Retu
end end
-- nil port case -- nil port case
if not port then if port == nil then
if not next(modem_host.open_ports) then if not next(modem_host.open_ports) then
return false, "no open ports" return false, "no open ports"
else else