-
Notifications
You must be signed in to change notification settings - Fork 0
/
telnet.lua
73 lines (63 loc) · 1.25 KB
/
telnet.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
--
-- setup a telnet server that hooks the sockets input
--
local client
local server
local fifo
local drained
local function sendnext(sock)
local s = table.remove(fifo, 1)
if s then
sock:send(s)
else
drained = true
end
end
local function output(s)
if client then
table.insert(fifo, s)
if drained then
drained = false
sendnext(client)
end
end
end
local function onReceive(sock, input)
node.input(input)
end
local function onDisconnect(sock)
node.output(nil)
client = nil
fifo = nil
drained = nil
end
local function listen(sock)
if client then
sock:send("Already in use.\n")
sock:close()
return
end
client = sock
fifo = {}
drained = true
node.output(output, 0)
sock:on("receive", onReceive)
sock:on("disconnection", onDisconnect)
sock:on("sent", sendnext)
sock:send("NodeMCU\n")
end
local function open(port)
server = net.createServer(net.TCP, 180)
server:listen(port or 23, listen)
end
local function close()
if client then
client:close()
client = nil
end
if server then
server:close()
server = nil
end
end
return { open = open, close = close }