|
| 1 | +/* |
| 2 | + ArduinoWiFiServer.h - Arduino compatible WiFiServer |
| 3 | + implementation for ESP8266Wifi library. |
| 4 | + Copyright (c) 2020 Juraj Andrassy |
| 5 | +
|
| 6 | + This library is free software; you can redistribute it and/or |
| 7 | + modify it under the terms of the GNU Lesser General Public |
| 8 | + License as published by the Free Software Foundation; either |
| 9 | + version 2.1 of the License, or (at your option) any later version. |
| 10 | +
|
| 11 | + This library is distributed in the hope that it will be useful, |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + Lesser General Public License for more details. |
| 15 | +
|
| 16 | + You should have received a copy of the GNU Lesser General Public |
| 17 | + License along with this library; if not, write to the Free Software |
| 18 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | +*/ |
| 20 | + |
| 21 | +#ifndef arduinowifiserver_h |
| 22 | +#define arduinowifiserver_h |
| 23 | + |
| 24 | +#include <ESP8266WiFi.h> |
| 25 | + |
| 26 | +#ifndef MAX_MONITORED_CLIENTS |
| 27 | +#define MAX_MONITORED_CLIENTS 5 |
| 28 | +#endif |
| 29 | + |
| 30 | +template <class TServer, class TClient> |
| 31 | +class ArduinoComptibleWiFiServerTemplate : public TServer { |
| 32 | +public: |
| 33 | + |
| 34 | + ArduinoComptibleWiFiServerTemplate(const IPAddress& addr, uint16_t port) : TServer(addr, port) {} |
| 35 | + ArduinoComptibleWiFiServerTemplate(uint16_t port) : TServer(port) {} |
| 36 | + virtual ~ArduinoComptibleWiFiServerTemplate() {} |
| 37 | + |
| 38 | + // https://www.arduino.cc/en/Reference/EthernetServerAccept |
| 39 | + TClient accept() { |
| 40 | + return TServer::available(); |
| 41 | + } |
| 42 | + |
| 43 | + // https://www.arduino.cc/en/Reference/WiFiServerAvailable |
| 44 | + TClient available() { |
| 45 | + |
| 46 | + // update connected clients |
| 47 | + for (uint8_t i = 0; i < MAX_MONITORED_CLIENTS; i++) { |
| 48 | + if (!connectedClients[i]) { |
| 49 | + connectedClients[i] = accept(); |
| 50 | + } |
| 51 | + } |
| 52 | + // find next client with data available |
| 53 | + for (uint8_t i = 0; i < MAX_MONITORED_CLIENTS; i++) { |
| 54 | + if (index == MAX_MONITORED_CLIENTS) { |
| 55 | + index = 0; |
| 56 | + } |
| 57 | + TClient& client = connectedClients[index]; |
| 58 | + index++; |
| 59 | + if (client.available()) |
| 60 | + return client; |
| 61 | + } |
| 62 | + return TClient(); // no client with data found |
| 63 | + } |
| 64 | + |
| 65 | + virtual size_t write(uint8_t b) override { |
| 66 | + return write(&b, 1); |
| 67 | + } |
| 68 | + |
| 69 | + virtual size_t write(const uint8_t *buf, size_t size) override { |
| 70 | + if (size == 0) |
| 71 | + return 0; |
| 72 | + size_t ret = 0; |
| 73 | + size_t a = size; |
| 74 | + while (true) { |
| 75 | + for (uint8_t i = 0; i < MAX_MONITORED_CLIENTS; i++) { |
| 76 | + WiFiClient& client = connectedClients[i]; |
| 77 | + if (client.status() == ESTABLISHED && client.availableForWrite() < (int) a) { |
| 78 | + a = client.availableForWrite(); |
| 79 | + } |
| 80 | + } |
| 81 | + if (a == 0) |
| 82 | + break; |
| 83 | + for (uint8_t i = 0; i < MAX_MONITORED_CLIENTS; i++) { |
| 84 | + if (connectedClients[i].status() == ESTABLISHED) { |
| 85 | + connectedClients[i].write(buf, a); |
| 86 | + } |
| 87 | + } |
| 88 | + ret += a; |
| 89 | + if (ret == size) |
| 90 | + break; |
| 91 | + buf += a; |
| 92 | + a = size - ret; |
| 93 | + } |
| 94 | + return ret; |
| 95 | + } |
| 96 | + |
| 97 | + using Print::write; |
| 98 | + |
| 99 | + virtual void flush() override { |
| 100 | + flush(0); |
| 101 | + } |
| 102 | + |
| 103 | + virtual void flush(unsigned int maxWaitMs) { |
| 104 | + for (uint8_t i = 0; i < MAX_MONITORED_CLIENTS; i++) { |
| 105 | + if (connectedClients[i].status() == ESTABLISHED) { |
| 106 | + connectedClients[i].flush(maxWaitMs); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + operator bool() { |
| 112 | + return (TServer::status() == LISTEN); |
| 113 | + } |
| 114 | + |
| 115 | + void close() { |
| 116 | + TServer::stop(); |
| 117 | + for (uint8_t i = 0; i < MAX_MONITORED_CLIENTS; i++) { |
| 118 | + if (connectedClients[i]) { |
| 119 | + connectedClients[i].stop(); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + void stop() {close();} |
| 124 | + void end() {close();} |
| 125 | + |
| 126 | +private: |
| 127 | + TClient connectedClients[MAX_MONITORED_CLIENTS]; |
| 128 | + uint8_t index = 0; |
| 129 | + |
| 130 | +}; |
| 131 | + |
| 132 | +typedef ArduinoComptibleWiFiServerTemplate<WiFiServer, WiFiClient> ArduinoWiFiServer; |
| 133 | +typedef ArduinoComptibleWiFiServerTemplate<WiFiServerSecure, WiFiClientSecure> ArduinoWiFiServerSecure; |
| 134 | + |
| 135 | +#endif |
0 commit comments