diff --git a/ClientCore/ClientCore.cpp b/ClientCore/ClientCore.cpp index ea4133c..1df8872 100644 --- a/ClientCore/ClientCore.cpp +++ b/ClientCore/ClientCore.cpp @@ -57,7 +57,7 @@ void ClientCore::UseFrame(QSharedPointer frame) switch (frame->type) { case FrameBufferType::FBT_SER_MSG_ASKCLIENTS: { InfoClientVec info = infoUnpack(frame->data); - clientsCall_(info); + emit sigClients(info); break; } case FrameBufferType::FBT_SER_MSG_YOURID: { @@ -67,7 +67,7 @@ void ClientCore::UseFrame(QSharedPointer frame) } case FrameBufferType::FBT_CLI_ANS_DIRFILE: { DirFileInfoVec info = infoUnpack(frame->data); - fileCall_(info); + emit sigFiles(info); break; } case FrameBufferType::FBT_CLI_ASK_DIRFILE: { @@ -96,14 +96,51 @@ void ClientCore::UseFrame(QSharedPointer frame) case FrameBufferType::FBT_CLI_ANS_HOME: { InfoMsg info = infoUnpack(frame->data); qInfo() << QString(tr("home: %1")).arg(info.msg); - pathCall_(info.msg); + emit sigPath(info.msg); break; } case FrameBufferType::FBT_SER_MSG_FORWARD_FAILED: { break; } + case FrameBufferType::FBT_CLI_REQ_SEND: { + emit sigReqSend(frame); + break; + } + case FrameBufferType::FBT_CLI_REQ_DOWN: { + emit sigReqDown(frame); + break; + } + case FrameBufferType::FBT_CLI_TRANS_DONE: { + sigTransDone(frame); + break; + break; + } + case FrameBufferType::FBT_CLI_CAN_SEND: { + sigCanSend(frame); + break; + } + case FrameBufferType::FBT_CLI_CANOT_SEND: { + sigCanotSend(frame); + break; + } + case FBT_CLI_CANOT_DOWN: { + sigCanotDown(frame); + break; + } + case FBT_CLI_CAN_DOWN: { + sigCanDown(frame); + break; + } + case FBT_CLI_FILE_BUFFER: { + sigFileBuffer(frame); + break; + } + case FBT_CLI_TRANS_FAILED: { + sigTransFailed(frame); + break; + } default: - frameCall_[static_cast(frame->type)](frame); + qCritical() << QString("unknown frame type: %1").arg(frame->type); break; } } @@ -139,31 +176,11 @@ bool ClientCore::Send(const char* data, qint64 len) return true; } -void ClientCore::SetClientsCall(const std::function& call) -{ - clientsCall_ = call; -} - -void ClientCore::SetPathCall(const std::function& call) -{ - pathCall_ = call; -} - -void ClientCore::SetFileCall(const std::function& call) -{ - fileCall_ = call; -} - void ClientCore::SetRemoteID(const QString& id) { remoteID_ = id; } -void ClientCore::SetFrameCall(FrameBufferType type, const std::function)>& call) -{ - frameCall_[type] = call; -} - QString ClientCore::GetRemoteID() { return remoteID_; diff --git a/ClientCore/ClientCore.h b/ClientCore/ClientCore.h index 5094900..d303154 100644 --- a/ClientCore/ClientCore.h +++ b/ClientCore/ClientCore.h @@ -67,6 +67,18 @@ public: signals: void sigDisconnect(); + void sigPath(const QString& path); + void sigClients(const InfoClientVec& clients); + void sigFiles(const DirFileInfoVec& files); + void sigReqSend(QSharedPointer frame); + void sigReqDown(QSharedPointer frame); + void sigTransDone(QSharedPointer frame); + void sigCanSend(QSharedPointer frame); + void sigCanotSend(QSharedPointer frame); + void sigCanotDown(QSharedPointer frame); + void sigCanDown(QSharedPointer frame); + void sigFileBuffer(QSharedPointer frame); + void sigTransFailed(QSharedPointer frame); private: void onReadyRead(); @@ -76,10 +88,6 @@ private: void UseFrame(QSharedPointer frame); public: - void SetClientsCall(const std::function& call); - void SetPathCall(const std::function& call); - void SetFileCall(const std::function& call); - void SetFrameCall(FrameBufferType type, const std::function)>& call); void SetRemoteID(const QString& id); QString GetRemoteID(); QString GetOwnID(); @@ -94,11 +102,6 @@ public: QByteArray recvBuffer_; LocalFile localFile_; - - std::function pathCall_; - std::function clientsCall_; - std::function fileCall_; - std::array)>, 256> frameCall_; }; @@ -124,8 +127,8 @@ signals: private: QString ip_; - qint16 port_; - ClientCore* core_; + qint16 port_{}; + ClientCore* core_{}; }; #endif // CLIENTCORE_H \ No newline at end of file diff --git a/ClientCore/FileTrans.cpp b/ClientCore/FileTrans.cpp index 18dbd07..67f50f7 100644 --- a/ClientCore/FileTrans.cpp +++ b/ClientCore/FileTrans.cpp @@ -4,7 +4,7 @@ FileTrans::FileTrans(ClientCore* clientCore) : clientCore_(clientCore) { - RegisterFrameCall(); + RegisterSignal(); downTask_ = QSharedPointer::create(); sendTask_ = QSharedPointer::create(); } @@ -42,7 +42,8 @@ void FileTrans::ReqSendFile(const TransTask& task) return; } - if (!clientCore_->Send(info, FBT_CLI_REQ_SEND, task.remoteId)) { + auto frame = clientCore_->GetBuffer(info, FBT_CLI_REQ_SEND, task.remoteId); + if (!ClientCore::asyncInvoke(clientCore_, [this, frame]() { return clientCore_->Send(frame); })) { qCritical() << QString(tr("send req send failed: %1")).arg(info.msg); sendTask_->state = TaskState::STATE_NONE; sendTask_->file.close(); @@ -68,7 +69,8 @@ void FileTrans::ReqDownFile(const TransTask& task) downTask_->state = TaskState::STATE_NONE; return; } - if (!clientCore_->Send(info, FBT_CLI_REQ_DOWN, task.remoteId)) { + auto frame = clientCore_->GetBuffer(info, FBT_CLI_REQ_DOWN, task.remoteId); + if (!ClientCore::asyncInvoke(clientCore_, [this, frame]() { return clientCore_->Send(frame); })) { qCritical() << QString(tr("send req send failed: %1")).arg(info.msg); sendTask_->state = TaskState::STATE_NONE; sendTask_->file.close(); @@ -97,17 +99,17 @@ qint32 FileTrans::GetDownProgress() return per; } -void FileTrans::RegisterFrameCall() +void FileTrans::RegisterSignal() { - clientCore_->SetFrameCall(FBT_CLI_REQ_SEND, [this](QSharedPointer frame) { fbtReqSend(frame); }); - clientCore_->SetFrameCall(FBT_CLI_REQ_DOWN, [this](QSharedPointer frame) { fbtReqDown(frame); }); - clientCore_->SetFrameCall(FBT_CLI_TRANS_DONE, [this](QSharedPointer frame) { fbtTransDone(frame); }); - clientCore_->SetFrameCall(FBT_CLI_CAN_SEND, [this](QSharedPointer frame) { fbtCanSend(frame); }); - clientCore_->SetFrameCall(FBT_CLI_CANOT_SEND, [this](QSharedPointer frame) { fbtCanotSend(frame); }); - clientCore_->SetFrameCall(FBT_CLI_CANOT_DOWN, [this](QSharedPointer frame) { fbtCanotDown(frame); }); - clientCore_->SetFrameCall(FBT_CLI_CAN_DOWN, [this](QSharedPointer frame) { fbtCanDown(frame); }); - clientCore_->SetFrameCall(FBT_CLI_FILE_BUFFER, [this](QSharedPointer frame) { fbtFileBuffer(frame); }); - clientCore_->SetFrameCall(FBT_CLI_TRANS_FAILED, [this](QSharedPointer frame) { fbtTransFailed(frame); }); + connect(clientCore_, &ClientCore::sigReqSend, this, [this](QSharedPointer frame) { fbtReqSend(frame); }); + connect(clientCore_, &ClientCore::sigReqDown, this, [this](QSharedPointer frame) { fbtReqDown(frame); }); + connect(clientCore_, &ClientCore::sigTransDone, this, [this](QSharedPointer frame) { fbtTransDone(frame); }); + connect(clientCore_, &ClientCore::sigCanSend, this, [this](QSharedPointer frame) { fbtCanSend(frame); }); + connect(clientCore_, &ClientCore::sigCanotSend, this, [this](QSharedPointer frame) { fbtCanotSend(frame); }); + connect(clientCore_, &ClientCore::sigCanotDown, this, [this](QSharedPointer frame) { fbtCanotDown(frame); }); + connect(clientCore_, &ClientCore::sigCanDown, this, [this](QSharedPointer frame) { fbtCanDown(frame); }); + connect(clientCore_, &ClientCore::sigFileBuffer, this, [this](QSharedPointer frame) { fbtFileBuffer(frame); }); + connect(clientCore_, &ClientCore::sigTransFailed, this, [this](QSharedPointer frame) { fbtTransFailed(frame); }); } // The other party requests to send, prepare to receive. @@ -121,7 +123,8 @@ void FileTrans::fbtReqSend(QSharedPointer frame) // recv is single thread recv, judge idle if (downTask_->state == TaskState::STATE_RUNNING) { info.msg = QString(tr("busy...")); - clientCore_->Send(info, FBT_CLI_CANOT_SEND, frame->fid); + auto f = clientCore_->GetBuffer(info, FBT_CLI_CANOT_SEND, frame->fid); + ClientCore::asyncInvoke(clientCore_, [this, f]() { return clientCore_->Send(f); }); return; } @@ -131,8 +134,9 @@ void FileTrans::fbtReqSend(QSharedPointer frame) if (!downTask_->file.open(QIODevice::WriteOnly)) { info.msg = QString(tr("open file failed: %1")).arg(newerPath); qCritical() << info.msg; - if (!clientCore_->Send(info, FBT_CLI_CANOT_SEND, frame->fid)) { - qCritical() << QString(tr("open recv file:%2 failed, and reply %2 failed.")).arg(info.msg, frame->fid); + auto f = clientCore_->GetBuffer(info, FBT_CLI_CANOT_SEND, frame->fid); + if (!ClientCore::asyncInvoke(clientCore_, [this, f]() { return clientCore_->Send(f); })) { + qCritical() << QString(tr("open recv file:%2 failed, and reply %2 failed.")).arg(info.msg, f->fid); downTask_->file.close(); return; } @@ -145,8 +149,7 @@ void FileTrans::fbtReqSend(QSharedPointer frame) info.msg = QString(tr("open recv file success: %1")).arg(newerPath); auto f = clientCore_->GetBuffer(info, FBT_CLI_CAN_SEND, frame->fid); - auto sendRet = ClientCore::asyncInvoke(clientCore_, [this, f]() { return clientCore_->Send(f); }); - if (!sendRet) { + if (!ClientCore::asyncInvoke(clientCore_, [this, f]() { return clientCore_->Send(f); })) { qCritical() << QString(tr("open recv file:%2 success, but reply %2 failed.")).arg(info.msg, frame->fid); downTask_->file.close(); return; @@ -182,7 +185,8 @@ void FileTrans::fbtTransDone(QSharedPointer frame) downTask_->file.close(); downTask_->state = TaskState::STATE_FINISH; qInfo() << QString(tr("recv file:%1 success.")).arg(downTask_->file.fileName()); - clientCore_->Send(info, FBT_CLI_CAN_DOWN, frame->fid); + auto f = clientCore_->GetBuffer(info, FBT_CLI_CAN_DOWN, frame->fid); + ClientCore::asyncInvoke(clientCore_, [this, f]() { return clientCore_->Send(f); }); return; } qCritical() << QString(tr("recv file:%1 done sigal, but file not opened.")).arg(info.msg); @@ -216,7 +220,8 @@ void FileTrans::fbtFileBuffer(QSharedPointer frame) downTask_->state = TaskState::STATE_FAILED; InfoMsg info; info.msg = downTask_->file.errorString(); - clientCore_->Send(info, FBT_CLI_TRANS_FAILED, frame->fid); + auto f = clientCore_->GetBuffer(info, FBT_CLI_TRANS_FAILED, frame->fid); + ClientCore::asyncInvoke(clientCore_, [this, f]() { return clientCore_->Send(f); }); downTask_->file.close(); } downTask_->tranSize += ws; @@ -257,21 +262,6 @@ void FileTrans::SendFile(const QSharedPointer& task) sendThread->run(); } -QFuture FileTrans::sendFrameAsync(const QSharedPointer& frame) -{ - auto promise = QSharedPointer>::create(); - QFuture future = promise->future(); - QMetaObject::invokeMethod( - clientCore_, - [this, frame, promise]() mutable { - bool ret = clientCore_->Send(frame); - promise->addResult(ret); - promise->finish(); - }, - Qt::QueuedConnection); - return future; -} - SendThread::SendThread(ClientCore* clientCore) : cliCore_(clientCore) { } diff --git a/ClientCore/FileTrans.h b/ClientCore/FileTrans.h index 714c2f1..25a89a1 100644 --- a/ClientCore/FileTrans.h +++ b/ClientCore/FileTrans.h @@ -81,9 +81,8 @@ private: void fbtTransFailed(QSharedPointer frame); private: - void RegisterFrameCall(); + void RegisterSignal(); void SendFile(const QSharedPointer& task); - QFuture sendFrameAsync(const QSharedPointer& frame); private: QSharedPointer downTask_; diff --git a/ClientCore/RemoteFile.cpp b/ClientCore/RemoteFile.cpp index 25366d0..4efcfb9 100644 --- a/ClientCore/RemoteFile.cpp +++ b/ClientCore/RemoteFile.cpp @@ -1,14 +1,18 @@ -#include "RemoteFile.h" +#include "RemoteFile.h" #include #include "LocalFile.h" +RemoteFile::RemoteFile(QObject* parent) : DirFileHelper(parent) +{ +} + void RemoteFile::setClientCore(ClientCore* cliCore) { cliCore_ = cliCore; - cliCore_->SetPathCall(pathCall_); - cliCore_->SetFileCall(fileCall_); + connect(cliCore_, &ClientCore::sigPath, this, [this](const QString& path) { sigHome(path); }); + connect(cliCore_, &ClientCore::sigFiles, this, [this](const DirFileInfoVec& files) { sigDirFile(files); }); } bool RemoteFile::GetHome() @@ -21,6 +25,7 @@ bool RemoteFile::GetHome() bool RemoteFile::GetDirFile(const QString& dir) { InfoMsg info; + info.msg = dir; auto frame = cliCore_->GetBuffer(info, FBT_CLI_ASK_DIRFILE, cliCore_->GetRemoteID()); return ClientCore::asyncInvoke(cliCore_, [this, frame]() { return cliCore_->Send(frame); }); } \ No newline at end of file diff --git a/ClientCore/RemoteFile.h b/ClientCore/RemoteFile.h index 3f58c84..2452769 100644 --- a/ClientCore/RemoteFile.h +++ b/ClientCore/RemoteFile.h @@ -1,4 +1,4 @@ -#ifndef REMOTE_FILE_H +#ifndef REMOTE_FILE_H #define REMOTE_FILE_H #include @@ -9,7 +9,7 @@ class RemoteFile : public DirFileHelper { public: - RemoteFile() = default; + RemoteFile(QObject* parent = nullptr); ~RemoteFile() override = default; public: @@ -20,7 +20,7 @@ public: bool GetDirFile(const QString& dir) override; private: - ClientCore* cliCore_; + ClientCore* cliCore_{}; }; #endif // REMOTE_FILE_H \ No newline at end of file diff --git a/Gui/Control/ConnectControl.cpp b/Gui/Control/ConnectControl.cpp index 773305b..36a06b2 100644 --- a/Gui/Control/ConnectControl.cpp +++ b/Gui/Control/ConnectControl.cpp @@ -21,7 +21,7 @@ Connecter::~Connecter() void Connecter::SetClientCore(ClientCore* clientCore) { clientCore_ = clientCore; - clientCore_->SetClientsCall([this](const InfoClientVec& clients) { HandleClients(clients); }); + connect(clientCore_, &ClientCore::sigClients, this, &Connecter::HandleClients); } void Connecter::SetRemoteCall(const std::function& call) diff --git a/Gui/Control/FileControl.cpp b/Gui/Control/FileControl.cpp index 5bd983a..a283523 100644 --- a/Gui/Control/FileControl.cpp +++ b/Gui/Control/FileControl.cpp @@ -26,13 +26,9 @@ void FileManager::SetModeStr(const QString& modeStr, int type, ClientCore* clien ui->lbMode->setText(modeStr); if (type == 0) { fileHelper_ = std::make_shared(); - fileHelper_->registerPathCall([this](const QString& path) { ShowPath(path); }); - fileHelper_->registerFileCall([this](const DirFileInfoVec& info) { ShowFile(info); }); } else { cliCore_ = clientCore; auto remotePtr = std::make_shared(); - remotePtr->registerPathCall([this](const QString& path) { ShowPath(path); }); - remotePtr->registerFileCall([this](const DirFileInfoVec& info) { ShowFile(info); }); remotePtr->setClientCore(clientCore); ui->tableWidget->setIsRemote(true); ui->tableWidget->setOwnIDCall([this]() { return cliCore_->GetOwnID(); }); @@ -40,6 +36,9 @@ void FileManager::SetModeStr(const QString& modeStr, int type, ClientCore* clien fileHelper_ = remotePtr; } ui->tableWidget->setBasePathCall([this]() { return curRoot_; }); + + connect(fileHelper_.get(), &DirFileHelper::sigHome, this, &FileManager::ShowPath); + connect(fileHelper_.get(), &DirFileHelper::sigDirFile, this, &FileManager::ShowFile); } void FileManager::SetOtherSideCall(const std::function& call) @@ -92,6 +91,7 @@ void FileManager::InitMenu(bool remote) void FileManager::ShowPath(const QString& path) { + QMutexLocker locker(&cbMut_); int existingIndex = ui->comboBox->findText(path); if (existingIndex != -1) { ui->comboBox->removeItem(existingIndex); @@ -104,8 +104,8 @@ void FileManager::ShowPath(const QString& path) void FileManager::ShowFile(const DirFileInfoVec& info) { - QAbstractItemModel* const mdl = ui->tableWidget->model(); - mdl->removeRows(0, mdl->rowCount()); + QMutexLocker locker(&tbMut_); + ui->tableWidget->setRowCount(0); ui->tableWidget->setRowCount(info.vec.size()); for (int i = 0; i < info.vec.size(); ++i) { diff --git a/Gui/Control/FileControl.h b/Gui/Control/FileControl.h index 4b7ff0c..111a716 100644 --- a/Gui/Control/FileControl.h +++ b/Gui/Control/FileControl.h @@ -6,6 +6,7 @@ #include #include #include +#include #include namespace Ui { @@ -45,6 +46,8 @@ private: QString curRoot_; QMenu* menu_; ClientCore* cliCore_; + QMutex cbMut_; + QMutex tbMut_; std::shared_ptr fileHelper_; }; diff --git a/Gui/frelayGUI.cpp b/Gui/frelayGUI.cpp index 704b97d..e444a4e 100644 --- a/Gui/frelayGUI.cpp +++ b/Gui/frelayGUI.cpp @@ -89,6 +89,9 @@ void frelayGUI::ControlLayout() void frelayGUI::ControlMsgHander(QtMsgType type, const QMessageLogContext& context, const QString& msg) { Q_UNUSED(context); + if (!logPrint) { + return; + } switch (type) { case QtDebugMsg: diff --git a/Util/LocalFile.cpp b/Util/LocalFile.cpp index f18d73f..9c4f47e 100644 --- a/Util/LocalFile.cpp +++ b/Util/LocalFile.cpp @@ -1,13 +1,17 @@ -#include "LocalFile.h" +#include "LocalFile.h" #include #include #include +LocalFile::LocalFile(QObject* parent) : DirFileHelper(parent) +{ +} + bool LocalFile::GetHome() { auto home = Util::GetUserHome(); - pathCall_(home); + emit sigHome(home); return true; } @@ -17,7 +21,7 @@ bool LocalFile::GetDirFile(const QString& dir) if (!GetDirFile(dir, vec)) { return false; } - fileCall_(vec); + emit sigDirFile(vec); return true; } diff --git a/Util/LocalFile.h b/Util/LocalFile.h index 2c4ffde..d7bf5c7 100644 --- a/Util/LocalFile.h +++ b/Util/LocalFile.h @@ -1,4 +1,4 @@ -#ifndef LOCALFILE_H +#ifndef LOCALFILE_H #define LOCALFILE_H #include @@ -8,7 +8,7 @@ class LocalFile : public DirFileHelper { public: - LocalFile() = default; + LocalFile(QObject* parent = nullptr); ~LocalFile() override = default; public: diff --git a/Util/Util.cpp b/Util/Util.cpp index d4d0afe..98962e7 100644 --- a/Util/Util.cpp +++ b/Util/Util.cpp @@ -109,12 +109,7 @@ QString DirFileHelper::GetErr() const return QString(); } -void DirFileHelper::registerPathCall(const std::function& call) +DirFileHelper::DirFileHelper(QObject* parent) : QObject(parent) { - pathCall_ = call; -} -void DirFileHelper::registerFileCall(const std::function& call) -{ - fileCall_ = call; -} +} \ No newline at end of file diff --git a/Util/Util.h b/Util/Util.h index 90becf0..6ebb015 100644 --- a/Util/Util.h +++ b/Util/Util.h @@ -22,18 +22,18 @@ class DirFileHelper : public QObject { Q_OBJECT public: - DirFileHelper() = default; + DirFileHelper(QObject* parent = nullptr); virtual ~DirFileHelper() = default; public: QString GetErr() const; - void registerPathCall(const std::function& call); - void registerFileCall(const std::function& call); + +signals: + void sigHome(const QString& path); + void sigDirFile(const DirFileInfoVec& dirFile); protected: QString err_; - std::function pathCall_; - std::function fileCall_; public: virtual bool GetHome() = 0;