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:
parent
0c622c600b
commit
5627ebde48
@ -5,7 +5,7 @@
|
|||||||
#include "List.h"
|
#include "List.h"
|
||||||
#include "TCPClient.h"
|
#include "TCPClient.h"
|
||||||
|
|
||||||
#define MAX_CLIENT -1
|
#define MAX_CLIENT 0
|
||||||
//#define DEBUG_TCPS
|
//#define DEBUG_TCPS
|
||||||
|
|
||||||
|
|
||||||
@ -63,6 +63,17 @@ class TCPServer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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:
|
protected:
|
||||||
virtual T* createNewClient(WiFiClient wc)
|
virtual T* createNewClient(WiFiClient wc)
|
||||||
{
|
{
|
||||||
@ -73,7 +84,7 @@ class TCPServer
|
|||||||
{
|
{
|
||||||
WiFiClient wc;
|
WiFiClient wc;
|
||||||
|
|
||||||
if(_maxClient == -1 || _maxClient > _clientList.count())
|
if(!_maxClient || _maxClient > _clientList.count())
|
||||||
{
|
{
|
||||||
wc = _wifiServer.available();
|
wc = _wifiServer.available();
|
||||||
}
|
}
|
||||||
@ -81,11 +92,15 @@ class TCPServer
|
|||||||
if(wc && wc.connected())
|
if(wc && wc.connected())
|
||||||
{
|
{
|
||||||
T *clientPointer = createNewClient(wc);
|
T *clientPointer = createNewClient(wc);
|
||||||
//Activate keepAlive
|
//We activate the TKA : (The check is internally done in the
|
||||||
(clientPointer->_client).keepAlive(15, 5, 5);
|
//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);
|
_clientList.addFirst(clientPointer);
|
||||||
#ifdef DEBUG_TCPS
|
#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());
|
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
|
#endif
|
||||||
greetClient(clientPointer);
|
greetClient(clientPointer);
|
||||||
}
|
}
|
||||||
@ -133,14 +148,14 @@ class TCPServer
|
|||||||
else if(!(_currentClient->_client).connected())
|
else if(!(_currentClient->_client).connected())
|
||||||
{
|
{
|
||||||
#ifdef DEBUG_TCPS
|
#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
|
#endif
|
||||||
_currentClient->_clientState = TCPClient::DISCARDED;
|
_currentClient->_clientState = TCPClient::DISCARDED;
|
||||||
}
|
}
|
||||||
else //Strange
|
else //Strange
|
||||||
{
|
{
|
||||||
#ifdef DEBUG_TCPS
|
#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
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -151,7 +166,7 @@ class TCPServer
|
|||||||
{
|
{
|
||||||
_currentClient->closeConnection();
|
_currentClient->closeConnection();
|
||||||
#ifdef DEBUG_TCPS
|
#ifdef DEBUG_TCPS
|
||||||
Serial.print("TCPServer : Client was discarded : ");Serial.println(_currentClient->_id);
|
Serial.printf("TCPServer : Client was discarded : %u\n", _currentClient->_id);
|
||||||
#endif
|
#endif
|
||||||
delete _currentClient;
|
delete _currentClient;
|
||||||
_currentClient = NULL;
|
_currentClient = NULL;
|
||||||
@ -169,7 +184,6 @@ class TCPServer
|
|||||||
{
|
{
|
||||||
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);
|
client->freeDataBuffer(client->_dataSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -183,15 +197,15 @@ class TCPServer
|
|||||||
if(_clientList.getRef(i)->_id == freeId)
|
if(_clientList.getRef(i)->_id == freeId)
|
||||||
{
|
{
|
||||||
freeId++;
|
freeId++;
|
||||||
i = -1;
|
i = -1; //We start from 0 again
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return freeId;
|
return freeId;
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean _serverStarted;
|
boolean _serverStarted;
|
||||||
uint8_t _maxClient;
|
uint8_t _maxClient, _TKACount = 0;
|
||||||
uint16_t _clientDataBufferSize;
|
uint16_t _clientDataBufferSize, _TKAIdleSec = 0, _TKAIntvSec = 0;
|
||||||
WiFiServer _wifiServer;
|
WiFiServer _wifiServer;
|
||||||
T *_currentClient; //current client to be processed
|
T *_currentClient; //current client to be processed
|
||||||
List<T> _clientList;
|
List<T> _clientList;
|
||||||
|
Loading…
Reference in New Issue
Block a user