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); CONNECTU(&streampacker, packetReceiveEvent, this, _readed);
CONNECTL(&eth, disconnected, [this](bool){ CONNECTL(&eth, disconnected, [this](bool){
// piCoutObj << "disconnected"; // piCoutObj << "disconnected";
opened_ = false; if (is_connected) disconnected();
is_connected = false; internalDisconnect();
cond_connect.notifyOne();
cond_buff.notifyOne();
piMSleep(100); piMSleep(100);
}); });
} }
@@ -40,12 +38,8 @@ 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; internalDisconnect();
disconnected();
cond_buff.notifyOne();
cond_connect.notifyOne();
}
close(); close();
stop(); stop();
} }
@@ -87,10 +81,7 @@ bool PICloudClient::openDevice() {
bool PICloudClient::closeDevice() { bool PICloudClient::closeDevice() {
PIThread::stop(false); PIThread::stop(false);
if (is_connected) { if (is_connected) {
is_connected = false; internalDisconnect();
disconnected();
cond_buff.notifyOne();
cond_connect.notifyOne();
} }
eth.stop(); eth.stop();
if (eth.isOpened()) eth.close(); 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) { 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);
@@ -136,7 +134,6 @@ void PICloudClient::_readed(PIByteArray & ba) {
is_connected = false; is_connected = false;
static_cast<PIThread*>(&eth)->stop(); static_cast<PIThread*>(&eth)->stop();
eth.close(); eth.close();
disconnected();
break; break;
case PICloud::TCP::Data: case PICloud::TCP::Data:
if (is_connected) { if (is_connected) {
@@ -150,6 +147,6 @@ void PICloudClient::_readed(PIByteArray & ba) {
//piCoutObj << "readed" << ba.toHex(); //piCoutObj << "readed" << ba.toHex();
} }
mutex_buff.unlock(); 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); buff.append(ba);
cond_buff.notifyOne(); cond_buff.notifyOne();
mutex_buff.unlock(); 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: private:
EVENT_HANDLER1(void, _readed, PIByteArray &, data); EVENT_HANDLER1(void, _readed, PIByteArray &, data);
void internalDisconnect();
PIByteArray buff; PIByteArray buff;
PIMutex mutex_buff; PIMutex mutex_buff;
PIMutex mutex_connect; PIMutex mutex_connect;

View File

@@ -32,6 +32,8 @@ int main(int argc, char * argv[]) {
piCout << "[Client] data:" << str; piCout << "[Client] data:" << str;
if (str == "ping_S") c.write(PIString("pong_S").toByteArray()); 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){ CONNECTL(&s, newConnection, ([&](PICloudServer::Client * cl){
piCout << "[Server] new client:" << cl; piCout << "[Server] new client:" << cl;
clients << cl; clients << cl;