upload all files
This commit is contained in:
76
Modules/socketactions.h
Normal file
76
Modules/socketactions.h
Normal file
@@ -0,0 +1,76 @@
|
||||
#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
|
||||
Reference in New Issue
Block a user