Added the new enableTCPKeepAlive method which enable the feature for all new connected client after the call (this fixes a very annoying crash) and I corrected a bug with the maxClient setting

This commit is contained in:
anschrammh 2020-12-16 00:40:54 +01:00
parent 0c622c600b
commit 5627ebde48

View File

@ -5,7 +5,7 @@
#include "List.h"
#include "TCPClient.h"
#define MAX_CLIENT -1
#define MAX_CLIENT 0
//#define DEBUG_TCPS
@ -62,6 +62,17 @@ class TCPServer
_serverStarted = false;
}
}
virtual void enableTCPKeepAlive(uint16_t timeBetween2KATransmitions = TCP_DEFAULT_KEEPALIVE_IDLE_SEC,
uint16_t timeBetweenFailedKARetransmissions = TCP_DEFAULT_KEEPALIVE_INTERVAL_SEC,
uint8_t retriesBeforeDrop = TCP_DEFAULT_KEEPALIVE_COUNT)
{
_TKAIdleSec = timeBetween2KATransmitions;
_TKAIntvSec = timeBetweenFailedKARetransmissions;
_TKACount = retriesBeforeDrop;
}
virtual void disableTCPKeepAlive(){ _TKAIdleSec = 0;_TKAIntvSec = 0;_TKACount = 0; }
protected:
virtual T* createNewClient(WiFiClient wc)
@ -73,7 +84,7 @@ class TCPServer
{
WiFiClient wc;
if(_maxClient == -1 || _maxClient > _clientList.count())
if(!_maxClient || _maxClient > _clientList.count())
{
wc = _wifiServer.available();
}
@ -81,11 +92,15 @@ class TCPServer
if(wc && wc.connected())
{
T *clientPointer = createNewClient(wc);
//Activate keepAlive
(clientPointer->_client).keepAlive(15, 5, 5);
//We activate the TKA : (The check is internally done in the
//ClientContext.h class : ie if one of the provided parameters is 0, then TKA is disabled)
(clientPointer->_client).keepAlive(_TKAIdleSec, _TKAIntvSec, _TKACount);
_clientList.addFirst(clientPointer);
#ifdef DEBUG_TCPS
Serial.printf("TCPServer : New client accepted. Id : %u , Number of clients : %u, local port : %u, remote port : %u\n",clientPointer->_id, _clientList.count(),clientPointer->_client.localPort(),clientPointer->_client.remotePort());
if(_TKAIdleSec && _TKAIntvSec && _TKACount)
Serial.printf("TCPKeepAlive enabled for client id : %u\n", clientPointer->_id);
#endif
greetClient(clientPointer);
}
@ -104,7 +119,7 @@ class TCPServer
virtual void greetClient(T *client)
{
client->_client.printf_P(PSTR("Successfully connected at %lu !\r\nYour are client with id : %u\r\n"), millis(), client->_id);
client->_client.printf_P(PSTR("Successfully connected at %lu !\r\nYour are client with id : %u\r\n"), millis(), client->_id);
}
virtual void getClientData()
@ -133,14 +148,14 @@ class TCPServer
else if(!(_currentClient->_client).connected())
{
#ifdef DEBUG_TCPS
Serial.print("TCPServer : Client disconnected and can be discarded : ");Serial.println(_currentClient->_id);
Serial.printf("TCPServer : Client disconnected and can be discarded : %u\n", _currentClient->_id);
#endif
_currentClient->_clientState = TCPClient::DISCARDED;
}
else //Strange
{
#ifdef DEBUG_TCPS
Serial.printf("Client status %u : %u and available : %u Is keepAlive enabled : %d\n", _currentClient->_id, (_currentClient->_client).status(), (_currentClient->_client).available(), (_currentClient->_client).isKeepAliveEnabled());
Serial.printf("Client(%u/%u) id : %u, with TCP status : %u and available : %u, Is keepAlive enabled : %d\n", _clientList.count()+1, _maxClient, _currentClient->_id, (_currentClient->_client).status(), (_currentClient->_client).available(), (_currentClient->_client).isKeepAliveEnabled());
#endif
}
@ -151,7 +166,7 @@ class TCPServer
{
_currentClient->closeConnection();
#ifdef DEBUG_TCPS
Serial.print("TCPServer : Client was discarded : ");Serial.println(_currentClient->_id);
Serial.printf("TCPServer : Client was discarded : %u\n", _currentClient->_id);
#endif
delete _currentClient;
_currentClient = NULL;
@ -167,9 +182,8 @@ class TCPServer
{
if(client->_dataSize > 0 && ((char) client->_data[0] != '\r' && (char) client->_data[0] != '\n'))
{
Serial.printf("Client --> %u : #%s#\r\n", client->_id, (char *)client->_data);
}
Serial.printf("Client --> %u : #%s#\r\n", client->_id, (char *)client->_data);
}
client->freeDataBuffer(client->_dataSize);
}
@ -183,15 +197,15 @@ class TCPServer
if(_clientList.getRef(i)->_id == freeId)
{
freeId++;
i = -1;
i = -1; //We start from 0 again
}
}
return freeId;
}
boolean _serverStarted;
uint8_t _maxClient;
uint16_t _clientDataBufferSize;
uint8_t _maxClient, _TKACount = 0;
uint16_t _clientDataBufferSize, _TKAIdleSec = 0, _TKAIntvSec = 0;
WiFiServer _wifiServer;
T *_currentClient; //current client to be processed
List<T> _clientList;