clientcore: Regardless of whether it is connected, the ClientCore will be uniformly managed by a new thread.
This commit is contained in:
@@ -4,6 +4,11 @@
|
||||
|
||||
ClientCore::ClientCore(QObject* parent) : QObject(parent)
|
||||
{
|
||||
}
|
||||
|
||||
void ClientCore::Instance()
|
||||
{
|
||||
qDebug() << "Instance() thread:" << QThread::currentThread();
|
||||
socket_ = new QTcpSocket(this);
|
||||
connect(socket_, &QTcpSocket::readyRead, this, &ClientCore::onReadyRead);
|
||||
connect(socket_, &QTcpSocket::disconnected, this, &ClientCore::onDisconnected);
|
||||
@@ -13,6 +18,17 @@ ClientCore::~ClientCore()
|
||||
{
|
||||
}
|
||||
|
||||
void ClientCore::DoConnect(const QString& ip, quint16 port)
|
||||
{
|
||||
qDebug() << "doConnect thread:" << QThread::currentThread();
|
||||
emit connecting();
|
||||
if (!Connect(ip, port)) {
|
||||
emit conFailed();
|
||||
return;
|
||||
}
|
||||
emit conSuccess();
|
||||
}
|
||||
|
||||
bool ClientCore::Connect(const QString& ip, quint16 port)
|
||||
{
|
||||
QMutexLocker locker(&conMutex_);
|
||||
@@ -26,11 +42,13 @@ bool ClientCore::Connect(const QString& ip, quint16 port)
|
||||
return false;
|
||||
}
|
||||
qInfo() << QString(tr("%1:%2 connected success.")).arg(ip).arg(port);
|
||||
connected_ = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void ClientCore::Disconnect()
|
||||
{
|
||||
connected_ = false;
|
||||
}
|
||||
|
||||
void ClientCore::onReadyRead()
|
||||
@@ -48,6 +66,7 @@ void ClientCore::onReadyRead()
|
||||
|
||||
void ClientCore::onDisconnected()
|
||||
{
|
||||
connected_ = false;
|
||||
qCritical() << QString("client %1 disconnected...").arg(remoteID_);
|
||||
emit sigDisconnect();
|
||||
}
|
||||
@@ -201,7 +220,6 @@ QString ClientCore::GetOwnID()
|
||||
SocketWorker::SocketWorker(ClientCore* core, QObject* parent) : QThread(parent), core_(core)
|
||||
{
|
||||
connect(core_, &ClientCore::sigDisconnect, this, [this]() {
|
||||
emit disconnected();
|
||||
thread()->quit();
|
||||
});
|
||||
}
|
||||
@@ -210,19 +228,9 @@ SocketWorker::~SocketWorker()
|
||||
{
|
||||
}
|
||||
|
||||
void SocketWorker::SetConnectInfo(const QString& ip, qint16 port)
|
||||
{
|
||||
ip_ = ip;
|
||||
port_ = port;
|
||||
}
|
||||
|
||||
void SocketWorker::run()
|
||||
{
|
||||
emit connecting();
|
||||
if (!core_->Connect(ip_, port_)) {
|
||||
emit conFailed();
|
||||
return;
|
||||
}
|
||||
emit conSuccess();
|
||||
qDebug() << "SocketWorker thread:" << QThread::currentThread();
|
||||
core_->Instance();
|
||||
exec();
|
||||
}
|
||||
|
||||
@@ -27,7 +27,9 @@ public:
|
||||
~ClientCore();
|
||||
|
||||
public:
|
||||
void Instance();
|
||||
bool Connect(const QString& ip, quint16 port);
|
||||
void DoConnect(const QString& ip, quint16 port);
|
||||
void Disconnect();
|
||||
bool Send(QSharedPointer<FrameBuffer> frame);
|
||||
bool Send(const char* data, qint64 len);
|
||||
@@ -44,7 +46,12 @@ public:
|
||||
f->type = type;
|
||||
return f;
|
||||
}
|
||||
template <typename Callable> static bool asyncInvoke(QObject* context, Callable&& func)
|
||||
/*
|
||||
When calling syncInvoke of ClientCore, the ClientCore should not be in the GUI's event loop thread.
|
||||
In other words, if a ClientCore instance is created in the GUI thread, it should be moved to another thread;
|
||||
otherwise, it will cause a deadlock and freeze the interface.
|
||||
*/
|
||||
template <typename Callable> static bool syncInvoke(QObject* context, Callable&& func)
|
||||
{
|
||||
auto promise = QSharedPointer<QPromise<bool>>::create();
|
||||
QFuture<bool> future = promise->future();
|
||||
@@ -81,6 +88,11 @@ signals:
|
||||
void sigTransFailed(QSharedPointer<FrameBuffer> frame);
|
||||
void sigFileInfo(QSharedPointer<FrameBuffer> frame);
|
||||
|
||||
signals:
|
||||
void conSuccess();
|
||||
void connecting();
|
||||
void conFailed();
|
||||
|
||||
private:
|
||||
void onReadyRead();
|
||||
void onDisconnected();
|
||||
@@ -99,9 +111,10 @@ public:
|
||||
QString remoteID_;
|
||||
|
||||
QMutex sockMut_;
|
||||
QTcpSocket* socket_;
|
||||
QTcpSocket* socket_{};
|
||||
QByteArray recvBuffer_;
|
||||
|
||||
bool connected_{ false };
|
||||
LocalFile localFile_;
|
||||
};
|
||||
|
||||
@@ -113,21 +126,10 @@ public:
|
||||
SocketWorker(ClientCore* core, QObject* parent = nullptr);
|
||||
~SocketWorker();
|
||||
|
||||
public:
|
||||
void SetConnectInfo(const QString& ip, qint16 port);
|
||||
|
||||
protected:
|
||||
void run() override;
|
||||
|
||||
signals:
|
||||
void conSuccess();
|
||||
void connecting();
|
||||
void conFailed();
|
||||
void disconnected();
|
||||
|
||||
private:
|
||||
QString ip_;
|
||||
qint16 port_{};
|
||||
ClientCore* core_{};
|
||||
};
|
||||
|
||||
|
||||
@@ -49,7 +49,7 @@ void FileTrans::ReqSendFile(const TransTask& task)
|
||||
}
|
||||
|
||||
auto frame = clientCore_->GetBuffer(info, FBT_CLI_REQ_SEND, task.remoteId);
|
||||
if (!ClientCore::asyncInvoke(clientCore_, [this, frame]() { return clientCore_->Send(frame); })) {
|
||||
if (!ClientCore::syncInvoke(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();
|
||||
@@ -79,7 +79,7 @@ void FileTrans::ReqDownFile(const TransTask& task)
|
||||
return;
|
||||
}
|
||||
auto frame = clientCore_->GetBuffer(info, FBT_CLI_REQ_DOWN, task.remoteId);
|
||||
if (!ClientCore::asyncInvoke(clientCore_, [this, frame]() { return clientCore_->Send(frame); })) {
|
||||
if (!ClientCore::syncInvoke(clientCore_, [this, frame]() { return clientCore_->Send(frame); })) {
|
||||
qCritical() << QString(tr("send req send failed: %1")).arg(info.msg);
|
||||
downTask_->state = TaskState::STATE_NONE;
|
||||
downTask_->file.close();
|
||||
@@ -154,7 +154,7 @@ void FileTrans::fbtReqSend(QSharedPointer<FrameBuffer> frame)
|
||||
if (downTask_->state == TaskState::STATE_RUNNING) {
|
||||
info.msg = QString(tr("busy..."));
|
||||
auto f = clientCore_->GetBuffer(info, FBT_CLI_CANOT_SEND, frame->fid);
|
||||
ClientCore::asyncInvoke(clientCore_, [this, f]() { return clientCore_->Send(f); });
|
||||
ClientCore::syncInvoke(clientCore_, [this, f]() { return clientCore_->Send(f); });
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -165,7 +165,7 @@ void FileTrans::fbtReqSend(QSharedPointer<FrameBuffer> frame)
|
||||
info.msg = QString(tr("open file failed: %1")).arg(newerPath);
|
||||
qCritical() << info.msg;
|
||||
auto f = clientCore_->GetBuffer(info, FBT_CLI_CANOT_SEND, frame->fid);
|
||||
if (!ClientCore::asyncInvoke(clientCore_, [this, f]() { return clientCore_->Send(f); })) {
|
||||
if (!ClientCore::syncInvoke(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;
|
||||
@@ -180,7 +180,7 @@ void FileTrans::fbtReqSend(QSharedPointer<FrameBuffer> frame)
|
||||
info.msg = QString(tr("open recv file success: %1")).arg(newerPath);
|
||||
qInfo() << info.msg;
|
||||
auto f = clientCore_->GetBuffer(info, FBT_CLI_CAN_SEND, frame->fid);
|
||||
if (!ClientCore::asyncInvoke(clientCore_, [this, f]() { return clientCore_->Send(f); })) {
|
||||
if (!ClientCore::syncInvoke(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;
|
||||
@@ -217,7 +217,7 @@ void FileTrans::fbtReqDown(QSharedPointer<FrameBuffer> frame)
|
||||
|
||||
// reply fileinfo
|
||||
auto f = clientCore_->GetBuffer(info, FBT_CLI_FILE_INFO, frame->fid);
|
||||
if (!ClientCore::asyncInvoke(clientCore_, [this, f]() { return clientCore_->Send(f); })) {
|
||||
if (!ClientCore::syncInvoke(clientCore_, [this, f]() { return clientCore_->Send(f); })) {
|
||||
qCritical() << QString(tr("send file %1 info failed.")).arg(info.fromPath);
|
||||
doTask->file.close();
|
||||
return;
|
||||
@@ -237,7 +237,7 @@ void FileTrans::fbtTransDone(QSharedPointer<FrameBuffer> frame)
|
||||
info.msg = QString(tr("recv file:%1 success.")).arg(downTask_->file.fileName());
|
||||
qInfo() << info.msg;
|
||||
auto f = clientCore_->GetBuffer(info, FBT_CLI_CAN_DOWN, frame->fid);
|
||||
ClientCore::asyncInvoke(clientCore_, [this, f]() { return clientCore_->Send(f); });
|
||||
ClientCore::syncInvoke(clientCore_, [this, f]() { return clientCore_->Send(f); });
|
||||
return;
|
||||
}
|
||||
qCritical() << QString(tr("recv file:%1 done sigal, but file not opened.")).arg(info.msg);
|
||||
@@ -272,7 +272,7 @@ void FileTrans::fbtFileBuffer(QSharedPointer<FrameBuffer> frame)
|
||||
InfoMsg info;
|
||||
info.msg = downTask_->file.errorString();
|
||||
auto f = clientCore_->GetBuffer(info, FBT_CLI_TRANS_FAILED, frame->fid);
|
||||
ClientCore::asyncInvoke(clientCore_, [this, f]() { return clientCore_->Send(f); });
|
||||
ClientCore::syncInvoke(clientCore_, [this, f]() { return clientCore_->Send(f); });
|
||||
downTask_->file.close();
|
||||
}
|
||||
downTask_->tranSize += ws;
|
||||
@@ -372,7 +372,7 @@ void SendThread::run()
|
||||
|
||||
InfoMsg info;
|
||||
auto f = cliCore_->GetBuffer(info, FBT_CLI_TRANS_DONE, task_->task.remoteId);
|
||||
ClientCore::asyncInvoke(cliCore_, [this, f]() { return cliCore_->Send(f); });
|
||||
ClientCore::syncInvoke(cliCore_, [this, f]() { return cliCore_->Send(f); });
|
||||
task_->file.close();
|
||||
task_->state = TaskState::STATE_FINISH;
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ bool RemoteFile::GetHome()
|
||||
{
|
||||
InfoMsg info;
|
||||
auto frame = cliCore_->GetBuffer(info, FBT_CLI_ASK_HOME, cliCore_->GetRemoteID());
|
||||
return ClientCore::asyncInvoke(cliCore_, [this, frame]() { return cliCore_->Send(frame); });
|
||||
return ClientCore::syncInvoke(cliCore_, [this, frame]() { return cliCore_->Send(frame); });
|
||||
}
|
||||
|
||||
bool RemoteFile::GetDirFile(const QString& dir)
|
||||
@@ -27,5 +27,5 @@ 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); });
|
||||
return ClientCore::syncInvoke(cliCore_, [this, frame]() { return cliCore_->Send(frame); });
|
||||
}
|
||||
Reference in New Issue
Block a user