2024-12-11 23:23:48 +08:00
|
|
|
#pragma once
|
2025-02-18 23:33:21 +08:00
|
|
|
#include <cstdint>
|
2025-01-21 14:39:38 +08:00
|
|
|
#include <filecomplete.h>
|
2025-01-07 12:07:43 +08:00
|
|
|
#include <fstream>
|
2025-01-21 21:43:09 +08:00
|
|
|
#include <map>
|
2024-12-18 16:55:32 +08:00
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
2024-12-11 23:23:48 +08:00
|
|
|
#include <net_base.h>
|
2024-12-18 16:55:32 +08:00
|
|
|
#include <of_util.h>
|
|
|
|
#include <string>
|
2025-02-18 23:33:21 +08:00
|
|
|
#include <unordered_map>
|
2024-12-11 23:23:48 +08:00
|
|
|
#include <util.h>
|
2024-12-12 22:43:24 +08:00
|
|
|
#include <vector>
|
2024-12-14 16:20:25 +08:00
|
|
|
|
|
|
|
using namespace ofen;
|
|
|
|
struct DownClientInfo {
|
|
|
|
std::vector<std::string> files;
|
|
|
|
std::string id;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum TransState {
|
2024-12-18 13:20:23 +08:00
|
|
|
TRANS_FAILED,
|
2024-12-14 16:20:25 +08:00
|
|
|
TRANS_ING,
|
|
|
|
TRANS_REDAY,
|
2024-12-18 22:04:54 +08:00
|
|
|
TRANS_DONE,
|
|
|
|
TRANS_BREAK
|
2024-12-14 16:20:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct TransInfomation {
|
|
|
|
std::string cur_remote_id_;
|
|
|
|
std::string cur_remote_file_;
|
|
|
|
std::string cur_file_;
|
2025-01-07 12:07:43 +08:00
|
|
|
std::fstream file_{};
|
2025-02-10 16:54:43 +08:00
|
|
|
uint16_t permissions{};
|
2025-02-10 23:16:45 +08:00
|
|
|
uint16_t remote_plat{};
|
2024-12-18 13:20:23 +08:00
|
|
|
TransState trans_state_{TRANS_FAILED};
|
2024-12-14 16:20:25 +08:00
|
|
|
};
|
2024-12-11 23:23:48 +08:00
|
|
|
|
2025-01-07 13:47:53 +08:00
|
|
|
constexpr int down_check_wait = 100; // millsec
|
2024-12-11 23:23:48 +08:00
|
|
|
class CClient
|
|
|
|
{
|
|
|
|
public:
|
2025-03-01 03:49:40 +08:00
|
|
|
CClient();
|
2024-12-11 23:23:48 +08:00
|
|
|
~CClient();
|
|
|
|
|
|
|
|
public:
|
2024-12-16 09:40:57 +08:00
|
|
|
void run(const std::string& ip, const std::string& port);
|
2024-12-11 23:23:48 +08:00
|
|
|
|
2024-12-12 22:43:24 +08:00
|
|
|
public:
|
|
|
|
bool get_task_list();
|
2024-12-14 16:20:25 +08:00
|
|
|
bool down_task(const std::string& param);
|
2025-01-07 14:00:17 +08:00
|
|
|
bool up_task(const std::string& param);
|
2024-12-13 23:03:12 +08:00
|
|
|
bool cancel_task();
|
2024-12-21 23:24:18 +08:00
|
|
|
bool down_one_file(const std::string& id, const std::string& file, const std::string& local_dir = "");
|
2024-12-18 13:20:23 +08:00
|
|
|
void report_trans_ret(TransState state, const std::string& key = "");
|
2025-01-07 14:00:17 +08:00
|
|
|
bool request_update_list(const std::string& param);
|
2025-02-07 12:45:13 +08:00
|
|
|
bool check_update_list(const std::string& content, std::map<std::string, std::string>& files);
|
2025-02-06 09:37:24 +08:00
|
|
|
bool down_update_file(const std::map<std::string, std::string>& files);
|
2024-12-13 23:03:12 +08:00
|
|
|
|
|
|
|
private:
|
2024-12-14 11:57:33 +08:00
|
|
|
bool send_frame(CFrameBuffer* buf);
|
2024-12-12 22:43:24 +08:00
|
|
|
|
|
|
|
private:
|
2024-12-13 12:35:08 +08:00
|
|
|
void handle_frame(CFrameBuffer* buf);
|
2024-12-18 19:52:04 +08:00
|
|
|
void send_file_data_th(const char* keys);
|
2024-12-18 13:55:56 +08:00
|
|
|
void hearts();
|
2024-12-18 22:04:54 +08:00
|
|
|
void judget_down_active();
|
2025-02-07 12:45:13 +08:00
|
|
|
std::string variable_handle(const std::string& task_list_path, const std::string& source, bool is_send);
|
2025-02-11 00:12:36 +08:00
|
|
|
std::string handle_user_select(const std::unordered_map<int, std::string>& source);
|
2024-12-12 22:43:24 +08:00
|
|
|
|
2024-12-11 23:23:48 +08:00
|
|
|
private:
|
2024-12-18 19:52:04 +08:00
|
|
|
std::mutex mutex_;
|
|
|
|
std::mutex send_mut_;
|
|
|
|
std::string work_key_;
|
|
|
|
std::thread hearts_;
|
|
|
|
CThreadSleep sleep_;
|
|
|
|
bool th_run_{false};
|
2025-01-07 13:47:53 +08:00
|
|
|
bool will_receive_{false};
|
2024-12-18 22:04:54 +08:00
|
|
|
bool downloading_{false};
|
2024-12-11 23:23:48 +08:00
|
|
|
asio::io_context io_context_;
|
|
|
|
std::shared_ptr<CTcpClient> client_;
|
2024-12-14 16:20:25 +08:00
|
|
|
std::map<int, std::shared_ptr<DownClientInfo>> task_list_;
|
|
|
|
std::shared_ptr<TransInfomation> down_;
|
2024-12-18 19:52:04 +08:00
|
|
|
std::vector<std::thread> ths_;
|
2024-12-14 16:20:25 +08:00
|
|
|
std::map<std::string, std::shared_ptr<TransInfomation>> up_;
|
2024-12-18 22:04:54 +08:00
|
|
|
std::thread th_down_active_;
|
2025-01-07 12:07:43 +08:00
|
|
|
long long cur_file_size_{};
|
|
|
|
long long cur_down_size_{};
|
2024-12-20 16:57:44 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::string list_file_;
|
|
|
|
std::string list_serve_id_;
|
2024-12-21 23:24:18 +08:00
|
|
|
std::thread update_list_th_;
|
2025-01-07 15:08:37 +08:00
|
|
|
std::string own_id_{};
|
2025-01-06 16:25:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class CFileOpr
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CFileOpr();
|
|
|
|
~CFileOpr();
|
2025-01-06 18:59:11 +08:00
|
|
|
|
2025-01-06 16:25:39 +08:00
|
|
|
public:
|
2025-03-01 03:49:40 +08:00
|
|
|
static bool get_file_list(const std::string& input, std::vector<std::string>& out);
|
2024-12-11 23:23:48 +08:00
|
|
|
};
|