Added the setPort method to the TCPServer, renamed _dataPort to _FTPPassiveDataPort in the FTPSErver class

This commit is contained in:
Anatole SCHRAMM 2022-10-04 13:52:43 +02:00
parent 6c53ee40e7
commit 9be8cb844a
2 changed files with 45 additions and 24 deletions

View File

@ -18,8 +18,8 @@ class FTPServer : public TCPServer<T>
enum BinaryFlag {OFF = 0, ON}; enum BinaryFlag {OFF = 0, ON};
enum FTPMsgCode {_150, _200, _215, _220, _221, _230, _226, _227, _250, _257, _331, _350, _451, _502, _504, _530, _550 }; enum FTPMsgCode {_150, _200, _215, _220, _221, _230, _226, _227, _250, _257, _331, _350, _451, _502, _504, _530, _550 };
FTPServer(uint16_t port = 21, SDClass *sdClass = NULL, const char *username = NULL, const char *password = NULL, uint8_t maxClient = MAX_CLIENT, uint16_t clientCommandDataBufferSize = 255) : TCPServer<T>(port, maxClient, clientCommandDataBufferSize), FTPServer(SDClass *sdClass = NULL, uint16_t port = 21, const char *username = NULL, const char *password = NULL, uint8_t maxClient = MAX_CLIENT, uint16_t clientCommandDataBufferSize = 255) : TCPServer<T>(port, maxClient, clientCommandDataBufferSize),
_dataServer(_dataPort), _FTPPassiveDataSocket(_FTPPassiveDataPort),
_sdClass(sdClass) _sdClass(sdClass)
{ {
if (username != NULL) if (username != NULL)
@ -43,41 +43,41 @@ class FTPServer : public TCPServer<T>
virtual ~FTPServer() virtual ~FTPServer()
{ {
_dataServer.stop(); _FTPPassiveDataSocket.stop();
free(_username); free(_password); free(_FTPDir); free(_username); free(_password); free(_FTPDir);
} }
virtual void start(void) virtual void start(void)
{ {
if(!TCPServer<T>::_serverStarted) if(!TCPServer<T>::isStarted())
{ {
_dataServer.begin(_dataPort); _FTPPassiveDataSocket.begin(_FTPPassiveDataPort);
TCPServer<T>::start(); TCPServer<T>::start();
} }
} }
virtual void stop(void) virtual void stop(void)
{ {
if(TCPServer<T>::_serverStarted) if(TCPServer<T>::isStarted())
{ {
_dataServer.stop(); _FTPPassiveDataSocket.stop();
TCPServer<T>::stop(); TCPServer<T>::stop();
} }
} }
virtual void setCustomDataPort(uint16_t port) virtual void setFTPPassiveDataPort(uint16_t port)
{ {
_dataPort = port; _FTPPassiveDataPort = port;
if(TCPServer<T>::_serverStarted) if(TCPServer<T>::isStarted())
{ {
_dataServer.stop(); _FTPPassiveDataSocket.stop();
_dataServer.begin(_dataPort); _FTPPassiveDataSocket.begin(_FTPPassiveDataPort);
} }
} }
virtual uint16_t getCustomDataPort(void) const virtual uint16_t getFTPPassiveDataPort(void) const
{ {
return _dataPort; return _FTPPassiveDataPort;
} }
virtual void setFTPDir(const char *FTPDir) virtual void setFTPDir(const char *FTPDir)
@ -169,7 +169,7 @@ class FTPServer : public TCPServer<T>
//#ifdef DEBUG_FTPS //#ifdef DEBUG_FTPS
//Serial.println("Listening for new data client"); //Serial.println("Listening for new data client");
//#endif //#endif
WiFiClient dataClient = _dataServer.available(); WiFiClient dataClient = _FTPPassiveDataSocket.available();
if(dataClient) if(dataClient)
{ {
@ -498,7 +498,7 @@ class FTPServer : public TCPServer<T>
return; return;
} }
client->_client.printf("227 Entering Passive Mode (%u,%u,%u,%u,%d,%d).\r\n", client->_client.localIP()[0], client->_client.localIP()[1], client->_client.localIP()[2], client->_client.localIP()[3], _dataPort / 256, _dataPort % 256); client->_client.printf("227 Entering Passive Mode (%u,%u,%u,%u,%d,%d).\r\n", client->_client.localIP()[0], client->_client.localIP()[1], client->_client.localIP()[2], client->_client.localIP()[3], _FTPPassiveDataPort / 256, _FTPPassiveDataPort % 256);
#ifdef DEBUG_FTPS #ifdef DEBUG_FTPS
Serial.println("Opening data server for new data client"); Serial.println("Opening data server for new data client");
#endif #endif
@ -506,7 +506,7 @@ class FTPServer : public TCPServer<T>
uint64_t timeOut(millis()); uint64_t timeOut(millis());
while(true) while(true)
{ {
WiFiClient dataClient = _dataServer.available(); WiFiClient dataClient = _FTPPassiveDataSocket.available();
//Serial.printf("Client state : %d\n", dataClient.status()); //Serial.printf("Client state : %d\n", dataClient.status());
if (dataClient) if (dataClient)
{ {
@ -1111,9 +1111,9 @@ class FTPServer : public TCPServer<T>
char *_username = nullptr; char *_username = nullptr;
char *_password = nullptr; char *_password = nullptr;
char *_FTPDir = nullptr; char *_FTPDir = nullptr;
uint16_t _dataPort = 1024; uint16_t _FTPPassiveDataPort = 1024;
WiFiServer _dataServer; //In passive mode, the FTP server opens two different ports (one for the commands and the other for the data stream) WiFiServer _FTPPassiveDataSocket; //In passive mode, the FTP server opens two different ports (one for the commands and the other for the data stream)
SDClass *_sdClass; SDClass *_sdClass;
}; };

View File

@ -13,7 +13,7 @@ template <typename T>
class TCPServer class TCPServer
{ {
public: public:
TCPServer(uint16_t port = 80, uint8_t maxClient = MAX_CLIENT, uint16_t clientDataBufferSize = 256) : _maxClient(maxClient), _clientDataBufferSize(clientDataBufferSize), _wifiServer(port) TCPServer(uint16_t port, uint8_t maxClient = MAX_CLIENT, uint16_t clientDataBufferSize = 256) : _maxClient(maxClient), _clientDataBufferSize(clientDataBufferSize), _port(port), _wifiServer(_port)
{ {
} }
@ -22,15 +22,30 @@ class TCPServer
{ {
_clientList.dispose(); _clientList.dispose();
} }
void setMaxClient(uint8_t maxClient)
{
_maxClient = maxClient;
}
uint8_t getMaxClient() uint8_t getMaxClient() const
{ {
return _maxClient; return _maxClient;
} }
void setPort(uint16_t port)
{
_port = port;
if(isStarted())
{
stop();
start();
}
}
uint16_t getPort() const uint16_t getPort() const
{ {
return _wifiServer.port(); return _port;
} }
uint8_t getConnectedClientsCount() uint8_t getConnectedClientsCount()
@ -48,7 +63,7 @@ class TCPServer
{ {
if(!_serverStarted) if(!_serverStarted)
{ {
!port ? _wifiServer.begin(getPort()) : _wifiServer.begin(port, backlog) ; !port ? _wifiServer.begin(_port) : _wifiServer.begin(port, backlog), _port = port ;
_serverStarted = true; _serverStarted = true;
} }
} }
@ -59,10 +74,16 @@ class TCPServer
{ {
_wifiServer.stop(); _wifiServer.stop();
_clientList.dispose(); _clientList.dispose();
_currentClient = nullptr;
_serverStarted = false; _serverStarted = false;
} }
} }
virtual boolean isStarted(void) const
{
return _serverStarted;
}
virtual void enableTCPKeepAlive(uint16_t timeBetween2KATransmitions = TCP_DEFAULT_KEEPALIVE_IDLE_SEC, virtual void enableTCPKeepAlive(uint16_t timeBetween2KATransmitions = TCP_DEFAULT_KEEPALIVE_IDLE_SEC,
uint16_t timeBetweenFailedKARetransmissions = TCP_DEFAULT_KEEPALIVE_INTERVAL_SEC, uint16_t timeBetweenFailedKARetransmissions = TCP_DEFAULT_KEEPALIVE_INTERVAL_SEC,
uint8_t retriesBeforeDrop = TCP_DEFAULT_KEEPALIVE_COUNT) uint8_t retriesBeforeDrop = TCP_DEFAULT_KEEPALIVE_COUNT)
@ -208,7 +229,7 @@ class TCPServer
boolean _serverStarted = false; boolean _serverStarted = false;
uint8_t _maxClient, _TKACount = 0; uint8_t _maxClient, _TKACount = 0;
uint16_t _clientDataBufferSize, _TKAIdleSec = 0, _TKAIntvSec = 0; uint16_t _clientDataBufferSize, _TKAIdleSec = 0, _TKAIntvSec = 0, _port;
WiFiServer _wifiServer; WiFiServer _wifiServer;
T *_currentClient = nullptr; //current client to be processed T *_currentClient = nullptr; //current client to be processed
List<T> _clientList; List<T> _clientList;