From 99e135caa2621d82c2f970a179e43599679b6365 Mon Sep 17 00:00:00 2001 From: andrey Date: Fri, 20 Aug 2021 17:22:25 +0300 Subject: [PATCH] PICloudClient disconnect --- libs/cloud/picloudclient.cpp | 29 +++++++++++++---------------- libs/cloud/picloudserver.cpp | 2 +- libs/main/cloud/picloudclient.h | 2 ++ main.cpp | 2 ++ 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/libs/cloud/picloudclient.cpp b/libs/cloud/picloudclient.cpp index 4fbd59b3..99bbfa93 100644 --- a/libs/cloud/picloudclient.cpp +++ b/libs/cloud/picloudclient.cpp @@ -29,10 +29,8 @@ PICloudClient::PICloudClient(const PIString & path, PIIODevice::DeviceMode mode) CONNECTU(&streampacker, packetReceiveEvent, this, _readed); CONNECTL(ð, disconnected, [this](bool){ // piCoutObj << "disconnected"; - opened_ = false; - is_connected = false; - cond_connect.notifyOne(); - cond_buff.notifyOne(); + if (is_connected) disconnected(); + internalDisconnect(); piMSleep(100); }); } @@ -40,12 +38,8 @@ PICloudClient::PICloudClient(const PIString & path, PIIODevice::DeviceMode mode) PICloudClient::~PICloudClient() { eth.close(); - if (is_connected) { - is_connected = false; - disconnected(); - cond_buff.notifyOne(); - cond_connect.notifyOne(); - } + if (is_connected) disconnected(); + internalDisconnect(); close(); stop(); } @@ -87,10 +81,7 @@ bool PICloudClient::openDevice() { bool PICloudClient::closeDevice() { PIThread::stop(false); if (is_connected) { - is_connected = false; - disconnected(); - cond_buff.notifyOne(); - cond_connect.notifyOne(); + internalDisconnect(); } eth.stop(); if (eth.isOpened()) eth.close(); @@ -120,6 +111,13 @@ int PICloudClient::writeDevice(const void * data, int size) { } +void PICloudClient::internalDisconnect() { + is_connected = false; + cond_buff.notifyOne(); + cond_connect.notifyOne(); +} + + void PICloudClient::_readed(PIByteArray & ba) { mutex_buff.lock(); PIPair hdr = tcp.parseHeader(ba); @@ -136,7 +134,6 @@ void PICloudClient::_readed(PIByteArray & ba) { is_connected = false; static_cast(ð)->stop(); eth.close(); - disconnected(); break; case PICloud::TCP::Data: if (is_connected) { @@ -150,6 +147,6 @@ void PICloudClient::_readed(PIByteArray & ba) { //piCoutObj << "readed" << ba.toHex(); } mutex_buff.unlock(); - while (buff.size_s() > threadedReadBufferSize()) piMSleep(100); + while (buff.size_s() > threadedReadBufferSize()) piMSleep(100); // FIXME: sleep here is bad } diff --git a/libs/cloud/picloudserver.cpp b/libs/cloud/picloudserver.cpp index 0cd50dcd..4f7eaa51 100644 --- a/libs/cloud/picloudserver.cpp +++ b/libs/cloud/picloudserver.cpp @@ -167,7 +167,7 @@ void PICloudServer::Client::pushBuffer(const PIByteArray & ba) { buff.append(ba); cond_buff.notifyOne(); mutex_buff.unlock(); - while (buff.size_s() > threadedReadBufferSize()) piMSleep(100); + while (buff.size_s() > threadedReadBufferSize()) piMSleep(100); // FIXME: sleep here is bad } diff --git a/libs/main/cloud/picloudclient.h b/libs/main/cloud/picloudclient.h index 1ea14883..49f7456b 100644 --- a/libs/main/cloud/picloudclient.h +++ b/libs/main/cloud/picloudclient.h @@ -52,6 +52,8 @@ protected: private: EVENT_HANDLER1(void, _readed, PIByteArray &, data); + void internalDisconnect(); + PIByteArray buff; PIMutex mutex_buff; PIMutex mutex_connect; diff --git a/main.cpp b/main.cpp index 66ab2f11..2d847deb 100644 --- a/main.cpp +++ b/main.cpp @@ -32,6 +32,8 @@ int main(int argc, char * argv[]) { piCout << "[Client] data:" << str; if (str == "ping_S") c.write(PIString("pong_S").toByteArray()); })); + CONNECTL(&c, connected, ([&](){piCout << "connected";})); + CONNECTL(&c, disconnected, ([&](){piCout << "disconnected";})); CONNECTL(&s, newConnection, ([&](PICloudServer::Client * cl){ piCout << "[Server] new client:" << cl; clients << cl;