picloud fix segfault on close

This commit is contained in:
2021-04-07 15:52:53 +03:00
parent 1fd9851068
commit fd2e0b2eed
6 changed files with 36 additions and 20 deletions

View File

@@ -32,13 +32,20 @@ PICloudClient::PICloudClient(const PIString & path, PIIODevice::DeviceMode mode)
opened_ = false; opened_ = false;
is_connected = false; is_connected = false;
cond_connect.notifyOne(); cond_connect.notifyOne();
cond_buff.notifyOne();
piMSleep(100); piMSleep(100);
}); });
} }
PICloudClient::~PICloudClient() { PICloudClient::~PICloudClient() {
stop(); eth.close();
if (is_connected) {
is_connected = false;
cond_buff.notifyOne();
cond_connect.notifyOne();
}
//stop();
close(); close();
} }
@@ -55,8 +62,9 @@ void PICloudClient::setKeepConnection(bool on) {
bool PICloudClient::openDevice() { bool PICloudClient::openDevice() {
piCout << "PICloudClient open device" << path(); PIString p = path();
bool op = eth.connect(path(), false); piCout << "PICloudClient open device" << p;
bool op = eth.connect(p, false);
if (op) { if (op) {
mutex_buff.lock(); mutex_buff.lock();
eth.startThreadedRead(); eth.startThreadedRead();
@@ -77,15 +85,20 @@ bool PICloudClient::openDevice() {
bool PICloudClient::closeDevice() { bool PICloudClient::closeDevice() {
is_connected = false; if (is_connected) {
is_connected = false;
cond_buff.notifyOne();
cond_connect.notifyOne();
}
eth.stop(); eth.stop();
eth.close(); if (eth.isOpened()) eth.close();
return true; return true;
} }
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;
mutex_buff.lock(); mutex_buff.lock();
cond_buff.wait(mutex_buff, [this](){return !buff.isEmpty();}); cond_buff.wait(mutex_buff, [this](){return !buff.isEmpty();});
int sz = piMini(max_size, buff.size()); int sz = piMini(max_size, buff.size());
@@ -96,8 +109,9 @@ int PICloudClient::readDevice(void * read_to, int max_size) {
} }
int PICloudClient::writeDevice(const void * data, int max_size) { int PICloudClient::writeDevice(const void * data, int size) {
return eth.write(data, max_size); piCoutObj << "writeDevice";
return tcp.sendData(PIByteArray(data, size));
} }
@@ -118,8 +132,10 @@ void PICloudClient::_readed(PIByteArray & ba) {
eth.close(); eth.close();
break; break;
case PICloud::TCP::Data: case PICloud::TCP::Data:
buff.append(ba); if (is_connected) {
cond_buff.notifyOne(); buff.append(ba);
cond_buff.notifyOne();
}
break; break;
default: default:
break; break;

View File

@@ -37,11 +37,6 @@ PICloudServer::PICloudServer(const PIString & path, PIIODevice::DeviceMode mode)
PICloudServer::~PICloudServer() { PICloudServer::~PICloudServer() {
stop(); stop();
eth.stop();
for (auto & c : clients) {
c->stop();
c->close();
}
close(); close();
} }
@@ -76,11 +71,14 @@ bool PICloudServer::closeDevice() {
int PICloudServer::readDevice(void * read_to, int max_size) { int PICloudServer::readDevice(void * read_to, int max_size) {
piCoutObj << "readDevice";
piMSleep(eth.readTimeout());
return -1; return -1;
} }
int PICloudServer::writeDevice(const void * data, int max_size) { int PICloudServer::writeDevice(const void * data, int max_size) {
piCoutObj << "writeDevice";
return -1; return -1;
} }
@@ -123,8 +121,8 @@ int PICloudServer::Client::readDevice(void * read_to, int max_size) {
} }
int PICloudServer::Client::writeDevice(const void * data, int max_size) { int PICloudServer::Client::writeDevice(const void * data, int size) {
return server->sendData(PIByteArray(data, max_size), client_id); return server->sendData(PIByteArray(data, size), client_id);
} }
@@ -147,6 +145,7 @@ void PICloudServer::_readed(PIByteArray & ba) {
if (oc) { if (oc) {
tcp.sendDisconnected(id); tcp.sendDisconnected(id);
} else { } else {
piCoutObj << "new Client" << id;
Client * c = new Client(this, id); Client * c = new Client(this, id);
clients << c; clients << c;
index_clients.insert(id, c); index_clients.insert(id, c);

View File

@@ -71,13 +71,14 @@ void PICloud::TCP::sendDisconnected(uint client_id) {
} }
void PICloud::TCP::sendData(const PIByteArray & data) { int PICloud::TCP::sendData(const PIByteArray & data) {
header.type = PICloud::TCP::Data; header.type = PICloud::TCP::Data;
PIByteArray ba; PIByteArray ba;
ba << header; ba << header;
ba.append(data); ba.append(data);
// piCout << "sendData" << ba.toHex(); // piCout << "sendData" << ba.toHex();
streampacker->send(ba); streampacker->send(ba);
return data.size_s();
} }

View File

@@ -43,7 +43,7 @@ protected:
bool closeDevice(); bool closeDevice();
bool isConnected() const {return is_connected;} bool isConnected() const {return is_connected;}
int readDevice(void * read_to, int max_size); int readDevice(void * read_to, int max_size);
int writeDevice(const void * data, int max_size); int writeDevice(const void * data, int size);
private: private:
EVENT_HANDLER1(void, _readed, PIByteArray &, data); EVENT_HANDLER1(void, _readed, PIByteArray &, data);

View File

@@ -44,7 +44,7 @@ public:
bool openDevice(); bool openDevice();
bool closeDevice(); bool closeDevice();
int readDevice(void * read_to, int max_size); int readDevice(void * read_to, int max_size);
int writeDevice(const void * data, int max_size); int writeDevice(const void * data, int size);
private: private:
void pushBuffer(const PIByteArray & ba); void pushBuffer(const PIByteArray & ba);

View File

@@ -61,7 +61,7 @@ public:
void sendStart(); void sendStart();
void sendConnected(uint client_id); void sendConnected(uint client_id);
void sendDisconnected(uint client_id); void sendDisconnected(uint client_id);
void sendData(const PIByteArray & data); int sendData(const PIByteArray & data);
int sendData(const PIByteArray & data, uint client_id); int sendData(const PIByteArray & data, uint client_id);
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> parseHeader(PIByteArray & ba); PIPair<PICloud::TCP::Type, PICloud::TCP::Role> parseHeader(PIByteArray & ba);
PIByteArray parseData(PIByteArray & ba); PIByteArray parseData(PIByteArray & ba);