.. _cb_tondisconnectedcb: TOnDisconnectedCB ------------------------------------------ This callback function is used to check whether the **connection with the robot controller** has been **terminated by an external cause or user intervention**. See :ref:`Rules for callback functions `. **Defined in:** ``DRFLEx.h`` .. code-block:: cpp // typedef (DRFL.h) typedef void (*TOnDisconnectedCB)(); // registration API (internal + wrapper) DRFL_API void _set_on_disconnected(LPROBOTCONTROL pCtrl, TOnDisconnectedCB pCallbackFunc); void set_on_disconnected(TOnDisconnectedCB pCallbackFunc) { _set_on_disconnected(_rbtCtrl, pCallbackFunc); }; **Parameter** |br| None **Return** |br| None **Example** .. code-block:: cpp #include "DRFLEx.h" #include #include #include #include using namespace DRAFramework; using namespace std; CDRFLEx drfl; atomic g_bDisconnected(false); // Callback triggered when the robot controller connection is lost void OnDisconnectedCB() { // Only record the event; main() reconnects g_bDisconnected = true; } void OnMonitoringStateCB(const ROBOT_STATE eState) { cout << "[STATE] " << (int)eState << endl; } void RegisterCallbacks() { drfl.set_on_monitoring_state(OnMonitoringStateCB); } int main() { // Connect to the robot controller if (!drfl.open_connection("192.168.137.100")) { cout << "Failed to connect to controller." << endl; return -1; } // Register disconnection callback; it stays registered after a disconnect drfl.set_on_disconnected(OnDisconnectedCB); RegisterCallbacks(); drfl.setup_monitoring_version(1); // Keep program alive to handle potential disconnects while (true) { if (g_bDisconnected.exchange(false)) { cout << "[DISCONNECT] Lost connection with the robot controller." << endl; if (drfl.open_connection("192.168.137.100")) { cout << "[RECONNECT] Connection re-established successfully." << endl; // The other callbacks were unregistered: register them again RegisterCallbacks(); drfl.setup_monitoring_version(1); } else { cout << "[ERROR] Failed to reconnect. Check network or controller power." << endl; g_bDisconnected = true; // retry on the next pass } } this_thread::sleep_for(chrono::seconds(1)); } drfl.close_connection(); return 0; } **Notes** - This callback is invoked when the **network session or controller link** is terminated. - Use it to log the event, alert the user, or signal the application to **reconnect**. Call ``open_connection`` outside the callback, as in the example. - When a connection ends (``close_connection``, loss of connection, or ``open_connection`` on an object that is already connected), every callback registered with a ``set_on_*`` function is unregistered, except those registered with ``set_on_disconnected``, ``set_on_monitoring_ctrl_io_ex`` (with DRCF_VERSION 3, the default), ``set_on_rt_monitoring_data`` and ``set_on_rt_log_alarm``, which stay registered. After each successful ``open_connection``, register the other callbacks again and call ``setup_monitoring_version`` again. ``open_connection`` on a connected object closes the existing connection first and prints "Already connected : ". - If no data is received from the controller for about 2 seconds, the connection ends and the disconnect callback is called. See :ref:`open_connection `. - After the connection has ended, call only ``open_connection``.