Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.4 KiB

  1. /*
  2. TELNET MODULE
  3. Copyright (C) 2017-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #pragma once
  6. #include <memory>
  7. #include <list>
  8. #include <Schedule.h>
  9. #include <ESP8266WiFi.h>
  10. #include <ESPAsyncTCP.h>
  11. struct AsyncBufferedClient {
  12. public:
  13. constexpr static const size_t BUFFERS_MAX = 5;
  14. using buffer_t = std::vector<uint8_t>;
  15. AsyncBufferedClient(AsyncClient* client);
  16. size_t write(char c);
  17. size_t write(const char* data, size_t size=0);
  18. void flush();
  19. size_t available();
  20. bool connect(const char *host, uint16_t port);
  21. void close(bool now = false);
  22. bool connected();
  23. private:
  24. void _addBuffer();
  25. static void _trySend(AsyncBufferedClient* client);
  26. static void _s_onAck(void* client_ptr, AsyncClient*, size_t, uint32_t);
  27. static void _s_onPoll(void* client_ptr, AsyncClient* client);
  28. std::unique_ptr<AsyncClient> _client;
  29. std::list<buffer_t> _buffers;
  30. };
  31. #if TELNET_SERVER == TELNET_SERVER_WIFISERVER
  32. using TTelnetServer = WiFiServer;
  33. using TTelnetClient = WiFiClient;
  34. #elif TELNET_SERVER == TELNET_SERVER_ASYNC
  35. using TTelnetServer = AsyncServer;
  36. #if TELNET_SERVER_ASYNC_BUFFERED
  37. using TTelnetClient = AsyncBufferedClient;
  38. #else
  39. using TTelnetClient = AsyncClient;
  40. #endif // TELNET_SERVER_ASYNC_BUFFERED
  41. #endif
  42. constexpr const char TELNET_IAC = 0xFF;
  43. constexpr const char TELNET_XEOF = 0xEC;