transition to kernel
This commit is contained in:
@@ -22,6 +22,7 @@ Event.addRoutine(function()
|
||||
-- need to prevent multiple shares
|
||||
if not peripheral then
|
||||
print('peripheral: invalid peripheral ' .. uri)
|
||||
socket:write('Invalid peripheral: ' .. uri)
|
||||
else
|
||||
print('peripheral: proxing ' .. uri)
|
||||
local proxy = {
|
||||
@@ -62,6 +63,11 @@ Event.addRoutine(function()
|
||||
print('peripheral: lost connection from ' .. socket.dhost)
|
||||
break
|
||||
end
|
||||
if not _G.device[peripheral.name] then
|
||||
print('periperal: detached')
|
||||
socket:close()
|
||||
break
|
||||
end
|
||||
if peripheral[data.fn] then
|
||||
socket:write({ peripheral[data.fn](table.unpack(data.args)) })
|
||||
end
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
local Event = require('event')
|
||||
local Socket = require('socket')
|
||||
|
||||
local fs = _G.fs
|
||||
|
||||
local fileUid = 0
|
||||
local fileHandles = { }
|
||||
|
||||
@@ -46,10 +48,10 @@ local function sambaConnection(socket)
|
||||
end
|
||||
local ret
|
||||
local s, m = pcall(function()
|
||||
ret = fn(unpack(msg.args))
|
||||
ret = fn(unpack(msg.args))
|
||||
end)
|
||||
if not s and m then
|
||||
printError('samba: ' .. m)
|
||||
_G.printError('samba: ' .. m)
|
||||
end
|
||||
socket:write({ response = ret })
|
||||
end
|
||||
@@ -58,7 +60,6 @@ local function sambaConnection(socket)
|
||||
end
|
||||
|
||||
Event.addRoutine(function()
|
||||
|
||||
print('samba: listening on port 139')
|
||||
|
||||
while true do
|
||||
@@ -72,11 +73,11 @@ Event.addRoutine(function()
|
||||
end
|
||||
end)
|
||||
|
||||
Event.on('network_attach', function(e, computer)
|
||||
Event.on('network_attach', function(_, computer)
|
||||
fs.mount(fs.combine('network', computer.label), 'netfs', computer.id)
|
||||
end)
|
||||
|
||||
Event.on('network_detach', function(e, computer)
|
||||
Event.on('network_detach', function(_, computer)
|
||||
print('samba: detaching ' .. computer.label)
|
||||
fs.unmount(fs.combine('network', computer.label))
|
||||
end)
|
||||
|
||||
@@ -15,7 +15,6 @@ local gpsLastPoint
|
||||
local gpsLastRequestTime
|
||||
|
||||
local function snmpConnection(socket)
|
||||
|
||||
while true do
|
||||
local msg = socket:read()
|
||||
if not msg then
|
||||
@@ -98,7 +97,6 @@ local function snmpConnection(socket)
|
||||
end
|
||||
|
||||
Event.addRoutine(function()
|
||||
|
||||
print('snmp: listening on port 161')
|
||||
|
||||
while true do
|
||||
@@ -137,7 +135,6 @@ local info = {
|
||||
local infoTimer = os.clock()
|
||||
|
||||
local function sendInfo()
|
||||
|
||||
if os.clock() - infoTimer >= 1 then -- don't flood
|
||||
infoTimer = os.clock()
|
||||
info.label = os.getComputerLabel()
|
||||
|
||||
126
sys/network/transport.lua
Normal file
126
sys/network/transport.lua
Normal file
@@ -0,0 +1,126 @@
|
||||
--[[
|
||||
Low level socket protocol implementation.
|
||||
|
||||
* sequencing
|
||||
* background read buffering
|
||||
]]--
|
||||
|
||||
local Event = require('event')
|
||||
|
||||
local os = _G.os
|
||||
|
||||
local computerId = os.getComputerID()
|
||||
local transport = {
|
||||
timers = { },
|
||||
sockets = { },
|
||||
}
|
||||
_G.transport = transport
|
||||
|
||||
function transport.open(socket)
|
||||
transport.sockets[socket.sport] = socket
|
||||
socket.activityTimer = os.clock()
|
||||
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)
|
||||
|
||||
--local timerId = os.startTimer(3)
|
||||
|
||||
--transport.timers[timerId] = socket
|
||||
--socket.timers[socket.wseq] = timerId
|
||||
|
||||
socket.wseq = socket.wseq + 1
|
||||
end
|
||||
|
||||
function transport.ping(socket)
|
||||
--debug('>> ' .. Util.tostring({ type = 'DATA', seq = socket.wseq }))
|
||||
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
|
||||
end
|
||||
|
||||
function transport.close(socket)
|
||||
transport.sockets[socket.sport] = nil
|
||||
end
|
||||
|
||||
Event.on('timer', function(_, timerId)
|
||||
local socket = transport.timers[timerId]
|
||||
|
||||
if socket and socket.connected then
|
||||
print('transport timeout - closing socket ' .. socket.sport)
|
||||
socket:close()
|
||||
transport.timers[timerId] = nil
|
||||
end
|
||||
end)
|
||||
|
||||
Event.on('modem_message', function(_, _, dport, dhost, msg, distance)
|
||||
if dhost == computerId and msg then
|
||||
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
|
||||
socket.connected = false
|
||||
socket:close()
|
||||
os.queueEvent('transport_' .. socket.sport)
|
||||
|
||||
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
|
||||
socket.activityTimer = os.clock()
|
||||
if msg.seq ~= socket.rseq then
|
||||
print('transport seq error - closing socket ' .. socket.sport)
|
||||
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
|
||||
os.queueEvent('transport_' .. socket.sport)
|
||||
end
|
||||
|
||||
--debug('>> ' .. Util.tostring({ type = 'ACK', seq = msg.seq }))
|
||||
--socket.transmit(socket.dport, socket.dhost, {
|
||||
-- type = 'ACK',
|
||||
-- seq = msg.seq,
|
||||
--})
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
print('Net transport started')
|
||||
@@ -2,16 +2,18 @@ local Event = require('event')
|
||||
local Socket = require('socket')
|
||||
local Util = require('util')
|
||||
|
||||
local os = _G.os
|
||||
local terminal = _ENV.multishell.term
|
||||
|
||||
local function vncHost(socket)
|
||||
local methods = { 'blit', 'clear', 'clearLine', 'setCursorPos', 'write',
|
||||
'setTextColor', 'setTextColour', 'setBackgroundColor',
|
||||
'setBackgroundColour', 'scroll', 'setCursorBlink', }
|
||||
|
||||
socket.term = multishell.term
|
||||
socket.oldTerm = Util.shallowCopy(socket.term)
|
||||
local oldTerm = Util.shallowCopy(terminal)
|
||||
|
||||
for _,k in pairs(methods) do
|
||||
socket.term[k] = function(...)
|
||||
terminal[k] = function(...)
|
||||
if not socket.queue then
|
||||
socket.queue = { }
|
||||
Event.onTimeout(0, function()
|
||||
@@ -23,7 +25,7 @@ local function vncHost(socket)
|
||||
f = k,
|
||||
args = { ... },
|
||||
})
|
||||
socket.oldTerm[k](...)
|
||||
oldTerm[k](...)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -35,17 +37,17 @@ local function vncHost(socket)
|
||||
end
|
||||
|
||||
if data.type == 'shellRemote' then
|
||||
os.queueEvent(unpack(data.event))
|
||||
os.queueEvent(table.unpack(data.event))
|
||||
elseif data.type == 'termInfo' then
|
||||
socket.term.getSize = function()
|
||||
terminal.getSize = function()
|
||||
return data.width, data.height
|
||||
end
|
||||
os.queueEvent('term_resize')
|
||||
end
|
||||
end
|
||||
|
||||
for k,v in pairs(socket.oldTerm) do
|
||||
socket.term[k] = v
|
||||
for k,v in pairs(oldTerm) do
|
||||
terminal[k] = v
|
||||
end
|
||||
os.queueEvent('term_resize')
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user