跳到主要内容

自动重连

概述​

OPC UA Server 维护重启 / 网络抖动时 Session 失效。C SDK 的重连是后台自动的, 没有手动重连入口:

  1. SDK 在 DarraUa_Session_Connect 成功后起一个 watchdog 线程, 轮询 SecureChannel 读线程是否存活
  2. 读线程退出 (链路断) → 状态切 UA_SESSION_RECONNECTING, 唤醒后台重连工作线程
  3. 工作线程按 reconnect_max_retries / reconnect_delay_ms 循环重建 TCP + SecureChannel + ActivateSession
  4. 成功 → 尽力转移订阅 → 状态切 UA_SESSION_CONNECTED; 重试耗尽 → 状态切 UA_SESSION_FAILED

业务侧通过状态回调感知这个过程。链路断开时正在执行的 RPC 直接失败并返回 UA_STATUS_BAD_CONNECTION_CLOSED, SDK 不会替你重发那个请求。

API​

函数 / 配置类别读写说明
DarraUa_SessionConfig.auto_reconnect配置—1 = 启用自动重连 (默认), 0 = 不重连
DarraUa_SessionConfig.reconnect_max_retries配置—重连最大尝试次数 (默认 3, 传 0 按 1 处理)
DarraUa_SessionConfig.reconnect_delay_ms配置—每次尝试前等待毫秒 (默认 2000)
DarraUa_Session_SetStateCallback(h, cb, ctx)回调读订阅会话状态变更
DarraUa_Session_GetState(h)方法读读当前会话状态
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);

回调签名是 void cb(DarraUa_SessionHandle h, DarraUa_SessionState new_state, DarraUa_Status status, void* user_context), 不是事件结构体。

代码示例​

#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);
/* 这里提示用户 / 上报, SDK 不会再自动重试 */
break;
default:
break;
}
}

int main(void)
{
DarraUa_SessionConfig cfg;
DarraUa_SessionConfig_Init(&cfg);
cfg.endpoint_url = "opc.tcp://localhost:4840";
cfg.auto_reconnect = 1; /* 默认就是 1 */
cfg.reconnect_max_retries = 3; /* 默认 3 */
cfg.reconnect_delay_ms = 2000; /* 默认 2000 */
cfg.connect_timeout_ms = 10000; /* 重连时 TCP 连接也用它 */

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) {
/* 链路断: 这一次调用已经失败, 不会自动重发。
后台正在重连, 状态回调会通知; 业务侧自己决定下一拍再试。 */
continue;
}
if (dv) {
/* ... 用值 ... */
DarraUa_DataValue_Delete(dv);
}
sleep(1);
}

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

重连策略​

  • 无手动重连入口: C 侧没有 DarraUa_Session_Reconnect 这类函数, 只能靠 auto_reconnect 配置 + 状态回调
  • 触发方式: watchdog 线程每 300 ms 查一次 SecureChannel 读线程, 发现读线程退出即触发重连
  • 重试次数: reconnect_max_retries 次 (传 0 按 1 处理), 每次尝试前先等 reconnect_delay_ms (含首次)
  • 超时: 重连里 TCP 连接用 connect_timeout_ms (默认 10000); 单 RPC 用 request_timeout_ms (默认 5000)
  • 重试耗尽: 状态切 UA_SESSION_FAILED, SDK 不再自动重试, 需业务侧重建会话
  • auto_reconnect=0: 链路一断状态直接切 UA_SESSION_FAILED, 不做任何重连

默认值以 DarraUa_SessionConfig_Init 为准: 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。

与订阅自恢复​

Session 重连成功后, SDK 自动调 DarraUa_Session_TransferSubscriptions 把已有订阅转到新 Session (sendInitialValues=1), 服务端成功转移即保留原 MonitoredItem, client_handle 不变。

转移失败时 SDK 会清空本地订阅表, 不会自动重建 MonitoredItem —— 需业务侧在状态回调看到 UA_SESSION_CONNECTED 后自己重新 DarraUa_Subscription_Create + DarraUa_MonitoredItem_Create。

若重连前启用了后台 publish (DarraUa_Session_StartAutoPublish), 重连成功后 SDK 会自动把它重新拉起。

详见 订阅自恢复。

跨语言对照​

C#PythonJavaC++RustC
ua.Reconnect()ua.reconnect()ua.reconnect()ua.Reconnect()ua.reconnect()—(C 无手动重连入口)

下一步​