Files
2026-01-07 21:26:28 +08:00

77 lines
1.6 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef SOCKETACTIONS_H
#define SOCKETACTIONS_H
#include <QObject>
#include <QTcpSocket>
#include <QTimer>
#include <QMutex>
#include <QWaitCondition>
#include <QElapsedTimer>
#include <QThread>
#include <QDateTime>
#include <QHostAddress>
#include <memory>
#include <string>
namespace sockAct {
// 日志级别枚举与Logger.h保持一致
enum LogLevel {
DEBUG = 0,
INFO = 1,
WARNING = 2,
ERROR = 3,
FATAL = 4
};
/**
* @brief 初始化Socket模块
*
* 初始化Qt网络模块必须在使用其他socket函数前调用
* @return 初始化是否成功
*/
bool sockInit();
/**
* @brief 连接到TCP服务器
*
* @param host 服务器地址
* @param port 服务器端口
* @param timeoutMs 连接超时时间(毫秒)
* @return 连接是否成功
*/
bool connectToHost(const std::string& host, quint16 port, int timeoutMs = 3000);
/**
* @brief 发送TCP数据并接收响应
*
* 发送数据到已连接的服务器并等待最多3秒接收响应
* 一旦收到数据立即返回否则等待3秒后返回空字符串
*
* @param data 要发送的数据
* @param timeoutMs 接收超时时间(毫秒)
* @return 接收到的数据,如果超时或出错则返回空字符串
*/
std::string tcpSend(const std::string& data, int timeoutMs = 3000);
/**
* @brief 断开连接并清理Socket资源
*/
void clearSocket();
/**
* @brief 检查是否已连接到服务器
* @return 连接状态
*/
bool isConnected();
/**
* @brief 获取最后一次的错误信息
* @return 错误描述
*/
std::string getLastError();
} // namespace sockAct
#endif // SOCKETACTIONS_H