PIEthernet fix tcp-server close (properly delete all clients)
PIEthernet::stopThreadedListen() method decompose client to 2 implementations - server-side and client-side
This commit is contained in:
@@ -303,18 +303,12 @@ bool PIEthernet::closeDevice() {
|
||||
// piCoutObj << "close";
|
||||
bool ned = connected_;
|
||||
connected_ = connecting_ = false;
|
||||
server_thread_.stop();
|
||||
PRIVATE->event.interrupt();
|
||||
if (server_thread_.isRunning()) {
|
||||
if (!server_thread_.waitForFinish(1_s)) server_thread_.terminate();
|
||||
}
|
||||
PRIVATE->event.destroy();
|
||||
if (sock_s == sock) sock_s = -1;
|
||||
closeSocket(sock);
|
||||
closeSocket(sock_s);
|
||||
stopThreadedListen();
|
||||
clients_mutex.lock();
|
||||
piDeleteAllAndClear(clients_);
|
||||
auto cl = clients_;
|
||||
clients_.clear();
|
||||
clients_mutex.unlock();
|
||||
piDeleteAll(cl);
|
||||
if (ned) {
|
||||
// piCoutObj << "Disconnect on close";
|
||||
disconnected(false);
|
||||
@@ -528,16 +522,32 @@ bool PIEthernet::listen(const PINetworkAddress & addr, bool threaded) {
|
||||
return listen(threaded);
|
||||
}
|
||||
|
||||
|
||||
void PIEthernet::stopThreadedListen() {
|
||||
server_thread_.stop();
|
||||
PRIVATE->event.interrupt();
|
||||
if (server_thread_.isRunning()) {
|
||||
if (!server_thread_.waitForFinish(1_s)) server_thread_.terminate();
|
||||
}
|
||||
PRIVATE->event.destroy();
|
||||
if (sock_s == sock) sock_s = -1;
|
||||
closeSocket(sock);
|
||||
closeSocket(sock_s);
|
||||
}
|
||||
|
||||
|
||||
PIEthernet * PIEthernet::client(int index) {
|
||||
PIMutexLocker locker(clients_mutex);
|
||||
return clients_[index];
|
||||
}
|
||||
|
||||
|
||||
int PIEthernet::clientsCount() const {
|
||||
PIMutexLocker locker(clients_mutex);
|
||||
return clients_.size_s();
|
||||
}
|
||||
|
||||
|
||||
PIVector<PIEthernet *> PIEthernet::clients() const {
|
||||
PIMutexLocker locker(clients_mutex);
|
||||
return clients_;
|
||||
|
||||
@@ -251,6 +251,8 @@ public:
|
||||
//! Start listen for incoming TCP connections on address "addr". Use only for TCP_Server
|
||||
bool listen(const PINetworkAddress & addr, bool threaded = false);
|
||||
|
||||
void stopThreadedListen();
|
||||
|
||||
PIEthernet * client(int index);
|
||||
int clientsCount() const;
|
||||
PIVector<PIEthernet *> clients() const;
|
||||
|
||||
@@ -243,6 +243,10 @@ public:
|
||||
//! \~russian Возвращает запущен ли поток чтения
|
||||
bool isThreadedRead() const;
|
||||
|
||||
//! \~english Returns if threaded read is stopping
|
||||
//! \~russian Возвращает останавливается ли поток чтения
|
||||
bool isThreadedReadStopping() const { return read_thread.isStopping(); }
|
||||
|
||||
//! \~english Start threaded read
|
||||
//! \~russian Запускает потоковое чтение
|
||||
void startThreadedRead();
|
||||
@@ -565,8 +569,6 @@ protected:
|
||||
|
||||
static PIIODevice * newDeviceByPrefix(const char * prefix);
|
||||
|
||||
bool isThreadedReadStopping() const { return read_thread.isStopping(); }
|
||||
|
||||
DeviceMode mode_ = ReadOnly;
|
||||
DeviceOptions options_;
|
||||
ReadRetFunc func_read = nullptr;
|
||||
|
||||
Reference in New Issue
Block a user