]> hydra-www.ietfng.org Git - acmetensortoys-esp-lua_core/commitdiff
fifosock: dramatically simplify
authorNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Sat, 29 Jul 2017 20:09:42 +0000 (16:09 -0400)
committerNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Mon, 31 Jul 2017 11:57:54 +0000 (07:57 -0400)
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
net/fifosock.lua
telnetd/telnetd.lua

index b2daeffb84a4806c3a960d49887d205b8ce86387..a232870253a573afc2954516a1e2e06e32907ad5 100644 (file)
--- 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
index d125894046364ba99ba62a51ccaed1f973ac7129..dee7f3a099b17b4ba248af57fea22324b2829bed 100644 (file)
@@ -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
index 504367d4711883d2922230578b8e06110ccd04da..e9fa5998fcd9f33a094ca593578b96261384bb4b 100644 (file)
@@ -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