2025-06-20 10:33:03 +08:00
|
|
|
// Server.h
|
2025-06-14 09:27:50 +08:00
|
|
|
#ifndef SERVER_H
|
|
|
|
|
#define SERVER_H
|
|
|
|
|
|
|
|
|
|
#include <QMap>
|
|
|
|
|
#include <QReadWriteLock>
|
|
|
|
|
#include <QTcpServer>
|
|
|
|
|
#include <QTcpSocket>
|
|
|
|
|
#include <QTimer>
|
|
|
|
|
|
|
|
|
|
#include "Protocol.h"
|
|
|
|
|
|
|
|
|
|
class Server : public QTcpServer
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
public:
|
|
|
|
|
explicit Server(QObject* parent = nullptr);
|
|
|
|
|
~Server();
|
|
|
|
|
|
|
|
|
|
bool startServer(quint16 port);
|
|
|
|
|
void stopServer();
|
|
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
|
void onNewConnection();
|
|
|
|
|
void onClientDisconnected();
|
|
|
|
|
void onReadyRead();
|
|
|
|
|
void monitorClients();
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
QByteArray getClients();
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
struct ClientInfo {
|
|
|
|
|
QTcpSocket* socket;
|
|
|
|
|
QString id;
|
|
|
|
|
qint64 connectTime;
|
|
|
|
|
QByteArray buffer;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void processClientData(QSharedPointer<ClientInfo> client);
|
|
|
|
|
bool forwardData(QSharedPointer<ClientInfo> client, QSharedPointer<FrameBuffer> frame);
|
|
|
|
|
void replyRequest(QSharedPointer<ClientInfo> client, QSharedPointer<FrameBuffer> frame);
|
|
|
|
|
bool sendData(QTcpSocket* socket, QSharedPointer<FrameBuffer> frame);
|
|
|
|
|
|
2025-11-09 11:53:59 +08:00
|
|
|
// 流量控制
|
|
|
|
|
bool sendWithFlowCheck(QTcpSocket* fsoc, QTcpSocket* tsoc, QSharedPointer<FrameBuffer> frame);
|
|
|
|
|
BlockLevel getBlockLevel(QTcpSocket* socket);
|
|
|
|
|
|
2025-06-15 20:37:25 +08:00
|
|
|
QString id_;
|
2025-06-14 09:27:50 +08:00
|
|
|
QMap<QString, QSharedPointer<ClientInfo>> clients_;
|
2025-11-09 11:53:59 +08:00
|
|
|
QMap<QString, int> flowBackCount_;
|
2025-06-14 09:27:50 +08:00
|
|
|
QReadWriteLock rwLock_;
|
|
|
|
|
QTimer* monitorTimer_;
|
|
|
|
|
};
|
|
|
|
|
|
2025-06-21 12:08:01 +08:00
|
|
|
#endif // SERVER_H
|