-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from jesstr/telnetsrv
Issue #44
- Loading branch information
Showing
1 changed file
with
35 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,42 @@ | ||
-- | ||
-- setup a telnet server that hooks the sockets input | ||
-- | ||
function setupTelnetServer() | ||
inUse = false | ||
function listenFun(sock) | ||
if inUse then | ||
sock:send("Already in use.\n") | ||
sock:close() | ||
return | ||
end | ||
inUse = true | ||
-- a simple telnet server | ||
|
||
-- restart server if needed | ||
if telnet_srv ~= nil then | ||
telnet_srv:close() | ||
end | ||
telnet_srv = net.createServer(net.TCP, 180) | ||
|
||
function s_output(str) | ||
if(sock ~=nil) then | ||
sock:send(str) | ||
end | ||
telnet_srv:listen(23, 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 | ||
|
||
node.output(s_output, 0) | ||
local function s_output(str) | ||
table.insert(fifo, str) | ||
if socket ~= nil and fifo_drained then | ||
fifo_drained = false | ||
sender(socket) | ||
end | ||
end | ||
|
||
sock:on("receive",function(sock, input) | ||
node.input(input) | ||
end) | ||
node.output(s_output, 0) -- re-direct output to function s_ouput. | ||
|
||
sock:on("disconnection",function(sock) | ||
node.output(nil) | ||
inUse = false | ||
end) | ||
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) | ||
|
||
sock:send("Welcome to NodeMCU world.\n> ") | ||
end | ||
print("Welcome to NodeMCU world.") | ||
end) | ||
|
||
telnetServer = net.createServer(net.TCP, 180) | ||
telnetServer:listen(23, listenFun) | ||
end | ||
print("Telnet server running...") |