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;
|
2025-04-07 17:28:50 +08:00
|
|
|
std::string uuid;
|
2024-12-14 16:20:25 +08:00
|
|
|
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
|
2025-04-12 22:51:06 +08:00
|
|
|
class TransmClient
|
2024-12-11 23:23:48 +08:00
|
|
|
{
|
|
|
|
public:
|
2025-04-12 22:51:06 +08:00
|
|
|
TransmClient();
|
|
|
|
~TransmClient();
|
2024-12-11 23:23:48 +08:00
|
|
|
|
|
|
|
public:
|
2025-03-28 12:56:06 +08:00
|
|
|
void run(const std::string& ip, const std::string& port, const std::string& config_dir);
|
2025-04-08 09:53:41 +08:00
|
|
|
bool get_clients();
|
|
|
|
bool cmd_fetch_files(const std::string& param);
|
|
|
|
bool cmd_sub_list(const std::string& param);
|
|
|
|
bool cmd_clear_submited();
|
|
|
|
bool cmd_upload_files(const std::string& param);
|
2025-04-08 12:01:35 +08:00
|
|
|
bool cmd_sub_task(const std::string& param, bool is_send);
|
2025-04-13 00:08:50 +08:00
|
|
|
bool cmd_ls(const std::string& param);
|
|
|
|
bool cmd_down_list(const std::string& param);
|
|
|
|
|
|
|
|
private:
|
2025-04-08 12:01:35 +08:00
|
|
|
bool variable_and_parse_files(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);
|
2025-04-07 16:41:28 +08:00
|
|
|
bool get_dir_files(const std::string& dir, std::string& out, std::string& error);
|
2025-04-13 00:08:50 +08:00
|
|
|
void report_trans_ret(TransState state, const std::string& key = "");
|
|
|
|
bool down_one_file(int remote_id, const std::string& file, const std::string& local_dir = "");
|
2024-12-13 23:03:12 +08:00
|
|
|
|
|
|
|
private:
|
2024-12-14 11:57:33 +08:00
|
|
|
bool send_frame(CFrameBuffer* buf);
|
2025-03-28 12:56:06 +08:00
|
|
|
void save_line_his(const std::string& input);
|
|
|
|
std::vector<std::string> load_line_his();
|
2025-04-08 12:01:35 +08:00
|
|
|
std::string variable_and_reverse_files(const std::string& source);
|
2025-04-07 16:41:28 +08:00
|
|
|
bool save_uuid();
|
|
|
|
std::string read_uuid();
|
|
|
|
void get_id();
|
2025-04-08 09:53:41 +08:00
|
|
|
void print_help(bool detail);
|
2025-04-13 00:08:50 +08:00
|
|
|
bool base_init(const std::string& ip, const std::string& port, const std::string& config_dir);
|
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-04-08 12:01:35 +08:00
|
|
|
std::string variable_handle(const std::string& task_list_path, const std::string& source, bool is_local);
|
|
|
|
std::string handle_user_select(const std::unordered_map<int, std::string>& source, bool is_send);
|
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_;
|
2025-04-08 21:13:02 +08:00
|
|
|
CMessageInfo msg_info_;
|
2024-12-11 23:23:48 +08:00
|
|
|
std::shared_ptr<CTcpClient> client_;
|
2025-04-07 23:12:27 +08:00
|
|
|
std::map<int, std::shared_ptr<DownClientInfo>> clients_;
|
2024-12-14 16:20:25 +08:00
|
|
|
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_;
|
2025-04-13 00:08:50 +08:00
|
|
|
std::thread context_th_;
|
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_;
|
2025-04-08 23:16:41 +08:00
|
|
|
std::string list_server_id_;
|
|
|
|
std::string list_server_uuid_;
|
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-03-28 12:56:06 +08:00
|
|
|
std::string config_path_{};
|
2025-04-07 16:41:28 +08:00
|
|
|
std::string uuid_path_{};
|
|
|
|
std::string uuid_{};
|
2025-04-13 00:53:52 +08:00
|
|
|
|
|
|
|
#ifdef USE_TRANSM_TEST
|
|
|
|
public:
|
|
|
|
enum TaskState {
|
|
|
|
TASK_STATE_IDLE,
|
|
|
|
TASK_STATE_RUNNING,
|
|
|
|
TASK_STATE_DONE,
|
|
|
|
TASK_STATE_ERROR
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
bool connect_for_test(const std::string& ip, const std::string& port, const std::string& config_dir);
|
|
|
|
void disconnect_for_test();
|
|
|
|
int test_index_by_id(const std::string& id);
|
|
|
|
std::string test_get_own_id() const;
|
|
|
|
void set_task_state(TaskState state);
|
|
|
|
TaskState get_task_state() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
TaskState task_state_{};
|
|
|
|
#endif
|
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
|
|
|
};
|