-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTelnetLogAsync.h
69 lines (63 loc) · 2.43 KB
/
TelnetLogAsync.h
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
// =================================================================================================
// eModbus: Copyright 2020 by Michael Harwerth, Bert Melis and the contributors to eModbus
// MIT license - see license.md for details
// =================================================================================================
#ifndef _TELNETLOGASYNC_H
#define _TELNETLOGASYNC_H
// Include Arduino.h to make Print and Serial known
#include <Arduino.h>
#include <vector>
#include "RingBuf.h"
#ifdef ESP8266
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#elif defined(ESP32)
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#else
#error "TelnetLogAsync requires an ESP8266 or ESP32 to run."
#endif
using std::vector;
class TelnetLog : public Print {
public:
TelnetLog(uint16_t port, uint8_t maxClients, size_t rbSize = 256);
~TelnetLog();
void begin(const char *label);
void end();
inline bool isActive() { return (TL_Client.size() ? true : false); };
size_t write(uint8_t c);
size_t write(const uint8_t *buffer, size_t size);
inline unsigned int getActiveClients() { return TL_Client.size(); }
protected:
struct ClientList {
AsyncClient *client;
RingBuf<uint8_t> *buffer;
ClientList(size_t bufSize, AsyncClient *c) {
buffer = new RingBuf<uint8_t>(bufSize);
client = c;
}
~ClientList() {
if (client) {
client->close(true);
client->stop();
delete client;
}
if (buffer) delete buffer;
}
};
// Telnet definitions
uint8_t TL_maxClients; // max. number of concurrent clients allowed
AsyncServer *TL_Server; // Hook for the AsyncServerTCP
std::vector<ClientList *> TL_Client; // List of clients connected
char myLabel[64]; // Welcome label to be shown to new clients
size_t myRBsize; // Size of client'S circular buffer
static void handleNewClient(void *srv, AsyncClient *client);
static void handleDisconnect(void *srv, AsyncClient *client);
static void handlePoll(void *srv, AsyncClient *client);
static void handleAck(void *srv, AsyncClient *client, size_t len, uint32_t aTime);
static void handleData(void *srv, AsyncClient* client, void *data, size_t len);
static void sendBytes(TelnetLog *server, AsyncClient *client);
};
#endif