2018-07-02 00:27:16 +02:00
|
|
|
--[[ A telnet server T. Ellison, May 2018
|
|
|
|
|
2019-02-17 19:26:29 +01:00
|
|
|
This version is more complex than the simple Lua example previously provided in our
|
|
|
|
distro. The main reason is that a single Lua command can produce a LOT of output,
|
2018-07-02 00:27:16 +02:00
|
|
|
and the server has to work within four constraints:
|
|
|
|
|
2019-02-17 19:26:29 +01:00
|
|
|
- The SDK rules are that you can only issue one send per task invocation, so any
|
2018-07-02 00:27:16 +02:00
|
|
|
overflow must be buffered, and the buffer emptied using an on:sent cb
|
|
|
|
|
2019-02-17 19:26:29 +01:00
|
|
|
- Since the interpeter invokes a node.output cb per field, you have a double
|
|
|
|
whammy that these fields are typically small, so using a simple array FIFO
|
2018-07-02 00:27:16 +02:00
|
|
|
would rapidly exhaust RAM.
|
|
|
|
|
|
|
|
- For network efficiency, you want to aggregate any FIFO buffered into sensible
|
|
|
|
sized packet, say 1024 bytes, but you also need to handle the case when larger
|
|
|
|
string span multiple packets. However, you must flush the buffer if necessary.
|
|
|
|
|
|
|
|
- The overall buffering strategy needs to be reasonably memory efficient and avoid
|
2019-02-17 19:26:29 +01:00
|
|
|
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
|
2018-07-02 00:27:16 +02:00
|
|
|
serial aggregation such as buf = buf .. str as this hammers the GC.
|
2019-02-17 19:26:29 +01:00
|
|
|
|
2018-07-02 00:27:16 +02:00
|
|
|
So this server adopts a simple buffering scheme using a two level FIFO. The node.output
|
|
|
|
cb adds cb records to the 1st level FIFO until the #recs is > 32 or the total size
|
|
|
|
would exceed 256 bytes. Once over this threashold, the contents of the FIFO are
|
|
|
|
concatenated into a 2nd level FIFO entry of upto 256 bytes, and the 1st level FIFO
|
|
|
|
cleared down to any residue.
|
|
|
|
|
|
|
|
]]
|
2019-02-17 19:26:29 +01:00
|
|
|
local node, table, tmr, wifi, uwrite, tostring =
|
2018-07-02 00:27:16 +02:00
|
|
|
node, table, tmr, wifi, uart.write, tostring
|
|
|
|
|
|
|
|
local function telnet_listener(socket)
|
2019-02-16 13:51:40 +01:00
|
|
|
local queueLine = (require "fifosock")(socket)
|
2018-07-02 00:27:16 +02:00
|
|
|
|
|
|
|
local function receiveLine(s, line)
|
|
|
|
node.input(line)
|
|
|
|
end
|
|
|
|
|
2018-08-27 13:46:39 +02:00
|
|
|
local function disconnect(s)
|
2019-02-16 13:51:40 +01:00
|
|
|
socket:on("disconnection", nil)
|
|
|
|
socket:on("reconnection", nil)
|
|
|
|
socket:on("connection", nil)
|
|
|
|
socket:on("receive", nil)
|
|
|
|
socket:on("sent", nil)
|
2018-07-02 00:27:16 +02:00
|
|
|
node.output(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
socket:on("receive", receiveLine)
|
2018-08-27 13:46:39 +02:00
|
|
|
socket:on("disconnection", disconnect)
|
2018-07-02 00:27:16 +02:00
|
|
|
node.output(queueLine, 0)
|
2019-02-13 06:49:45 +01:00
|
|
|
print(("Welcome to NodeMCU world (%d mem free, %s)"):format(node.heap(), wifi.sta.getip()))
|
2018-07-02 00:27:16 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local listenerSocket
|
|
|
|
return {
|
|
|
|
open = function(this, ssid, pwd, port)
|
|
|
|
if ssid then
|
|
|
|
wifi.setmode(wifi.STATION, false)
|
|
|
|
wifi.sta.config { ssid = ssid, pwd = pwd, save = false }
|
|
|
|
end
|
2019-02-13 06:49:45 +01:00
|
|
|
local t = tmr.create()
|
|
|
|
t:alarm(500, tmr.ALARM_AUTO, function()
|
2018-07-08 16:13:11 +02:00
|
|
|
if (wifi.sta.status() == wifi.STA_GOTIP) then
|
2019-02-13 06:49:45 +01:00
|
|
|
t:unregister()
|
|
|
|
t=nil
|
|
|
|
print(("Telnet server started (%d mem free, %s)"):format(node.heap(), wifi.sta.getip()))
|
|
|
|
net.createServer(net.TCP, 180):listen(port or 23, telnet_listener)
|
2018-07-02 00:27:16 +02:00
|
|
|
else
|
|
|
|
uwrite(0,".")
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end,
|
|
|
|
|
|
|
|
close = function(this)
|
|
|
|
if listenerSocket then
|
|
|
|
listenerSocket:close()
|
|
|
|
package.loaded.telnet = nil
|
|
|
|
listenerSocket = nil
|
|
|
|
collectgarbage()
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
}
|