자동 재연결
개요
OPC UA Server 유지보수 재시작 / 네트워크 불안정 시 세션이 만료됩니다. C++ 측**Reconnect() 방법이 없다** — 의 ConnectionConfig 세 개 스위치에 의한 재연결은 구성 시에 결정되며, 회선 단절 및 재연결 과정은 이벤트 형태로 서비스 측에 보고됩니다.
세 개의 스위치가 재시도 주기를 직접 결정하며, 그 외에는 조정할 수 있는 항목이 없습니다. 연결 끊김/재연결 과정은 Session::Events() 상의필드노출, 직접 할당하면 됩니다.
API
| 명칭 | 분류 | 읽기 및 쓰기 | 설명 |
|---|---|---|---|
| ConnectionConfig::auto_reconnect | 구성 | 쓰기 | 기본값 true; 이 기능을 끄면 연결이 끊어져도 다시 연결되지 않습니다. |
| ConnectionConfig::reconnect_max_retries | 구성 | 쓰기 | 기본값 3 |
| ConnectionConfig::reconnect_delay_ms | 구성 | 쓰기 | 기본값 2000 |
| ConnectionConfig::connect_timeout_ms | 구성 | 쓰기 | 기본값 10000,첫 악수타임아웃(OPNHello//Create/Activate 4단계), 재연결 타임아웃 아님 |
| SessionEvents::on_reconnecting | 사건 | 읽기 | Reconnecting 상태로 진입 |
| SessionEvents::on_reconnected | 사건 | 읽기 | 에서 Reconnecting 로 돌아가기 Connected |
| SessionEvents::on_disconnected | 사건 | 읽기 | 에서 Connected 로 떨어지다 Disconnected / Failed |
| SessionEvents::on_state_changed | 사건 | 읽기 | 상태가 바뀔 때마다 트리거됨 |
| Session::State() | 방법 | 읽기 | 현재 SessionState |
| Session::IsConnected() | 방법 | 읽기 | State() == SessionState::Connected |
namespace darra::opcua {
struct ConnectionConfig {
// ...
uint32_t connect_timeout_ms = 10000; // 첫 악수 (Hello/OPN/Create/Activate 4단계)
bool auto_reconnect = true;
uint32_t reconnect_max_retries = 3;
uint32_t reconnect_delay_ms = 2000;
// ...
};
enum class SessionState : int {
Disconnected = 0, Connecting = 1, Connected = 2,
Reconnecting = 3, Closing = 4, Failed = 5
};
/// 이것들은**필드**, operator=를 사용하여 값을 할당하는 것이지, OnXxx(callback) 메서드가 아닙니다.
class SessionEvents {
public:
std::function<void(std::string const& endpoint)> on_connected;
std::function<void(std::string const& endpoint, Status reason)> on_disconnected;
std::function<void(std::string const& endpoint)> on_reconnecting;
std::function<void(std::string const& endpoint)> on_reconnected;
std::function<void(SessionState old_state, SessionState new_state, Status)> on_state_changed;
std::function<void(Status code, std::string const& cat, std::string const&)> on_communication_error;
// ...
};
SessionState Session::State() const;
bool Session::IsConnected() const;
}
| 매개변수 / 필드 | 기본값 | 설명 |
|---|---|---|
| auto_reconnect | true | C 측에 대응 DarraUa_SessionConfig.auto_reconnect |
| reconnect_max_retries | 3 | C 측에 대응 DarraUa_SessionConfig.reconnect_max_retries |
| reconnect_delay_ms | 2000 | C 측에 대응 DarraUa_SessionConfig.reconnect_delay_ms |
| connect_timeout_ms | 10000 | 첫 악수 4단계 시간 초과 |
| on_reconnecting / on_reconnected | — | 형식 매개변수는 모두 std::string const& endpoint |
| on_disconnected | — | 형식 매개변수 (std::string const& endpoint, Status reason) |
| on_state_changed | — | 형식 매개변수 (SessionState old, SessionState now, Status reason) |
이벤트 트리거 규칙:
- 상태가 바뀔 때마다 먼저 트리거된다
on_state_changed Connected게다가 이전에는Connected: 이전에는Reconnecting→on_reconnected; 그렇지 않으면 →on_connectedDisconnected/로 떨어졌고, 그Failed전에는 →Connectedon_disconnected(endpoint, reason)- 들어가기
Reconnecting→on_reconnecting
코드 예시
#include <darra/opcua/client.hpp>
#include <darra/opcua/safe_api.hpp>
#include <chrono>
#include <iostream>
#include <thread>
using namespace darra::opcua;
ConnectionConfig cfg;
cfg.endpoint_url = "opc.tcp://localhost:4840";
cfg.auto_reconnect = true;
cfg.reconnect_max_retries = 3;
cfg.reconnect_delay_ms = 2000;
Session s(cfg);
// 필드 할당, s.Events().OnReconnecting(...)가 아님
s.Events().on_reconnecting = [](std::string const& ep) {
std::cout << "重连中... " << ep << "\n";
};
s.Events().on_reconnected = [](std::string const& ep) {
std::cout << "已恢复 " << ep << "\n";
};
s.Events().on_disconnected = [](std::string const& ep, Status reason) {
std::cerr << "断开 " << ep << " reason="
<< Stack::StatusName(static_cast<uint32_t>(reason)) << "\n";
};
s.Connect();
while (running) {
auto r = ReadSafe(s, "ns=2;s=Temp"); // 자유 함수, 첫 번째 매개변수는 Session&입니다.
if (r) {
double d = 0;
if (r.value.Value().TryGetDouble(d)) Use(d);
} else {
std::cerr << "读失败 " << Stack::StatusName(static_cast<uint32_t>(r.status)) << "\n";
}
std::this_thread::sleep_for(std::chrono::seconds(1));
}
재연결 동작
- 세 개의 스위치가 재시도 주기를SDK 직접 결정하며, 별도의 재시도 래핑 계층을 추가하지 않습니다.
- SDK 아니요매번 실패할 때마다 자동으로 다시 연결하여 RPC 재시도합니다.
Session::Read/Write/Browse/Call실패 시 직접 예외를 발생시킵니다darra::opcua::Exception(상태 코드를 확인하려면 를 사용하세요ex.status()); 예외를 처리하고 싶지 않다면 다음을 사용하세요. Safe API, 그것은 예외를 접어서Result::status - 완전한 실패 시, 비즈니스 측에서 사용자에게 알림을
on_disconnected표시합니다.
구독 및 자동 복구
TransferSubscriptions 는수동이 메서드는 재연결 후 자동으로 호출되지 않습니다. 구독을 유지하려면 비즈니스 측에서 on_reconnected 내에서 직접 호출하여 각 항목의 를 반환해야 s.TransferSubscriptions({旧订阅 id}, /*send_initial_values=*/true)합니다 Status.
자세한 내용은 구독 자동 복구.
다국어 대조
어떤 언어에도 공개적인 Reconnect() 방법이 드러나지 않는다 —— 대조되는 것은재연결 구성 + 이벤트:
| 항목 | C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|---|
| 재연결 스위치 | 생성자에서 (구성 필드) auto_reconnect를 노출하지 않음 | OpcUaSession(auto_reconnect=) | 구성 필드 auto_reconnect | ConnectionConfig::auto_reconnect | ConnectionConfig.auto_reconnect | DarraUa_SessionConfig.auto_reconnect |
| 재시도 횟수 | 생성자에서 (구성 필드) reconnect_max_retries를 노출하지 않음 | 생성자에서 (구성 필드) reconnect_max_retries를 노출하지 않음 | 구성 필드 reconnect_max_retries | ConnectionConfig::reconnect_max_retries | ConnectionConfig.reconnect_max_retries | DarraUa_SessionConfig.reconnect_max_retries |
| 재시도 간격 | 생성자에서 (구성 필드) reconnect_delay_ms를 노출하지 않음 | 생성자에서 (구성 필드) reconnect_delay_ms를 노출하지 않음 | 구성 필드 reconnect_delay_ms | ConnectionConfig::reconnect_delay_ms | ConnectionConfig.reconnect_delay_ms | DarraUa_SessionConfig.reconnect_delay_ms |
| 재연결 시작 이벤트 | Events.Reconnecting | events.reconnecting | onReconnecting | SessionEvents::on_reconnecting | on_reconnecting | 없음 (오직 DarraUa_Session_SetStateCallback) |
| 재연결 성공 이벤트 | Events.Reconnected | events.reconnected | onReconnected | SessionEvents::on_reconnected | on_reconnected | 없음 (위와 같음) |
C++의 on_reconnecting / on_reconnected 매개변수는 std::string const&(endpoint)이며, Python/Java/Rust는 ConnectionEventArgs를 전달합니다. C 측에는 재연결 이벤트 콜백이 없으므로, DarraUa_Session_SetStateCallback을 사용하여 DarraUa_SessionState 변화를 감시할 수밖에 없습니다. Python은 생성자를 공개할 때 auto_reconnect만 노출하며, 나머지 두 언어는 구조체 필드의 구성만 변경할 수 있습니다.