BAOS SDK v2  1.2.1
An SDK providing access to IP-BAOS devices through BAOS binary protocol version 2.x
ConnectionReconnect.cpp

This sample shows how to react on unexpected connection close events and how to reconnect automatically.

Usage:

ConnectionReconnect <ip_address>
// e.g.:
ConnectionReconnect 10.0.0.102
//
// Copyright (c) 2002-2026 WEINZIERL ENGINEERING GmbH
// All rights reserved.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY,
// WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
//
#include "Helper.h"
#include <baos/Sdk.h>
#include <atomic>
#include <chrono>
#include <iostream>
#include <thread>
using namespace wz::baos;
using namespace wz::baos::ip;
using namespace wz::baos::protocol;
int main(int argc, char* argv[])
{
try
{
std::cout << "******************************************" << std::endl;
std::cout << "ConnectionReconnect sample" << std::endl;
std::cout << "******************************************" << std::endl;
std::cout << std::endl;
setBaosLogLevel(wz::baos::LogLevel::info);
CommandLineOptions options;
options.setDurationOptionSupported(true);
options.parse(argc, argv);
if (options.wasHelpdisplayed())
{
return 0;
}
const std::string ipAddress = options.getIpAddress();
std::cout << "Connection requested for " << ipAddress << std::endl;
BaosTcpConnection connection;
std::atomic<bool> reconnectRequested{false};
std::atomic<bool> stopRequested{false};
[&reconnectRequested](const ConnectionEvent& event)
{
std::cout << "Connection event=" << toString(event.type);
std::cout << std::endl;
if (event.type == ConnectionEvent::Type::ClosedAfterError)
{
reconnectRequested = true;
}
});
connection.setServerItemsIndCallback(
[](const ServerItems& serverItems)
{
std::cout << "Received " << serverItems.size() << " server item indication(s)" << std::endl;
});
connection.open(ipAddress);
std::thread inputThread;
if (options.isInteractive())
{
inputThread = std::thread(
[&stopRequested]()
{
std::cout << "Press [Enter] to stop. Unplug network or restart device to trigger reconnect."
<< std::endl;
std::getchar();
stopRequested = true;
});
}
else
{
std::cout << "Running for " << options.getDurationSeconds() << " second(s) ..." << std::endl;
}
const auto startTime = std::chrono::steady_clock::now();
auto reconnectNotBefore = startTime;
const auto reconnectDelay = std::chrono::seconds(2);
while (!stopRequested)
{
const auto now = std::chrono::steady_clock::now();
if (!options.isInteractive() && now - startTime >= std::chrono::seconds(options.getDurationSeconds()))
{
break;
}
if (reconnectRequested && now >= reconnectNotBefore)
{
try
{
std::cout << "Trying to reconnect ..." << std::endl;
connection.reopen();
reconnectRequested = false;
std::cout << "Reconnect successful" << std::endl;
}
catch (const std::exception& e)
{
std::cout << "Reconnect failed: " << e.what() << std::endl;
reconnectNotBefore = std::chrono::steady_clock::now() + reconnectDelay;
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
connection.close();
if (inputThread.joinable())
{
inputThread.join();
}
}
catch (const std::exception& e)
{
std::cerr << "Failed: " << e.what() << std::endl;
return -1;
}
return 0;
}
Specialization for a TCP IP v4 BAOSConnection.
BaosConLessTcpConnection BaosTcpConnection
A specialization for (connection-less) connection to the BAOS device via a TCP connection.
Definition: BaosTcpConnection.h:168
Global BAOS SDK function and options.
Definition: Helper.h:23
void setConnectionEventCallback(ConnectionEventCb callback)
Groups BAOS binary protocol specific types , defines and classes for Indications, Responses etc.
Definition: Defines.h:33
std::vector< ServerItem > ServerItems
Type for a list of ServerItems.
Definition: Defines.h:440
Global BAOS sdk namespace.
Definition: config.h:69
BAOSLIB_EXPORT void setBaosLogLevel(wz::baos::LogLevel level)