From 2b620d37e997d06888fb653fc2e04a50c8547d42 Mon Sep 17 00:00:00 2001 From: Ralf Ramsauer Date: Thu, 1 Oct 2015 22:57:01 +0200 Subject: [PATCH] automatic shutdown & reconnection --- doorlockd/client/doorlock-client.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/doorlockd/client/doorlock-client.cpp b/doorlockd/client/doorlock-client.cpp index 637114f..496f937 100644 --- a/doorlockd/client/doorlock-client.cpp +++ b/doorlockd/client/doorlock-client.cpp @@ -28,17 +28,17 @@ namespace po = boost::program_options; namespace ba = boost::asio; using ba::ip::tcp; -ba::io_service io_service; +static ba::io_service io_service; const static std::string subscriptionCommand = "{ \"command\": \"subscribe\"}"; // The receive buffer length of the TCP socket -const int constexpr SOCKET_BUFFERLENGTH = 2048; +constexpr static int SOCKET_BUFFERLENGTH = 2048; static volatile bool run = true; -std::unique_ptr mainWindow = nullptr; +static std::unique_ptr mainWindow = nullptr; static void onDoorlockUpdate(const Clientmessage &msg) { @@ -173,9 +173,12 @@ int main(int argc, char** argv) // In normal operation, it never returns while (run) { doorlock_client(hostname, port); - l(LogLevel::error, "client aborted, retrying in 5 seconds"); - sleep(5); - // Todo: Write message to QT frontend + if (run) { + l(LogLevel::error, "client aborted, retrying in 5 seconds"); + // Todo: Write message to QT frontend + + sleep(5); + } } // This will stop the Qapplication @@ -186,9 +189,10 @@ int main(int argc, char** argv) // This routine will never return in normal operation app.exec(); + run = false; + // Stop the IO service io_service.stop(); - io_service.reset(); clientThread.join();