-
Notifications
You must be signed in to change notification settings - Fork 13.3k
/
ArduinoWiFiServer.h
139 lines (117 loc) · 3.78 KB
/
ArduinoWiFiServer.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
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
/*
ArduinoWiFiServer.h - Arduino compatible WiFiServer
implementation for ESP8266Wifi library.
Copyright (c) 2020 Juraj Andrassy
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef arduinowifiserver_h
#define arduinowifiserver_h
#include <ESP8266WiFi.h>
#ifndef MAX_MONITORED_CLIENTS
#define MAX_MONITORED_CLIENTS 5
#endif
template <class TServer, class TClient>
class ArduinoCompatibleWiFiServerTemplate : public TServer, public Print {
public:
ArduinoCompatibleWiFiServerTemplate(const IPAddress& addr, uint16_t port) : TServer(addr, port) {}
ArduinoCompatibleWiFiServerTemplate(uint16_t port = 23) : TServer(port) {}
virtual ~ArduinoCompatibleWiFiServerTemplate() {}
// https://www.arduino.cc/en/Reference/WiFiServerAvailable
TClient available() {
acceptClients();
// find next client with data available
for (uint8_t i = 0; i < MAX_MONITORED_CLIENTS; i++) {
if (index == MAX_MONITORED_CLIENTS) {
index = 0;
}
TClient& client = connectedClients[index];
index++;
if (client.available())
return client;
}
return TClient(); // no client with data found
}
virtual size_t write(uint8_t b) override {
return write(&b, 1);
}
virtual size_t write(const uint8_t *buf, size_t size) override {
static uint32_t lastCheck;
uint32_t m = millis();
if (m - lastCheck > 100) {
lastCheck = m;
acceptClients();
}
if (size == 0)
return 0;
size_t ret = 0;
size_t a = size;
while (true) {
for (uint8_t i = 0; i < MAX_MONITORED_CLIENTS; i++) {
WiFiClient& client = connectedClients[i];
if (client.status() == ESTABLISHED && client.availableForWrite() < (int) a) {
a = client.availableForWrite();
}
}
if (a == 0)
break;
for (uint8_t i = 0; i < MAX_MONITORED_CLIENTS; i++) {
if (connectedClients[i].status() == ESTABLISHED) {
connectedClients[i].write(buf, a);
}
}
ret += a;
if (ret == size)
break;
buf += a;
a = size - ret;
}
return ret;
}
using Print::write;
virtual void flush() override {
flush(0);
}
virtual void flush(unsigned int maxWaitMs) {
for (uint8_t i = 0; i < MAX_MONITORED_CLIENTS; i++) {
if (connectedClients[i].status() == ESTABLISHED) {
connectedClients[i].flush(maxWaitMs);
}
}
}
operator bool() {
return (TServer::status() == LISTEN);
}
void close() {
TServer::stop();
for (uint8_t i = 0; i < MAX_MONITORED_CLIENTS; i++) {
if (connectedClients[i]) {
connectedClients[i].stop();
}
}
}
void stop() {close();}
void end() {close();}
private:
TClient connectedClients[MAX_MONITORED_CLIENTS];
uint8_t index = 0;
void acceptClients() {
for (uint8_t i = 0; i < MAX_MONITORED_CLIENTS; i++) {
if (!connectedClients[i]) {
connectedClients[i] = TServer::accept();
}
}
}
};
typedef ArduinoCompatibleWiFiServerTemplate<WiFiServer, WiFiClient> ArduinoWiFiServer;
typedef ArduinoCompatibleWiFiServerTemplate<WiFiServerSecure, WiFiClientSecure> ArduinoWiFiServerSecure;
#endif