nodemcu-firmware/lua_examples/telnet/README.md

80 lines
3.1 KiB
Markdown
Raw Normal View History

2018-07-02 00:27:16 +02:00
# Telnet Module
| Since | Origin / Contributor | Maintainer | Source |
| :----- | :-------------------- | :---------- | :------ |
| 2014-12-22 | [Zeroday](https://github.com/funshine) | [Terry Ellison](https://github.com/TerryE) | [simple_telnet.lua](./simple_telnet.lua) |
| 2018-05-24 | [Terry Ellison](https://github.com/TerryE) | [Terry Ellison](https://github.com/TerryE) | [telnet.lua](./telnet.lua) |
2019-02-17 19:26:29 +01:00
The Lua telnet example previously provided in our distro has been moved to this
file `simple_telnet.lua` in this folder. This README discusses the version complex
2018-07-02 00:27:16 +02:00
implementation at the Lua module `telnet.lua`. The main reason for this complex
2019-02-17 19:26:29 +01:00
alternative is that a single Lua command can produce a LOT of output, and the
2018-07-02 00:27:16 +02:00
telnet 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 callback (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 would rapidly
2018-07-02 00:27:16 +02:00
exhaust RAM.
- For network efficiency, the module aggregates any FIFO buffered into sensible
sized packet, say 1024 bytes, but it must 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 serial
aggregation such as buf = buf .. str as this hammers the GC.
2019-02-17 19:26:29 +01:00
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
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
2018-07-02 00:27:16 +02:00
level FIFO cleared down to any residue.
The sender dumps the 2nd level FIFO aggregating records up to 1024 bytes and once this
is empty dumps an aggrate of the 1st level.
Lastly remember that owing to architectural limitations of the firmware, this server
can only service stdin and stdout. Lua errors are still sent to stderr which is
2019-02-17 19:26:29 +01:00
the UART0 device. Hence errors will fail silently. If you want to capture
2018-07-02 00:27:16 +02:00
errors then you will need to wrap any commands in a `pcall()` and print any
error return.
## telnet:open()
Open a telnet server based on the provided parameters.
#### Syntax
`telnet:open(ssid, pwd, port)`
#### Parameters
2019-02-17 19:26:29 +01:00
`ssid` and `password`. Strings. SSID and Password for the Wifi network. If these are
2018-07-02 00:27:16 +02:00
`nil` then the wifi is assumed to be configured or autoconfigured.
`port`. Integer TCP listenting port for the Telnet service. The default is 2323
2019-02-17 19:26:29 +01:00
2018-07-02 00:27:16 +02:00
#### Returns
Nothing returned (this is evaluted as `nil` in a scalar context).
## telnet:close()
Close a telnet server and release all resources.
#### Syntax
`telnet:close()`
#### Parameters
None
#### Returns
Nothing returned (this is evaluted as `nil` in a scalar context).