1 Star 0 Fork 0

小邓/client

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
my_tcpsocket.cpp 12.30 KB
一键复制 编辑 原始数据 按行查看 历史
小邓 提交于 2024-07-07 01:26 +08:00 . 清理到部分不必要的输出和注释
#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);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xiao-deng-a/client.git
git@gitee.com:xiao-deng-a/client.git
xiao-deng-a
client
client
master

搜索帮助