Commit 161c8555 authored by Nathaniel Wesley Filardo's avatar Nathaniel Wesley Filardo Committed by Terry Ellison
Browse files

lua_examples/telnet: small tweaks (#2455)

* Remove the old telnet.lua, which has been duplicated as
  simple_telnet.lua.

* Fix some typos and whitespace issues
parent 35a2c267
-- a simple telnet server
telnet_srv = net.createServer(net.TCP, 180)
telnet_srv:listen(2323, function(socket)
local fifo = {}
local fifo_drained = true
local function sender(c)
if #fifo > 0 then
c:send(table.remove(fifo, 1))
else
fifo_drained = true
end
end
local function s_output(str)
table.insert(fifo, str)
if socket ~= nil and fifo_drained then
fifo_drained = false
sender(socket)
end
end
node.output(s_output, 0) -- re-direct output to function s_ouput.
socket:on("receive", function(c, l)
node.input(l) -- works like pcall(loadstring(l)) but support multiple separate line
end)
socket:on("disconnection", function(c)
node.output(nil) -- un-regist the redirect output function, output goes to serial
end)
socket:on("sent", sender)
print("Welcome to NodeMCU world.")
end)
......@@ -25,8 +25,8 @@ string span multiple packets. However, you must flush the buffer if necessary.
- The overall buffering strategy needs to be reasonably memory efficient and avoid
hitting the GC too hard, so where practical avoid aggregating small strings to more
than 256 chars (as NodeMCU handles \<256 using stack buffers), and avoid serial a
ggregation such as buf = buf .. str as this hammers the GC.
than 256 chars (as NodeMCU handles \<256 using stack buffers), and avoid serial
aggregation such as buf = buf .. str as this hammers the GC.
So this server adopts a simple buffering scheme using a two level FIFO. The
`node.output` CB adds records to the 1st level FIFO until the #recs is \> 32 or the
......
......@@ -55,7 +55,7 @@ local function telnet_listener(socket)
-- debug("entering sendLine")
if not s then return end
if fifo2l + fifo1l == 0 then -- both FIFOs empty, so clear down s
if fifo2l + fifo1l == 0 then -- both FIFOs empty, so clear down s
s = nil
-- debug("Q cleared")
return
......@@ -125,13 +125,13 @@ local function telnet_listener(socket)
node.input(line)
end
local function discontect(s)
local function disconnect(s)
fifo1, fifo1l, fifo2, fifo2l, s = {}, 0, {}, 0, nil
node.output(nil)
end
socket:on("receive", receiveLine)
socket:on("disconnection", discontect)
socket:on("disconnection", disconnect)
socket:on("sent", sendLine)
node.output(queueLine, 0)
end
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment