diff --git a/apps/mirrorHost.lua b/apps/mirrorHost.lua index 3454609..050a1c9 100644 --- a/apps/mirrorHost.lua +++ b/apps/mirrorHost.lua @@ -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) diff --git a/sys/apis/socket.lua b/sys/apis/socket.lua index 77a79a9..cb55ed8 100644 --- a/sys/apis/socket.lua +++ b/sys/apis/socket.lua @@ -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 diff --git a/sys/services/transort.lua b/sys/services/transort.lua index 2a1bf5e..7b9e849 100644 --- a/sys/services/transort.lua +++ b/sys/services/transort.lua @@ -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 }))