FTPServer in progress
This commit is contained in:
parent
0a924b6848
commit
bd23b4a3b2
@ -5,6 +5,7 @@ _ftpCommand({'\0'}),
|
||||
_cmdParameters(NULL),
|
||||
_loggedIn(false),
|
||||
_username(NULL),
|
||||
_dataSocketOpen(false),
|
||||
_ftpClientState(FTPServer<FTPClient>::FTPClientState::WAITING_FOR_COMMANDS),
|
||||
_binaryFlag(FTPServer<FTPClient>::BinaryFlag::OFF)
|
||||
{
|
||||
|
@ -23,6 +23,7 @@ class FTPClient : public TCPClient
|
||||
Dictionary<DictionaryHelper::StringEntity> *_cmdParameters;
|
||||
boolean _loggedIn;
|
||||
char *_username;
|
||||
boolean _dataSocketOpen;
|
||||
|
||||
FTPServer<FTPClient>::FTPClientState _ftpClientState;
|
||||
FTPServer<FTPClient>::BinaryFlag _binaryFlag;
|
||||
|
@ -14,7 +14,8 @@ class FTPServer : public TCPServer<T>
|
||||
FTPServer(unsigned int port = 21, const char *login = NULL, const char *password = NULL, uint8_t maxClient = MAX_CLIENT, uint16_t clientCommandDataBufferSize = 255) : TCPServer<T>(port, maxClient, clientCommandDataBufferSize),
|
||||
_login(NULL),
|
||||
_password(NULL),
|
||||
_dataServer(1024)
|
||||
_dataPort(1024),
|
||||
_dataServer(_dataPort)
|
||||
{
|
||||
if(login != NULL)
|
||||
{
|
||||
@ -49,6 +50,27 @@ class FTPServer : public TCPServer<T>
|
||||
|
||||
virtual void processClientData(T *client)
|
||||
{
|
||||
if(client->_dataSocketOpen)
|
||||
{
|
||||
#ifdef DEBUG_FTPS
|
||||
Serial.println("Listening for new data client");
|
||||
#endif
|
||||
WiFiClient dataClient = _dataServer.available();
|
||||
|
||||
if(dataClient)
|
||||
{
|
||||
if(dataClient.connected())
|
||||
{
|
||||
client->_dataSocketOpen = false;
|
||||
client->setDataClient(dataClient);
|
||||
#ifdef DEBUG_FTPS
|
||||
Serial.println("Data client accepted successfully");
|
||||
#endif
|
||||
}
|
||||
else
|
||||
dataClient.stop();
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef DEBUG_FTPS
|
||||
if(client->_newDataAvailable)
|
||||
@ -56,12 +78,15 @@ class FTPServer : public TCPServer<T>
|
||||
Serial.print("Client --> ");Serial.print(client->_id);Serial.print(" : ");Serial.print((char *)client->_data);Serial.println("#");
|
||||
}
|
||||
#endif
|
||||
|
||||
switch(client->_ftpClientState)
|
||||
|
||||
if(client->_dataSize > 0)
|
||||
{
|
||||
case WAITING_FOR_COMMANDS:
|
||||
processCommands(client);
|
||||
break;
|
||||
switch(client->_ftpClientState)
|
||||
{
|
||||
case WAITING_FOR_COMMANDS:
|
||||
processCommands(client);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -154,15 +179,18 @@ class FTPServer : public TCPServer<T>
|
||||
}
|
||||
else if(strcmp(client->_ftpCommand,"PASV") == 0)
|
||||
{
|
||||
_dataServer.begin();
|
||||
client->_client.printf("227 Entering Passive Mode (%d,%d,%d,%d,%d,%d).\r\n", 192,168,0,29,1024/256, 1024%256);
|
||||
_dataServer.begin(_dataPort);
|
||||
client->_client.printf("227 Entering Passive Mode (%u,%u,%u,%u,%d,%d).\r\n", WiFi.localIP()[0], WiFi.localIP()[1], WiFi.localIP()[2], WiFi.localIP()[3], _dataPort/256, _dataPort%256);
|
||||
client->_dataSocketOpen = true;
|
||||
}
|
||||
ese if()
|
||||
else
|
||||
client->_client.println("502 Command not implemented.");
|
||||
}
|
||||
|
||||
char *_login;
|
||||
char *_password;
|
||||
unsigned int _dataPort;
|
||||
|
||||
WiFiServer _dataServer; //In passive mode, the FTP server opens two different ports (one for the commands and the other for the data stream)
|
||||
};
|
||||
|
@ -6,7 +6,7 @@
|
||||
#include "TCPClient.h"
|
||||
|
||||
#define MAX_CLIENT -1
|
||||
#define DEBUG
|
||||
#define DEBUG_TCPS
|
||||
|
||||
|
||||
template <typename T>
|
||||
@ -78,14 +78,19 @@ class TCPServer
|
||||
wc = _wifiServer.available();
|
||||
}
|
||||
|
||||
if(wc && wc.connected())
|
||||
if(wc)
|
||||
{
|
||||
T *clientPointer = createNewClient(wc);
|
||||
_clientList.addFirst(clientPointer);
|
||||
#ifdef DEBUG
|
||||
Serial.print("TCPServer : New client accepted : ");Serial.println(clientPointer->_id);
|
||||
#endif
|
||||
greetClient(clientPointer);
|
||||
if(wc.connected())
|
||||
{
|
||||
T *clientPointer = createNewClient(wc);
|
||||
_clientList.addFirst(clientPointer);
|
||||
#ifdef DEBUG_TCPS
|
||||
Serial.print("TCPServer : New client accepted : ");Serial.println(clientPointer->_id);
|
||||
#endif
|
||||
greetClient(clientPointer);
|
||||
}
|
||||
else
|
||||
wc.stop();
|
||||
}
|
||||
|
||||
_currentClient = _clientList.removeLastRef();//We pick a client in the list to process it's request
|
||||
@ -132,7 +137,7 @@ class TCPServer
|
||||
}
|
||||
else if(!(_currentClient->_client).connected())
|
||||
{
|
||||
#ifdef DEBUG
|
||||
#ifdef DEBUG_TCPS
|
||||
Serial.print("TCPServer : Client disconnected and can be discarded : ");Serial.println(_currentClient->_id);
|
||||
#endif
|
||||
_currentClient->_clientState = TCPClient::DISCARDED;
|
||||
@ -140,14 +145,14 @@ class TCPServer
|
||||
|
||||
if(_currentClient->_dataSize > 0)
|
||||
{
|
||||
processClientData(_currentClient);//We process the actual data
|
||||
_currentClient->_newDataAvailable = false;
|
||||
}
|
||||
processClientData(_currentClient);//We process the actual data
|
||||
|
||||
if(_currentClient->_clientState == TCPClient::ClientState::DISCARDED)
|
||||
{
|
||||
_currentClient->closeConnection();
|
||||
#ifdef DEBUG
|
||||
#ifdef DEBUG_TCPS
|
||||
Serial.print("TCPServer : Client was discarded : ");Serial.println(_currentClient->_id);
|
||||
#endif
|
||||
delete _currentClient;
|
||||
|
@ -63,19 +63,22 @@ class WEBServer : public TCPServer<T>
|
||||
|
||||
virtual void processClientData(T *client)
|
||||
{
|
||||
switch(client->_WEBClientState)
|
||||
if(client->_dataSize > 0)
|
||||
{
|
||||
case ACCEPTED:
|
||||
queryParser(client);
|
||||
break;
|
||||
case QUERY_PARSED:
|
||||
sendDataToClient(client);
|
||||
break;
|
||||
case RESPONSE_SENT:
|
||||
break;
|
||||
case DONE:
|
||||
break;
|
||||
}
|
||||
switch(client->_WEBClientState)
|
||||
{
|
||||
case ACCEPTED:
|
||||
queryParser(client);
|
||||
break;
|
||||
case QUERY_PARSED:
|
||||
sendDataToClient(client);
|
||||
break;
|
||||
case RESPONSE_SENT:
|
||||
break;
|
||||
case DONE:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void queryParser(T *client)
|
||||
|
Loading…
Reference in New Issue
Block a user