-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTelnetLogAsync.cpp
More file actions
151 lines (129 loc) · 4.21 KB
/
TelnetLogAsync.cpp
File metadata and controls
151 lines (129 loc) · 4.21 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// =================================================================================================
// eModbus: Copyright 2020 by Michael Harwerth, Bert Melis and the contributors to eModbus
// MIT license - see license.md for details
// =================================================================================================
#include "TelnetLogAsync.h"
TelnetLog::TelnetLog(uint16_t p, uint8_t mc, size_t rbSize) {
TL_maxClients = mc;
TL_Server = new AsyncServer(p);
myRBsize = rbSize;
TL_Client.clear();
TL_Server->onClient(&handleNewClient, (void *)this);
}
TelnetLog::~TelnetLog() {
delete TL_Server;
for (auto it : TL_Client) {
delete it;
}
TL_Client.clear();
std::vector<ClientList *>().swap(TL_Client);
}
void TelnetLog::begin(const char * label) {
strncpy(myLabel, label, sizeof(myLabel));
TL_Server->begin();
TL_Server->setNoDelay(true);
}
void TelnetLog::end() {
TL_Server->end();
for (auto it : TL_Client) {
delete it;
}
TL_Client.clear();
}
size_t TelnetLog::write(uint8_t c) {
// Loop over clients
for (auto cl : TL_Client) {
if (cl->client->connected()) { // } && client->canSend()) {
cl->buffer->push_back(c);
}
}
return 1;
}
size_t TelnetLog::write(const uint8_t *buffer, size_t len) {
// Loop over clients
for (auto cl : TL_Client) {
if (cl->client->connected()) { // } && client->canSend()) {
cl->buffer->push_back(buffer, len);
}
}
return len;
}
void TelnetLog::handleNewClient(void *srv, AsyncClient* newClient) {
char buffer[80];
TelnetLog *s = reinterpret_cast<TelnetLog *>(srv);
// Space left?
if (s->TL_Client.size() < s->TL_maxClients) {
// add to list
ClientList *c = new ClientList(s->myRBsize, newClient);
s->TL_Client.push_back(c);
// register events
newClient->onData(&handleData, srv);
newClient->onPoll(&handlePoll, srv);
newClient->onAck(&handleAck, srv);
newClient->onDisconnect(&handleDisconnect, srv);
snprintf(buffer, 80, "Welcome to '%s'!\n", s->myLabel);
newClient->add(buffer, strlen(buffer));
snprintf(buffer, 80, "Millis since start: %ul\n", (uint32_t)millis());
newClient->add(buffer, strlen(buffer));
snprintf(buffer, 80, "Free heap RAM: %d\n", ESP.getFreeHeap());
newClient->add(buffer, strlen(buffer));
snprintf(buffer, 80, "Server IP: %d.%d.%d.%d\n", WiFi.localIP()[0], WiFi.localIP()[1], WiFi.localIP()[2], WiFi.localIP()[3]);
newClient->add(buffer, strlen(buffer));
memset(buffer, '-', 80);
buffer[78] = '\n';
buffer[79] = 0;
newClient->add(buffer, strlen(buffer));
newClient->send();
} else {
// No, maximum number of clients reached
newClient->close(true);
newClient->stop();
delete newClient;
}
}
void TelnetLog::handleDisconnect(void *srv, AsyncClient *c) {
TelnetLog *s = reinterpret_cast<TelnetLog *>(srv);
for (auto it = s->TL_Client.begin(); it != s->TL_Client.end();) {
if ((*it)->client == c) {
delete (*it);
s->TL_Client.erase(it);
it = s->TL_Client.end();
} else {
it++;
}
}
}
void TelnetLog::handleData(void *srv, AsyncClient* client, void *data, size_t len) {
// Do nothing for now, ignore data
}
void TelnetLog::sendBytes(TelnetLog *s, AsyncClient *client) {
if (client->connected()) {
size_t numBytes = client->space();
if (numBytes) {
for (auto it : s->TL_Client) {
if (it->client == client) {
size_t numSend = it->buffer->size();
if (numSend && client->canSend()) {
if (numSend <= numBytes) {
client->write((const char *)it->buffer->data(), numSend, ASYNC_WRITE_FLAG_COPY);
it->buffer->pop(numSend);
} else {
client->write((const char *)it->buffer->data(), numBytes, ASYNC_WRITE_FLAG_COPY);
it->buffer->pop(numBytes);
}
break;
}
break;
}
}
}
}
}
void TelnetLog::handlePoll(void *srv, AsyncClient *client) {
TelnetLog *s = reinterpret_cast<TelnetLog *>(srv);
sendBytes(s, client);
}
void TelnetLog::handleAck(void *srv, AsyncClient *client, size_t len, uint32_t aTime) {
TelnetLog *s = reinterpret_cast<TelnetLog *>(srv);
sendBytes(s, client);
}