From 50bff123642958ab9809f4f162d714f9c23ff5bc Mon Sep 17 00:00:00 2001 From: peri4 Date: Thu, 22 Feb 2024 17:10:38 +0300 Subject: [PATCH] map and cloud fix, add cloud debug --- CMakeLists.txt | 2 +- libs/cloud/picloudserver.cpp | 14 ++----- libs/main/containers/pimap.h | 1 + libs/main/io_devices/piethernet.cpp | 17 ++++---- utils/cloud_dispatcher/dispatcherserver.cpp | 43 +++++++-------------- 5 files changed, 29 insertions(+), 48 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7bd73ff4..73d54585 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ cmake_policy(SET CMP0017 NEW) # need include() with .cmake project(PIP) set(PIP_MAJOR 3) set(PIP_MINOR 15) -set(PIP_REVISION 1) +set(PIP_REVISION 2) set(PIP_SUFFIX ) set(PIP_COMPANY SHS) set(PIP_DOMAIN org.SHS) diff --git a/libs/cloud/picloudserver.cpp b/libs/cloud/picloudserver.cpp index 9bde2fd5..f3d35488 100644 --- a/libs/cloud/picloudserver.cpp +++ b/libs/cloud/picloudserver.cpp @@ -34,12 +34,12 @@ PICloudServer::PICloudServer(const PIString & path, PIIODevice::DeviceMode mode) opened_ = true; cvar.notifyOne(); open_mutex.unlock(); - // piCoutObj << "connected"; + piCoutObj << "connected"; tcp.sendStart(); }); CONNECTL(ð, disconnected, [this](bool) { if (is_deleted) return; - // piCoutObj << "disconnected"; + piCoutObj << "disconnected"; clients_mutex.lock(); for (auto c: clients_) { c->is_connected = false; @@ -89,7 +89,7 @@ PIVector PICloudServer::clients() const { bool PICloudServer::openDevice() { - // piCout << "PICloudServer open device" << path(); + piCoutObj << "open device" << path(); if (is_deleted) return false; bool op = eth.connect(PINetworkAddress::resolve(path()), false); if (op) { @@ -293,12 +293,6 @@ void PICloudServer::clientDeleted(PIObject * o) { clients_mutex.lock(); clients_.removeOne(c); removed_clients_.removeAll(c); - auto it = index_clients.makeIterator(); - while (it.next()) { - if (it.value() == c) { - index_clients.remove(it.key()); - break; - } - } + index_clients.removeWhere([c](uint, Client * v) { return v == c; }); clients_mutex.unlock(); } diff --git a/libs/main/containers/pimap.h b/libs/main/containers/pimap.h index 50b847d7..01010f7c 100644 --- a/libs/main/containers/pimap.h +++ b/libs/main/containers/pimap.h @@ -425,6 +425,7 @@ public: --i; } } + return *this; } //! \~english Same as \a remove(). diff --git a/libs/main/io_devices/piethernet.cpp b/libs/main/io_devices/piethernet.cpp index cf29729c..07adfd5f 100644 --- a/libs/main/io_devices/piethernet.cpp +++ b/libs/main/io_devices/piethernet.cpp @@ -357,8 +357,7 @@ void PIEthernet::applyTimeout(int fd, int opt, double ms) { void PIEthernet::applyOptInt(int level, int opt, int val) { - if (sock < 0) return; - ethSetsockoptInt(sock, level, opt, val); + if (sock != -1) ethSetsockoptInt(sock, level, opt, val); if (sock_s != sock && sock_s != -1) ethSetsockoptInt(sock_s, level, opt, val); } @@ -1335,14 +1334,16 @@ void PIEthernet::ethClosesocket(int sock, bool shutdown) { int PIEthernet::ethSetsockopt(int sock, int level, int optname, const void * optval, int optlen) { if (sock < 0) return -1; - return setsockopt(sock, - level, - optname, + auto ret = setsockopt(sock, + level, + optname, #ifdef WINDOWS - (char *) + (char *) #endif - optval, - optlen); + optval, + optlen); + if (ret != 0) piCout << "setsockopt error:" << ethErrorString(); + return ret; } diff --git a/utils/cloud_dispatcher/dispatcherserver.cpp b/utils/cloud_dispatcher/dispatcherserver.cpp index d7b5310b..72ef2784 100644 --- a/utils/cloud_dispatcher/dispatcherserver.cpp +++ b/utils/cloud_dispatcher/dispatcherserver.cpp @@ -28,7 +28,7 @@ void DispatcherServer::start() { void DispatcherServer::picoutStatus() { - map_mutex.lock(); + PIMutexLocker locker(map_mutex); piCout << PICoutManipulators::NewLine; piCout << "Connections:"; for (auto c: clients) { @@ -40,16 +40,12 @@ void DispatcherServer::picoutStatus() { piCout << " " << it.key(); it.value()->printStatus(); } - map_mutex.unlock(); } void DispatcherServer::cleanClients() { - map_mutex.lock(); - for (auto c: rmrf_clients) { - delete c; - } - rmrf_clients.clear(); + PIMutexLocker locker(map_mutex); + piDeleteAllAndClear(rmrf_clients); for (auto c: clients) { if (!index_c_servers.contains(c) && !index_c_clients.contains(c)) { if (!rm_clients.contains(c)) rm_clients << c; @@ -88,12 +84,11 @@ void DispatcherServer::cleanClients() { index_c_clients.remove(c); rm_clients.removeAll(c); } - map_mutex.unlock(); } void DispatcherServer::updateConnectionsTile(TileList * tl) { - map_mutex.lock(); + PIMutexLocker locker(map_mutex); tl->content.clear(); for (auto c: clients) { PIString role = "Invalid"; @@ -118,12 +113,11 @@ void DispatcherServer::updateConnectionsTile(TileList * tl) { for (auto c: rmrf_clients) { tl->content << TileList::Row("[NULL]" + c->address(), PIScreenTypes::CellFormat()); } - map_mutex.unlock(); } void DispatcherServer::updateServersTile(TileList * tl, PISet servers) { - map_mutex.lock(); + PIMutexLocker locker(map_mutex); tl->content.clear(); auto mi = c_servers.makeIterator(); while (mi.next()) { @@ -132,12 +126,11 @@ void DispatcherServer::updateServersTile(TileList * tl, PISetgetConnection())) tl->selected << (tl->content.size_s() - 1); } - map_mutex.unlock(); } void DispatcherServer::updateClientsTile(TileList * tl, PISet servers) { - map_mutex.lock(); + PIMutexLocker locker(map_mutex); tl->content.clear(); auto mi = c_servers.makeIterator(); while (mi.next()) { @@ -146,26 +139,23 @@ void DispatcherServer::updateClientsTile(TileList * tl, PISetcontent << TileList::Row(c->address(), PIScreenTypes::CellFormat()); } } - map_mutex.unlock(); } const DispatcherClient * DispatcherServer::getConnection(int index) { + PIMutexLocker locker(map_mutex); const DispatcherClient * ret = nullptr; - map_mutex.lock(); if (index >= 0 && index < clients.size_s()) ret = clients[index]; - map_mutex.unlock(); return ret; } const DispatcherClient * DispatcherServer::getServer(int index) { + PIMutexLocker locker(map_mutex); const DispatcherClient * ret = nullptr; - map_mutex.lock(); if (index >= 0 && index < clients.size_s()) { if (index_c_servers.contains(clients[index])) ret = clients[index]; } - map_mutex.unlock(); return ret; } @@ -173,14 +163,13 @@ const DispatcherClient * DispatcherServer::getServer(int index) { PISet DispatcherServer::getServers(PISet ids) { PISet ret; if (ids.isEmpty()) return ret; - map_mutex.lock(); + PIMutexLocker locker(map_mutex); int i = 0; auto mi = c_servers.makeIterator(); while (mi.next()) { if (ids.contains(i)) ret << mi.value()->getConnection(); i++; } - map_mutex.unlock(); return ret; } @@ -196,7 +185,7 @@ void DispatcherServer::disconnectClient(DispatcherClient * client) { return; } piCoutObj << "remove ..." << client->clientId(); - map_mutex.lock(); + PIMutexLocker locker(map_mutex); clients.removeAll(client); rm_clients.removeAll(client); CloudServer * cs = index_c_servers.value(client, nullptr); @@ -221,8 +210,7 @@ void DispatcherServer::disconnectClient(DispatcherClient * client) { index_c_clients.remove(client); } // client->close(); - rmrf_clients << client; - map_mutex.unlock(); + if (!rmrf_clients.contains(client)) rmrf_clients << client; piCoutObj << "remove done" << client->clientId(); } @@ -235,7 +223,7 @@ void DispatcherServer::newConnection(PIEthernet * cl) { DispatcherClient * client = new DispatcherClient(cl, client_gid++); CONNECTU(client, disconnectEvent, this, disconnectClient); CONNECTL(client, registerServer, [this](const PIByteArray & sname, DispatcherClient * c) { - map_mutex.lock(); + PIMutexLocker locker(map_mutex); CloudServer * cs = c_servers.value(sname, nullptr); if (cs) { rm_clients << c; @@ -247,10 +235,9 @@ void DispatcherServer::newConnection(PIEthernet * cl) { index_c_servers.insert(c, cs); c->authorise(true); } - map_mutex.unlock(); }); CONNECTL(client, registerClient, [this](const PIByteArray & sname, DispatcherClient * c) { - map_mutex.lock(); + PIMutexLocker locker(map_mutex); CloudServer * cs = c_servers.value(sname, nullptr); if (cs) { piCoutObj << "add new Client to Server ->" << sname.toHex(); @@ -261,11 +248,9 @@ void DispatcherServer::newConnection(PIEthernet * cl) { rm_clients << c; piCoutObj << "Client can't connect to Server ->" << sname.toHex(); } - map_mutex.unlock(); }); // piCoutObj << "add client" << client; - map_mutex.lock(); + PIMutexLocker locker(map_mutex); clients.push_back(client); - map_mutex.unlock(); client->start(); }