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

View File

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

View File

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

View File

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

View File

@@ -61,7 +61,7 @@ public:
void sendStart();
void sendConnected(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);
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> parseHeader(PIByteArray & ba);
PIByteArray parseData(PIByteArray & ba);