PICloudClient disconnect

This commit is contained in:
2021-08-20 17:22:25 +03:00
parent 9de7045d63
commit 99e135caa2
4 changed files with 18 additions and 17 deletions

View File

@@ -29,10 +29,8 @@ PICloudClient::PICloudClient(const PIString & path, PIIODevice::DeviceMode mode)
CONNECTU(&streampacker, packetReceiveEvent, this, _readed);
CONNECTL(&eth, disconnected, [this](bool){
// piCoutObj << "disconnected";
opened_ = false;
is_connected = false;
cond_connect.notifyOne();
cond_buff.notifyOne();
if (is_connected) disconnected();
internalDisconnect();
piMSleep(100);
});
}
@@ -40,12 +38,8 @@ PICloudClient::PICloudClient(const PIString & path, PIIODevice::DeviceMode mode)
PICloudClient::~PICloudClient() {
eth.close();
if (is_connected) {
is_connected = false;
disconnected();
cond_buff.notifyOne();
cond_connect.notifyOne();
}
if (is_connected) disconnected();
internalDisconnect();
close();
stop();
}
@@ -87,10 +81,7 @@ bool PICloudClient::openDevice() {
bool PICloudClient::closeDevice() {
PIThread::stop(false);
if (is_connected) {
is_connected = false;
disconnected();
cond_buff.notifyOne();
cond_connect.notifyOne();
internalDisconnect();
}
eth.stop();
if (eth.isOpened()) eth.close();
@@ -120,6 +111,13 @@ int PICloudClient::writeDevice(const void * data, int size) {
}
void PICloudClient::internalDisconnect() {
is_connected = false;
cond_buff.notifyOne();
cond_connect.notifyOne();
}
void PICloudClient::_readed(PIByteArray & ba) {
mutex_buff.lock();
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> hdr = tcp.parseHeader(ba);
@@ -136,7 +134,6 @@ void PICloudClient::_readed(PIByteArray & ba) {
is_connected = false;
static_cast<PIThread*>(&eth)->stop();
eth.close();
disconnected();
break;
case PICloud::TCP::Data:
if (is_connected) {
@@ -150,6 +147,6 @@ void PICloudClient::_readed(PIByteArray & ba) {
//piCoutObj << "readed" << ba.toHex();
}
mutex_buff.unlock();
while (buff.size_s() > threadedReadBufferSize()) piMSleep(100);
while (buff.size_s() > threadedReadBufferSize()) piMSleep(100); // FIXME: sleep here is bad
}

View File

@@ -167,7 +167,7 @@ void PICloudServer::Client::pushBuffer(const PIByteArray & ba) {
buff.append(ba);
cond_buff.notifyOne();
mutex_buff.unlock();
while (buff.size_s() > threadedReadBufferSize()) piMSleep(100);
while (buff.size_s() > threadedReadBufferSize()) piMSleep(100); // FIXME: sleep here is bad
}

View File

@@ -52,6 +52,8 @@ protected:
private:
EVENT_HANDLER1(void, _readed, PIByteArray &, data);
void internalDisconnect();
PIByteArray buff;
PIMutex mutex_buff;
PIMutex mutex_connect;

View File

@@ -32,6 +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(&s, newConnection, ([&](PICloudServer::Client * cl){
piCout << "[Server] new client:" << cl;
clients << cl;