2017-05-09 05:57:00 +00:00
|
|
|
--[[
|
|
|
|
Low level socket protocol implementation.
|
|
|
|
|
|
|
|
* sequencing
|
|
|
|
* background read buffering
|
|
|
|
]]--
|
|
|
|
|
2018-01-13 20:17:26 +00:00
|
|
|
local Event = require('event')
|
2017-10-25 03:01:40 +00:00
|
|
|
|
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 = {
|
2017-05-09 05:57:00 +00:00
|
|
|
timers = { },
|
|
|
|
sockets = { },
|
2018-01-20 12:18:13 +00:00
|
|
|
UID = 0,
|
2017-05-09 05:57:00 +00:00
|
|
|
}
|
2017-10-25 03:01:40 +00:00
|
|
|
_G.transport = transport
|
2017-05-09 05:57:00 +00:00
|
|
|
|
|
|
|
function transport.open(socket)
|
2018-01-20 12:18:13 +00:00
|
|
|
transport.UID = transport.UID + 1
|
|
|
|
|
2017-05-09 05:57:00 +00:00
|
|
|
transport.sockets[socket.sport] = socket
|
2018-01-10 21:46:37 +00:00
|
|
|
socket.activityTimer = os.clock()
|
2018-01-20 12:18:13 +00:00
|
|
|
socket.uid = transport.UID
|
2017-05-09 05:57:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function transport.read(socket)
|
|
|
|
local data = table.remove(socket.messages, 1)
|
|
|
|
if data then
|
|
|
|
return unpack(data)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function transport.write(socket, data)
|
|
|
|
--debug('>> ' .. Util.tostring({ type = 'DATA', seq = socket.wseq }))
|
|
|
|
socket.transmit(socket.dport, socket.dhost, data)
|
|
|
|
|
2018-01-10 21:46:37 +00:00
|
|
|
--local timerId = os.startTimer(3)
|
2017-05-09 05:57:00 +00:00
|
|
|
|
2018-01-10 21:46:37 +00:00
|
|
|
--transport.timers[timerId] = socket
|
|
|
|
--socket.timers[socket.wseq] = timerId
|
2017-05-09 05:57:00 +00:00
|
|
|
|
|
|
|
socket.wseq = socket.wseq + 1
|
|
|
|
end
|
|
|
|
|
2018-01-07 03:25:33 +00:00
|
|
|
function transport.ping(socket)
|
|
|
|
--debug('>> ' .. Util.tostring({ type = 'DATA', seq = socket.wseq }))
|
2018-01-10 21:46:37 +00:00
|
|
|
if os.clock() - socket.activityTimer > 10 then
|
|
|
|
socket.activityTimer = os.clock()
|
|
|
|
socket.transmit(socket.dport, socket.dhost, {
|
|
|
|
type = 'PING',
|
|
|
|
seq = -1,
|
|
|
|
})
|
|
|
|
|
|
|
|
local timerId = os.startTimer(3)
|
|
|
|
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)
|
|
|
|
transport.sockets[socket.sport] = nil
|
|
|
|
end
|
|
|
|
|
2018-01-13 20:17:26 +00:00
|
|
|
Event.on('timer', function(_, timerId)
|
|
|
|
local socket = transport.timers[timerId]
|
2018-01-07 03:25:33 +00:00
|
|
|
|
2018-01-13 20:17:26 +00:00
|
|
|
if socket and socket.connected then
|
|
|
|
print('transport timeout - closing socket ' .. socket.sport)
|
|
|
|
socket:close()
|
|
|
|
transport.timers[timerId] = nil
|
|
|
|
end
|
|
|
|
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)
|
|
|
|
if dhost == computerId and msg then
|
2017-05-09 05:57:00 +00:00
|
|
|
local socket = transport.sockets[dport]
|
|
|
|
if socket and socket.connected then
|
|
|
|
|
|
|
|
--if msg.type then debug('<< ' .. Util.tostring(msg)) end
|
|
|
|
|
|
|
|
if msg.type == 'DISC' then
|
|
|
|
-- received disconnect from other end
|
2018-01-20 12:18:13 +00:00
|
|
|
if socket.connected then
|
|
|
|
os.queueEvent('transport_' .. socket.uid)
|
|
|
|
end
|
2017-05-09 05:57:00 +00:00
|
|
|
socket.connected = false
|
|
|
|
socket:close()
|
|
|
|
|
|
|
|
elseif msg.type == 'ACK' then
|
2018-01-07 03:25:33 +00:00
|
|
|
local ackTimerId = socket.timers[msg.seq]
|
|
|
|
if ackTimerId then
|
|
|
|
os.cancelTimer(ackTimerId)
|
|
|
|
socket.timers[msg.seq] = nil
|
2018-01-10 21:46:37 +00:00
|
|
|
socket.activityTimer = os.clock()
|
2018-01-07 03:25:33 +00:00
|
|
|
transport.timers[ackTimerId] = nil
|
|
|
|
end
|
2017-05-09 05:57:00 +00:00
|
|
|
|
2017-05-10 10:11:25 +00:00
|
|
|
elseif msg.type == 'PING' then
|
2018-01-10 21:46:37 +00:00
|
|
|
socket.activityTimer = os.clock()
|
2017-05-10 10:11:25 +00:00
|
|
|
socket.transmit(socket.dport, socket.dhost, {
|
|
|
|
type = 'ACK',
|
|
|
|
seq = msg.seq,
|
|
|
|
})
|
|
|
|
|
2017-05-09 05:57:00 +00:00
|
|
|
elseif msg.type == 'DATA' and msg.data then
|
2018-01-10 21:46:37 +00:00
|
|
|
socket.activityTimer = os.clock()
|
2017-05-09 05:57:00 +00:00
|
|
|
if msg.seq ~= socket.rseq then
|
2018-01-13 20:17:26 +00:00
|
|
|
print('transport seq error - closing socket ' .. socket.sport)
|
2017-05-09 05:57:00 +00:00
|
|
|
socket:close()
|
|
|
|
else
|
|
|
|
socket.rseq = socket.rseq + 1
|
|
|
|
table.insert(socket.messages, { msg.data, distance })
|
|
|
|
|
|
|
|
-- use resume instead ??
|
|
|
|
if not socket.messages[2] then -- table size is 1
|
2018-01-20 12:18:13 +00:00
|
|
|
os.queueEvent('transport_' .. socket.uid)
|
2017-05-09 05:57:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
--debug('>> ' .. Util.tostring({ type = 'ACK', seq = msg.seq }))
|
2018-01-10 21:46:37 +00:00
|
|
|
--socket.transmit(socket.dport, socket.dhost, {
|
|
|
|
-- type = 'ACK',
|
|
|
|
-- seq = msg.seq,
|
|
|
|
--})
|
2017-05-09 05:57:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-01-13 20:17:26 +00:00
|
|
|
end)
|