new PIClientServer::ClientBase::stopAndWait() method for blocking stop read.

PIClientServer::ClientBase::close() now non-blocking
This commit is contained in:
2024-09-21 19:56:39 +03:00
parent 4acab04895
commit 3641e636d2
6 changed files with 24 additions and 7 deletions

View File

@@ -41,6 +41,7 @@ PIClientServer::Client::Client() {
PIClientServer::Client::~Client() { PIClientServer::Client::~Client() {
if (tcp) tcp->setDebug(false); if (tcp) tcp->setDebug(false);
close(); close();
stopAndWait();
} }

View File

@@ -27,6 +27,7 @@ PIClientServer::ClientBase::ClientBase() {}
PIClientServer::ClientBase::~ClientBase() { PIClientServer::ClientBase::~ClientBase() {
close(); close();
stopAndWait();
if (own_tcp) piDeleteSafety(tcp); if (own_tcp) piDeleteSafety(tcp);
} }
@@ -34,7 +35,13 @@ PIClientServer::ClientBase::~ClientBase() {
void PIClientServer::ClientBase::close() { void PIClientServer::ClientBase::close() {
if (!tcp) return; if (!tcp) return;
can_write = false; can_write = false;
tcp->interrupt(); tcp->stop();
stream.clear();
}
void PIClientServer::ClientBase::stopAndWait() {
if (!tcp) return;
tcp->stopAndWait(10_s); tcp->stopAndWait(10_s);
if (tcp->isThreadedRead()) tcp->terminateThreadedRead(); if (tcp->isThreadedRead()) tcp->terminateThreadedRead();
tcp->close(); tcp->close();

View File

@@ -47,6 +47,7 @@ public:
const PIEthernet * getTCP() const { return tcp; } const PIEthernet * getTCP() const { return tcp; }
void close(); void close();
void stopAndWait();
int write(const void * d, const size_t s); int write(const void * d, const size_t s);
int write(const PIByteArray & ba) { return write(ba.data(), ba.size()); } int write(const PIByteArray & ba) { return write(ba.data(), ba.size()); }

View File

@@ -69,6 +69,8 @@ protected:
#include <iostream> #include <iostream>
int main(int argc, char * argv[]) { int main(int argc, char * argv[]) {
PILog log; PILog log;
log.setColorConsole(false);
log.setOutput(PILog::File, false);
log.setLogName("test"); log.setLogName("test");
log.setDir("logs"); log.setDir("logs");
// log.setTimestampFormat("hh-mm-ss"); // log.setTimestampFormat("hh-mm-ss");

View File

@@ -154,7 +154,7 @@ TEST(ClientServer, DynamicClients) {
auto s = createServer<true, true, 10_KiB>(); auto s = createServer<true, true, 10_KiB>();
const auto spawnClient = [&clients, &clients_mutex]() { const auto spawnClient = [&clients, &clients_mutex]() {
if (clients.size() > 100) return; // if (clients.size() > 100) return;
auto c = new ClientSendThread(); auto c = new ClientSendThread();
c->startSend(); c->startSend();
clients_mutex.lock(); clients_mutex.lock();
@@ -180,7 +180,7 @@ TEST(ClientServer, DynamicClients) {
} }
piCout << "+++++++"; piCout << "+++++++";
}, },
52_Hz); 12_Hz);
deleteThread.start( deleteThread.start(
[&clients, &clients_mutex]() { [&clients, &clients_mutex]() {
@@ -199,9 +199,9 @@ TEST(ClientServer, DynamicClients) {
} }
piCout << "----------"; piCout << "----------";
}, },
53_Hz); 13_Hz);
(10_s).sleep(); (2_s).sleep();
EXPECT_GE(s->clientsCount(), 10); EXPECT_GE(s->clientsCount(), 10);

View File

@@ -44,7 +44,10 @@ class TestServerClient
using Base = TestClientBase<WriteSize, WithPong>; using Base = TestClientBase<WriteSize, WithPong>;
public: public:
~TestServerClient() { close(); } ~TestServerClient() {
close();
stopAndWait();
}
private: private:
void readed(PIByteArray data) override { Base::readInternal(data); } void readed(PIByteArray data) override { Base::readInternal(data); }
@@ -66,7 +69,10 @@ class TestClient
using Base = TestClientBase<WriteSize, WithPong>; using Base = TestClientBase<WriteSize, WithPong>;
public: public:
~TestClient() { close(); } ~TestClient() {
close();
stopAndWait();
}
private: private:
void readed(PIByteArray data) override { Base::readInternal(data); } void readed(PIByteArray data) override { Base::readInternal(data); }