| 147 | } |
| 148 | |
| 149 | uint8_t FtpServer::handleFTP() |
| 150 | { |
| 151 | #ifdef FTP_ADDITIONAL_DEBUG |
| 152 | // int8_t data0 = data.status(); |
| 153 | ftpTransfer transferStage0 = transferStage; |
| 154 | ftpCmd cmdStage0 = cmdStage; |
| 155 | ftpDataConn dataConn0 = dataConn; |
| 156 | #endif |
| 157 | |
| 158 | if((int32_t)(millisDelay - millis()) <= 0) { |
| 159 | if(cmdStage == FTP_Stop) { |
| 160 | if(client.connected()) { |
| 161 | DEBUG_PRINTLN(F("Disconnect client!")); |
| 162 | disconnectClient(); |
| 163 | } |
| 164 | cmdStage = FTP_Init; |
| 165 | } else if(cmdStage == FTP_Init) { // Ftp server waiting for connection |
| 166 | abortTransfer(); |
| 167 | iniVariables(); |
| 168 | DEBUG_PRINT(F(" Ftp server waiting for connection on port ")); |
| 169 | DEBUG_PRINTLN(cmdPort); |
| 170 | |
| 171 | cmdStage = FTP_Client; |
| 172 | } else if(cmdStage == FTP_Client) { // Ftp server idle |
| 173 | #ifdef ESP8266 |
| 174 | if(ftpServer.hasClient()) { |
| 175 | client.stop(); |
| 176 | client = ftpServer.available(); |
| 177 | } |
| 178 | #else |
| 179 | if(client && !client.connected()) { |
| 180 | client.stop(); |
| 181 | DEBUG_PRINTLN(F("CLIENT STOP!!")); |
| 182 | } |
| 183 | client = ftpServer.accept(); |
| 184 | #endif |
| 185 | if(client.connected()) // A client connected |
| 186 | { |
| 187 | clientConnected(); |
| 188 | millisEndConnection = millis() + 1000L * FTP_AUTH_TIME_OUT; // wait client id for 10 s. |
| 189 | cmdStage = FTP_User; |
| 190 | } |
| 191 | } else if(readChar() > 0) // got response |
| 192 | { |
| 193 | processCommand(); |
| 194 | if(cmdStage == FTP_Stop) |
| 195 | millisEndConnection = millis() + 1000L * FTP_AUTH_TIME_OUT; // wait authentication for 10 s. |
| 196 | else if(cmdStage < FTP_Cmd) |
| 197 | millisDelay = millis() + 200; // delay of 100 ms |
| 198 | else |
| 199 | millisEndConnection = millis() + 1000L * FTP_TIME_OUT; |
| 200 | } else if(!client.connected()) { |
| 201 | cmdStage = FTP_Init; |
| 202 | } |
| 203 | if(transferStage == FTP_Retrieve) // Retrieve data |
| 204 | { |
| 205 | if(!doRetrieve()) { |
| 206 | transferStage = FTP_Close; |