From 5cc8ef1eb01d1b1697dfc6b22fb73359a99ac0d7 Mon Sep 17 00:00:00 2001 From: andrey Date: Fri, 20 Aug 2021 18:25:59 +0300 Subject: [PATCH 1/3] fail reconnect --- libs/cloud/picloudclient.cpp | 33 +++++++++++++++------------- libs/cloud/picloudserver.cpp | 5 +++-- libs/main/console/pikbdlistener.h | 2 +- libs/main/core/piobject.cpp | 13 ++++------- libs/main/io_devices/pibinarylog.cpp | 18 +++++++-------- 5 files changed, 35 insertions(+), 36 deletions(-) diff --git a/libs/cloud/picloudclient.cpp b/libs/cloud/picloudclient.cpp index 99bbfa93..be155475 100644 --- a/libs/cloud/picloudclient.cpp +++ b/libs/cloud/picloudclient.cpp @@ -28,7 +28,9 @@ PICloudClient::PICloudClient(const PIString & path, PIIODevice::DeviceMode mode) CONNECTL(ð, connected, [this](){opened_ = true; tcp.sendStart();}); CONNECTU(&streampacker, packetReceiveEvent, this, _readed); CONNECTL(ð, disconnected, [this](bool){ -// piCoutObj << "disconnected"; + piCoutObj << "disconnected"; + static_cast(ð)->stop(); + opened_ = false; if (is_connected) disconnected(); internalDisconnect(); piMSleep(100); @@ -37,11 +39,12 @@ PICloudClient::PICloudClient(const PIString & path, PIIODevice::DeviceMode mode) PICloudClient::~PICloudClient() { + piCoutObj << "~PICloudClient()"; + PIThread::stop(); eth.close(); - if (is_connected) disconnected(); internalDisconnect(); - close(); - stop(); + //close(); + //stop(false); } @@ -57,22 +60,23 @@ void PICloudClient::setKeepConnection(bool on) { bool PICloudClient::openDevice() { -// piCout << "PICloudClient open device" << path(); + piCout << "PICloudClient open device" << path(); bool op = eth.connect(PIEthernet::Address::resolve(path()), false); if (op) { - mutex_buff.lock(); + mutex_connect.lock(); eth.startThreadedRead(); - bool conn_ok = cond_connect.waitFor(mutex_buff, (int)eth.readTimeout()); -// piCoutObj << "conn_ok" << conn_ok; - mutex_buff.unlock(); + piCoutObj << "connecting..."; + bool conn_ok = cond_connect.waitFor(mutex_connect, (int)eth.readTimeout()); + piCoutObj << "conn_ok" << conn_ok; if (!conn_ok) { eth.stop(); eth.close(); piMSleep(100); } - return isConnected(); + mutex_connect.unlock(); + return is_connected; } else { - eth.close(); + //eth.close(); return false; } } @@ -84,15 +88,15 @@ bool PICloudClient::closeDevice() { internalDisconnect(); } eth.stop(); - if (eth.isOpened()) eth.close(); + eth.close(); return true; } int PICloudClient::readDevice(void * read_to, int max_size) { // piCoutObj << "readDevice"; - if (!is_connected) return -1; - int sz = -1; + if (!is_connected) return 0; + int sz = 0; mutex_buff.lock(); cond_buff.wait(mutex_buff, [this](){return !buff.isEmpty() || !is_connected;}); if (is_connected) { @@ -131,7 +135,6 @@ void PICloudClient::_readed(PIByteArray & ba) { } break; case PICloud::TCP::Disconnect: - is_connected = false; static_cast(ð)->stop(); eth.close(); break; diff --git a/libs/cloud/picloudserver.cpp b/libs/cloud/picloudserver.cpp index 4f7eaa51..ffea8800 100644 --- a/libs/cloud/picloudserver.cpp +++ b/libs/cloud/picloudserver.cpp @@ -29,6 +29,7 @@ PICloudServer::PICloudServer(const PIString & path, PIIODevice::DeviceMode mode) CONNECTL(ð, connected, [this](){tcp.sendStart();}); CONNECTL(ð, disconnected, [this](bool){ piCoutObj << "disconnected"; + static_cast(ð)->stop(); opened_ = false; piMSleep(100); }); @@ -142,8 +143,8 @@ bool PICloudServer::Client::closeDevice() { int PICloudServer::Client::readDevice(void * read_to, int max_size) { - if (!is_connected) return -1; - int sz = -1; + if (!is_connected) return 0; + int sz = 0; mutex_buff.lock(); cond_buff.wait(mutex_buff, [this](){return !buff.isEmpty() || !is_connected;}); if (is_connected) { diff --git a/libs/main/console/pikbdlistener.h b/libs/main/console/pikbdlistener.h index e578bd42..b257ed67 100644 --- a/libs/main/console/pikbdlistener.h +++ b/libs/main/console/pikbdlistener.h @@ -25,7 +25,7 @@ #include "pithread.h" -#define WAIT_FOR_EXIT while (!PIKbdListener::exiting) piMSleep(PIP_MIN_MSLEEP*5); +#define WAIT_FOR_EXIT while (!PIKbdListener::exiting) piMSleep(PIP_MIN_MSLEEP*5); // TODO: rewrite with condvar class PIP_EXPORT PIKbdListener: public PIThread diff --git a/libs/main/core/piobject.cpp b/libs/main/core/piobject.cpp index 9d6d7ac0..2da587df 100644 --- a/libs/main/core/piobject.cpp +++ b/libs/main/core/piobject.cpp @@ -700,8 +700,7 @@ PIObject::Deleter::Deleter() { stopping = started = posted = false; CONNECTL(&(PRIVATE->thread), started, [this](){proc();}); PRIVATE->thread.startOnce(); - while (!started) - piMSleep(1); + while (!started) piMSleep(1); } @@ -710,8 +709,7 @@ PIObject::Deleter::~Deleter() { stopping = true; PRIVATE->cond_var.notifyAll(); #ifndef WINDOWS - while (PRIVATE->thread.isRunning()) - piMSleep(1); + while (PRIVATE->thread.isRunning()) piMSleep(1); #endif deleteAll(); //piCout << "~Deleter ok"; @@ -774,12 +772,9 @@ void PIObject::Deleter::deleteObject(PIObject * o) { //piCout << "[Deleter] delete" << (uintptr_t)o << "..."; if (o->isPIObject()) { //piCout << "[Deleter] delete" << (uintptr_t)o << "wait atomic ..."; - while (o->isInEvent()) { - piMSleep(1); - } + while (o->isInEvent()) piMSleep(1); //piCout << "[Deleter] delete" << (uintptr_t)o << "wait atomic done"; - if (o->isPIObject()) - delete o; + if (o->isPIObject()) delete o; } //piCout << "[Deleter] delete" << (uintptr_t)o << "done"; } diff --git a/libs/main/io_devices/pibinarylog.cpp b/libs/main/io_devices/pibinarylog.cpp index bfa6c647..14afc0ec 100644 --- a/libs/main/io_devices/pibinarylog.cpp +++ b/libs/main/io_devices/pibinarylog.cpp @@ -181,7 +181,7 @@ bool PIBinaryLog::threadedRead(uchar *readed, int size) { pausemutex.lock(); if (is_pause) { pausemutex.unlock(); - piMSleep(100); + piMSleep(100); // TODO: rewrite with condvar return false; } else if (pause_time > PISystemTime()) { startlogtime += pause_time; @@ -204,22 +204,22 @@ bool PIBinaryLog::threadedRead(uchar *readed, int size) { int dtc; if (is_started) { if (is_pause) { - piMSleep(100); + piMSleep(100); // TODO: rewrite with condvar return false; } if (delay > 0) { - cdelay = delay * play_speed; - dtc = int(cdelay) /100; + cdelay = delay * play_speed;// TODO: rewrite with condvar + dtc = int(cdelay) / 100;// TODO: rewrite with condvar if (play_speed <= 0.) dtc = 2; //piCout << play_speed << dtc; for (int j=0; j Date: Fri, 20 Aug 2021 18:30:19 +0300 Subject: [PATCH 2/3] fail --- libs/cloud/picloudclient.cpp | 8 ++++---- libs/cloud/picloudserver.cpp | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/libs/cloud/picloudclient.cpp b/libs/cloud/picloudclient.cpp index be155475..c01661da 100644 --- a/libs/cloud/picloudclient.cpp +++ b/libs/cloud/picloudclient.cpp @@ -25,12 +25,12 @@ PICloudClient::PICloudClient(const PIString & path, PIIODevice::DeviceMode mode) tcp.setRole(PICloud::TCP::Client); setName("cloud_client"); is_connected = false; - CONNECTL(ð, connected, [this](){opened_ = true; tcp.sendStart();}); + CONNECTL(ð, connected, [this](){/*opened_ = true;*/ tcp.sendStart();}); CONNECTU(&streampacker, packetReceiveEvent, this, _readed); CONNECTL(ð, disconnected, [this](bool){ piCoutObj << "disconnected"; static_cast(ð)->stop(); - opened_ = false; + //opened_ = false; if (is_connected) disconnected(); internalDisconnect(); piMSleep(100); @@ -95,8 +95,8 @@ bool PICloudClient::closeDevice() { int PICloudClient::readDevice(void * read_to, int max_size) { // piCoutObj << "readDevice"; - if (!is_connected) return 0; - int sz = 0; + if (!is_connected) return -1; + int sz = -1; mutex_buff.lock(); cond_buff.wait(mutex_buff, [this](){return !buff.isEmpty() || !is_connected;}); if (is_connected) { diff --git a/libs/cloud/picloudserver.cpp b/libs/cloud/picloudserver.cpp index ffea8800..0513772d 100644 --- a/libs/cloud/picloudserver.cpp +++ b/libs/cloud/picloudserver.cpp @@ -143,8 +143,8 @@ bool PICloudServer::Client::closeDevice() { int PICloudServer::Client::readDevice(void * read_to, int max_size) { - if (!is_connected) return 0; - int sz = 0; + if (!is_connected) return -1; + int sz = -1; mutex_buff.lock(); cond_buff.wait(mutex_buff, [this](){return !buff.isEmpty() || !is_connected;}); if (is_connected) { From c937d7251a851cb68a12be11625943966229aca3 Mon Sep 17 00:00:00 2001 From: Andrey Date: Mon, 23 Aug 2021 13:56:21 +0300 Subject: [PATCH 3/3] it works --- libs/cloud/picloudclient.cpp | 56 +++++++++++++++++++---------- libs/cloud/picloudserver.cpp | 17 +++++---- libs/main/cloud/picloudclient.h | 1 + libs/main/io_devices/piiodevice.cpp | 2 +- main.cpp | 4 +-- 5 files changed, 52 insertions(+), 28 deletions(-) diff --git a/libs/cloud/picloudclient.cpp b/libs/cloud/picloudclient.cpp index c01661da..b69ab812 100644 --- a/libs/cloud/picloudclient.cpp +++ b/libs/cloud/picloudclient.cpp @@ -25,26 +25,35 @@ PICloudClient::PICloudClient(const PIString & path, PIIODevice::DeviceMode mode) tcp.setRole(PICloud::TCP::Client); setName("cloud_client"); is_connected = false; - CONNECTL(ð, connected, [this](){/*opened_ = true;*/ tcp.sendStart();}); + is_deleted = false; +// setReopenEnabled(false); + CONNECTL(ð, connected, [this](){opened_ = true; tcp.sendStart();}); CONNECTU(&streampacker, packetReceiveEvent, this, _readed); CONNECTL(ð, disconnected, [this](bool){ - piCoutObj << "disconnected"; + if (is_deleted) return; + //piCoutObj << "eth disconnected"; static_cast(ð)->stop(); - //opened_ = false; + opened_ = false; if (is_connected) disconnected(); internalDisconnect(); - piMSleep(100); + //piCoutObj << "eth disconnected done"; }); } PICloudClient::~PICloudClient() { - piCoutObj << "~PICloudClient()"; + //piCoutObj << "~PICloudClient()"; PIThread::stop(); - eth.close(); + //eth.close(); + if (is_connected) disconnected(); + is_connected = false; + close(); + //piCoutObj << "~PICloudClient() closed"; internalDisconnect(); - //close(); - //stop(false); +// stop(false); + is_deleted = true; + internalDisconnect(); + //piCoutObj << "~PICloudClient() done"; } @@ -60,20 +69,21 @@ void PICloudClient::setKeepConnection(bool on) { bool PICloudClient::openDevice() { - piCout << "PICloudClient open device" << path(); + //piCoutObj << "open";// << path(); bool op = eth.connect(PIEthernet::Address::resolve(path()), false); if (op) { mutex_connect.lock(); eth.startThreadedRead(); - piCoutObj << "connecting..."; + //piCoutObj << "connecting..."; bool conn_ok = cond_connect.waitFor(mutex_connect, (int)eth.readTimeout()); - piCoutObj << "conn_ok" << conn_ok; + //piCoutObj << "conn_ok" << conn_ok << is_connected; + mutex_connect.unlock(); if (!conn_ok) { + mutex_connect.lock(); eth.stop(); eth.close(); - piMSleep(100); + mutex_connect.unlock(); } - mutex_connect.unlock(); return is_connected; } else { //eth.close(); @@ -83,7 +93,7 @@ bool PICloudClient::openDevice() { bool PICloudClient::closeDevice() { - PIThread::stop(false); + //PIThread::stop(); if (is_connected) { internalDisconnect(); } @@ -94,8 +104,9 @@ bool PICloudClient::closeDevice() { int PICloudClient::readDevice(void * read_to, int max_size) { -// piCoutObj << "readDevice"; - if (!is_connected) return -1; + if (is_deleted) return -1; + //piCoutObj << "readDevice"; + if (!is_connected && eth.isClosed()) openDevice(); int sz = -1; mutex_buff.lock(); cond_buff.wait(mutex_buff, [this](){return !buff.isEmpty() || !is_connected;}); @@ -105,11 +116,14 @@ int PICloudClient::readDevice(void * read_to, int max_size) { buff.remove(0, sz); } mutex_buff.unlock(); + if (!is_connected) opened_ = false; + //piCoutObj << "readDevice done" << sz; return sz; } int PICloudClient::writeDevice(const void * data, int size) { + if (is_deleted) return -1; // piCoutObj << "writeDevice"; return tcp.sendData(PIByteArray(data, size)); } @@ -123,24 +137,30 @@ void PICloudClient::internalDisconnect() { void PICloudClient::_readed(PIByteArray & ba) { - mutex_buff.lock(); + if (is_deleted) return; + //piCoutObj << "_readed"; PIPair hdr = tcp.parseHeader(ba); if (hdr.second == tcp.role()) { switch (hdr.first) { case PICloud::TCP::Connect: if (tcp.parseConnect(ba) == 1) { + mutex_connect.lock(); is_connected = true; connected(); + mutex_connect.unlock(); cond_connect.notifyOne(); } break; case PICloud::TCP::Disconnect: static_cast(ð)->stop(); + opened_ = false; eth.close(); break; case PICloud::TCP::Data: if (is_connected) { + mutex_buff.lock(); buff.append(ba); + mutex_buff.unlock(); cond_buff.notifyOne(); } break; @@ -149,7 +169,7 @@ void PICloudClient::_readed(PIByteArray & ba) { } //piCoutObj << "readed" << ba.toHex(); } - mutex_buff.unlock(); while (buff.size_s() > threadedReadBufferSize()) piMSleep(100); // FIXME: sleep here is bad + //piCoutObj << "_readed done"; } diff --git a/libs/cloud/picloudserver.cpp b/libs/cloud/picloudserver.cpp index 0513772d..ad7500f4 100644 --- a/libs/cloud/picloudserver.cpp +++ b/libs/cloud/picloudserver.cpp @@ -26,11 +26,12 @@ PICloudServer::PICloudServer(const PIString & path, PIIODevice::DeviceMode mode) tcp.setServerName(server_name); setName("cloud_server__" + server_name); CONNECTU(&streampacker, packetReceiveEvent, this, _readed); - CONNECTL(ð, connected, [this](){tcp.sendStart();}); + CONNECTL(ð, connected, [this](){opened_ = true; tcp.sendStart();}); CONNECTL(ð, disconnected, [this](bool){ - piCoutObj << "disconnected"; + //piCoutObj << "disconnected"; static_cast(ð)->stop(); opened_ = false; + ping_timer.stop(false); piMSleep(100); }); CONNECTL(&ping_timer, tickEvent, [this] (void *, int){ @@ -58,14 +59,14 @@ PIVector PICloudServer::clients() const { bool PICloudServer::openDevice() { - piCout << "PICloudServer open device" << path(); + //piCout << "PICloudServer open device" << path(); bool op = eth.connect(PIEthernet::Address::resolve(path()), false); if (op) { eth.startThreadedRead(); ping_timer.start(5000); return true; } - ping_timer.stop(); + ping_timer.stop(false); eth.close(); return false; } @@ -73,6 +74,7 @@ bool PICloudServer::openDevice() { bool PICloudServer::closeDevice() { eth.stop(); + ping_timer.stop(false); clients_mutex.lock(); for (auto c : clients_) { c->close(); @@ -88,7 +90,8 @@ bool PICloudServer::closeDevice() { int PICloudServer::readDevice(void * read_to, int max_size) { //piCoutObj << "readDevice"; - piMSleep(eth.readTimeout()); + if (!opened_) openDevice(); + else piMSleep(eth.readTimeout()); return -1; } @@ -184,7 +187,7 @@ void PICloudServer::_readed(PIByteArray & ba) { if (oc) { tcp.sendDisconnected(id); } else { - piCoutObj << "new Client" << id; + //piCoutObj << "new Client" << id; Client * c = new Client(this, id); CONNECTU(c, deleted, this, clientDeleted); clients_mutex.lock(); @@ -196,7 +199,7 @@ void PICloudServer::_readed(PIByteArray & ba) { } break; case PICloud::TCP::Disconnect: { uint id = tcp.parseDisconnect(ba); - piCoutObj << "remove Client" << id; + //piCoutObj << "remove Client" << id; clients_mutex.lock(); Client * oc = index_clients.value(id, nullptr); clients_mutex.unlock(); diff --git a/libs/main/cloud/picloudclient.h b/libs/main/cloud/picloudclient.h index 49f7456b..cc944b2b 100644 --- a/libs/main/cloud/picloudclient.h +++ b/libs/main/cloud/picloudclient.h @@ -60,6 +60,7 @@ private: PIConditionVariable cond_buff; PIConditionVariable cond_connect; std::atomic_bool is_connected; + std::atomic_bool is_deleted; }; #endif // PICLOUDCLIENT_H diff --git a/libs/main/io_devices/piiodevice.cpp b/libs/main/io_devices/piiodevice.cpp index 6036a7a5..482e0367 100644 --- a/libs/main/io_devices/piiodevice.cpp +++ b/libs/main/io_devices/piiodevice.cpp @@ -239,7 +239,7 @@ void PIIODevice::check_start(void * data, int delim) { //cout << "check " << tread_started_ << endl; if (open()) { thread_started_ = true; - timer.stop(); + timer.stop(false); } } diff --git a/main.cpp b/main.cpp index 2d847deb..764f7dd0 100644 --- a/main.cpp +++ b/main.cpp @@ -32,8 +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(&c, connected, ([](){piCout << "connected";})); + CONNECTL(&c, disconnected, ([](){piCout << "disconnected";})); CONNECTL(&s, newConnection, ([&](PICloudServer::Client * cl){ piCout << "[Server] new client:" << cl; clients << cl;