fail reconnect

This commit is contained in:
2021-08-20 18:25:59 +03:00
parent 99e135caa2
commit 5cc8ef1eb0
5 changed files with 35 additions and 36 deletions

View File

@@ -28,7 +28,9 @@ PICloudClient::PICloudClient(const PIString & path, PIIODevice::DeviceMode mode)
CONNECTL(&eth, connected, [this](){opened_ = true; tcp.sendStart();});
CONNECTU(&streampacker, packetReceiveEvent, this, _readed);
CONNECTL(&eth, disconnected, [this](bool){
// piCoutObj << "disconnected";
piCoutObj << "disconnected";
static_cast<PIThread*>(&eth)->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<PIThread*>(&eth)->stop();
eth.close();
break;

View File

@@ -29,6 +29,7 @@ PICloudServer::PICloudServer(const PIString & path, PIIODevice::DeviceMode mode)
CONNECTL(&eth, connected, [this](){tcp.sendStart();});
CONNECTL(&eth, disconnected, [this](bool){
piCoutObj << "disconnected";
static_cast<PIThread*>(&eth)->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) {

View File

@@ -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

View File

@@ -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";
}

View File

@@ -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<dtc; j++) {
cdelay = delay * play_speed;
dtc = int(cdelay) /100;
piMSleep(100);
cdelay = delay * play_speed;// TODO: rewrite with condvar
dtc = int(cdelay) / 100;// TODO: rewrite with condvar
piMSleep(100);// TODO: rewrite with condvar
if (play_speed <= 0.) {dtc = 2; j = 0;}
//piCout << " " << play_speed << dtc << j;
}
cdelay = cdelay - dtc*100;
cdelay = cdelay - dtc*100;// TODO: rewrite with condvar
PISystemTime::fromMilliseconds(cdelay).sleep();
}
} else is_started = true;
@@ -228,7 +228,7 @@ bool PIBinaryLog::threadedRead(uchar *readed, int size) {
case PlayStaticDelay:
if (is_started) {
if (is_pause) {
piMSleep(100);
piMSleep(100);// TODO: rewrite with condvar
return false;
}
play_delay.sleep();