Untitled

mail@pastecode.io avatar
unknown
c_cpp
7 months ago
3.0 kB
3
Indexable
Never
#include "chatclient.h"

ChatClient::ChatClient(QObject *parent)
    : QObject{parent}
{
    socket = new QTcpSocket;

    blockSize = 0;
    Data.clear();

    QObject::connect(socket, &QTcpSocket::readyRead, this, &ChatClient::slotReadyRead);
    QObject::connect(socket, &QTcpSocket::disconnected, socket, &QTcpSocket::deleteLater);
}

void ChatClient::connect(const QString address, const QString port, const QString name){
    QHostAddress tmp_address(address);
    unsigned short tmp_port = port.toUShort();
    socket->connectToHost(tmp_address, tmp_port);
    this->name = name;
    sendToServer(name, UserJoin);
}
void ChatClient::disconnect(){
    sendToServer(name, UserLeft);
    socket->disconnectFromHost();
}

void ChatClient::slotReadyRead(){
    socket = (QTcpSocket*) QObject::sender();
    QDataStream in(socket);
    in.setVersion(QDataStream::Qt_6_4);
    if(in.status()==QDataStream::Ok){
        for(;;){
            if(blockSize == 0){
                if(socket->bytesAvailable() <2){
                    break;
                }
                in >> blockSize;
            }
            if(socket->bytesAvailable() < blockSize){
                break;
            }
            QString message;
            quint16 message_type;
            in >> message_type >> message;
            switch (message_type){
            case 0: //Message
                emit displayMessage(message, QColor("black"));
                break;
            case 1: //UserJoin
                //message  = "User " + message + " join to this chat.";
                emit displayMessage("User " + message + " join to this chat.", QColor("green"));
                users.append(message);
                emit displayUser(message);
                break;
            case 2: //UserLeft
                //message  = "User " + message + " left this chat.";
                emit displayMessage("User " + message + " left this chat.", QColor("red"));
                users.remove(users.indexOf(message));
                emit hideUser(message);
                break;
            case 3: //RefuseConnection
                emit displayMessage("Server refuse connection", QColor("red"));
                socket->disconnectFromHost();
                break;
            case 4: //OnlineUsers
                users.append(message);
                emit displayUser(message);
                break;
            default:
                break;

            }

            blockSize = 0;
            //emit displayMessage(message);
        }
    }
    else{
        emit displayMessage("Read from socket error", QColor("red"));
    }
}

void ChatClient::sendToServer(QString message, quint16 message_type){
    Data.clear();
    QDataStream out(&Data, QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_6_4);
    out << quint16(0) << message_type << message;
    out.device()->seek(0);
    out << quint16(Data.size() - sizeof(quint16));
    socket->write(Data);
}
Leave a Comment