From 56ecb85d9c6ad9df2b572aeba87be9fe2dcafd6d Mon Sep 17 00:00:00 2001 From: Nathaniel Wesley Filardo Date: Sat, 29 Jul 2017 16:09:42 -0400 Subject: [PATCH] fifosock: dramatically simplify All we really need here is a thin shim around send; stop trying to be clever, it just makes things obfucsated when they go wrong. Chase consequences around the tree. Extensive testing was done looking for memory leaks despite the newfound simplicity, with none found. --- init.lua | 9 +++++---- net/fifosock.lua | 20 ++------------------ telnetd/telnetd.lua | 26 ++++++++++++++++---------- 3 files changed, 23 insertions(+), 32 deletions(-) diff --git a/init.lua b/init.lua index b2daeff..a232870 100644 --- a/init.lua +++ b/init.lua @@ -12,11 +12,12 @@ local function goab() dofile("diag.lc") dofile("nwfnet-go.lc") tcpserv = net.createServer(net.TCP, 180) - tcpserv:listen(23,function(k) + tcpserv:listen(23,function(k) local telnetd = dofile "telnetd.lc" - telnetd.on["conn"] = function(k) - tmr.unregister(6) - k:send(string.format("NODE-%06X RECOVERY (auto reboot cancelled)",node.chipid())) end + telnetd.on["conn"] = function(s) + tmr.unregister(6) + s(string.format("NODE-%06X RECOVERY (auto reboot cancelled)",node.chipid())) + end telnetd.server(k) end) end diff --git a/net/fifosock.lua b/net/fifosock.lua index d125894..dee7f3a 100644 --- a/net/fifosock.lua +++ b/net/fifosock.lua @@ -1,26 +1,10 @@ --- STATELESS --- --- Wrap a socket object so that it queues sends. Must call :fini after --- done to avoid a memory leak (that I don't understand in full) --- --- Ideally, import this once, wrap all the sockets you need, and then forget --- it. If one needs new sockets periodically, it is unclear to me whether --- it's better to load this once and hold it in RAM or to load it every --- time. +-- Wrap a fifo around a socket's send return function(fifo,sock) local function dosend(s) sock:send(s) end sock:on("sent", function() fifo:dequeue(dosend) end) - local nsock = {} - function nsock.send(_,s) + return function(s) if s == nil or s == "" then return end fifo:queue(s,dosend) end - function nsock.fini(_) sock = nil end - local sockit = getmetatable(sock)["__index"] - setmetatable(nsock,{ __index = function(_,k) - local fn = sockit[k] - return function(a,...) if a == nsock then fn(sock,...) else fn(a,...) end end - end }) - return nsock end diff --git a/telnetd/telnetd.lua b/telnetd/telnetd.lua index 504367d..e9fa599 100644 --- a/telnetd/telnetd.lua +++ b/telnetd/telnetd.lua @@ -19,17 +19,23 @@ function self.rx(tx,input,k) if rt ~= nil then self.tryin(r,rt(),function(c2) tx(c.." "..c2.."?") end, function() tx(c.." ??") end,tx) else tx(c.."?") - end end end, - function(_) tx("?") end,tx) - k(true) + end + k(true) + end + end, + function(_) tx("?") k(true) end,tx) end function self.server(sock_) - local sock = (dofile("fifosock.lc"))((require "fifo")(), sock_) - local dosend = function(...) sock:send(...) end - local k = function(c) if c then sock:send("\n$ ") else sock:close() end end - sock:on("receive",function(s_,input) self.rx(dosend,input,k) end) - sock:on("disconnection",function(s_) tryon("disconn",sock) ; sock:fini() end) - tryon("conn",sock) - sock:send("\n$ ") + local fsend = (dofile("fifosock.lc"))((require "fifo")(), sock_) + local function teardown(rawsock) + rawsock:on("sent", nil) + rawsock:on("receive", nil) + rawsock:on("disconnection", nil) + tryon("disconn",fsend) + end + sock_:on("receive",function(s_,input) self.rx(fsend,input,function(c) if c then fsend("\n$ ") else s_:close() teardown(s_) end end) end) + sock_:on("disconnection",function(s_, x) teardown(s_) end) + tryon("conn",fsend) + fsend("\n$ ") end return self -- 2.50.1