33
44extern uint8_t networkGetType (uint8_t user);
55
6+ #define PVT_SERVER_MAX_CLIENTS 4
7+
68class NetworkServer : public Server
79{
810 protected:
@@ -11,7 +13,12 @@ class NetworkServer : public Server
1113 Server * _server; // Ethernet or WiFi server
1214 uint8_t _networkType;
1315 uint16_t _port;
14- Client * _client;
16+ #if defined(COMPILE_ETHERNET)
17+ EthernetClient _ethernetClient[PVT_SERVER_MAX_CLIENTS];
18+ #endif // COMPILE_ETHERNET
19+ #if defined(COMPILE_WIFI)
20+ WiFiClient _wifiClient[PVT_SERVER_MAX_CLIENTS];
21+ #endif // COMPILE_WIFI
1522
1623 public:
1724
@@ -24,22 +31,6 @@ class NetworkServer : public Server
2431 _networkType{networkType},
2532 _port{port}
2633 {
27- #if defined(COMPILE_ETHERNET)
28- if (_networkType == NETWORK_TYPE_ETHERNET)
29- {
30- _client = new EthernetClient;
31- }
32- else
33- #endif // COMPILE_ETHERNET
34- #if defined(COMPILE_WIFI)
35- {
36- _client = new WiFiClient;
37- }
38- #else // COMPILE_WIFI
39- {
40- _client = nullptr ;
41- }
42- #endif // COMPILE_WIFI
4334 }
4435
4536 // ------------------------------
@@ -64,9 +55,19 @@ class NetworkServer : public Server
6455 delete _server;
6556 _server = nullptr ;
6657 }
67- if (_client)
68- delete _client;
69- _client = nullptr ;
58+ // for (uint8_t i = 0; i < PVT_SERVER_MAX_CLIENTS; i++)
59+ // {
60+ // if (_ethernetClient[i])
61+ // {
62+ // delete _ethernetClient[i];
63+ // _ethernetClient[i] = nullptr;
64+ // }
65+ // if (_wifiClient[i])
66+ // {
67+ // delete _wifiClient[i];
68+ // _wifiClient[i] = nullptr;
69+ // }
70+ // }
7071 }
7172
7273 // ------------------------------
@@ -75,57 +76,83 @@ class NetworkServer : public Server
7576
7677 void begin ()
7778 {
78- if (_server)
79- _server->begin ();
79+ #if defined(COMPILE_ETHERNET)
80+ if (_networkType == NETWORK_TYPE_ETHERNET)
81+ if (_server)
82+ {
83+ ((EthernetServer *)_server)->begin ();
84+ }
85+ #endif // COMPILE_ETHERNET
86+ #if defined(COMPILE_WIFI)
87+ if (_networkType == NETWORK_TYPE_WIFI)
88+ if (_server)
89+ {
90+ ((WiFiServer *)_server)->begin ();
91+ }
92+ #endif // COMPILE_WIFI
8093 }
8194
8295 // ------------------------------
8396 // Determine if new client is available
8497 // ------------------------------
8598
86- Client *available ()
99+ Client *available (uint8_t index )
87100 {
101+ if (index < PVT_SERVER_MAX_CLIENTS)
102+ {
88103#if defined(COMPILE_ETHERNET)
89104 if (_networkType == NETWORK_TYPE_ETHERNET)
90105 if (_server)
91106 {
92- *_client = ((EthernetServer *)_server)->available ();
93- return _client;
107+ // if (!_ethernetClient[index])
108+ // _ethernetClient[index] = new EthernetClient;
109+ // if (!_ethernetClient[index])
110+ // return nullptr;
111+ _ethernetClient[index] = ((EthernetServer *)_server)->available ();
112+ return &_ethernetClient[index];
94113 }
95- #endif // COMPILE_WIFI
114+ #endif // COMPILE_ETHERNET
96115#if defined(COMPILE_WIFI)
97116 if (_networkType == NETWORK_TYPE_WIFI)
98117 if (_server)
99118 {
100- *_client = ((WiFiServer *)_server)->available ();
101- return _client;
119+ // if (!_wifiClient[index])
120+ // _wifiClient[index] = new WiFiClient;
121+ // if (!_wifiClient[index])
122+ // return nullptr;
123+ _wifiClient[index] = ((WiFiServer *)_server)->available ();
124+ return &_wifiClient[index];
102125 }
103126#endif // COMPILE_WIFI
127+ }
104128 return nullptr ;
105129 }
106130
107131 // ------------------------------
108132 // Accept new client
109133 // ------------------------------
110134
111- Client *accept ()
135+ Client *accept (uint8_t index )
112136 {
137+ if (index < PVT_SERVER_MAX_CLIENTS)
138+ {
113139#if defined(COMPILE_ETHERNET)
114140 if (_networkType == NETWORK_TYPE_ETHERNET)
115141 if (_server)
116142 {
117- *_client = ((EthernetServer *)_server)->accept ();
118- return _client ;
143+ _ethernetClient[index] = ((EthernetServer *)_server)->accept ();
144+ return &_ethernetClient[index] ;
119145 }
120- #endif // COMPILE_WIFI
146+ #endif // COMPILE_ETHERNET
121147#if defined(COMPILE_WIFI)
122148 if (_networkType == NETWORK_TYPE_WIFI)
123149 if (_server)
124150 {
125- *_client = ((WiFiServer *)_server)->accept ();
126- return _client ;
151+ _wifiClient[index] = ((WiFiServer *)_server)->accept ();
152+ return &_wifiClient[index] ;
127153 }
128154#endif // COMPILE_WIFI
155+ }
129156 return nullptr ;
130157 }
131158
@@ -135,7 +162,17 @@ class NetworkServer : public Server
135162
136163 operator bool ()
137164 {
138- return _server;
165+ #if defined(COMPILE_ETHERNET)
166+ if (_networkType == NETWORK_TYPE_ETHERNET)
167+ if (_server)
168+ return (*((EthernetServer *)_server));
169+ #endif // COMPILE_ETHERNET
170+ #if defined(COMPILE_WIFI)
171+ if (_networkType == NETWORK_TYPE_WIFI)
172+ if (_server)
173+ return (*((WiFiServer *)_server));
174+ #endif // COMPILE_WIFI
175+ return false ;
139176 }
140177
141178 // ------------------------------
@@ -157,8 +194,16 @@ class NetworkServer : public Server
157194
158195 size_t write (uint8_t b)
159196 {
160- if (_server)
161- return _server->write (b);
197+ #if defined(COMPILE_ETHERNET)
198+ if (_networkType == NETWORK_TYPE_ETHERNET)
199+ if (_server)
200+ return ((EthernetServer *)_server)->write (b);
201+ #endif // COMPILE_ETHERNET
202+ #if defined(COMPILE_WIFI)
203+ if (_networkType == NETWORK_TYPE_WIFI)
204+ if (_server)
205+ return ((WiFiServer *)_server)->write (b);
206+ #endif // COMPILE_WIFI
162207 return 0 ;
163208 }
164209
@@ -168,9 +213,26 @@ class NetworkServer : public Server
168213
169214 size_t write (const uint8_t *buf, size_t size)
170215 {
171- if (_server)
172- return _server->write (buf, size);
173- return 0 ;
216+ #if defined(COMPILE_ETHERNET)
217+ if (_networkType == NETWORK_TYPE_ETHERNET)
218+ if (_server)
219+ return ((EthernetServer *)_server)->write (buf, size);
220+ #endif // COMPILE_ETHERNET
221+ #if defined(COMPILE_WIFI)
222+ if (_networkType == NETWORK_TYPE_WIFI)
223+ if (_server)
224+ return ((WiFiServer *)_server)->write (buf, size);
225+ #endif // COMPILE_WIFI
226+ return 0 ;
227+ }
228+
229+ void statusreport () // EthernetServer only - and only if uncommented in EthernetServer.cpp
230+ {
231+ #if defined(COMPILE_ETHERNET)
232+ if (_networkType == NETWORK_TYPE_ETHERNET)
233+ if (_server)
234+ ((EthernetServer *)_server)->statusreport ();
235+ #endif // COMPILE_ETHERNET
174236 }
175237
176238 protected:
0 commit comments