Skip to content

Commit

Permalink
fix: Searching for the wrong IP, changing to the correct one after le…
Browse files Browse the repository at this point in the history
…aving, requires a long search time

Searching for the wrong IP, changing to the correct one after leaving, requires a long search time

Log: Searching for the wrong IP, changing to the correct one after leaving, requires a long search time
  • Loading branch information
liyigang1 committed Feb 2, 2024
1 parent 99e3579 commit 0a3c0a7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
24 changes: 24 additions & 0 deletions src/plugins/daemon/core/service/comshare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "comshare.h"

#include <QTime>

Comshare::Comshare()
{
Expand Down Expand Up @@ -60,3 +61,26 @@ bool Comshare::checkTransCanConnect()
return _cur_status <= CurrentStatus::CURRENT_STATUS_DISCONNECT ||
_cur_status > CURRENT_STATUS_TRAN_FILE_RCV;
}

void Comshare::searchIp(const QString &token, const int64 time)
{
QMutexLocker lk(&_search_lock);
_search_ips.insert(token, time);
}

bool Comshare::checkSearchRes(const QString &token, const int64 time)
{
QMutexLocker lk(&_search_lock);
if (!_search_ips.contains(token)) {
return false;
}
for (auto it = _search_ips.begin(); it != _search_ips.end();) {
if (it.value() <= time) {
it = _search_ips.erase(it);
} else {
it++;
}
}

return true;
}
5 changes: 5 additions & 0 deletions src/plugins/daemon/core/service/comshare.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <QList>
#include <QReadWriteLock>
#include <QMap>
#include <QMutex>

typedef enum income_type_t {
IN_LOGIN_RESULT= 100,
Expand Down Expand Up @@ -91,12 +92,16 @@ class Comshare
CurrentStatus currentStatus();
bool checkTransCanConnect();
bool checkCanTransJob();
void searchIp(const QString &token, const int64 time);
bool checkSearchRes(const QString &token, const int64 time);
private:
QReadWriteLock _lock;
std::atomic_int _cur_status {0};// 0是没有连接,1是文件投送连接,
// 2文件投送申请,3文件发送,4文件接收,5键鼠共享连接,6键鼠共享中
QMap<QString, QString> _target_app; // appname
QMap<QString, QString> _target_ip; //
QMutex _search_lock;
QMap<QString, int64> _search_ips;
};

#endif // COMSHARE_H
14 changes: 9 additions & 5 deletions src/plugins/daemon/core/service/discoveryjob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void DiscoveryJob::announcerRun(const fastring &info)
_announcer_p = co::make<searchlight::Announcer>("ulink_service", UNI_RPC_PORT_BASE, info);

((searchlight::Announcer*)_announcer_p)->start([this](const QString &ip){
QtConcurrent::run([this, ip](){
UNIGO([this, ip](){
auto selfIp = Util::getFirstIp();
if (selfIp.empty())
return;
Expand Down Expand Up @@ -211,9 +211,7 @@ void DiscoveryJob::searchDeviceByIp(const QString &ip, const bool remove)
((searchlight::Discoverer*)_discoverer_p)->setSearchIp("");
return;
}
RemoteServiceSender sender("dde-cooperation", ip, 51597, false);
PingPong ping;
ping.ip = "search-ping";

SearchDeviceResult ev;
auto offline = Util::getFirstIp().empty();
if (offline) {
Expand All @@ -222,11 +220,17 @@ void DiscoveryJob::searchDeviceByIp(const QString &ip, const bool remove)
// 通知前端
req.add_member("api", "Frontend.searchDeviceRes");
SendIpcService::instance()->handleSendToClient("dde-cooperation", req.str().c_str());
((searchlight::Discoverer*)_discoverer_p)->setSearchIp("");
return;
}

int64 startTime = QDateTime::currentMSecsSinceEpoch();
RemoteServiceSender sender("dde-cooperation", ip, 51597, false);
Comshare::instance()->searchIp(QString::number(quintptr(&sender))+ip, startTime);
PingPong ping;
ping.ip = "search-ping";
auto result = sender.doSendProtoMsg(SEARCH_DEVICE_BY_IP, ping.as_json().str().c_str(), QByteArray());
if (!Comshare::instance()->checkSearchRes(QString::number(quintptr(&sender))+ip, startTime))
return;
if (result.errorType < INVOKE_OK){
// 通知前端搜索失败
// 通知前端搜索结果
Expand Down
8 changes: 5 additions & 3 deletions src/plugins/daemon/core/service/ipc/handleipcservice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,9 @@ void HandleIpcService::handleShareServerStart(const bool ok, const QString msg)

void HandleIpcService::handleSearchDevice(co::Json json)
{
SearchDevice de;
de.from_json(json);
DiscoveryJob::instance()->searchDeviceByIp(de.ip.c_str(), de.remove);
UNIGO([json](){
SearchDevice de;
de.from_json(json);
DiscoveryJob::instance()->searchDeviceByIp(de.ip.c_str(), de.remove);
});
}

0 comments on commit 0a3c0a7

Please sign in to comment.