server: base server code.

This commit is contained in:
2025-06-14 09:27:50 +08:00
parent ff14e30d31
commit 16572fa29c
14 changed files with 500 additions and 3 deletions

50
Server/Server.h Normal file
View File

@@ -0,0 +1,50 @@
// Server.h
#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);
QMap<QString, QSharedPointer<ClientInfo>> clients_;
QReadWriteLock rwLock_;
QTimer* monitorTimer_;
};
#endif // SERVER_H