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:
95
libs/client_server/piclientserver_client_base.cpp
Normal file
95
libs/client_server/piclientserver_client_base.cpp
Normal file
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "piclientserver_client_base.h"
|
||||
|
||||
#include "piethernet.h"
|
||||
#include "piliterals_time.h"
|
||||
|
||||
|
||||
PIClientServer::ClientBase::ClientBase() {}
|
||||
|
||||
|
||||
PIClientServer::ClientBase::~ClientBase() {
|
||||
stop();
|
||||
if (own_tcp) piDeleteSafety(tcp);
|
||||
}
|
||||
|
||||
|
||||
void PIClientServer::ClientBase::stop() {
|
||||
if (!tcp) return;
|
||||
can_write = false;
|
||||
tcp->interrupt();
|
||||
tcp->stopAndWait(10_s);
|
||||
if (tcp->isThreadedRead()) tcp->terminateThreadedRead();
|
||||
stream.clear();
|
||||
}
|
||||
|
||||
|
||||
int PIClientServer::ClientBase::write(const void * d, const size_t s) {
|
||||
if (!tcp) return -1;
|
||||
if (!can_write) return 0;
|
||||
PIMutexLocker guard(write_mutex);
|
||||
// piCout << "... send ...";
|
||||
stream.send(PIByteArray(d, s));
|
||||
// piCout << "... send ok";
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
void PIClientServer::ClientBase::enableSymmetricEncryption(const PIByteArray & key) {
|
||||
if (key.isNotEmpty()) {
|
||||
stream.setCryptEnabled(true);
|
||||
stream.setCryptKey(key);
|
||||
} else
|
||||
stream.setCryptEnabled(false);
|
||||
}
|
||||
|
||||
|
||||
void PIClientServer::ClientBase::init() {
|
||||
if (!tcp) return;
|
||||
CONNECTL(&stream, sendRequest, [this](const PIByteArray & ba) {
|
||||
if (!can_write) return;
|
||||
tcp->send(ba);
|
||||
// piMSleep(1);
|
||||
});
|
||||
CONNECTL(&stream, packetReceiveEvent, [this](PIByteArray & ba) { readed(ba); });
|
||||
CONNECTL(tcp, threadedReadEvent, [this](const uchar * readed, ssize_t size) {
|
||||
if (!can_write) return;
|
||||
stream.received(readed, size);
|
||||
});
|
||||
CONNECTL(tcp, connected, [this]() {
|
||||
can_write = true;
|
||||
// piCout << "Connected";
|
||||
connected();
|
||||
});
|
||||
CONNECTL(tcp, disconnected, [this](bool) {
|
||||
can_write = false;
|
||||
stream.clear();
|
||||
// piCout << "Disconnected";
|
||||
disconnected();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
void PIClientServer::ClientBase::destroy() {
|
||||
can_write = false;
|
||||
write_mutex.lock();
|
||||
piDeleteSafety(tcp);
|
||||
// piCout << "Destroyed";
|
||||
}
|
||||
Reference in New Issue
Block a user