提交原始齿轮泵代码
This commit is contained in:
@@ -0,0 +1,156 @@
|
||||
#include "serverthread.h"
|
||||
#include <QDebug>
|
||||
#include <QDateTime>
|
||||
#include <QStringList>
|
||||
|
||||
ServerThread::ServerThread(QObject *parent)
|
||||
: QThread(parent)
|
||||
, m_running(false)
|
||||
, m_processedCount(0)
|
||||
, m_totalProcessedBytes(0)
|
||||
{
|
||||
qDebug() << "ServerThread 创建 - 线程ID:" << QThread::currentThreadId();
|
||||
}
|
||||
|
||||
ServerThread::~ServerThread()
|
||||
{
|
||||
stopThread();
|
||||
qDebug() << "ServerThread 销毁 - 处理任务数:" << m_processedCount;
|
||||
}
|
||||
|
||||
void ServerThread::stopThread()
|
||||
{
|
||||
qDebug() << "ServerThread 停止中...";
|
||||
|
||||
m_running = false;
|
||||
m_condition.wakeAll();
|
||||
|
||||
if (isRunning()) {
|
||||
wait(5000); // 等待最多5秒
|
||||
if (isRunning()) {
|
||||
qWarning() << "ServerThread 强制终止";
|
||||
terminate();
|
||||
wait();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ServerThread::addTask(const WorkTask &task)
|
||||
{
|
||||
QMutexLocker locker(&m_mutex);
|
||||
m_taskQueue.enqueue(task);
|
||||
m_condition.wakeOne();
|
||||
}
|
||||
|
||||
int ServerThread::pendingTasks() const
|
||||
{
|
||||
QMutexLocker locker(&m_mutex);
|
||||
return m_taskQueue.size();
|
||||
}
|
||||
|
||||
quint64 ServerThread::processedCount() const
|
||||
{
|
||||
return m_processedCount;
|
||||
}
|
||||
|
||||
quint64 ServerThread::totalProcessedBytes() const
|
||||
{
|
||||
return m_totalProcessedBytes;
|
||||
}
|
||||
|
||||
bool ServerThread::isProcessing() const
|
||||
{
|
||||
QMutexLocker locker(&m_mutex);
|
||||
return !m_taskQueue.isEmpty();
|
||||
}
|
||||
|
||||
void ServerThread::run()
|
||||
{
|
||||
m_running = true;
|
||||
|
||||
while (m_running) {
|
||||
WorkTask task;
|
||||
bool hasTask = false;
|
||||
|
||||
{
|
||||
QMutexLocker locker(&m_mutex);
|
||||
|
||||
if (m_taskQueue.isEmpty()) {
|
||||
// 等待新任务,超时1秒后继续循环检查m_running
|
||||
m_condition.wait(&m_mutex, 1000);
|
||||
continue;
|
||||
}
|
||||
|
||||
task = m_taskQueue.dequeue();
|
||||
hasTask = true;
|
||||
}
|
||||
|
||||
if (!hasTask || !m_running) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 处理任务
|
||||
try {
|
||||
QByteArray result;
|
||||
|
||||
switch (task.taskType) {
|
||||
case 0: // 处理接收到的数据
|
||||
result = processReceivedData(task.data);
|
||||
break;
|
||||
|
||||
case 1: // 打包待发送数据
|
||||
result = packSendData(task.data);
|
||||
break;
|
||||
|
||||
default:
|
||||
emit sig_taskError(task.socketId, QString("未知任务类型: %1").arg(task.taskType));
|
||||
continue;
|
||||
}
|
||||
|
||||
// 更新统计
|
||||
m_processedCount++;
|
||||
m_totalProcessedBytes += task.data.size();
|
||||
|
||||
// 发送处理结果
|
||||
if (!result.isEmpty()) {
|
||||
emit sig_taskProcessed(task.socketId, result);
|
||||
}
|
||||
|
||||
} catch (const std::exception &e) {
|
||||
emit sig_taskError(task.socketId, QString("处理异常: %1").arg(e.what()));
|
||||
} catch (...) {
|
||||
emit sig_taskError(task.socketId, "未知处理异常");
|
||||
}
|
||||
|
||||
// 短暂休眠,避免CPU占用过高
|
||||
msleep(1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
QByteArray ServerThread::processReceivedData(const QByteArray &data)
|
||||
{
|
||||
QString text = QString::fromUtf8(data);
|
||||
|
||||
// 普通数据处理示例
|
||||
QByteArray result;
|
||||
|
||||
// 添加时间戳前缀
|
||||
QString timestamp = QDateTime::currentDateTime().toString("[hh:mm:ss] ");
|
||||
result = timestamp.toUtf8();
|
||||
|
||||
// 回显处理后的数据
|
||||
result += text.toUtf8();
|
||||
|
||||
// 可以在此添加更多业务逻辑
|
||||
// 例如:数据验证、转换、存储等
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
QByteArray ServerThread::packSendData(const QByteArray &data)
|
||||
{
|
||||
// 添加到待发送队列或进行预处理
|
||||
// 这里简单返回原数据,实际打包在TcpServer中完成
|
||||
return data;
|
||||
}
|
||||
Reference in New Issue
Block a user