#pragma once #include #include #include #include #include #include #include #define TRANSFER_KEY_LENGTH (32) namespace ts::server::file { enum struct ExecuteStatus { UNKNOWN, WAITING, SUCCESS, ERROR }; template constexpr std::size_t variant_index() { if constexpr (index == std::variant_size_v) { return index; } else if constexpr (std::is_same_v, T>) { return index; } else { return variant_index(); } } struct EmptyExecuteResponse { }; template class ExecuteResponse { typedef std::variant variant_t; public: ExecuteStatus status{ExecuteStatus::WAITING}; [[nodiscard]] inline const auto& response() const { return std::get(this->response_); } template ::value>> [[nodiscard]] inline const error_t& error() const { return std::get(this->response_); } inline void wait() const { std::unique_lock nlock{this->notify_mutex}; this->notify_cv.wait(nlock, [&]{ return this->status != ExecuteStatus::WAITING; }); } template [[nodiscard]] inline bool wait_for(const std::chrono::duration<_Rep, _Period>& time) const { std::unique_lock nlock{this->notify_mutex}; return this->notify_cv.wait_for(nlock, time, [&]{ return this->status != ExecuteStatus::WAITING; }); } template inline void emplace_success(Args&&... args) { constexpr auto success_index = variant_index(); std::lock_guard rlock{this->notify_mutex}; this->response_.template emplace(std::forward(args)...); this->status = ExecuteStatus::SUCCESS; this->notify_cv.notify_all(); } template inline void emplace_fail(Args&&... args) { constexpr auto error_index = variant_index(); std::lock_guard rlock{this->notify_mutex}; this->response_.template emplace(std::forward(args)...); this->status = ExecuteStatus::ERROR; this->notify_cv.notify_all(); } [[nodiscard]] inline bool succeeded() const { return this->status == ExecuteStatus::SUCCESS; } ExecuteResponse(std::mutex& notify_mutex, std::condition_variable& notify_cv) : notify_mutex{notify_mutex}, notify_cv{notify_cv} {} private: variant_t response_{}; /* void* as default value so we don't initialize error_t or response_t */ std::mutex& notify_mutex; std::condition_variable& notify_cv; }; namespace filesystem { template struct DetailedError { ErrorCodes error_type{ErrorCodes::UNKNOWN}; std::string error_message{}; DetailedError(ErrorCodes type, std::string extraMessage) : error_type{type}, error_message{std::move(extraMessage)} {} }; enum struct DirectoryQueryErrorType { UNKNOWN, PATH_EXCEEDS_ROOT_PATH, PATH_IS_A_FILE, PATH_DOES_NOT_EXISTS, FAILED_TO_LIST_FILES, MAX }; constexpr std::array directory_query_error_messages = { "unknown error", "path exceeds base path", "path is a file", "path does not exists", "failed to list files" }; typedef DetailedError DirectoryQueryError; struct DirectoryEntry { enum Type { UNKNOWN, DIRECTORY, FILE }; Type type{Type::UNKNOWN}; std::string name{}; std::chrono::system_clock::time_point modified_at{}; size_t size{0}; }; enum struct DirectoryModifyErrorType { UNKNOWN, PATH_EXCEEDS_ROOT_PATH, PATH_ALREADY_EXISTS, FAILED_TO_CREATE_DIRECTORIES }; typedef DetailedError DirectoryModifyError; enum struct FileModifyErrorType { UNKNOWN, PATH_EXCEEDS_ROOT_PATH, TARGET_PATH_EXCEEDS_ROOT_PATH, PATH_DOES_NOT_EXISTS, TARGET_PATH_ALREADY_EXISTS, FAILED_TO_DELETE_FILES, FAILED_TO_RENAME_FILE, SOME_FILES_ARE_LOCKED }; typedef DetailedError FileModifyError; enum struct ServerCommandErrorType { UNKNOWN, FAILED_TO_CREATE_DIRECTORIES, FAILED_TO_DELETE_DIRECTORIES }; typedef DetailedError ServerCommandError; class AbstractProvider { public: typedef ExecuteResponse> directory_query_response_t; /* server */ [[nodiscard]] virtual std::shared_ptr> initialize_server(ServerId /* server */) = 0; [[nodiscard]] virtual std::shared_ptr> delete_server(ServerId /* server */) = 0; /* channels */ [[nodiscard]] virtual std::shared_ptr query_channel_directory(ServerId /* server */, ChannelId /* channel */, const std::string& /* path */) = 0; [[nodiscard]] virtual std::shared_ptr> create_channel_directory(ServerId /* server */, ChannelId /* channel */, const std::string& /* path */) = 0; [[nodiscard]] virtual std::shared_ptr> delete_channel_file(ServerId /* server */, ChannelId /* channel */, const std::string& /* path */) = 0; [[nodiscard]] virtual std::shared_ptr> rename_channel_file(ServerId /* server */, ChannelId /* channel */, const std::string& /* path */, const std::string& /* target */) = 0; /* icons */ [[nodiscard]] virtual std::shared_ptr query_icon_directory(ServerId /* server */) = 0; [[nodiscard]] virtual std::shared_ptr> delete_icon(ServerId /* server */, const std::string& /* name */) = 0; /* avatars */ [[nodiscard]] virtual std::shared_ptr query_avatar_directory(ServerId /* server */) = 0; [[nodiscard]] virtual std::shared_ptr> delete_avatar(ServerId /* server */, const std::string& /* name */) = 0; private: }; } namespace transfer { typedef uint32_t transfer_id; struct Transfer { transfer_id server_transfer_id{0}; transfer_id client_transfer_id{0}; ServerId server_id{0}; ClientId client_id{0}; ChannelId channel_id{0}; char transfer_key[TRANSFER_KEY_LENGTH]{}; std::chrono::system_clock::time_point initialized_timestamp{}; enum Direction { DIRECTION_UNKNOWN, DIRECTION_UPLOAD, DIRECTION_DOWNLOAD } direction{DIRECTION_UNKNOWN}; struct Address { std::string hostname{}; uint16_t port{0}; }; std::vector
server_addresses{}; enum TargetType { TARGET_TYPE_UNKNOWN, TARGET_TYPE_CHANNEL_FILE, TARGET_TYPE_ICON, TARGET_TYPE_AVATAR } target_type{TARGET_TYPE_UNKNOWN}; std::string target_file_path{}; int64_t max_bandwidth{-1}; size_t expected_file_size{0}; /* incl. the offset! */ size_t file_offset{0}; bool override_exiting{false}; }; struct TransferStatistics { uint64_t network_bytes_send{0}; uint64_t network_bytes_received{0}; uint64_t delta_network_bytes_send{0}; uint64_t delta_network_bytes_received{0}; uint64_t file_bytes_transferred{0}; uint64_t delta_file_bytes_transferred{0}; size_t file_start_offset{0}; size_t file_current_offset{0}; size_t file_total_size{0}; }; struct TransferInitError { enum Type { UNKNOWN } error_type{UNKNOWN}; std::string error_message{}; }; struct TransferActionError { enum Type { UNKNOWN, UNKNOWN_TRANSFER } error_type{UNKNOWN}; std::string error_message{}; }; struct TransferError { enum Type { UNKNOWN, TRANSFER_TIMEOUT, DISK_IO_ERROR, DISK_TIMEOUT, DISK_INITIALIZE_ERROR, NETWORK_IO_ERROR, UNEXPECTED_CLIENT_DISCONNECT, UNEXPECTED_DISK_EOF } error_type{UNKNOWN}; std::string error_message{}; }; class AbstractProvider { public: struct TransferInfo { std::string file_path{}; bool override_exiting{false}; /* only for upload valid */ size_t file_offset{0}; size_t expected_file_size{0}; int64_t max_bandwidth{-1}; }; virtual std::shared_ptr>> initialize_channel_transfer(Transfer::Direction /* direction */, ServerId /* server */, ChannelId /* channel */, const TransferInfo& /* info */) = 0; virtual std::shared_ptr>> initialize_icon_transfer(Transfer::Direction /* direction */, ServerId /* server */, const TransferInfo& /* info */) = 0; virtual std::shared_ptr>> initialize_avatar_transfer(Transfer::Direction /* direction */, ServerId /* server */, const TransferInfo& /* info */) = 0; virtual std::shared_ptr> stop_transfer(transfer_id /* id */, bool /* flush */) = 0; std::function&)> callback_transfer_registered{}; /* transfer has been registered */ std::function&)> callback_transfer_started{}; /* transfer has been started */ std::function&)> callback_transfer_finished{}; /* transfer has been finished */ std::function&, const TransferError&)> callback_transfer_aborted{}; /* an error happened while transferring the data */ std::function&, const TransferStatistics&)> callback_transfer_statistics{}; }; } class AbstractFileServer { public: [[nodiscard]] virtual filesystem::AbstractProvider& file_system() = 0; [[nodiscard]] virtual transfer::AbstractProvider& file_transfer() = 0; private: }; extern bool initialize(std::string& /* error */); extern void finalize(); extern std::shared_ptr server(); }