提交原始齿轮泵代码
This commit is contained in:
@@ -0,0 +1,147 @@
|
||||
#ifndef TCPSERVER_H
|
||||
#define TCPSERVER_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QTcpServer>
|
||||
#include <QTcpSocket>
|
||||
#include <QHash>
|
||||
#include <QTimer>
|
||||
#include <QHostAddress>
|
||||
#include <QDateTime>
|
||||
#include <QList>
|
||||
#include <QString>
|
||||
#include <QThread>
|
||||
#include "message.h"
|
||||
#include "serverthread.h"
|
||||
|
||||
/**
|
||||
TCP 服务端模块,用于处理外部tcp发来的数据
|
||||
**/
|
||||
|
||||
// 客户端连接信息
|
||||
struct ClientInfo {
|
||||
QString address;
|
||||
quint16 port;
|
||||
quint64 socketId;
|
||||
QByteArray buffer; // 接收缓冲区
|
||||
qint64 lastActiveTime; // 最后活跃时间
|
||||
quint64 totalReceived; // 总接收字节数
|
||||
quint64 totalSent; // 总发送字节数
|
||||
|
||||
ClientInfo() {
|
||||
port = 0;
|
||||
socketId = 0;
|
||||
lastActiveTime = 0;
|
||||
totalReceived = 0;
|
||||
totalSent = 0;
|
||||
}
|
||||
};
|
||||
|
||||
class TcpServer : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit TcpServer(QObject *parent = 0);
|
||||
~TcpServer();
|
||||
|
||||
// 服务器控制
|
||||
bool start(quint16 port, const QHostAddress &address = QHostAddress::Any);
|
||||
void stop();
|
||||
bool isRunning() const;
|
||||
|
||||
// 发送数据
|
||||
bool sendToClient(quint64 socketId, const QByteArray &data);
|
||||
bool sendToAll(const QByteArray &data);
|
||||
bool sendToAllExcept(quint64 socketId, const QByteArray &data);
|
||||
|
||||
// 线程管理
|
||||
void setWorkerThreadCount(int count);
|
||||
int workerThreadCount() const;
|
||||
|
||||
// 获取服务器状态
|
||||
quint16 serverPort() const;
|
||||
int clientCount() const;
|
||||
QList<ClientInfo> getClientList() const;
|
||||
ClientInfo getClientInfo(quint64 socketId) const;
|
||||
|
||||
// 配置选项
|
||||
void setMaxPendingConnections(int maxConnections);
|
||||
void setMaxBufferSize(int maxSize);
|
||||
void setHeartbeatInterval(int seconds);
|
||||
void setConnectionTimeout(int seconds);
|
||||
|
||||
signals:
|
||||
// 服务器状态信号
|
||||
void sig_serverStarted(quint16 port);
|
||||
void sig_serverStopped();
|
||||
void sig_serverError(const QString &error);
|
||||
|
||||
// 客户端连接信号
|
||||
void sig_clientConnected(quint64 socketId, const QString &address, quint16 port);
|
||||
void sig_clientDisconnected(quint64 socketId, const QString &reason);
|
||||
void sig_clientError(quint64 socketId, const QString &error);
|
||||
|
||||
// 数据接收信号
|
||||
void sig_dataReceived(quint64 socketId, const QByteArray &data);
|
||||
void sig_rawDataReceived(quint64 socketId, const QByteArray &rawData);
|
||||
|
||||
// 统计信号
|
||||
void sig_statisticsUpdated(int clientCount, int threadCount,
|
||||
quint64 totalReceived, quint64 totalSent);
|
||||
|
||||
// 日志信号
|
||||
void sig_logMessage(const QString &message, int level = 0);
|
||||
|
||||
private slots:
|
||||
void slt_onNewConnection();
|
||||
void slt_onReadyRead();
|
||||
void slt_onDisconnected();
|
||||
void slt_onSocketError(QAbstractSocket::SocketError socketError);
|
||||
void slt_onHeartbeatCheck();
|
||||
void slt_onTaskProcessed(quint64 socketId, const QByteArray &processedData);
|
||||
void slt_onTaskError(quint64 socketId, const QString &error);
|
||||
|
||||
private:
|
||||
// 核心组件
|
||||
QTcpServer *m_server;
|
||||
QHash<quint64, QTcpSocket*> m_clientMap;
|
||||
QHash<quint64, ClientInfo> m_clientInfoMap;
|
||||
Message m_message;
|
||||
|
||||
// 工作线程池
|
||||
QList<ServerThread*> m_workerThreads;
|
||||
int m_nextThreadIndex;
|
||||
|
||||
// 配置参数
|
||||
int m_maxPendingConnections; //最大连接数量
|
||||
int m_maxBufferSize;
|
||||
int m_heartbeatInterval; //心跳
|
||||
int m_connectionTimeout;
|
||||
int m_workerThreadCount; //工作线程数量
|
||||
|
||||
// 定时器
|
||||
QTimer *m_heartbeatTimer;
|
||||
|
||||
// 统计信息
|
||||
quint64 m_nextSocketId;
|
||||
quint64 m_totalReceived;
|
||||
quint64 m_totalSent;
|
||||
|
||||
// 内部方法
|
||||
void setupSocket(QTcpSocket *socket);
|
||||
void processBuffer(quint64 socketId);
|
||||
void distributeToWorkerThread(quint64 socketId, const QByteArray &data, int taskType);
|
||||
void cleanupClient(quint64 socketId, const QString &reason);
|
||||
void updateClientActivity(quint64 socketId);
|
||||
bool validateSocket(quint64 socketId);
|
||||
void emitStatistics();
|
||||
ServerThread* getNextWorkerThread();
|
||||
|
||||
// Qt4兼容的辅助方法
|
||||
qint64 getCurrentTimestamp() const;
|
||||
QString getPeerAddress(QTcpSocket *socket) const;
|
||||
quint16 getPeerPort(QTcpSocket *socket) const;
|
||||
};
|
||||
|
||||
#endif // TCPSERVER_H
|
||||
Reference in New Issue
Block a user