thread #170
@@ -36,9 +36,14 @@ PICloudServer::PICloudServer(const PIString & path, PIIODevice::DeviceMode mode)
|
|||||||
CONNECTL(ð, disconnected, [this](bool){
|
CONNECTL(ð, disconnected, [this](bool){
|
||||||
if (is_deleted) return;
|
if (is_deleted) return;
|
||||||
//piCoutObj << "disconnected" << ð
|
//piCoutObj << "disconnected" << ð
|
||||||
|
clients_mutex.lock();
|
||||||
|
removed_clients_.append(clients_);
|
||||||
for (auto c : clients_) {
|
for (auto c : clients_) {
|
||||||
delete c;
|
c->is_connected = false;
|
||||||
|
c->close();
|
||||||
}
|
}
|
||||||
|
clients_.clear();
|
||||||
|
clients_mutex.unlock();
|
||||||
opened_ = false;
|
opened_ = false;
|
||||||
ping_timer.stop();
|
ping_timer.stop();
|
||||||
piMSleep(100);
|
piMSleep(100);
|
||||||
@@ -54,8 +59,12 @@ PICloudServer::~PICloudServer() {
|
|||||||
is_deleted = true;
|
is_deleted = true;
|
||||||
stop();
|
stop();
|
||||||
close();
|
close();
|
||||||
//piCout << "wait";
|
|
||||||
waitThreadedReadFinished();
|
waitThreadedReadFinished();
|
||||||
|
//piCout << "wait";
|
||||||
|
for (auto c : removed_clients_) {
|
||||||
|
delete c;
|
||||||
|
}
|
||||||
|
removed_clients_.clear();
|
||||||
//piCoutObj << "~PICloudServer done" << this;
|
//piCoutObj << "~PICloudServer done" << this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,10 +101,11 @@ bool PICloudServer::closeDevice() {
|
|||||||
eth.stopAndWait();
|
eth.stopAndWait();
|
||||||
ping_timer.stop();
|
ping_timer.stop();
|
||||||
eth.close();
|
eth.close();
|
||||||
for (auto c : clients_) {
|
clients_mutex.lock();
|
||||||
delete c;
|
removed_clients_.append(clients_);
|
||||||
}
|
|
||||||
clients_.clear();
|
clients_.clear();
|
||||||
|
index_clients.clear();
|
||||||
|
clients_mutex.unlock();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -233,12 +243,15 @@ void PICloudServer::_readed(PIByteArray & ba) {
|
|||||||
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.take(id, nullptr);
|
||||||
|
clients_.removeOne(oc);
|
||||||
clients_mutex.unlock();
|
clients_mutex.unlock();
|
||||||
if (oc) {
|
if (oc) {
|
||||||
oc->stopAndWait();
|
oc->stopAndWait();
|
||||||
oc->is_connected = false;
|
oc->is_connected = false;
|
||||||
delete oc;
|
oc->close();
|
||||||
|
removed_clients_ << oc;
|
||||||
|
//delete oc;
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
case PICloud::TCP::Data: {
|
case PICloud::TCP::Data: {
|
||||||
@@ -260,6 +273,7 @@ void PICloudServer::clientDeleted(PIObject * o) {
|
|||||||
//piCoutObj << "clientDeleted" << c;
|
//piCoutObj << "clientDeleted" << c;
|
||||||
clients_mutex.lock();
|
clients_mutex.lock();
|
||||||
clients_.removeOne(c);
|
clients_.removeOne(c);
|
||||||
|
removed_clients_.removeAll(c);
|
||||||
auto it = index_clients.makeIterator();
|
auto it = index_clients.makeIterator();
|
||||||
while (it.next()) {
|
while (it.next()) {
|
||||||
if (it.value() == c) {
|
if (it.value() == c) {
|
||||||
|
|||||||
@@ -83,6 +83,7 @@ private:
|
|||||||
int sendData(const PIByteArray & data, uint client_id);
|
int sendData(const PIByteArray & data, uint client_id);
|
||||||
|
|
||||||
PIVector<Client *> clients_;
|
PIVector<Client *> clients_;
|
||||||
|
PIVector<Client *> removed_clients_;
|
||||||
PIMap<uint, Client *> index_clients;
|
PIMap<uint, Client *> index_clients;
|
||||||
PITimer ping_timer;
|
PITimer ping_timer;
|
||||||
mutable PIMutex clients_mutex;
|
mutable PIMutex clients_mutex;
|
||||||
|
|||||||
@@ -315,10 +315,10 @@ public:
|
|||||||
|
|
||||||
//! \~english Remove element with key `key` from the array and return it.
|
//! \~english Remove element with key `key` from the array and return it.
|
||||||
//! \~russian Удаляет элемент с ключом `key` из массива и возвращает его.
|
//! \~russian Удаляет элемент с ключом `key` из массива и возвращает его.
|
||||||
inline T take(const Key & key) const {
|
inline T take(const Key & key, const T & default_ = T()) {
|
||||||
bool f(false);
|
bool f(false);
|
||||||
ssize_t i = _find(key, f);
|
ssize_t i = _find(key, f);
|
||||||
if (!f) return T();
|
if (!f) return default_;
|
||||||
T ret(pim_content[pim_index[i].index]);
|
T ret(pim_content[pim_index[i].index]);
|
||||||
_remove(i);
|
_remove(i);
|
||||||
return ret;
|
return ret;
|
||||||
|
|||||||
@@ -122,7 +122,7 @@ void PIThreadNotifier::wait() {
|
|||||||
//! \~russian
|
//! \~russian
|
||||||
//! Если ожидают несколько потоков, то уведомляет один случайный.\n
|
//! Если ожидают несколько потоков, то уведомляет один случайный.\n
|
||||||
//! Если вызвать "n" раз, то все ожидающие потоки уведомятся суммарно "n" раз.
|
//! Если вызвать "n" раз, то все ожидающие потоки уведомятся суммарно "n" раз.
|
||||||
void PIThreadNotifier::notifyOnce() {
|
void PIThreadNotifier::notify() {
|
||||||
m.lock();
|
m.lock();
|
||||||
cnt++;
|
cnt++;
|
||||||
v.notifyAll();
|
v.notifyAll();
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ public:
|
|||||||
|
|
||||||
//! \~english Notify one waiting thread, which waiting on \a wait() function
|
//! \~english Notify one waiting thread, which waiting on \a wait() function
|
||||||
//! \~russian Уведомить один из ожидающих потоков, которые висят на методе \a wait()
|
//! \~russian Уведомить один из ожидающих потоков, которые висят на методе \a wait()
|
||||||
void notifyOnce();
|
void notify();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ullong cnt;
|
ullong cnt;
|
||||||
|
|||||||
@@ -55,6 +55,7 @@ int main(int argc, char * argv[]) {
|
|||||||
cl->stop();
|
cl->stop();
|
||||||
clients->removeAll(cl);
|
clients->removeAll(cl);
|
||||||
piCout << "[Server] client closed ok" << cl;
|
piCout << "[Server] client closed ok" << cl;
|
||||||
|
//cl->deleteLater();
|
||||||
}));
|
}));
|
||||||
cl->startThreadedRead();
|
cl->startThreadedRead();
|
||||||
}));
|
}));
|
||||||
|
|||||||
Reference in New Issue
Block a user