Files
RkApp/ApCreate/NetraLib/include/Netra.hpp

412 lines
16 KiB
C++
Raw Normal View History

2025-09-28 16:03:54 +08:00
#pragma once
#include "QCL_Include.hpp"
namespace QCL
{
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* @class TcpServer
* @brief 线TCP服务器类
*
* 使线
* 线
* 线Socket句柄
*/
class TcpServer
{
public:
/**
* @brief
* @param port
*/
TcpServer(int port);
/**
* @brief stop()
*/
~TcpServer();
/**
* @brief socket线
* @return truefalse
*/
bool start();
/**
* @brief 线退
*/
void stop();
/**
* @brief
* @param clientSock Socket描述符
* @param message
*/
void sendToClient(int clientSock, const std::string &message);
/**
* @brief
* @param clientSock Socket描述符
* @param flag:false ,true
*/
std::string receiveFromClient(int clientSock, bool flag = true);
/**
* @brief IP和端口
* @param clientSock Socket描述符
*/
char *getClientIPAndPort(int clientSock);
/**
* @brief Socket的副本
* @return Socket的vector线
*/
std::vector<int> getClientSockets();
private:
/**
* @brief 线
*/
void acceptClients();
private:
int serverSock_; ///< 服务器监听Socket描述符
int port_; ///< 服务器监听端口
std::atomic<bool> running_; ///< 服务器运行状态标志(线程安全)
std::vector<std::thread> clientThreads_; ///< 用于处理每个客户端的线程集合
std::thread acceptThread_; ///< 负责监听新连接的线程
std::mutex clientsMutex_; ///< 保护clientSockets_的互斥锁
std::vector<int> clientSockets_; ///< 当前所有连接的客户端Socket集合
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* @brief 线
*
*
* -
* -
* -
* -
* -
*
*
* -
* -
* -
* - 使 std::mutex 线
*/
class WriteFile
{
public:
/**
* @brief
* @param filePath
*/
explicit WriteFile(const std::string &filePath);
/**
* @brief 线
* @param content
* @return true
* @return false
*/
bool overwriteText(const std::string &content);
/**
* @brief 线
* @param content
* @return true
* @return false
*/
bool appendText(const std::string &content);
/**
* @brief 线
* @param data
* @return true
* @return false
*/
bool overwriteBinary(const std::vector<char> &data);
/**
* @brief 线
* @param data
* @return true
* @return false
*/
bool appendBinary(const std::vector<char> &data);
/**
* @brief
* @param pattern
* @param includePattern true pattern false
* @return size_t pattern 0
*/
size_t countBytesPattern(const std::string &pattern, bool includePattern = false);
/**
* @brief
* @param pattern
* @param content
* @return true false
*
*
* 1. pattern pattern content
* 2. pattern content
*/
bool writeAfterPatternOrAppend(const std::string &pattern, const std::string &content);
/**
* @brief
* @param content
* @param pos
* @param length >= content.size() < content.size() length
* @return true false
*
*
* 1. pos length
* 2. length > content.size() content '\0'
* 3. length < content.size() content length
* 4. length
*
*
* : "ABCDEFG"
* insertAfterPos("XY", 2, 3) // 在索引 2 后插入
* : "ABX Y\0CDEFG" ( \0 )
*/
bool insertAfterPos(const std::string &content, size_t pos, size_t length);
/**
* @brief
* @param content
* @param pos
* @param length
* @return true false pos
*
*
* 1. pos length
* 2. content.size() >= length length
* 3. content.size() < length content '\0' length
* 4. pos + length
*
*
* : "ABCDEFG"
* overwriteAtPos("XY", 2, 3)
* : "ABXYEFG" "CDE" "XY\0" \0
*/
bool overwriteAtPos(const std::string &content, size_t pos, size_t length);
private:
std::string filePath_; ///< 文件路径
std::mutex writeMutex_; ///< 线程锁,保证多线程写入安全
/**
* @brief 线
* @param content
* @param mode /
* @return true
* @return false
*/
bool writeToFile(const std::string &content, std::ios::openmode mode);
/**
* @brief 线
* @param data
* @param mode /
* @return true
* @return false
*/
bool writeBinary(const std::vector<char> &data, std::ios::openmode mode);
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* @brief ReadFile -
*
*
* 1. /
* 2.
* 3.
* 4.
* 5.
* 6.
*
*
* - WriteFile
* -
* -
* - 使 std::mutex 线
*/
class ReadFile
{
public:
/**
* @brief
* @param filename
*/
explicit ReadFile(const std::string &filename);
/**
* @brief
*/
~ReadFile();
/**
* @brief
* @return true
* @return false
*/
bool Open();
/**
* @brief
*/
void Close();
/**
* @brief
*/
bool IsOpen() const;
/**
* @brief
* @return
*/
std::string ReadAllText();
/**
* @brief
* @return
*/
std::vector<char> ReadAllBinary();
/**
* @brief
* @return vector
*/
std::vector<std::string> ReadLines();
/**
* @brief
* @param count
* @return
*/
std::vector<char> ReadBytes(size_t count);
/**
* @brief
* @param marker
* @return 0
*/
size_t GetBytesBefore(const std::string &marker, bool includeMarker = false);
/**
* @brief
* @param pos
* @param count 0
* @return
*/
std::vector<char> ReadBytesFrom(size_t pos, size_t count = 0);
/**
* @brief
*/
bool FileExists() const;
/**
* @brief
*/
size_t GetFileSize() const;
/**
* @brief
*/
void Reset();
private:
std::string filename_; // 文件路径
std::ifstream file_; // 文件流对象
mutable std::mutex mtx_; // 可变,保证 const 方法也能加锁
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 屏蔽所有信号
void blockAllSignals();
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 字符串操作
// 去除字符串的左空格
std::string Ltrim(const std::string &s);
// 去除字符串右侧的空格
std::string Rtrim(const std::string &s);
// 去除字符串左右两侧的空格
std::string LRtrim(const std::string &s);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// c++进行格式化输出
// 通用类型转字符串
template <typename T>
std::string to_string_any(const T &value)
{
std::ostringstream oss;
oss << value;
return oss.str();
}
// 递归获取 tuple 中 index 对应参数
template <std::size_t I = 0, typename Tuple>
std::string get_tuple_arg(const Tuple &tup, std::size_t index)
{
if constexpr (I < std::tuple_size_v<Tuple>)
{
if (I == index)
return to_string_any(std::get<I>(tup));
else
return get_tuple_arg<I + 1>(tup, index);
}
else
{
throw std::runtime_error("Too few arguments for format string");
}
}
// format 函数
template <typename... Args>
std::string format(const std::string &fmt, const Args &...args)
{
std::ostringstream oss;
std::tuple<const Args &...> tup(args...);
size_t pos = 0;
size_t arg_idx = 0;
while (pos < fmt.size())
{
if (fmt[pos] == '{' && pos + 1 < fmt.size() && fmt[pos + 1] == '{')
{
oss << '{';
pos += 2;
}
else if (fmt[pos] == '}' && pos + 1 < fmt.size() && fmt[pos + 1] == '}')
{
oss << '}';
pos += 2;
}
else if (fmt[pos] == '{' && pos + 1 < fmt.size() && fmt[pos + 1] == '}')
{
oss << get_tuple_arg(tup, arg_idx++);
pos += 2;
}
else
{
oss << fmt[pos++];
}
}
if (arg_idx < sizeof...(Args))
throw std::runtime_error("Too many arguments for format string");
return oss.str();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}