Skip to content

Commit 11de3d3

Browse files
committed
Instantiate the clients inside the server - working on Ethernet!
1 parent 4ad577e commit 11de3d3

File tree

3 files changed

+163
-76
lines changed

3 files changed

+163
-76
lines changed

Firmware/RTK_Surveyor/Network.ino

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,19 +450,16 @@ NetworkServer::NetworkServer(uint8_t user, uint16_t port) :
450450
if (_networkType == NETWORK_TYPE_ETHERNET)
451451
{
452452
_server = new EthernetServer(port);
453-
_client = new EthernetClient;
454453
}
455454
else
456455
#endif // COMPILE_ETHERNET
457456
#if defined(COMPILE_WIFI)
458457
{
459458
_server = new WiFiServer(port);
460-
_client = new WiFiClient;
461459
}
462460
#else // COMPILE_WIFI
463461
{
464462
_server = nullptr;
465-
_client = nullptr;
466463
}
467464
#endif // COMPILE_WIFI
468465
}

Firmware/RTK_Surveyor/NetworkServer.h

Lines changed: 102 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
extern uint8_t networkGetType(uint8_t user);
55

6+
#define PVT_SERVER_MAX_CLIENTS 4
7+
68
class 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:

Firmware/RTK_Surveyor/PvtServer.ino

Lines changed: 61 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ pvtServer.ino
3737
// Constants
3838
//----------------------------------------
3939

40-
#define PVT_SERVER_MAX_CLIENTS 4
4140
#define PVT_SERVER_CLIENT_DATA_TIMEOUT (15 * 1000)
4241

4342
// Define the PVT server states
@@ -312,31 +311,37 @@ void pvtServerStopClient(int index)
312311
bool connected;
313312
bool dataSent;
314313

315-
// Done with this client connection
316-
if ((settings.debugPvtServer || PERIODIC_DISPLAY(PD_PVT_SERVER_DATA)) && (!inMainMenu))
314+
// Determine if a client was allocated
315+
if (pvtServerClient[index])
317316
{
318-
PERIODIC_CLEAR(PD_PVT_SERVER_DATA);
319-
320-
// Determine the shutdown reason
321-
connected = pvtServerClient[index]->connected()
322-
&& (!(pvtServerClientWriteError & (1 << index)));
323-
dataSent = ((millis() - pvtServerTimer) < PVT_SERVER_CLIENT_DATA_TIMEOUT)
324-
|| (pvtServerClientDataSent & (1 << index));
325-
if (!dataSent)
326-
systemPrintf("PVT Server: No data sent over %d seconds\r\n",
327-
PVT_SERVER_CLIENT_DATA_TIMEOUT / 1000);
328-
if (!connected)
329-
systemPrintf("PVT Server: Link to client broken\r\n");
330-
systemPrintf("PVT server client %d disconnected from %d.%d.%d.%d\r\n",
331-
index,
332-
pvtServerClientIpAddress[index][0],
333-
pvtServerClientIpAddress[index][1],
334-
pvtServerClientIpAddress[index][2],
335-
pvtServerClientIpAddress[index][3]);
336-
}
317+
// Done with this client connection
318+
if ((settings.debugPvtServer || PERIODIC_DISPLAY(PD_PVT_SERVER_DATA)) && (!inMainMenu))
319+
{
320+
//PERIODIC_CLEAR(PD_PVT_SERVER_DATA);
321+
322+
// Determine the shutdown reason
323+
connected = pvtServerClient[index]->connected()
324+
&& (!(pvtServerClientWriteError & (1 << index)));
325+
dataSent = ((millis() - pvtServerTimer) < PVT_SERVER_CLIENT_DATA_TIMEOUT)
326+
|| (pvtServerClientDataSent & (1 << index));
327+
if (!dataSent)
328+
systemPrintf("PVT Server: No data sent over %d seconds\r\n",
329+
PVT_SERVER_CLIENT_DATA_TIMEOUT / 1000);
330+
if (!connected)
331+
systemPrintf("PVT Server: Link to client broken\r\n");
332+
systemPrintf("PVT server client %d disconnected from %d.%d.%d.%d\r\n",
333+
index,
334+
pvtServerClientIpAddress[index][0],
335+
pvtServerClientIpAddress[index][1],
336+
pvtServerClientIpAddress[index][2],
337+
pvtServerClientIpAddress[index][3]);
338+
}
337339

338-
// Shutdown the PVT server client link
339-
pvtServerClient[index]->stop();
340+
// Shutdown the PVT server client link
341+
pvtServerClient[index]->stop();
342+
delete pvtServerClient[index];
343+
pvtServerClient[index] = nullptr;
344+
}
340345
pvtServerClientConnected &= ~(1 << index);
341346
pvtServerClientWriteError &= ~(1 << index);
342347
}
@@ -441,7 +446,7 @@ void pvtServerUpdate()
441446
// Walk the list of PVT server clients
442447
for (index = 0; index < PVT_SERVER_MAX_CLIENTS; index++)
443448
{
444-
// Determine if the client data structure is in use
449+
// Determine if the client data structure is still in use
445450
if (pvtServerClientConnected & (1 << index))
446451
{
447452
// Data structure in use
@@ -473,30 +478,53 @@ void pvtServerUpdate()
473478
// Walk the list of PVT server clients
474479
for (index = 0; index < PVT_SERVER_MAX_CLIENTS; index++)
475480
{
476-
// Determine if the client data structure is in use
481+
// Determine if the client data structure is not in use
477482
if (!(pvtServerClientConnected & (1 << index)))
478483
{
479484
// Data structure not in use
485+
NETWORK_DATA * network;
486+
network = &networkData;
487+
if (!network)
488+
break;
489+
480490
// Check for another PVT server client
481-
Client *client = pvtServer->available();
491+
// Use accept, not available:
492+
// Ethernet accept will return the connected client even if no data received
493+
// Ethernet available expects the client to send data first
494+
// The client instances are stored within the NetworkServer instance
495+
Client *client = pvtServer->accept(index);
482496

483497
// Done if no PVT server client found
484-
if (!client)
498+
if (!*client)
485499
break;
486500

487-
NETWORK_DATA * network;
488-
network = &networkData;
489-
if (!network)
501+
// Check if the data structure has been initialized
502+
if (pvtServerClient[index] == nullptr)
503+
{
504+
pvtServerClient[index] = new NetworkClient(client, network->type);
505+
506+
// Check for allocation failure
507+
if(pvtServerClient[index] == nullptr)
508+
{
509+
if (settings.debugPvtServer)
510+
Serial.printf("ERROR: Failed to allocate PVT server client %d!\r\n", index);
511+
break;
512+
}
513+
}
514+
else
515+
{
516+
// This should never happen...
517+
Serial.printf("ERROR: pvtServerClient[%d] already exists!\r\n", index);
490518
break;
519+
}
491520

492521
// Start processing the new PVT server client connection
493-
pvtServerClient[index] = new NetworkClient(client, network->type);
494522
pvtServerClientIpAddress[index] = pvtServerClient[index]->remoteIP();
495523
pvtServerClientConnected |= 1 << index;
496524
pvtServerClientDataSent |= 1 << index;
497525
if ((settings.debugPvtServer || PERIODIC_DISPLAY(PD_PVT_SERVER_DATA)) && (!inMainMenu))
498526
{
499-
PERIODIC_CLEAR(PD_PVT_SERVER_DATA);
527+
PERIODIC_CLEAR(PD_PVT_SERVER_DATA); // This will only print the first client...
500528
systemPrintf("PVT server client %d connected to %d.%d.%d.%d\r\n",
501529
index,
502530
pvtServerClientIpAddress[index][0],

0 commit comments

Comments
 (0)