Skip to content

Commit fc0ae18

Browse files
committed
fix: Fix share state issue
The shared target info is incorrect and do not check self sharing status that cause not update the share state if try to stop share. Log: Fix share state not update. Bug: https://pms.uniontech.com/bug-view-273413.html
1 parent c80d339 commit fc0ae18

7 files changed

+60
-12
lines changed

src/lib/cooperation/core/discover/discovercontroller.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -416,14 +416,14 @@ void DiscoverController::compatAddDeivces(StringMap infoMap)
416416
QString ip = ipList[0];
417417
QString sharedip = ipList[1];
418418

419-
if(ip == CooperationUtil::localIPAddress()) {
419+
if (ip == CooperationUtil::localIPAddress()) {
420420
WLOG << "Ignore local host ip: " << ip.toStdString();
421421
continue;
422422
}
423423

424424
devInfo->setIpAddress(ip);
425425
if (devInfo->discoveryMode() == DeviceInfo::DiscoveryMode::Everyone) {
426-
if (sharedip == CooperationUtil::localIPAddress())
426+
if (sharedip == CooperationUtil::localIPAddress() || _connectedDevice == devInfo->ipAddress())
427427
devInfo->setConnectStatus(DeviceInfo::Connected);
428428

429429
d->onlineDeviceList.append(devInfo);
@@ -452,8 +452,9 @@ void DiscoverController::startDiscover()
452452

453453
// 延迟,为了展示发现界面
454454
QTimer::singleShot(500, [this]() {
455-
// 兼容,获取发现列表,refresh则清除数据
456-
Q_EMIT startDiscoveryDevice();
455+
// clear current list first.
457456
refresh();
457+
// compation: start get nodes
458+
Q_EMIT startDiscoveryDevice();
458459
});
459460
}

src/lib/cooperation/core/net/compatwrapper.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ void CompatWrapperPrivate::ipcCompatSlot(int type, const QString& msg)
8484

8585
auto ip = QString::fromStdString(nodeInfo.os.ipv4);
8686
auto sharedip = QString::fromStdString(nodeInfo.os.share_connect_ip);
87+
auto sharing = ShareHelper::instance()->selfSharing(sharedip);
88+
if (sharing > 0) {
89+
// self shared ip but not running, reset the sharedip as empty.
90+
sharedip = "";
91+
}
8792

8893
// typedef QMap<QString, QString> StringMap;
8994
StringMap infoMap;

src/lib/cooperation/core/net/helper/sharehelper.cpp

+30-8
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,11 @@ void ShareHelperPrivate::notifyMessage(const QString &body, const QStringList &a
109109
void ShareHelperPrivate::stopCooperation()
110110
{
111111
if (targetDeviceInfo && targetDeviceInfo->connectStatus() == DeviceInfo::Connected) {
112-
ShareHelper::instance()->disconnectToDevice(targetDeviceInfo);
113112
#ifdef linux
114113
static QString body(tr("Coordination with \"%1\" has ended"));
115114
notifyMessage(body.arg(CommonUitls::elidedText(targetDeviceInfo->deviceName(), Qt::ElideMiddle, 15)), {}, 3 * 1000);
116115
#endif
116+
ShareHelper::instance()->disconnectToDevice(targetDeviceInfo);
117117
}
118118
}
119119

@@ -172,7 +172,9 @@ void ShareHelperPrivate::onActionTriggered(const QString &action)
172172
auto info = DiscoverController::instance()->findDeviceByIP(senderDeviceIp);
173173
if (!info) {
174174
WLOG << "AcceptAction, but not find: " << senderDeviceIp.toStdString();
175-
return;
175+
// create by remote connect info, that is not discoveried.
176+
info = DeviceInfoPointer(new DeviceInfo(senderDeviceIp, targetDevName));
177+
info->setPeripheralShared(true);
176178
}
177179

178180
// 更新设备列表中的状态
@@ -266,13 +268,17 @@ void ShareHelper::disconnectToDevice(const DeviceInfoPointer info)
266268

267269
ShareCooperationServiceManager::instance()->stop();
268270

269-
if (d->targetDeviceInfo) {
270-
d->targetDeviceInfo->setConnectStatus(DeviceInfo::Connectable);
271-
DiscoverController::instance()->updateDeviceState({ d->targetDeviceInfo });
272-
273-
static QString body(tr("Coordination with \"%1\" has ended"));
274-
d->notifyMessage(body.arg(CommonUitls::elidedText(d->targetDeviceInfo->deviceName(), Qt::ElideMiddle, 15)), {}, 3 * 1000);
271+
// The targetDeviceInfo can be null
272+
if (d->targetDeviceInfo.isNull()) {
273+
d->targetDeviceInfo = DeviceInfoPointer::create(*info.data());
275274
}
275+
276+
info->setConnectStatus(DeviceInfo::Connectable);
277+
d->targetDeviceInfo->setConnectStatus(DeviceInfo::Connectable);
278+
DiscoverController::instance()->updateDeviceState({ d->targetDeviceInfo });
279+
280+
static QString body(tr("Coordination with \"%1\" has ended"));
281+
d->notifyMessage(body.arg(CommonUitls::elidedText(d->targetDeviceInfo->deviceName(), Qt::ElideMiddle, 15)), {}, 3 * 1000);
276282
}
277283

278284
void ShareHelper::buttonClicked(const QString &id, const DeviceInfoPointer info)
@@ -504,3 +510,19 @@ void ShareHelper::onShareExcepted(int type, const QString &remote)
504510
break;
505511
}
506512
}
513+
514+
int ShareHelper::selfSharing(const QString &shareIp)
515+
{
516+
if (shareIp == CooperationUtil::localIPAddress()) {
517+
auto server = ShareCooperationServiceManager::instance()->server();
518+
auto client = ShareCooperationServiceManager::instance()->client();
519+
if (server.isNull() && client.isNull()) {
520+
return 1;
521+
}
522+
523+
auto sharing = (server && server->isRunning()) || (client && client->isRunning());
524+
return sharing ? 0 : 1;
525+
}
526+
527+
return -1;
528+
}

src/lib/cooperation/core/net/helper/sharehelper.h

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public Q_SLOTS:
3737
// exception: network connection(ping out) or other io
3838
void onShareExcepted(int type, const QString &remote);
3939

40+
// check the self is sharing with someone
41+
int selfSharing(const QString &shareIp);
42+
4043
private:
4144
explicit ShareHelper(QObject *parent = nullptr);
4245
~ShareHelper();

src/lib/cooperation/core/net/networkutil.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,11 @@ void NetworkUtil::handleCompatDiscover()
362362
for (const auto &peerInfo : nodeList.peers) {
363363
auto ip = QString::fromStdString(peerInfo.os.ipv4);
364364
auto sharedip = QString::fromStdString(peerInfo.os.share_connect_ip);
365+
auto sharing = ShareHelper::instance()->selfSharing(sharedip);
366+
if (sharing > 0) {
367+
// self shared ip but not running, reset the sharedip as empty.
368+
sharedip = "";
369+
}
365370
for (const auto &appInfo : peerInfo.apps) {
366371
if (appInfo.appname != ipc::CooperRegisterName)
367372
continue;

src/lib/cooperation/core/share/sharecooperationservice.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,15 @@ void ShareCooperationService::setBarrierProfile(const QString &dir)
162162
_cooConfig->setProfileDir(dir);
163163
}
164164

165+
bool ShareCooperationService::isRunning()
166+
{
167+
if (!barrierProcess()) {
168+
return false;
169+
}
170+
171+
return barrierProcess()->state() == QProcess::Running;
172+
}
173+
165174
bool ShareCooperationService::startBarrier()
166175
{
167176
LOG << "starting process";

src/lib/cooperation/core/share/sharecooperationservice.h

+3
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ class ShareCooperationService : public QObject
6464

6565
void setEnableCrypto(bool enable);
6666
void setBarrierProfile(const QString &dir);
67+
68+
bool isRunning();
69+
6770
public slots:
6871
bool restartBarrier();
6972
bool startBarrier();

0 commit comments

Comments
 (0)