transm/client/client.h

145 lines
4.0 KiB
C
Raw Permalink Normal View History

2024-12-11 23:23:48 +08:00
#pragma once
#include <cstdint>
#include <filecomplete.h>
#include <fstream>
#include <map>
#include <memory>
#include <mutex>
2024-12-11 23:23:48 +08:00
#include <net_base.h>
#include <of_util.h>
#include <string>
#include <unordered_map>
2024-12-11 23:23:48 +08:00
#include <util.h>
#include <vector>
using namespace ofen;
struct DownClientInfo {
std::vector<std::string> files;
2025-04-07 17:28:50 +08:00
std::string uuid;
std::string id;
};
enum TransState {
TRANS_FAILED,
TRANS_ING,
TRANS_REDAY,
TRANS_DONE,
TRANS_BREAK
};
struct TransInfomation {
std::string cur_remote_id_;
std::string cur_remote_file_;
std::string cur_file_;
std::fstream file_{};
uint16_t permissions{};
uint16_t remote_plat{};
TransState trans_state_{TRANS_FAILED};
};
2024-12-11 23:23:48 +08:00
constexpr int down_check_wait = 100; // millsec
class TransmClient
2024-12-11 23:23:48 +08:00
{
public:
TransmClient();
~TransmClient();
2024-12-11 23:23:48 +08:00
public:
void run(const std::string& ip, const std::string& port, const std::string& config_dir);
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);
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 = "");
private:
bool send_frame(CFrameBuffer* buf);
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);
bool save_uuid();
std::string read_uuid();
void get_id();
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);
private:
void handle_frame(CFrameBuffer* buf);
void send_file_data_th(const char* keys);
void hearts();
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-11 23:23:48 +08:00
private:
std::mutex mutex_;
std::mutex send_mut_;
std::string work_key_;
std::thread hearts_;
CThreadSleep sleep_;
bool th_run_{false};
bool will_receive_{false};
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_;
std::shared_ptr<TransInfomation> down_;
std::vector<std::thread> ths_;
std::map<std::string, std::shared_ptr<TransInfomation>> up_;
2025-04-13 00:08:50 +08:00
std::thread context_th_;
std::thread th_down_active_;
long long cur_file_size_{};
long long cur_down_size_{};
private:
std::string list_file_;
std::string list_server_id_;
std::string list_server_uuid_;
std::thread update_list_th_;
std::string own_id_{};
std::string config_path_{};
std::string uuid_path_{};
std::string uuid_{};
#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
};
class CFileOpr
{
public:
CFileOpr();
~CFileOpr();
public:
static bool get_file_list(const std::string& input, std::vector<std::string>& out);
2024-12-11 23:23:48 +08:00
};