1
0
mirror of https://github.com/kepler155c/opus synced 2024-06-14 09:26:54 +00:00

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
end) end)
-- ensure socket is connected
process:newThread('pinger', function() process:newThread('pinger', function()
while true do while true do
os.sleep(3) os.sleep(3)
if not socket.connected then if not socket:ping() then
break break
end end
socket:ping()
end end
end) end)

View File

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

View File

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