Skip to main content

Automatic Reconnection

Overview​

Sessions expire on the OPC UA Server during maintenance restarts or network jitter. The C SDK reconnects automatically in the background — there is no manual reconnect entry point:

  1. SDK After a successful DarraUa_Session_Connect operation, start a watchdog thread to periodically check whether the read thread is still SecureChannel active.
  2. Read thread exits (connection lost) → State changes UA_SESSION_RECONNECTING; wake up the background reconnection worker thread
  3. Work threads are reconstructed in a /reconnect_max_retries reconnect_delay_ms loop TCP+ SecureChannel + ActivateSession
  4. Success → Attempted to transfer subscription → Status changed UA_SESSION_CONNECTED; Retry limit reached → Status changed UA_SESSION_FAILED

The business layer learns about this process through the status callback. When the link drops, the RPC in flight fails immediately and returns UA_STATUS_BAD_CONNECTION_CLOSED; the SDK does not resend that request for you.

API​

Functions / ConfigurationCategoryReading and WritingNote
DarraUa_SessionConfig.auto_reconnectConfiguration—1 = Enable automatic reconnection (default), 0 = Do not reconnect
DarraUa_SessionConfig.reconnect_max_retriesConfiguration—Maximum number of reconnection attempts (default 3; if 0 is passed, it is treated as 1)
DarraUa_SessionConfig.reconnect_delay_msConfiguration—Wait milliseconds before each attempt (default: 2000)
DarraUa_Session_SetStateCallback(h, cb, ctx)CallbackReadSubscribe to Session Status Changes
DarraUa_Session_GetState(h)MethodsReadRead the current session status
typedef enum {
UA_SESSION_DISCONNECTED = 0,
UA_SESSION_CONNECTING = 1,
UA_SESSION_CONNECTED = 2,
UA_SESSION_RECONNECTING = 3,
UA_SESSION_CLOSING = 4,
UA_SESSION_FAILED = 5
} DarraUa_SessionState;

typedef void (*DarraUa_OnSessionStateChange)(
DarraUa_SessionHandle h,
DarraUa_SessionState new_state,
DarraUa_Status status,
void* user_context);

DarraUa_Status DarraUa_Session_SetStateCallback(
DarraUa_SessionHandle h, DarraUa_OnSessionStateChange cb, void* user_context);
DarraUa_SessionState DarraUa_Session_GetState(DarraUa_SessionHandle h);

The callback signature is void cb(DarraUa_SessionHandle h, DarraUa_SessionState new_state, DarraUa_Status status, void* user_context), not the event structure.

Code Example​

#include <opcua.h>
#include <opcua_safe.h>
#include <stdio.h>

static void on_state(DarraUa_SessionHandle h,
DarraUa_SessionState new_state,
DarraUa_Status status,
void* user_context)
{
(void)h; (void)user_context;
switch (new_state) {
case UA_SESSION_RECONNECTING:
printf("重连中... (0x%08X)\n", (unsigned)status);
break;
case UA_SESSION_CONNECTED:
printf("已恢复\n");
break;
case UA_SESSION_FAILED:
fprintf(stderr, "重连失败, 已放弃: 0x%08X\n", (unsigned)status);
/* This prompts the user / reports the issue; it will no SDK longer automatically retry. */
break;
default:
break;
}
}

int main(void)
{
DarraUa_SessionConfig cfg;
DarraUa_SessionConfig_Init(&cfg);
cfg.endpoint_url = "opc.tcp://localhost:4840";
cfg.auto_reconnect = 1; /* The default value is 1. */
cfg.reconnect_max_retries = 3; /* Default 3 */
cfg.reconnect_delay_ms = 2000; /* Default 2000 */
cfg.connect_timeout_ms = 10000; /* When reconnecting, use this for the connection TCP as well */

DarraUa_SessionHandle session = UA_INVALID_SESSION_HANDLE;
if (DarraUa_Session_Create(&cfg, &session) != UA_STATUS_GOOD) return 1;

DarraUa_Session_SetStateCallback(session, on_state, NULL);
if (DarraUa_Session_Connect(session) != UA_STATUS_GOOD) return 1;

while (running) {
DarraUa_DataValue* dv = NULL;
DarraUa_Status st = DarraUa_Session_ReadSafe(
session, "ns=2;s=Temp", UA_ATTR_VALUE, &dv);

if (st == UA_STATUS_BAD_CONNECTION_CLOSED) {
/* Connection lost: This call has failed and will not be automatically retried.
The backend is reconnecting; a status callback will notify you. The business side will decide on its own when to retry. */
continue;
}
if (dv) {
/* ... using values ... */
DarraUa_DataValue_Delete(dv);
}
sleep(1);
}

DarraUa_Session_Disconnect(session);
DarraUa_Session_Close(session);
return 0;
}

Reconnection Policy​

  • No manual reconnection option: There are no such DarraUa_Session_Reconnect functions on the C side; you have to rely on configuration auto_reconnect and state callbacks.
  • Trigger Method: Each time the watchdog thread 300 ms checks the read SecureChannel thread, it triggers a reconnection if it detects that the read thread has exited.
  • Number of retries: reconnect_max_retries times (treat 0 as 1), wait before each attempt (reconnect_delay_ms including the first one)
  • Timeout: For reconnection TCP Connection connect_timeout_ms (default 10000); Single RPC use request_timeout_ms (default 5000)
  • Retry Exhaustion: State changed UA_SESSION_FAILED; automatic SDK retries are no longer enabled; the business side must re-establish the session
  • auto_reconnect=0: If the connection is lost UA_SESSION_FAILED, the system switches states immediately without attempting to reconnect.

The default values are as DarraUa_SessionConfig_Init follows: session_timeout_ms=60000, connect_timeout_ms=10000, request_timeout_ms=5000, keepalive_interval_ms=10000, auto_reconnect=1, reconnect_max_retries=3, reconnect_delay_ms=2000.

Subscriptions and Auto-Renewal​

After the session reconnects successfully, the system SDK automatically DarraUa_Session_TransferSubscriptions transfers existing subscriptions to the new session (sendInitialValues=1). Once the server successfully completes the transfer MonitoredItem, the original client_handle remains unchanged.

When the transfer fails the SDK clears the local subscription table and does not rebuild the MonitoredItems — the business logic must, on seeing UA_SESSION_CONNECTED in the status callback, call DarraUa_Subscription_Create + DarraUa_MonitoredItem_Create again.

If the background publish(DarraUa_Session_StartAutoPublish) function was enabled before the reconnection, it SDK will automatically be restarted after the reconnection is successful.

For more information, see Subscription Resumes Automatically。

Cross-Language Comparison​

C#PythonJavaC++RustC
ua.Reconnect()ua.reconnect()ua.reconnect()ua.Reconnect()ua.reconnect()— (C: No manual reconnection entry)

Next Step​