Something went wrong. Try again.
A fork of https://github.com/crosspoint-reader/crosspoint-reader
Something went wrong. Try again.
3.8 kB · 142 lines
C
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143#pragma once
#include <HalStorage.h>#include <NetworkUdp.h>#include <WebServer.h>#include <WebSocketsServer.h>
#include <memory>#include <string>#include <vector>
// Structure to hold file informationstruct FileInfo { String name; size_t size; bool isEpub; bool isDirectory;};
class CrossPointWebServer { public: struct WsUploadStatus { bool inProgress = false; size_t received = 0; size_t total = 0; std::string filename; std::string lastCompleteName; size_t lastCompleteSize = 0; unsigned long lastCompleteAt = 0; };
// Used by POST upload handler struct UploadState { HalFile file; String fileName; String path = "/"; size_t size = 0; bool success = false; String error = "";
// Upload write buffer - batches small writes into larger SD card operations // 4KB is a good balance: large enough to reduce syscall overhead, small enough // to keep individual write times short and avoid watchdog issues static constexpr size_t UPLOAD_BUFFER_SIZE = 4096; // 4KB buffer std::vector<uint8_t> buffer; size_t bufferPos = 0;
UploadState() { buffer.resize(UPLOAD_BUFFER_SIZE); } } upload;
CrossPointWebServer(); ~CrossPointWebServer();
// Start the web server (call after WiFi is connected) void begin();
// Stop the web server void stop();
// Call this periodically to handle client requests void handleClient();
// Check if server is running bool isRunning() const { return running; }
WsUploadStatus getWsUploadStatus() const;
// Get the port number uint16_t getPort() const { return port; }
private: std::unique_ptr<WebServer> server = nullptr; std::unique_ptr<WebSocketsServer> wsServer = nullptr; bool running = false; bool apMode = false; // true when running in AP mode, false for STA mode uint16_t port = 80; uint16_t wsPort = 81; // WebSocket port NetworkUDP udp; bool udpActive = false;
// WebSocket upload state void onWebSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length); static void wsEventCallback(uint8_t num, WStype_t type, uint8_t* payload, size_t length); void abortWsUpload(const char* tag);
// File scanning void scanFiles(const char* path, const std::function<void(FileInfo)>& callback) const; String formatFileSize(size_t bytes) const; bool isEpubFile(const String& filename) const;
// Request handlers void handleRoot() const; void handleJszip() const; void handleNotFound() const; void handleStatus() const; void handleFileList() const; void handleFileListData() const; void handleDownload() const; void handleUpload(UploadState& state) const; void handleUploadPost(UploadState& state) const; void handleCreateFolder() const; void handleRename() const; void handleMove() const; void handleDelete() const;
// Settings handlers void handleSettingsPage() const; void handleGetSettings() const; void handlePostSettings();
// Font management handlers void handleFontsPage() const; void handleFontList() const; void handleFontUpload(); void handleFontUploadData(); void handleFontDelete();
// Font upload state struct FontUploadState { HalFile file; std::string familyName; std::string filePath; bool valid = false; bool magicChecked = false; size_t bytesWritten = 0; static constexpr size_t BUFFER_SIZE = 4096; std::vector<uint8_t> buffer; size_t bufferPos = 0;
FontUploadState() { buffer.resize(BUFFER_SIZE); } } fontUpload;
// OPDS server handlers void handleGetOpdsServers() const; void handlePostOpdsServer(); void handleDeleteOpdsServer();
// Wi-Fi credential handlers void handleGetWifiNetworks() const; void handlePostWifiNetwork(); void handleDeleteWifiNetwork();};