networking improvements

This commit is contained in:
kepler155c@gmail.com 2017-05-10 19:59:09 -04:00
parent 3a922ad2f4
commit e287958faa
3 changed files with 24 additions and 28 deletions

View File

@ -32,13 +32,13 @@ while true do
end
end)
-- ensure socket is connected
process:newThread('pinger', function()
while true do
os.sleep(3)
if not socket.connected then
if not socket:ping() then
break
end
socket:ping()
end
end)

View File

@ -26,22 +26,22 @@ exchange.publicKey = modexp(exchange.base, exchange.secretKey, exchange.primeMod
function socketClass:read(timeout)
if not self.connected then
Logger.log('socket', 'read: No connection')
return
end
local data, distance = transport.read(self)
if data then
return data, distance
end
if not self.connected then
Logger.log('socket', 'read: No connection')
return
end
local timerId = os.startTimer(timeout or 5)
while true do
local e, id = os.pullEvent()
if e == 'transport_' .. self.dport then
if e == 'transport_' .. self.sport then
data, distance = transport.read(self)
if data then
@ -59,29 +59,25 @@ function socketClass:read(timeout)
end
function socketClass:write(data)
if not self.connected then
Logger.log('socket', 'write: No connection')
return false
if self.connected then
transport.write(self, {
type = 'DATA',
seq = self.wseq,
data = data,
})
return true
end
transport.write(self, {
type = 'DATA',
seq = self.wseq,
data = data,
})
return true
end
function socketClass:ping()
if not self.connected then
Logger.log('socket', 'ping: No connection')
return false
if self.connected then
transport.write(self, {
type = 'PING',
seq = self.wseq,
data = data,
})
return true
end
transport.write(self, {
type = 'PING',
seq = self.wseq,
data = data,
})
return true
end
function socketClass:close()
@ -166,7 +162,7 @@ function Socket.connect(host, port)
socket:close()
end
function trusted(msg, port)
local function trusted(msg, port)
if port == 19 or msg.shost == os.getComputerID() then
-- no auth for trust server or loopback

View File

@ -92,7 +92,7 @@ while true do
-- use resume instead ??
if not socket.messages[2] then -- table size is 1
os.queueEvent('transport_' .. dport)
os.queueEvent('transport_' .. socket.sport)
end
--debug('>> ' .. Util.tostring({ type = 'ACK', seq = msg.seq }))