Call: Use signal slots uniformly for calling.
This commit is contained in:
@@ -57,7 +57,7 @@ void ClientCore::UseFrame(QSharedPointer<FrameBuffer> frame)
|
||||
switch (frame->type) {
|
||||
case FrameBufferType::FBT_SER_MSG_ASKCLIENTS: {
|
||||
InfoClientVec info = infoUnpack<InfoClientVec>(frame->data);
|
||||
clientsCall_(info);
|
||||
emit sigClients(info);
|
||||
break;
|
||||
}
|
||||
case FrameBufferType::FBT_SER_MSG_YOURID: {
|
||||
@@ -67,7 +67,7 @@ void ClientCore::UseFrame(QSharedPointer<FrameBuffer> frame)
|
||||
}
|
||||
case FrameBufferType::FBT_CLI_ANS_DIRFILE: {
|
||||
DirFileInfoVec info = infoUnpack<DirFileInfoVec>(frame->data);
|
||||
fileCall_(info);
|
||||
emit sigFiles(info);
|
||||
break;
|
||||
}
|
||||
case FrameBufferType::FBT_CLI_ASK_DIRFILE: {
|
||||
@@ -96,14 +96,51 @@ void ClientCore::UseFrame(QSharedPointer<FrameBuffer> frame)
|
||||
case FrameBufferType::FBT_CLI_ANS_HOME: {
|
||||
InfoMsg info = infoUnpack<InfoMsg>(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<uint32_t>(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<void(const InfoClientVec& clients)>& call)
|
||||
{
|
||||
clientsCall_ = call;
|
||||
}
|
||||
|
||||
void ClientCore::SetPathCall(const std::function<void(const QString& path)>& call)
|
||||
{
|
||||
pathCall_ = call;
|
||||
}
|
||||
|
||||
void ClientCore::SetFileCall(const std::function<void(const DirFileInfoVec& files)>& call)
|
||||
{
|
||||
fileCall_ = call;
|
||||
}
|
||||
|
||||
void ClientCore::SetRemoteID(const QString& id)
|
||||
{
|
||||
remoteID_ = id;
|
||||
}
|
||||
|
||||
void ClientCore::SetFrameCall(FrameBufferType type, const std::function<void(QSharedPointer<FrameBuffer>)>& call)
|
||||
{
|
||||
frameCall_[type] = call;
|
||||
}
|
||||
|
||||
QString ClientCore::GetRemoteID()
|
||||
{
|
||||
return remoteID_;
|
||||
|
||||
@@ -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<FrameBuffer> frame);
|
||||
void sigReqDown(QSharedPointer<FrameBuffer> frame);
|
||||
void sigTransDone(QSharedPointer<FrameBuffer> frame);
|
||||
void sigCanSend(QSharedPointer<FrameBuffer> frame);
|
||||
void sigCanotSend(QSharedPointer<FrameBuffer> frame);
|
||||
void sigCanotDown(QSharedPointer<FrameBuffer> frame);
|
||||
void sigCanDown(QSharedPointer<FrameBuffer> frame);
|
||||
void sigFileBuffer(QSharedPointer<FrameBuffer> frame);
|
||||
void sigTransFailed(QSharedPointer<FrameBuffer> frame);
|
||||
|
||||
private:
|
||||
void onReadyRead();
|
||||
@@ -76,10 +88,6 @@ private:
|
||||
void UseFrame(QSharedPointer<FrameBuffer> frame);
|
||||
|
||||
public:
|
||||
void SetClientsCall(const std::function<void(const InfoClientVec& clients)>& call);
|
||||
void SetPathCall(const std::function<void(const QString& path)>& call);
|
||||
void SetFileCall(const std::function<void(const DirFileInfoVec& files)>& call);
|
||||
void SetFrameCall(FrameBufferType type, const std::function<void(QSharedPointer<FrameBuffer>)>& call);
|
||||
void SetRemoteID(const QString& id);
|
||||
QString GetRemoteID();
|
||||
QString GetOwnID();
|
||||
@@ -94,11 +102,6 @@ public:
|
||||
QByteArray recvBuffer_;
|
||||
|
||||
LocalFile localFile_;
|
||||
|
||||
std::function<void(const QString& path)> pathCall_;
|
||||
std::function<void(const InfoClientVec& clients)> clientsCall_;
|
||||
std::function<void(const DirFileInfoVec& files)> fileCall_;
|
||||
|
||||
std::array<std::function<void(QSharedPointer<FrameBuffer>)>, 256> frameCall_;
|
||||
};
|
||||
|
||||
@@ -124,8 +127,8 @@ signals:
|
||||
|
||||
private:
|
||||
QString ip_;
|
||||
qint16 port_;
|
||||
ClientCore* core_;
|
||||
qint16 port_{};
|
||||
ClientCore* core_{};
|
||||
};
|
||||
|
||||
#endif // CLIENTCORE_H
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
FileTrans::FileTrans(ClientCore* clientCore) : clientCore_(clientCore)
|
||||
{
|
||||
RegisterFrameCall();
|
||||
RegisterSignal();
|
||||
downTask_ = QSharedPointer<DoTransTask>::create();
|
||||
sendTask_ = QSharedPointer<DoTransTask>::create();
|
||||
}
|
||||
@@ -42,7 +42,8 @@ void FileTrans::ReqSendFile(const TransTask& task)
|
||||
return;
|
||||
}
|
||||
|
||||
if (!clientCore_->Send<InfoMsg>(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<InfoMsg>(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<FrameBuffer> frame) { fbtReqSend(frame); });
|
||||
clientCore_->SetFrameCall(FBT_CLI_REQ_DOWN, [this](QSharedPointer<FrameBuffer> frame) { fbtReqDown(frame); });
|
||||
clientCore_->SetFrameCall(FBT_CLI_TRANS_DONE, [this](QSharedPointer<FrameBuffer> frame) { fbtTransDone(frame); });
|
||||
clientCore_->SetFrameCall(FBT_CLI_CAN_SEND, [this](QSharedPointer<FrameBuffer> frame) { fbtCanSend(frame); });
|
||||
clientCore_->SetFrameCall(FBT_CLI_CANOT_SEND, [this](QSharedPointer<FrameBuffer> frame) { fbtCanotSend(frame); });
|
||||
clientCore_->SetFrameCall(FBT_CLI_CANOT_DOWN, [this](QSharedPointer<FrameBuffer> frame) { fbtCanotDown(frame); });
|
||||
clientCore_->SetFrameCall(FBT_CLI_CAN_DOWN, [this](QSharedPointer<FrameBuffer> frame) { fbtCanDown(frame); });
|
||||
clientCore_->SetFrameCall(FBT_CLI_FILE_BUFFER, [this](QSharedPointer<FrameBuffer> frame) { fbtFileBuffer(frame); });
|
||||
clientCore_->SetFrameCall(FBT_CLI_TRANS_FAILED, [this](QSharedPointer<FrameBuffer> frame) { fbtTransFailed(frame); });
|
||||
connect(clientCore_, &ClientCore::sigReqSend, this, [this](QSharedPointer<FrameBuffer> frame) { fbtReqSend(frame); });
|
||||
connect(clientCore_, &ClientCore::sigReqDown, this, [this](QSharedPointer<FrameBuffer> frame) { fbtReqDown(frame); });
|
||||
connect(clientCore_, &ClientCore::sigTransDone, this, [this](QSharedPointer<FrameBuffer> frame) { fbtTransDone(frame); });
|
||||
connect(clientCore_, &ClientCore::sigCanSend, this, [this](QSharedPointer<FrameBuffer> frame) { fbtCanSend(frame); });
|
||||
connect(clientCore_, &ClientCore::sigCanotSend, this, [this](QSharedPointer<FrameBuffer> frame) { fbtCanotSend(frame); });
|
||||
connect(clientCore_, &ClientCore::sigCanotDown, this, [this](QSharedPointer<FrameBuffer> frame) { fbtCanotDown(frame); });
|
||||
connect(clientCore_, &ClientCore::sigCanDown, this, [this](QSharedPointer<FrameBuffer> frame) { fbtCanDown(frame); });
|
||||
connect(clientCore_, &ClientCore::sigFileBuffer, this, [this](QSharedPointer<FrameBuffer> frame) { fbtFileBuffer(frame); });
|
||||
connect(clientCore_, &ClientCore::sigTransFailed, this, [this](QSharedPointer<FrameBuffer> frame) { fbtTransFailed(frame); });
|
||||
}
|
||||
|
||||
// The other party requests to send, prepare to receive.
|
||||
@@ -121,7 +123,8 @@ void FileTrans::fbtReqSend(QSharedPointer<FrameBuffer> frame)
|
||||
// recv is single thread recv, judge idle
|
||||
if (downTask_->state == TaskState::STATE_RUNNING) {
|
||||
info.msg = QString(tr("busy..."));
|
||||
clientCore_->Send<InfoMsg>(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<FrameBuffer> frame)
|
||||
if (!downTask_->file.open(QIODevice::WriteOnly)) {
|
||||
info.msg = QString(tr("open file failed: %1")).arg(newerPath);
|
||||
qCritical() << info.msg;
|
||||
if (!clientCore_->Send<InfoMsg>(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<FrameBuffer> 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<FrameBuffer> frame)
|
||||
downTask_->file.close();
|
||||
downTask_->state = TaskState::STATE_FINISH;
|
||||
qInfo() << QString(tr("recv file:%1 success.")).arg(downTask_->file.fileName());
|
||||
clientCore_->Send<InfoMsg>(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<FrameBuffer> frame)
|
||||
downTask_->state = TaskState::STATE_FAILED;
|
||||
InfoMsg info;
|
||||
info.msg = downTask_->file.errorString();
|
||||
clientCore_->Send<InfoMsg>(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<DoTransTask>& task)
|
||||
sendThread->run();
|
||||
}
|
||||
|
||||
QFuture<bool> FileTrans::sendFrameAsync(const QSharedPointer<FrameBuffer>& frame)
|
||||
{
|
||||
auto promise = QSharedPointer<QPromise<bool>>::create();
|
||||
QFuture<bool> 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)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -81,9 +81,8 @@ private:
|
||||
void fbtTransFailed(QSharedPointer<FrameBuffer> frame);
|
||||
|
||||
private:
|
||||
void RegisterFrameCall();
|
||||
void RegisterSignal();
|
||||
void SendFile(const QSharedPointer<DoTransTask>& task);
|
||||
QFuture<bool> sendFrameAsync(const QSharedPointer<FrameBuffer>& frame);
|
||||
|
||||
private:
|
||||
QSharedPointer<DoTransTask> downTask_;
|
||||
|
||||
@@ -1,14 +1,18 @@
|
||||
#include "RemoteFile.h"
|
||||
#include "RemoteFile.h"
|
||||
|
||||
#include <InfoPack.hpp>
|
||||
|
||||
#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); });
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
#ifndef REMOTE_FILE_H
|
||||
#ifndef REMOTE_FILE_H
|
||||
#define REMOTE_FILE_H
|
||||
|
||||
#include <InfoMsg.h>
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user