opus/sys/apps/network/transport.lua

127 lines
3.0 KiB
Lua
Raw Normal View History

2017-05-09 05:57:00 +00:00
--[[
2018-01-24 22:39:38 +00:00
Low level socket protocol implementation.
2017-05-09 05:57:00 +00:00
2018-01-24 22:39:38 +00:00
* sequencing
* background read buffering
2017-05-09 05:57:00 +00:00
]]--
local Event = require('opus.event')
2019-06-29 20:35:33 +00:00
local SHA = require('opus.crypto.sha2')
2017-10-25 03:01:40 +00:00
2019-06-29 20:35:33 +00:00
local network = _G.network
2018-01-13 20:17:26 +00:00
local os = _G.os
2017-05-09 05:57:00 +00:00
local computerId = os.getComputerID()
2017-10-25 03:01:40 +00:00
local transport = {
2018-01-24 22:39:38 +00:00
timers = { },
sockets = { },
UID = 0,
2017-05-09 05:57:00 +00:00
}
2019-06-29 20:35:33 +00:00
getmetatable(network).__index.getTransport = function()
return transport
end
2017-05-09 05:57:00 +00:00
function transport.open(socket)
2018-01-24 22:39:38 +00:00
transport.UID = transport.UID + 1
2018-01-20 12:18:13 +00:00
2018-01-24 22:39:38 +00:00
transport.sockets[socket.sport] = socket
socket.activityTimer = os.clock()
socket.uid = transport.UID
2017-05-09 05:57:00 +00:00
end
function transport.read(socket)
2018-01-24 22:39:38 +00:00
local data = table.remove(socket.messages, 1)
if data then
return unpack(data)
end
2017-05-09 05:57:00 +00:00
end
function transport.write(socket, data)
2018-01-24 22:39:38 +00:00
socket.transmit(socket.dport, socket.dhost, data)
2019-06-29 20:35:33 +00:00
socket.wseq = SHA.digest(socket.wseq):toHex()
2017-05-09 05:57:00 +00:00
end
2018-01-07 03:25:33 +00:00
function transport.ping(socket)
2018-01-24 22:39:38 +00:00
if os.clock() - socket.activityTimer > 10 then
socket.activityTimer = os.clock()
socket.transmit(socket.dport, socket.dhost, {
type = 'PING',
seq = -1,
})
2019-06-29 20:35:33 +00:00
local timerId = os.startTimer(3)
2018-01-24 22:39:38 +00:00
transport.timers[timerId] = socket
socket.timers[-1] = timerId
end
2018-01-07 03:25:33 +00:00
end
2017-05-09 05:57:00 +00:00
function transport.close(socket)
2018-01-24 22:39:38 +00:00
transport.sockets[socket.sport] = nil
2017-05-09 05:57:00 +00:00
end
2018-01-13 20:17:26 +00:00
Event.on('timer', function(_, timerId)
2018-01-24 22:39:38 +00:00
local socket = transport.timers[timerId]
2018-01-07 03:25:33 +00:00
2018-01-24 22:39:38 +00:00
if socket and socket.connected then
print('transport timeout - closing socket ' .. socket.sport)
socket:close()
transport.timers[timerId] = nil
end
2018-01-13 20:17:26 +00:00
end)
2017-05-09 05:57:00 +00:00
2018-01-13 20:17:26 +00:00
Event.on('modem_message', function(_, _, dport, dhost, msg, distance)
2018-03-30 17:12:46 +00:00
if dhost == computerId and type(msg) == 'table' then
2018-01-24 22:39:38 +00:00
local socket = transport.sockets[dport]
if socket and socket.connected then
2018-12-02 23:40:19 +00:00
if socket.co and coroutine.status(socket.co) == 'dead' then
2019-05-03 19:30:09 +00:00
_G._syslog('socket coroutine dead')
2018-12-02 23:40:19 +00:00
socket:close()
2018-01-24 22:39:38 +00:00
2018-12-02 23:40:19 +00:00
elseif msg.type == 'DISC' then
2018-01-24 22:39:38 +00:00
-- received disconnect from other end
2019-06-29 20:35:33 +00:00
if msg.seq == socket.rseq then
if socket.connected then
os.queueEvent('transport_' .. socket.uid)
end
socket.connected = false
socket:close()
2018-01-24 22:39:38 +00:00
end
elseif msg.type == 'ACK' then
local ackTimerId = socket.timers[msg.seq]
if ackTimerId then
os.cancelTimer(ackTimerId)
socket.timers[msg.seq] = nil
socket.activityTimer = os.clock()
transport.timers[ackTimerId] = nil
end
elseif msg.type == 'PING' then
socket.activityTimer = os.clock()
socket.transmit(socket.dport, socket.dhost, {
type = 'ACK',
seq = msg.seq,
})
elseif msg.type == 'DATA' and msg.data then
if msg.seq ~= socket.rseq then
print('transport seq error - closing socket ' .. socket.sport)
2019-05-03 19:30:09 +00:00
_syslog(msg.data)
2019-06-29 20:35:33 +00:00
_syslog('expected ' .. socket.rseq)
_syslog('got ' .. msg.seq)
2018-01-24 22:39:38 +00:00
else
2019-06-29 20:35:33 +00:00
socket.activityTimer = os.clock()
socket.rseq = SHA.digest(socket.rseq):toHex()
2018-01-24 22:39:38 +00:00
table.insert(socket.messages, { msg.data, distance })
if not socket.messages[2] then -- table size is 1
os.queueEvent('transport_' .. socket.uid)
end
end
end
end
end
2018-01-13 20:17:26 +00:00
end)