From d97beaf2d157363c9a2d9a1b142127168ccf3d93 Mon Sep 17 00:00:00 2001 From: Philipp Jungkamp Date: Wed, 29 Jun 2022 21:40:57 +0000 Subject: [PATCH] Wait for all messages to be send when stopping --- lib/nodes/iec60870.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/nodes/iec60870.cpp b/lib/nodes/iec60870.cpp index d83d30882..0aefdeef5 100644 --- a/lib/nodes/iec60870.cpp +++ b/lib/nodes/iec60870.cpp @@ -380,12 +380,9 @@ void SlaveNode::destroySlave() noexcept return; } - if (CS104_Slave_isRunning(server.slave)) { - CS104_Slave_stop(server.slave); - } + this->stopSlave(); CS104_Slave_destroy(server.slave); - server.created = false; } @@ -410,13 +407,17 @@ void SlaveNode::stopSlave() noexcept { auto &server = this->server; - if (!server.created) { + if (!server.created || !CS104_Slave_isRunning(server.slave)) { return; } - if (CS104_Slave_isRunning(server.slave)) { - CS104_Slave_stop(server.slave); + // wait for all messages to be send + while ( (CS104_Slave_getNumberOfQueueEntries(server.slave, NULL) != 0) && + (CS104_Slave_getOpenConnections(server.slave) != 0)) { + sleep(1); } + + CS104_Slave_stop(server.slave); } void SlaveNode::debugPrintMessage(IMasterConnection connection, uint8_t* message, int message_size, bool sent) const noexcept @@ -459,7 +460,7 @@ bool SlaveNode::onInterrogation(IMasterConnection connection, CS101_ASDU asdu, Q case IEC60870_QOI_STATION: { IMasterConnection_sendACT_CON(connection, asdu, false); - this->logger->info("received general interrogation"); + this->logger->debug("received general interrogation"); auto guard = std::lock_guard { this->output.last_values_mutex };