used new example with pipe based telnet server
This commit is contained in:
parent
eb1760cbaa
commit
fc5bd38ac1
89
telnet.lua
89
telnet.lua
@ -1,54 +1,51 @@
|
|||||||
-- Telnet client
|
-- Telnet client
|
||||||
telnetClient=nil
|
--[[ A telnet server T. Ellison, June 2019
|
||||||
local lines = {}
|
|
||||||
|
|
||||||
-- Helper function
|
This version of the telnet server demonstrates the use of the new stdin and stout
|
||||||
function s_output(str)
|
pipes, which is a C implementation of the Lua fifosock concept moved into the
|
||||||
uart.write(0, "T:" .. tostring(str))
|
Lua core. These two pipes are referenced in the Lua registry.
|
||||||
if (telnetClient ~= nil) then
|
|
||||||
if (#lines > 0) then
|
]]
|
||||||
table.insert(lines, str)
|
--luacheck: no unused args
|
||||||
else
|
|
||||||
table.insert(lines, str)
|
local telnetS = nil
|
||||||
telnetClient:send("\r") -- Send something, so the queue is read after sending
|
|
||||||
end
|
local function telnet_session(socket)
|
||||||
end
|
local node = node
|
||||||
|
local stdout
|
||||||
|
|
||||||
|
local function output_CB(opipe) -- upval: socket
|
||||||
|
stdout = opipe
|
||||||
|
local rec = opipe:read(1400)
|
||||||
|
if rec and (#rec > 0) then socket:send(rec) end
|
||||||
|
return false -- don't repost as the on:sent will do this
|
||||||
|
end
|
||||||
|
|
||||||
|
local function onsent_CB(skt) -- upval: stdout
|
||||||
|
local rec = stdout:read(1400)
|
||||||
|
if rec and #rec > 0 then skt:send(rec) end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function disconnect_CB(skt) -- upval: socket, stdout
|
||||||
|
node.output()
|
||||||
|
socket, stdout = nil, nil -- set upvals to nl to allow GC
|
||||||
|
end
|
||||||
|
|
||||||
|
node.output(output_CB, 0)
|
||||||
|
socket:on("receive", function(_,rec) node.input(rec) end)
|
||||||
|
socket:on("sent", onsent_CB)
|
||||||
|
socket:on("disconnection", disconnect_CB)
|
||||||
|
print( ("Welcome to the Wordclock. (%d mem free, %s)"):format(node.heap(), wifi.sta.getip()))
|
||||||
|
print("- mydofile(\"commands\")")
|
||||||
|
print("- storeConfig()")
|
||||||
|
print("Visite https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_en for further commands")
|
||||||
|
uart.write(0, "New client connected\r\n")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- Telnet Server
|
-- Telnet Server
|
||||||
function startTelnetServer()
|
function startTelnetServer()
|
||||||
s=net.createServer(net.TCP, 180)
|
telnetS=net.createServer(net.TCP, 180)
|
||||||
s:listen(23,function(c)
|
telnetS:listen(23, telnet_session)
|
||||||
telnetClient=c
|
|
||||||
node.output(s_output, 0)
|
|
||||||
c:on("receive",function(c,l)
|
|
||||||
node.input(l)
|
|
||||||
end)
|
|
||||||
c:on("disconnection",function(c)
|
|
||||||
node.output(nil)
|
|
||||||
telnetClient=nil
|
|
||||||
end)
|
|
||||||
c:on("sent", function()
|
|
||||||
if (#lines > 0) then
|
|
||||||
local line1=nil
|
|
||||||
for k,v in pairs(lines) do if (k==1) then
|
|
||||||
line1=v
|
|
||||||
end
|
|
||||||
uart.write(0, "Lines:" .. tostring(v) .. "\r\n")
|
|
||||||
|
|
||||||
end
|
|
||||||
if ( line1 ~= nil ) then
|
|
||||||
table.remove(lines, 1)
|
|
||||||
uart.write(0, "Telent[" .. tostring(#lines) .. "]" .. tostring(line1) .. "\r\n" )
|
|
||||||
telnetClient:send(line1)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
print("Welcome to the Wordclock.")
|
|
||||||
print("- mydofile(\"commands\")")
|
|
||||||
print("- storeConfig()")
|
|
||||||
print("Visite https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_en for further commands")
|
|
||||||
uart.write(0, "New client connected\r\n")
|
|
||||||
end)
|
|
||||||
print("Telnetserver started")
|
print("Telnetserver started")
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user