socket: Unified Soket thread management.

This commit is contained in:
2025-06-19 11:59:32 +08:00
parent beaadfbf85
commit b2aa0d3752
17 changed files with 235 additions and 88 deletions

View File

@@ -7,7 +7,7 @@
#include "GuiUtil/Public.h"
#include "ui_ConnectControl.h"
Connecter::Connecter(QWidget* parent) : QWidget(parent), ui(new Ui::Connecter), th_(nullptr)
Connecter::Connecter(QWidget* parent) : QWidget(parent), ui(new Ui::Connecter)
{
ui->setupUi(this);
InitControl();
@@ -47,29 +47,32 @@ void Connecter::Connect()
return;
}
if (th_) {
if (th_->isRunning()) {
th_->quit();
th_->wait(1000);
}
delete th_;
}
sockWorker_ = new SocketWorker(clientCore_, nullptr);
clientCore_->moveToThread(sockWorker_);
auto* worker = new ConnectWorker(clientCore_, nullptr);
th_ = new QThread();
worker->moveToThread(th_);
clientCore_->moveToThread(th_);
connect(th_, &QThread::started,
[this, worker, ip, port]() { worker->doConnect(ip, port.toInt(), this->parent()->thread()); });
connect(worker, &ConnectWorker::connecting, this, [this]() { setState(ConnectState::CS_CONNECTING); });
connect(worker, &ConnectWorker::connectResult, this, [this](bool success) {
emit sendConnect(success ? ConnectState::CS_CONNECTED : ConnectState::CS_DISCONNECT);
th_->quit();
connect(sockWorker_, &SocketWorker::conSuccess, this, [this]() {
setState(ConnectState::CS_CONNECTED);
qInfo() << QString(tr("Connected."));
});
connect(th_, &QThread::finished, worker, &QObject::deleteLater);
connect(th_, &QThread::finished, th_, &QObject::deleteLater);
th_->start();
connect(sockWorker_, &SocketWorker::conFailed, this, [this]() {
setState(ConnectState::CS_DISCONNECT);
qInfo() << QString(tr("Connect failed."));
});
connect(sockWorker_, &SocketWorker::connecting, this, [this]() {
setState(ConnectState::CS_CONNECTING);
qInfo() << QString(tr("Connecting..."));
});
connect(sockWorker_, &SocketWorker::disconnected, this, [this]() {
setState(ConnectState::CS_DISCONNECT);
qInfo() << QString(tr("Disconnected."));
});
connect(sockWorker_, &QThread::finished, sockWorker_, &QObject::deleteLater);
sockWorker_->SetConnectInfo(ip, port.toInt());
sockWorker_->start();
}
void Connecter::setState(ConnectState cs)
@@ -78,7 +81,6 @@ void Connecter::setState(ConnectState cs)
case CS_CONNECTING:
ui->btnConnect->setEnabled(false);
ui->btnDisconnect->setEnabled(false);
qInfo() << QString(tr("Connecting..."));
break;
case CS_CONNECTED:
ui->btnConnect->setEnabled(false);
@@ -99,7 +101,8 @@ void Connecter::RefreshClient()
auto frame = QSharedPointer<FrameBuffer>::create();
frame->data = infoPack(info);
frame->type = FBT_SER_MSG_ASKCLIENTS;
if (!clientCore_->Send(frame)) {
auto sendRet = ClientCore::asyncInvoke(clientCore_, [this, frame]() { return clientCore_->Send(frame); });
if (!sendRet) {
qCritical() << QString(tr("send ask client list failed."));
return;
}

View File

@@ -50,35 +50,8 @@ private:
private:
QMenu* menu_;
QThread* th_;
QThread* mainTh_;
SocketWorker* sockWorker_{};
QStandardItemModel* model_;
};
class ConnectWorker : public QObject
{
Q_OBJECT
public:
explicit ConnectWorker(ClientCore* clientCore, QObject* parent = nullptr)
: QObject(parent), clientCore_(clientCore)
{
}
public slots:
void doConnect(const QString& ip, int port, QThread* parentThread)
{
emit connecting();
bool connected = clientCore_->Connect(ip, port);
clientCore_->moveToThread(parentThread);
emit connectResult(connected);
}
signals:
void connectResult(bool success);
void connecting();
private:
ClientCore* clientCore_;
};
#endif // CONNECTCONTROL_H

View File

@@ -42,6 +42,11 @@ void FileManager::SetModeStr(const QString& modeStr, int type, ClientCore* clien
ui->tableWidget->setBasePathCall([this]() { return curRoot_; });
}
void FileManager::SetOtherSideCall(const std::function<QString()>& call)
{
ui->tableWidget->setOtherSideCall(call);
}
void FileManager::InitControl()
{
QStringList headers;

View File

@@ -22,6 +22,7 @@ public:
public:
void SetModeStr(const QString& modeStr, int type = 0, ClientCore* clientCore = nullptr);
void SetOtherSideCall(const std::function<QString()>& call);
QString GetCurRoot();
signals:

View File

@@ -42,6 +42,11 @@ void CustomTableWidget::setRemoteIDCall(const std::function<QString()>& call)
ridCall_ = call;
}
void CustomTableWidget::setOtherSideCall(const std::function<QString()>& call)
{
otherSideCall_ = call;
}
void CustomTableWidget::dropEvent(QDropEvent* event)
{
if (!event->mimeData()->hasFormat("application/x-qabstractitemmodeldatalist")) {
@@ -51,12 +56,29 @@ void CustomTableWidget::dropEvent(QDropEvent* event)
QByteArray encoded = event->mimeData()->data("application/x-qabstractitemmodeldatalist");
QDataStream stream(&encoded, QIODevice::ReadOnly);
QVector<TransTask> tasks;
QList<QTableWidgetItem*> draggedItems;
while (!stream.atEnd()) {
int row, col;
QMap<int, QVariant> roleData;
stream >> row >> col >> roleData;
if (col != 1) {
continue;
}
TransTask task;
task.isUpload = isRemote_;
task.localId = oidCall_();
task.remoteId = ridCall_();
if (isRemote_) {
task.remotePath = basePathCall_();
task.localPath = Util::Join(otherSideCall_(), roleData[Qt::DisplayRole].toString());
} else {
task.localPath = basePathCall_();
task.remotePath = Util::Join(otherSideCall_(), roleData[Qt::DisplayRole].toString());
}
tasks.push_back(task);
}
emit sigTasks(tasks);
}
void CustomTableWidget::dragEnterEvent(QDragEnterEvent* event)

View File

@@ -20,6 +20,7 @@ public:
void setBasePathCall(const std::function<QString()>& call);
void setOwnIDCall(const std::function<QString()>& call);
void setRemoteIDCall(const std::function<QString()>& call);
void setOtherSideCall(const std::function<QString()>& call);
protected:
void dropEvent(QDropEvent* event) override;