Remaking the upload and download scheme to rapid streaming (WIP)

This commit is contained in:
Niklas Jensen
2026-01-16 21:18:16 +01:00
committed by Rune Harlyk
parent 50ef91ab22
commit f0c4f0f929
7 changed files with 733 additions and 353 deletions
+6 -16
View File
@@ -41,22 +41,12 @@ DEFINE_MESSAGE_TRAITS(ServoPWMData, servo_pwm)
DEFINE_MESSAGE_TRAITS(ServoStateData, servo_state)
DEFINE_MESSAGE_TRAITS(CorrelationRequest, correlation_request)
DEFINE_MESSAGE_TRAITS(CorrelationResponse, correlation_response)
// DEFINE_MESSAGE_TRAITS(FSDeleteRequest, fs_delete_request)
// DEFINE_MESSAGE_TRAITS(FSDeleteResponse, fs_delete_response)
// DEFINE_MESSAGE_TRAITS(FSMkdirRequest, fs_mkdir_request)
// DEFINE_MESSAGE_TRAITS(FSMkdirResponse, fs_mkdir_response)
// DEFINE_MESSAGE_TRAITS(FSListRequest, fs_list_request)
// DEFINE_MESSAGE_TRAITS(FSListResponse, fs_list_response)
// DEFINE_MESSAGE_TRAITS(FSDownloadStartRequest, fs_download_start_request)
// DEFINE_MESSAGE_TRAITS(FSDownloadStartResponse, fs_download_start_response)
// DEFINE_MESSAGE_TRAITS(FSDownloadChunkRequest, fs_download_chunk_request)
// DEFINE_MESSAGE_TRAITS(FSDownloadChunkResponse, fs_download_chunk_response)
// DEFINE_MESSAGE_TRAITS(FSUploadStartRequest, fs_upload_start_request)
// DEFINE_MESSAGE_TRAITS(FSUploadStartResponse, fs_upload_start_response)
// DEFINE_MESSAGE_TRAITS(FSUploadChunkRequest, fs_upload_chunk_request)
// DEFINE_MESSAGE_TRAITS(FSUploadChunkResponse, fs_upload_chunk_response)
// DEFINE_MESSAGE_TRAITS(FSCancelTransferRequest, fs_cancel_transfer_request)
// DEFINE_MESSAGE_TRAITS(FSCancelTransferResponse, fs_cancel_transfer_response)
// Streaming file transfer messages
DEFINE_MESSAGE_TRAITS(FSDownloadData, fs_download_data)
DEFINE_MESSAGE_TRAITS(FSDownloadComplete, fs_download_complete)
DEFINE_MESSAGE_TRAITS(FSUploadData, fs_upload_data)
DEFINE_MESSAGE_TRAITS(FSUploadComplete, fs_upload_complete)
#undef DEFINE_MESSAGE_TRAITS
+53 -12
View File
@@ -4,28 +4,54 @@
#include <platform_shared/message.pb.h>
#include <map>
#include <string>
#include <functional>
// Make sure that this aligns with socket_message.FSDownloadChunkResponse.data max_size (and for the corresponsing request)
// Make sure that this aligns with socket_message.FSDownloadData.data max_size (and for upload)
#define FS_MAX_CHUNK_SIZE 16384 // ~= 16 kb
#define FS_TRANSFER_TIMEOUT 30000 // 30 seconds
namespace FileSystemWS {
struct TransferState {
struct DownloadState {
std::string path;
File file;
uint32_t fileSize;
uint32_t chunkSize;
uint32_t totalChunks;
uint32_t chunksProcessed;
uint32_t chunksSent;
uint32_t lastActivityTime;
bool isUpload;
int clientId;
};
struct UploadState {
std::string path;
File file;
uint32_t fileSize;
uint32_t totalChunks;
uint32_t chunksReceived;
uint32_t bytesReceived;
uint32_t lastActivityTime;
int clientId;
bool hasError;
std::string errorMessage;
};
// Callback type for sending messages to clients
using SendCallback = std::function<void(const socket_message_FSDownloadData&, int clientId)>;
using SendCompleteCallback = std::function<void(const socket_message_FSDownloadComplete&, int clientId)>;
using SendUploadCompleteCallback = std::function<void(const socket_message_FSUploadComplete&, int clientId)>;
class FileSystemHandler {
public:
FileSystemHandler();
// Set callbacks for sending streaming data
void setSendCallbacks(
SendCallback sendData,
SendCompleteCallback sendComplete,
SendUploadCompleteCallback sendUploadComplete
);
// Delete file/directory
socket_message_FSDeleteResponse handleDelete(const socket_message_FSDeleteRequest& req);
@@ -35,27 +61,42 @@ class FileSystemHandler {
// List directory
socket_message_FSListResponse handleList(const socket_message_FSListRequest& req);
// Download operations (ESP -> Client)
socket_message_FSDownloadStartResponse handleDownloadStart(const socket_message_FSDownloadStartRequest& req);
socket_message_FSDownloadChunkResponse handleDownloadChunk(const socket_message_FSDownloadChunkRequest& req);
// Streaming download - starts the download and streams all chunks
void handleDownloadRequest(const socket_message_FSDownloadRequest& req, int clientId);
// Upload operations (Client -> ESP)
socket_message_FSUploadStartResponse handleUploadStart(const socket_message_FSUploadStartRequest& req);
socket_message_FSUploadChunkResponse handleUploadChunk(const socket_message_FSUploadChunkRequest& req);
// Streaming upload - start upload session
socket_message_FSUploadStartResponse handleUploadStart(const socket_message_FSUploadStart& req, int clientId);
// Streaming upload - receive chunk data (fire-and-forget from client)
void handleUploadData(const socket_message_FSUploadData& req);
// Cancel transfer
socket_message_FSCancelTransferResponse handleCancelTransfer(const socket_message_FSCancelTransferRequest& req);
socket_message_FSCancelTransferResponse handleCancelTransfer(const socket_message_FSCancelTransfer& req);
// Cleanup expired transfers
void cleanupExpiredTransfers();
// Process pending downloads (call from main loop)
void processPendingDownloads();
private:
std::map<std::string, TransferState> transfers_;
std::map<std::string, DownloadState> downloads_;
std::map<std::string, UploadState> uploads_;
uint32_t transferIdCounter_;
SendCallback sendDataCallback_;
SendCompleteCallback sendCompleteCallback_;
SendUploadCompleteCallback sendUploadCompleteCallback_;
std::string generateTransferId();
void listDirectory(const std::string& path, socket_message_FSListResponse& response);
bool deleteRecursive(const std::string& path);
// Send next chunk for a download
bool sendNextDownloadChunk(const std::string& transferId);
// Finalize upload and send completion message
void finalizeUpload(const std::string& transferId, bool success, const std::string& error = "");
};
extern FileSystemHandler fsHandler;