picloud revert to condvars and fix

This commit is contained in:
2021-08-20 16:36:28 +03:00
parent 0e65151e9f
commit 9de7045d63
3 changed files with 44 additions and 52 deletions

View File

@@ -24,16 +24,16 @@
PICloudClient::PICloudClient(const PIString & path, PIIODevice::DeviceMode mode) : PIIODevice(path, mode), PICloudBase() { PICloudClient::PICloudClient(const PIString & path, PIIODevice::DeviceMode mode) : PIIODevice(path, mode), PICloudBase() {
tcp.setRole(PICloud::TCP::Client); tcp.setRole(PICloud::TCP::Client);
setName("cloud_client"); setName("cloud_client");
is_connected = is_connecting = false; is_connected = false;
CONNECTL(&eth, connected, [this](){opened_ = true; tcp.sendStart();}); CONNECTL(&eth, connected, [this](){opened_ = true; tcp.sendStart();});
CONNECTU(&streampacker, packetReceiveEvent, this, _readed); CONNECTU(&streampacker, packetReceiveEvent, this, _readed);
CONNECTL(&eth, disconnected, [this](bool){ CONNECTL(&eth, disconnected, [this](bool){
//piCoutObj << "eth.disconnected"; // piCoutObj << "disconnected";
bool ned = is_connected; opened_ = false;
opened_ = is_connected = false; is_connected = false;
notifyBuffer(); cond_connect.notifyOne();
if (ned) cond_buff.notifyOne();
disconnected(); piMSleep(100);
}); });
} }
@@ -41,10 +41,11 @@ PICloudClient::PICloudClient(const PIString & path, PIIODevice::DeviceMode mode)
PICloudClient::~PICloudClient() { PICloudClient::~PICloudClient() {
eth.close(); eth.close();
if (is_connected) { if (is_connected) {
disconnected();
is_connected = false; is_connected = false;
disconnected();
cond_buff.notifyOne();
cond_connect.notifyOne();
} }
notifyBuffer();
close(); close();
stop(); stop();
} }
@@ -62,26 +63,20 @@ void PICloudClient::setKeepConnection(bool on) {
bool PICloudClient::openDevice() { bool PICloudClient::openDevice() {
if (isOpened()) return true; // piCout << "PICloudClient open device" << path();
bool op = eth.connect(PIEthernet::Address::resolve(path()), false); bool op = eth.connect(PIEthernet::Address::resolve(path()), false);
if (op) { if (op) {
mutex_buff.lock();
eth.startThreadedRead(); eth.startThreadedRead();
is_connecting = true; bool conn_ok = cond_connect.waitFor(mutex_buff, (int)eth.readTimeout());
PITimeMeasurer tm;
while (tm.elapsed_m() < (int)eth.readTimeout()) {
if (isConnected()) break;
if (!is_connecting) return false;
piMSleep(PIP_MIN_MSLEEP);
}
is_connecting = false;
bool conn_ok = isConnected();
// piCoutObj << "conn_ok" << conn_ok; // piCoutObj << "conn_ok" << conn_ok;
mutex_buff.unlock();
if (!conn_ok) { if (!conn_ok) {
eth.stop(); eth.stop();
eth.close(); eth.close();
//piMSleep(100); piMSleep(100);
} }
return conn_ok; return isConnected();
} else { } else {
eth.close(); eth.close();
return false; return false;
@@ -90,15 +85,15 @@ bool PICloudClient::openDevice() {
bool PICloudClient::closeDevice() { bool PICloudClient::closeDevice() {
eth.stop();
PIThread::stop(false); PIThread::stop(false);
if (is_connected) if (is_connected) {
is_connected = false;
disconnected(); disconnected();
is_connected = is_connecting = false; cond_buff.notifyOne();
notifyBuffer(); cond_connect.notifyOne();
waitForFinish(1000); }
if (eth.isOpened()) eth.stop();
eth.close(); if (eth.isOpened()) eth.close();
return true; return true;
} }
@@ -106,10 +101,11 @@ bool PICloudClient::closeDevice() {
int PICloudClient::readDevice(void * read_to, int max_size) { int PICloudClient::readDevice(void * read_to, int max_size) {
// piCoutObj << "readDevice"; // piCoutObj << "readDevice";
if (!is_connected) return -1; if (!is_connected) return -1;
int sz = -1;
mutex_buff.lock(); mutex_buff.lock();
cond_buff.wait(mutex_buff, [this](){return !buff.isEmpty() || !is_connected;}); cond_buff.wait(mutex_buff, [this](){return !buff.isEmpty() || !is_connected;});
int sz = piMini(max_size, buff.size()); if (is_connected) {
if (sz > 0) { sz = piMini(max_size, buff.size());
memcpy(read_to, buff.data(), sz); memcpy(read_to, buff.data(), sz);
buff.remove(0, sz); buff.remove(0, sz);
} }
@@ -124,13 +120,6 @@ int PICloudClient::writeDevice(const void * data, int size) {
} }
void PICloudClient::notifyBuffer() {
mutex_buff.lock();
cond_buff.notifyOne();
mutex_buff.unlock();
}
void PICloudClient::_readed(PIByteArray & ba) { void PICloudClient::_readed(PIByteArray & ba) {
mutex_buff.lock(); mutex_buff.lock();
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> hdr = tcp.parseHeader(ba); PIPair<PICloud::TCP::Type, PICloud::TCP::Role> hdr = tcp.parseHeader(ba);
@@ -140,13 +129,14 @@ void PICloudClient::_readed(PIByteArray & ba) {
if (tcp.parseConnect(ba) == 1) { if (tcp.parseConnect(ba) == 1) {
is_connected = true; is_connected = true;
connected(); connected();
cond_connect.notifyOne();
} }
break; break;
case PICloud::TCP::Disconnect: case PICloud::TCP::Disconnect:
//piCoutObj << "TCP::Disconnect ..."; is_connected = false;
static_cast<PIThread*>(&eth)->stop(); static_cast<PIThread*>(&eth)->stop();
eth.close(); eth.close();
//piCoutObj << "TCP::Disconnect ok"; disconnected();
break; break;
case PICloud::TCP::Data: case PICloud::TCP::Data:
if (is_connected) { if (is_connected) {

View File

@@ -28,7 +28,7 @@ PICloudServer::PICloudServer(const PIString & path, PIIODevice::DeviceMode mode)
CONNECTU(&streampacker, packetReceiveEvent, this, _readed); CONNECTU(&streampacker, packetReceiveEvent, this, _readed);
CONNECTL(&eth, connected, [this](){tcp.sendStart();}); CONNECTL(&eth, connected, [this](){tcp.sendStart();});
CONNECTL(&eth, disconnected, [this](bool){ CONNECTL(&eth, disconnected, [this](bool){
//piCoutObj << "disconnected"; piCoutObj << "disconnected";
opened_ = false; opened_ = false;
piMSleep(100); piMSleep(100);
}); });
@@ -57,7 +57,7 @@ PIVector<PICloudServer::Client *> PICloudServer::clients() const {
bool PICloudServer::openDevice() { bool PICloudServer::openDevice() {
//piCout << "PICloudServer open device" << path(); piCout << "PICloudServer open device" << path();
bool op = eth.connect(PIEthernet::Address::resolve(path()), false); bool op = eth.connect(PIEthernet::Address::resolve(path()), false);
if (op) { if (op) {
eth.startThreadedRead(); eth.startThreadedRead();
@@ -131,21 +131,23 @@ bool PICloudServer::Client::openDevice() {
bool PICloudServer::Client::closeDevice() { bool PICloudServer::Client::closeDevice() {
PIThread::stop(false);
if (is_connected) { if (is_connected) {
server->clientDisconnect(client_id); server->clientDisconnect(client_id);
is_connected = false; is_connected = false;
cond_buff.notifyOne();
} }
cond_buff.notifyOne();
return true; return true;
} }
int PICloudServer::Client::readDevice(void * read_to, int max_size) { int PICloudServer::Client::readDevice(void * read_to, int max_size) {
if (!is_connected) return -1; if (!is_connected) return -1;
int sz = -1;
mutex_buff.lock(); mutex_buff.lock();
cond_buff.wait(mutex_buff, [this](){return !buff.isEmpty() || !is_connected;}); cond_buff.wait(mutex_buff, [this](){return !buff.isEmpty() || !is_connected;});
int sz = piMini(max_size, buff.size()); if (is_connected) {
if (sz > 0) { sz = piMini(max_size, buff.size());
memcpy(read_to, buff.data(), sz); memcpy(read_to, buff.data(), sz);
buff.remove(0, sz); buff.remove(0, sz);
} }
@@ -181,7 +183,7 @@ void PICloudServer::_readed(PIByteArray & ba) {
if (oc) { if (oc) {
tcp.sendDisconnected(id); tcp.sendDisconnected(id);
} else { } else {
//piCoutObj << "new Client" << id; piCoutObj << "new Client" << id;
Client * c = new Client(this, id); Client * c = new Client(this, id);
CONNECTU(c, deleted, this, clientDeleted); CONNECTU(c, deleted, this, clientDeleted);
clients_mutex.lock(); clients_mutex.lock();
@@ -193,7 +195,7 @@ void PICloudServer::_readed(PIByteArray & ba) {
} break; } break;
case PICloud::TCP::Disconnect: { case PICloud::TCP::Disconnect: {
uint id = tcp.parseDisconnect(ba); uint id = tcp.parseDisconnect(ba);
//piCoutObj << "remove Client" << id; piCoutObj << "remove Client" << id;
clients_mutex.lock(); clients_mutex.lock();
Client * oc = index_clients.value(id, nullptr); Client * oc = index_clients.value(id, nullptr);
clients_mutex.unlock(); clients_mutex.unlock();

View File

@@ -50,14 +50,14 @@ protected:
int writeDevice(const void * data, int size); int writeDevice(const void * data, int size);
DeviceInfoFlags deviceInfoFlags() const {return PIIODevice::Reliable;} DeviceInfoFlags deviceInfoFlags() const {return PIIODevice::Reliable;}
void notifyBuffer();
private: private:
EVENT_HANDLER1(void, _readed, PIByteArray &, data); EVENT_HANDLER1(void, _readed, PIByteArray &, data);
PIByteArray buff; PIByteArray buff;
PIMutex mutex_buff; PIMutex mutex_buff;
PIMutex mutex_connect;
PIConditionVariable cond_buff; PIConditionVariable cond_buff;
std::atomic_bool is_connected, is_connecting; PIConditionVariable cond_connect;
std::atomic_bool is_connected;
}; };
#endif // PICLOUDCLIENT_H #endif // PICLOUDCLIENT_H