2024-12-11 22:51:43 +08:00
|
|
|
#pragma once
|
2024-12-12 22:43:24 +08:00
|
|
|
#include "of_util.h"
|
2025-01-21 14:39:38 +08:00
|
|
|
#include <chrono>
|
2024-12-12 22:43:24 +08:00
|
|
|
#include <cstdint>
|
2024-12-12 23:11:55 +08:00
|
|
|
#include <functional>
|
2025-01-21 14:39:38 +08:00
|
|
|
#include <iomanip>
|
2025-01-21 14:47:42 +08:00
|
|
|
#include <sstream>
|
2025-01-21 14:39:38 +08:00
|
|
|
#include <spdlog/fmt/bundled/color.h>
|
|
|
|
#include <spdlog/fmt/fmt.h>
|
2024-12-11 10:22:14 +08:00
|
|
|
#include <spdlog/sinks/rotating_file_sink.h>
|
|
|
|
#include <spdlog/sinks/stdout_color_sinks.h>
|
|
|
|
#include <spdlog/spdlog.h>
|
2024-12-12 22:43:24 +08:00
|
|
|
|
2024-12-20 13:22:28 +08:00
|
|
|
constexpr size_t g_BuffSize = 102400;
|
|
|
|
const size_t MAX_FRAME_SIZE = 10 * g_BuffSize;
|
2024-12-14 11:57:33 +08:00
|
|
|
enum FrameType : int16_t {
|
|
|
|
TYPE_DEFAULT = 0,
|
|
|
|
TYPE_GET_LIST,
|
|
|
|
TYPE_DOWN_LIST,
|
|
|
|
TYPE_UP_LIST,
|
|
|
|
TYPE_CANCEL_LIST,
|
|
|
|
TYPE_OPEN_FILE,
|
|
|
|
TYPE_TRANS_FILE,
|
2024-12-14 16:20:25 +08:00
|
|
|
TYPE_TRANS_DONE,
|
2024-12-14 11:57:33 +08:00
|
|
|
TYPE_INTERRUPT,
|
|
|
|
TYPE_NO_HIT_TASK,
|
2024-12-18 13:55:56 +08:00
|
|
|
TYPE_WAITTING,
|
2024-12-18 16:55:32 +08:00
|
|
|
TYPE_HEARTS,
|
2024-12-18 22:04:54 +08:00
|
|
|
TYPE_OFFLINE,
|
2024-12-20 16:57:44 +08:00
|
|
|
TYPE_JUDGE_ACTIVE,
|
|
|
|
TYPE_REQUEST_UPDATE_LIST,
|
|
|
|
TYPE_CONFIRM_UPDATE_LIST,
|
|
|
|
TYPE_UNCONFIRM_UPDATE_LIST,
|
|
|
|
TYPE_DONE_UPDATE_LIST,
|
2024-12-21 23:24:18 +08:00
|
|
|
TYPE_FAILED_UPDATE_LIST,
|
2025-01-07 12:07:43 +08:00
|
|
|
TYPE_GET_ID,
|
|
|
|
TYPE_FILE_SIZE
|
2024-12-14 11:57:33 +08:00
|
|
|
};
|
2024-12-11 10:22:14 +08:00
|
|
|
|
2024-12-11 17:00:59 +08:00
|
|
|
using namespace ofen;
|
2024-12-11 10:22:14 +08:00
|
|
|
std::shared_ptr<spdlog::logger> get_logger(const std::string& mark, const std::string& log_file);
|
2024-12-11 17:00:59 +08:00
|
|
|
class CFrameBuffer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CFrameBuffer();
|
|
|
|
~CFrameBuffer();
|
2024-12-13 12:35:08 +08:00
|
|
|
|
2024-12-12 23:11:55 +08:00
|
|
|
public:
|
2024-12-14 11:57:33 +08:00
|
|
|
std::string fid_{};
|
|
|
|
std::string tid_{};
|
2024-12-12 22:43:24 +08:00
|
|
|
|
2024-12-11 22:51:43 +08:00
|
|
|
public:
|
2024-12-14 11:57:33 +08:00
|
|
|
FrameType type_{};
|
2024-12-11 22:51:43 +08:00
|
|
|
char* data_{};
|
|
|
|
int len_{};
|
2024-12-12 22:43:24 +08:00
|
|
|
char mark_{};
|
2024-12-11 17:00:59 +08:00
|
|
|
};
|
2024-12-13 12:35:08 +08:00
|
|
|
|
2024-12-12 23:11:55 +08:00
|
|
|
using ExFun_t = std::function<void(CFrameBuffer* buf)>;
|
2024-12-11 17:00:59 +08:00
|
|
|
/*
|
|
|
|
【 transm TCP 数据协议 】
|
|
|
|
header 2 char: 0xFF 0xFE
|
2024-12-12 22:43:24 +08:00
|
|
|
type 2 char:
|
|
|
|
mark 1 char:
|
2024-12-14 16:20:25 +08:00
|
|
|
from 32 char:
|
|
|
|
to 32 char:
|
2024-12-11 17:00:59 +08:00
|
|
|
len 4 char:
|
|
|
|
data xxxxx:
|
|
|
|
tail 2 char: 0xFF 0xFF
|
|
|
|
*/
|
|
|
|
class CTransProtocal
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CTransProtocal();
|
|
|
|
~CTransProtocal();
|
2024-12-12 22:43:24 +08:00
|
|
|
|
2024-12-11 17:00:59 +08:00
|
|
|
public:
|
2024-12-11 22:51:43 +08:00
|
|
|
static CFrameBuffer* parse(CMutBuffer& buffer);
|
|
|
|
static bool pack(CFrameBuffer* buf, char** out_buf, int& len);
|
2025-01-07 12:07:43 +08:00
|
|
|
static void display_progress(float percent);
|
2024-12-13 16:59:31 +08:00
|
|
|
};
|
2025-01-21 14:39:38 +08:00
|
|
|
|
|
|
|
inline std::string now_str()
|
|
|
|
{
|
|
|
|
auto now = std::chrono::system_clock::now();
|
|
|
|
auto time_t_now = std::chrono::system_clock::to_time_t(now);
|
|
|
|
auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
|
|
|
|
|
|
|
|
std::ostringstream timestamp;
|
|
|
|
timestamp << std::put_time(std::localtime(&time_t_now), "[%Y-%m-%d %H:%M:%S") << "." << std::setfill('0')
|
|
|
|
<< std::setw(3) << milliseconds.count() << "] ";
|
|
|
|
|
|
|
|
return timestamp.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Args> void pdebug(const std::string& format, Args&&... args)
|
|
|
|
{
|
|
|
|
fmt::print(fg(fmt::color::steel_blue), now_str() + format + "\n", std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
template <typename... Args> void pinfo(const std::string& format, Args&&... args)
|
|
|
|
{
|
|
|
|
fmt::print(fg(fmt::color::gray), now_str() + format + "\n", std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
template <typename... Args> void pwarn(const std::string& format, Args&&... args)
|
|
|
|
{
|
|
|
|
fmt::print(fg(fmt::color::yellow_green), now_str() + format + "\n", std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
template <typename... Args> void perror(const std::string& format, Args&&... args)
|
|
|
|
{
|
|
|
|
fmt::print(fg(fmt::color::orange_red), now_str() + format + "\n", std::forward<Args>(args)...);
|
|
|
|
}
|