Skip to content

Commit

Permalink
Added easy setting of maxSessions.
Browse files Browse the repository at this point in the history
  • Loading branch information
yasheena authored Aug 4, 2022
1 parent 90744f8 commit 70c6472
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 15 deletions.
34 changes: 25 additions & 9 deletions FtpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
* transfers (transfer queue).
*
* On default in FtpServerKey.h the define FTP_MAX_SESSIONS is set to 2
* for two concurrent FTP connections.
* for two concurrent FTP connections. But you can also use another value
* by using the 3rd parameter of the FtpServer constructor, which I added
* in MultiFTPServer (compared to SimpleFTPServer). A single new method
* added for the MultiFTPServer is getMaxSessions() to get the actual
* number of concurrently useable FTP sessions.
*
**************************************************************************/

Expand Down Expand Up @@ -73,29 +77,37 @@ const char * FtpServer::pass;
const char * FtpServer::welcomeMessage;
uint16_t FtpServer::cmdPort;
uint16_t FtpServer::pasvPort;
FtpServer* FtpServer::sessions[FTP_MAX_SESSIONS] = {};
uint8_t FtpServer::maxSessions = 0;
FtpServer** FtpServer::sessions = NULL;

FtpServer::FtpServer( uint16_t _cmdPort, uint16_t _pasvPort )
FtpServer::FtpServer( uint16_t _cmdPort, uint16_t _pasvPort, uint8_t _maxSessions )
{
millisDelay = 0;
nbMatch = 0;
iCL = 0;

iniVariables();

if (!!FtpServer::sessions[0]) {
if (FtpServer::maxSessions > 0) {
return;
}

FtpServer::sessions = (FtpServer**) malloc(_maxSessions * sizeof(FtpServer*));
if (FtpServer::sessions == NULL) {
return;
}

FtpServer::cmdPort = _cmdPort;
FtpServer::pasvPort = _pasvPort;
FtpServer::ftpServer = new FTP_SERVER_NETWORK_SERVER_CLASS(_cmdPort);
FtpServer::dataServer = new FTP_SERVER_NETWORK_SERVER_CLASS(_pasvPort);
FtpServer::sessions[0] = this;
idx = 0;
for (uint8_t i = 1; i < FTP_MAX_SESSIONS; i++) {
FtpServer::maxSessions = 1;
for (uint8_t i = 1; i < _maxSessions; i++) {
FtpServer::sessions[i] = new FtpServer(_cmdPort, _pasvPort);
if (FtpServer::sessions[i] == nullptr) break;
FtpServer::sessions[i]->idx = i;
FtpServer::maxSessions = i + 1;
}
}

Expand Down Expand Up @@ -131,7 +143,7 @@ void FtpServer::begin( const char * _user, const char * _pass, const char * _wel
FtpServer::dataServer->setNoDelay( true );
#endif

for (uint8_t i = 0; i < FTP_MAX_SESSIONS; i++) {
for (uint8_t i = 0; i < FtpServer::maxSessions; i++) {
FtpServer* that = FtpServer::sessions[i];
that->millisDelay = 0;
that->cmdStage = FTP_Stop;
Expand All @@ -146,7 +158,7 @@ void FtpServer::begin( const char * _welcomeMessage ) {

void FtpServer::end()
{
for (uint8_t i = 0; i < FTP_MAX_SESSIONS; i++) {
for (uint8_t i = 0; i < FtpServer::maxSessions; i++) {
FtpServer* that = FtpServer::sessions[i];
if(that->client.connected()) {
that->disconnectClient();
Expand Down Expand Up @@ -194,8 +206,12 @@ void FtpServer::iniVariables()
transferStage = FTP_Close;
}

uint8_t FtpServer::getMaxSessions() {
return FtpServer::maxSessions;
}

void FtpServer::handleFTP() {
for (uint8_t i = 0; i < FTP_MAX_SESSIONS; i++) {
for (uint8_t i = 0; i < FtpServer::maxSessions; i++) {
FtpServer::sessions[i]->_handleFTP();
}
}
Expand Down
16 changes: 11 additions & 5 deletions FtpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
* transfers (transfer queue).
*
* On default in FtpServerKey.h the define FTP_MAX_SESSIONS is set to 2
* for two concurrent FTP connections.
* for two concurrent FTP connections. But you can also use another value
* by using the 3rd parameter of the FtpServer constructor, which I added
* in MultiFTPServer (compared to SimpleFTPServer). A single new method
* added for the MultiFTPServer is getMaxSessions() to get the actual
* number of concurrently useable FTP sessions.
*
**************************************************************************/

Expand All @@ -36,7 +40,7 @@
#ifndef FTP_SERVER_H
#define FTP_SERVER_H

#define FTP_SERVER_VERSION "2.1.2 (2022-07-06)"
#define FTP_SERVER_VERSION "2.1.2 (2022-08-04) Multi"

#if ARDUINO >= 100
#include "Arduino.h"
Expand Down Expand Up @@ -517,7 +521,7 @@ enum FtpTransferOperation {
class FtpServer
{
public:
FtpServer( uint16_t _cmdPort = FTP_CMD_PORT, uint16_t _pasvPort = FTP_DATA_PORT_PASV );
FtpServer( uint16_t _cmdPort = FTP_CMD_PORT, uint16_t _pasvPort = FTP_DATA_PORT_PASV, uint8_t _maxSessions = FTP_MAX_SESSIONS );

void begin( const char * _user, const char * _pass, const char * welcomeMessage = "Welcome to Simply FTP server" );
void begin( const char * welcomeMessage = "Welcome to Simply FTP server" );
Expand All @@ -526,6 +530,7 @@ class FtpServer
void setLocalIp(IPAddress localIp);
void credentials( const char * _user, const char * _pass );
void handleFTP();
uint8_t getMaxSessions();

void setCallback(void (*_callbackParam)(FtpOperation ftpOperation, unsigned int freeSpace, unsigned int totalSpace) )
{
Expand Down Expand Up @@ -730,8 +735,9 @@ class FtpServer
millisBeginTrans, // store time of beginning of a transaction
bytesTransfered; //

static FtpServer* sessions[FTP_MAX_SESSIONS]; // concurrently running ftp servers
uint8_t idx; // index of this object in the sessions list
static uint8_t maxSessions; // maximum possible ftp sessions
static FtpServer** sessions; // concurrently running ftp servers
uint8_t idx; // index of this object in the sessions list
};

#endif // FTP_SERVER_H
6 changes: 5 additions & 1 deletion FtpServerKey.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
* transfers (transfer queue).
*
* On default in FtpServerKey.h the define FTP_MAX_SESSIONS is set to 2
* for two concurrent FTP connections.
* for two concurrent FTP connections. But you can also use another value
* by using the 3rd parameter of the FtpServer constructor, which I added
* in MultiFTPServer (compared to SimpleFTPServer). A single new method
* added for the MultiFTPServer is getMaxSessions() to get the actual
* number of concurrently useable FTP sessions.
*
**************************************************************************/

Expand Down

0 comments on commit 70c6472

Please sign in to comment.