代码拉取完成,页面将自动刷新
#include "my_tcpsocket.h"
#include <QMessageBox>
#include "client_socket.h"
#include <QStandardItem>
my_tcpsocket::my_tcpsocket(QObject *parent)
    : QTcpSocket{parent}
{
    downloadFileInfo = new TransFile;
    downloadFileInfo->bTransform=false;
    // timer = new QTimer;
    // connect(timer,&QTimer::timeout,client_socket::getInstance().getM_pServerFileSystem(),&serverFileSystem::UpdateFileInfo);
}
void my_tcpsocket::handleRegistRespond(PDU *pdu)
{
    uint ret = pdu->msg[0];
    if(ret)
    {
        QMessageBox::information(NULL,"注册","注册成功");
    }
    else
    {
        QMessageBox::information(NULL,"注册","注册失败");
    }
}
void my_tcpsocket::handleLoginRespond(PDU *pdu)
{
    uint ret = pdu->msg[0] - '0';
    if(ret)
    {
        QMessageBox::information(NULL,"登录","登录成功");
        client_socket::getInstance().hide();
        QString currentPath = QDir::currentPath();
        QString userPath = currentPath+"/"+client_socket::getInstance().getM_userID();
        client_socket::getInstance().setM_rootPath(userPath);
        client_socket::getInstance().get_mainWidget() = new OperateWidget();
        client_socket::getInstance().get_mainWidget()->show();
    }
    else
    {
        QMessageBox::information(NULL,"登录","登录失败");
    }
}
void my_tcpsocket::handleChatPrivate(PDU *pdu)
{
    client_socket::getInstance().get_mainWidget()->get_ChatWidget()->handlePrivateChat(pdu);
}
void my_tcpsocket::handleChatFlushFriend(PDU *pdu)
{
    client_socket::getInstance().get_mainWidget()->get_ChatWidget()->FlushFriendList(pdu);
}
void my_tcpsocket::handleAddFriend(PDU *pdu)
{
    QString msg = pdu->msg;
    QStringList list = msg.split('-');
    QString myID = list[0];
    QString otherID=list[1];
    QDialog dialog;
    dialog.setWindowTitle("add friend");
    // 创建一个布局
    QVBoxLayout* layout = new QVBoxLayout(&dialog);
    QLabel *infoLabel = new QLabel(QString("你(%1)确定添加%2为好友吗").arg(myID).arg(otherID), &dialog);
    layout->addWidget(infoLabel);
    // 创建一个按钮框,包含 OK 和 Cancel 按钮
    QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, &dialog);
    layout->addWidget(buttonBox);
    // 连接按钮框中的按钮到对应的槽函数
    QObject::connect(buttonBox, &QDialogButtonBox::accepted, &dialog, &QDialog::accept);
    QObject::connect(buttonBox, &QDialogButtonBox::rejected, &dialog, &QDialog::reject);
    // 执行对话框,等待用户操作
    if (dialog.exec() == QDialog::Accepted) {
        // 用户点击了 OK 按钮,获取输入的好友 ID
        pdu->uiMsgType=MSG_TYPE::MSG_TYPE_CHAT_ADD_FRIEND_RESPOND;
        client_socket::getInstance().getTcpSocket() .write((char*)pdu,pdu->uiPduLen);
    }
}
void my_tcpsocket::handleAddFriendSuccess(PDU *pdu)
{
    uint ret = pdu->msg[0];
    if(ret)
    {
        client_socket::getInstance().get_mainWidget()->get_ChatWidget()->flushFriend();
    }
    else
    {
        QMessageBox::information(NULL,"添加好友","对方拒绝添加你为好友");
    }
}
void my_tcpsocket::handleFileUploadRespond(PDU *pdu)
{
    uint ret = QString(pdu->msg[0]).toUInt();
    switch (ret) {
    case 0:
    {
        qDebug()<<"文件上传失败";
        if(this->state() ==QAbstractSocket::ConnectedState)
        {
            this->disconnectFromHost();
            if(this->state()!=QAbstractSocket::UnconnectedState)
            {
                this->waitForDisconnected(3000);
            }
        }
        client_socket::getInstance().get_mainWidget()->get_fileSystem()->handleThread();
        break;
    }
    case 1:
    {
        emit readyUpdate();
        break;
    }
    case 2:
    {
        qDebug()<<"文件上传成功";
        if(this->state() ==QAbstractSocket::ConnectedState)
        {
            this->disconnectFromHost();
            if(this->state()!=QAbstractSocket::UnconnectedState)
            {
                this->waitForDisconnected(3000);
            }
        }
       client_socket::getInstance().get_mainWidget()->get_fileSystem()->handleThread();
        break;
    }
    default:
        break;
    }
}
void my_tcpsocket::handleFileDownloadRespond(PDU *pdu)
{
    // qDebug()<<"处理文件下载回应";
    uint ret = QString(pdu->msg[0]).toUInt();
    // qDebug()<<ret;
    if(ret==0)
    {
        // QMessageBox::information(NULL,"下载文件","下载文件失败");
        qDebug()<<"文件下载失败";
        if(this->state() ==QAbstractSocket::ConnectedState)
        {
            this->disconnectFromHost();
            if(this->state()!=QAbstractSocket::UnconnectedState)
            {
                this->waitForDisconnected(3000);
            }
        }
   client_socket::getInstance().get_mainWidget()->get_fileSystem()->handleThread();
    }
    else
    {
        // QMessageBox::information(NULL,"下载文件","下载文件开始");
        qint64 ifileSize = 0;
        QString msg= pdu->msg;
        QStringList list = msg.split(' ');
        ifileSize = list[1].toLongLong();
        QString filepath = list[0];
        // qDebug() << "下载文件中:" << filepath << ifileSize;
        if(filepath.size() > 0 && downloadFileInfo->file.open(QIODevice::WriteOnly))
        {
            downloadFileInfo->bTransform = true;
            downloadFileInfo->iTotalSize = ifileSize;
            downloadFileInfo->iReceivedSize = 0;
        }
        else
        {
            qDebug()<<"文件下载失败";
            client_socket::getInstance().get_mainWidget()->get_fileSystem()->handleThread();
        }
    }
}
void my_tcpsocket::handleFileShareRespond(PDU *pdu)
{
}
void my_tcpsocket::handleFileCreateRespond(PDU *pdu)
{
    uint ret = QString(pdu->msg[0]).toUInt();
    switch (ret) {
    case 0:
    {
        QMessageBox::information(NULL,"创建文件","创建文件失败");
        break;
    }
    case 1:
    {
        QMessageBox::information(NULL,"创建文件","创建文件成功");
        break;
    }
    case 2:
    {
        QMessageBox::information(NULL,"创建文件","文件或目录已存在");
        break;
    }
    default:
        break;
    }
}
void my_tcpsocket::handleFileMoveRespond(PDU *pdu)
{
    uint ret = QString(pdu->msg[0]).toUInt();
    switch (ret) {
    case 0:
    {
        QMessageBox::information(NULL,"移动文件","移动文件失败");
        break;
    }
    case 1:
    {
        QMessageBox::information(NULL,"移动文件","移动文件成功");
        break;
    }
    default:
        break;
    }
}
void my_tcpsocket::handleFileDeleteRespond(PDU *pdu)
{
    uint ret = QString(pdu->msg[0]).toUInt();
    switch (ret) {
    case 0:
    {
        QMessageBox::information(NULL,"删除文件","文件夹不存在或为空");
        break;
    }
    case 1:
    {
        QMessageBox::information(NULL,"删除文件","删除文件失败(可能权限不足)");
        break;
    }
    case 2:
    {
        QMessageBox::information(NULL,"删除文件","删除文件成功");
        break;
    }
    default:
        break;
    }
}
void my_tcpsocket::handleFileUpdateFileInfoRespond(PDU *pdu)
{
    // qDebug()<<"handleFileUpdateInfo";
    uint ret = pdu->msg[0] - '0';
    // qDebug()<<ret;
    if(ret)
    {
        client_socket::getInstance().get_mainWidget()->get_fileSystem()->flushTreeView(pdu);
        // QMessageBox::information(NULL,"刷新文件","刷新文件成功");
    }
    else
    {
        QMessageBox::information(NULL,"刷新文件","文件夹不存在或为空");
    }
}
TransFile *my_tcpsocket::get_downloadFileInfo()
{
    return downloadFileInfo;
}
// QTimer *my_tcpsocket::getTimer()
// {
//     return timer;
// }
void my_tcpsocket::handleMessage(PDU *pdu)
{
    MSG_TYPE ret = discerning_type(pdu);
    if (ret == MSG_TYPE::MSG_TYPE_LOGIN )
    {
        handleLogin(pdu);
    }
    else if (MSG_TYPE::MSG_TYPE_CHAT == ret)
    {
        handleChat(pdu);
    }
    else if (MSG_TYPE::MSG_TYPE_FILE == ret)
    {
        handleFile(pdu);
    }
}
MSG_TYPE my_tcpsocket::discerning_type(PDU *pdu)
{
    uint type = static_cast<uint>(pdu->uiMsgType);
    if (type & 0x00ff0000)
    {
        return MSG_TYPE::MSG_TYPE_FILE;
    }
    else if (type & 0x0000ff00)
    {
        return MSG_TYPE::MSG_TYPE_CHAT;
    }
    else
    {
        return MSG_TYPE::MSG_TYPE_LOGIN;
    }
}
void my_tcpsocket::handleLogin(PDU *pdu)
{
    MSG_TYPE type = pdu->uiMsgType;
    switch (type)
    {
    case MSG_TYPE::MSG_TYPE_LOGIN_REGISTER_RESPOND:
    {
        handleRegistRespond(pdu);
        break;
    }
    case  MSG_TYPE::MSG_TYPE_LOGIN_IN_RESPOND:
    {
        handleLoginRespond(pdu);
        break;
    }
    case  MSG_TYPE::MSG_TYPE_LOGIN_FORGET_PASSWORD_RESPOND:
    {
        break;
    }
    case  MSG_TYPE::MSG_TYPE_LOGIN_OUT_RESPOND:
    {
        break;
    }
    }
}
void my_tcpsocket::handleChat(PDU *pdu)
{
    MSG_TYPE type = pdu->uiMsgType;
    switch (type)
    {
    case  MSG_TYPE::MSG_TYPE_CHAT_PRIVATE_REQUEST:
    {
        handleChatPrivate(pdu);
        break;
    }
    case MSG_TYPE::MSG_TYPE_CHAT_FLUSH_FRIEND_RESPOND:
    {
        handleChatFlushFriend(pdu);
        break;
    }
    case MSG_TYPE::MSG_TYPE_CHAT_ADD_FRIEND_REQUEST:
    {
        handleAddFriend(pdu);
        break;
    }
    case MSG_TYPE::MSG_TYPE_CHAT_ADD_FRIEND_RESPOND:
    {
        handleAddFriendSuccess(pdu);
        break;
    }
    }
}
void my_tcpsocket::handleFile(PDU *pdu)
{
    // // qDebug()<<"handleFILE";
    MSG_TYPE type = pdu->uiMsgType;
    switch (type)
    {
    case  MSG_TYPE::MSG_TYPE_FILE_UPDATEFILEINFO_RESPOND:
    {
        handleFileUpdateFileInfoRespond(pdu);
        break;
    }
    case MSG_TYPE::MSG_TYPE_FILE_UPLOAD_RESPOND:
    {
        handleFileUploadRespond(pdu);
        break;
    }
    case  MSG_TYPE::MSG_TYPE_FILE_DOWNLOAD_RESPOND:
    {
        handleFileDownloadRespond(pdu);
        break;
    }
    case  MSG_TYPE::MSG_TYPE_FILE_SHARE_RESPOND:
    {
        handleFileShareRespond(pdu);
        break;
    }
    case  MSG_TYPE::MSG_TYPE_FILE_CREATE_RESPOND:
    {
        handleFileCreateRespond(pdu);
        break;
    }
    case  MSG_TYPE::MSG_TYPE_FILE_DELETE_RESPOND:
    {
        handleFileDeleteRespond(pdu);
        break;
    }
    case  MSG_TYPE::MSG_TYPE_FILE_MOVE_RESPOND:
    {
        handleFileMoveRespond(pdu);
        break;
    }
    }
}
void my_tcpsocket::receiveMsg()
{
    if(downloadFileInfo->bTransform)
    {
        QByteArray baBuffer = this->readAll();
        downloadFileInfo->file.write(baBuffer);
        downloadFileInfo->iReceivedSize += baBuffer.size();
        if(downloadFileInfo->iReceivedSize == downloadFileInfo->iTotalSize)
        {
            qDebug()<<"下载文件成功"<<downloadFileInfo->iTotalSize/1024/1024;
            downloadFileInfo->file.close();
            downloadFileInfo->file.setFileName("");
            downloadFileInfo->bTransform = false;
            downloadFileInfo->iTotalSize = 0;
            downloadFileInfo->iReceivedSize = 0;
            if(this->state() ==QAbstractSocket::ConnectedState)
            {
                this->disconnectFromHost();
                if(this->state()!=QAbstractSocket::UnconnectedState)
                {
                    this->waitForDisconnected(3000);
                }
            }
            client_socket::getInstance().get_mainWidget()->get_fileSystem()->handleThread();
        }
        else if(downloadFileInfo->iReceivedSize > downloadFileInfo->iTotalSize)
        {
            qDebug()<<"下载文件失败";
            downloadFileInfo->file.close();
            downloadFileInfo->file.setFileName("");
            downloadFileInfo->bTransform = false;
            downloadFileInfo->iTotalSize = 0;
            downloadFileInfo->iReceivedSize = 0;
            if(this->state() ==QAbstractSocket::ConnectedState)
            {
                this->disconnectFromHost();
                if(this->state()!=QAbstractSocket::UnconnectedState)
                {
                    this->waitForDisconnected(3000);
                }
            }
             client_socket::getInstance().get_mainWidget()->get_fileSystem()->handleThread();
        }
        return;
    }
    uint strLen=0;
    uint uiPduLen = 0;
    strLen = this->read((char*)&uiPduLen, sizeof(unsigned));
    uint uiMsgLen = uiPduLen - sizeof(PDU);
    PDU* pdu = mkPDU(uiMsgLen);
    strLen = this->read((char*)pdu + sizeof(uint), uiPduLen - sizeof(uint));
    handleMessage(pdu);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
 马建仓 AI 助手
马建仓 AI 助手