Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Ftp classes #1633

Merged
merged 6 commits into from
Feb 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Sming/SmingCore/Network/Ftp/FtpDataFileList.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/****
* Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
* Created 2015 by Skurydin Alexey
* http://github.com/anakod/Sming
* All files of the Sming Core are provided under the LGPL v3 license.
*
* FtpDataFileList.h
*
****/

#ifndef _SMING_CORE_NETWORK_FTP_FTP_DATA_FILE_LIST_H_
#define _SMING_CORE_NETWORK_FTP_FTP_DATA_FILE_LIST_H_

#include "FtpDataStream.h"
#include "FileSystem.h"

class FtpDataFileList : public FtpDataStream
{
public:
explicit FtpDataFileList(FtpServerConnection* connection) : FtpDataStream(connection)
{
}

void transferData(TcpConnectionEvent sourceEvent) override
{
if(completed) {
return;
}
Vector<String> list = fileList();
debug_d("send file list: %d", list.count());
for(unsigned i = 0; i < list.count(); i++) {
writeString("01-01-15 01:00AM " + String(fileGetSize(list[i])) + " " + list[i] + "\r\n");
}
completed = true;
finishTransfer();
}
};

#endif /* _SMING_CORE_NETWORK_FTP_FTP_DATA_FILE_LIST_H_ */
48 changes: 48 additions & 0 deletions Sming/SmingCore/Network/Ftp/FtpDataRetrieve.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/****
* Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
* Created 2015 by Skurydin Alexey
* http://github.com/anakod/Sming
* All files of the Sming Core are provided under the LGPL v3 license.
*
* FtpDataRetrieve.h
*
****/

#ifndef _SMING_CORE_NETWORK_FTP_FTP_DATA_RETRIEVE_H_
#define _SMING_CORE_NETWORK_FTP_FTP_DATA_RETRIEVE_H_

#include "FtpDataStream.h"
#include "FileSystem.h"

class FtpDataRetrieve : public FtpDataStream
{
public:
FtpDataRetrieve(FtpServerConnection* connection, const String& fileName)
: FtpDataStream(connection), file(fileOpen(fileName, eFO_ReadOnly))
{
}

~FtpDataRetrieve()
{
fileClose(file);
}

void transferData(TcpConnectionEvent sourceEvent) override
{
if(completed) {
return;
}
char buf[1024];
int len = fileRead(file, buf, sizeof(buf));
write(buf, len, TCP_WRITE_FLAG_COPY);
if(fileIsEOF(file)) {
completed = true;
finishTransfer();
}
}

private:
file_t file;
};

#endif /* _SMING_CORE_NETWORK_FTP_FTP_DATA_RETRIEVE_H_ */
55 changes: 55 additions & 0 deletions Sming/SmingCore/Network/Ftp/FtpDataStore.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/****
* Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
* Created 2015 by Skurydin Alexey
* http://github.com/anakod/Sming
* All files of the Sming Core are provided under the LGPL v3 license.
*
* FtpDataStore.h
*
****/

#ifndef _SMING_CORE_NETWORK_FTP_FTP_DATA_STORE_H_
#define _SMING_CORE_NETWORK_FTP_FTP_DATA_STORE_H_

#include "FtpDataStream.h"
#include "FileSystem.h"

class FtpDataStore : public FtpDataStream
{
public:
FtpDataStore(FtpServerConnection* connection, const String& fileName)
: FtpDataStream(connection), file(fileOpen(fileName, eFO_WriteOnly | eFO_CreateNewAlways))
{
}

~FtpDataStore()
{
fileClose(file);
}

err_t onReceive(pbuf* buf) override
{
if(completed) {
return TcpConnection::onReceive(buf);
}

if(buf == nullptr) {
completed = true;
response(226, "Transfer completed");
return TcpConnection::onReceive(buf);
}

pbuf* cur = buf;
while(cur != nullptr && cur->len > 0) {
fileWrite(file, (uint8_t*)cur->payload, cur->len);
cur = cur->next;
}

return TcpConnection::onReceive(buf);
}

private:
file_t file;
};

#endif /* _SMING_CORE_NETWORK_FTP_FTP_DATA_STORE_H_ */
80 changes: 80 additions & 0 deletions Sming/SmingCore/Network/Ftp/FtpDataStream.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/****
* Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
* Created 2015 by Skurydin Alexey
* http://github.com/anakod/Sming
* All files of the Sming Core are provided under the LGPL v3 license.
*
* FtpDataStream.h
*
****/

#ifndef _SMING_CORE_NETWORK_FTP_FTP_DATA_STREAM_H_
#define _SMING_CORE_NETWORK_FTP_FTP_DATA_STREAM_H_

#include "FtpServerConnection.h"
#include "TcpConnection.h"

class FtpDataStream : public TcpConnection
{
public:
explicit FtpDataStream(FtpServerConnection* connection) : TcpConnection(true), parent(connection)
{
}

err_t onConnected(err_t err) override
{
//response(125, "Connected");
setTimeOut(300); // Update timeout
return TcpConnection::onConnected(err);
}

err_t onSent(uint16_t len) override
{
sent += len;
if(written < sent || !completed) {
return TcpConnection::onSent(len);
}
finishTransfer();
return TcpConnection::onSent(len);
}

void finishTransfer()
{
close();
parent->dataTransferFinished(this);
}

void response(int code, String text = nullptr)
{
parent->response(code, text);
}

int write(const char* data, int len, uint8_t apiflags = 0)
{
written += len;
return TcpConnection::write(data, len, apiflags);
}

void onReadyToSendData(TcpConnectionEvent sourceEvent) override
{
if(!parent->isCanTransfer()) {
return;
}
if(completed && written == 0) {
finishTransfer();
}
transferData(sourceEvent);
}

virtual void transferData(TcpConnectionEvent sourceEvent)
{
}

protected:
FtpServerConnection* parent = nullptr;
bool completed = false;
unsigned written = 0;
unsigned sent = 0;
};

#endif /* _SMING_CORE_NETWORK_FTP_FTP_DATA_STREAM_H_ */
Loading