36 lines
1 KiB
C++
36 lines
1 KiB
C++
#pragma once
|
|
|
|
#include <QTcpServer>
|
|
#include <QTcpSocket>
|
|
#include <QMap>
|
|
#include <QByteArray>
|
|
#include <QString>
|
|
|
|
class ChatServer : public QTcpServer
|
|
{
|
|
Q_OBJECT
|
|
|
|
public:
|
|
explicit ChatServer(QObject *parent = nullptr);
|
|
|
|
protected:
|
|
void incomingConnection(qintptr socketDescriptor) override;
|
|
|
|
private slots:
|
|
void onReadyRead();
|
|
void onDisconnected();
|
|
|
|
private:
|
|
void processFrame(QTcpSocket *socket, const QByteArray &data);
|
|
void broadcast(QTcpSocket *exclude, const QByteArray &frame);
|
|
void sendSystemMessage(QTcpSocket *socket, const QString &text);
|
|
void broadcastUserList();
|
|
QByteArray buildFrame(const QString &type, const QString &from,
|
|
const QString &text, const QString &ts);
|
|
|
|
// max bytes we buffer per client before dropping them (prevents memory abuse)
|
|
static constexpr int MAX_BUFFER_SIZE = 1024 * 64; // 64 KB
|
|
|
|
QMap<QTcpSocket *, QString> m_clients; // socket → username
|
|
QMap<QTcpSocket *, QByteArray> m_buffers; // socket → incoming data buffer
|
|
};
|