GetEndpoints
사전 준비 / 부대 조치
- Endpoint로 끌어온 후 다음을 사용하여 생성자 세션을 실제로 생성합니다.
- 서비스 간 검색을 하려면 다음을 사용하십시오. FindServers.
서명
namespace darra::opcua {
class Discovery {
public:
static std::vector<EndpointDescription> GetEndpoints(
std::string const& endpoint_url,
uint32_t timeout_ms = 10000);
};
}
임시 SecureChannel(None mode)로 서버에 연결하여 Endpoint 목록을 가져온 후 즉시 연결을 끊습니다. 세션이 필요하지 않습니다, 하지만 호출하기 전에 Stack::Init().
| 매개변수 | 기본값 | 설명 |
|---|---|---|
| endpoint_url | — | 대상 서버의 엔드포인트 URL; 빈 문자열 전달 시 예외 발생 Exception("endpoint_url 不能为空", Status::DarraInvalidArgument) |
| timeout_ms | 10000 | 전체 쿼리 시간 초과 (밀리초) |
EndpointDescription 필드
struct EndpointDescription {
std::string endpoint_url;
std::string security_policy_uri;
MessageSecurityMode security_mode = MessageSecurityMode::None;
std::string server_application_uri;
std::string server_product_uri;
std::string server_application_name;
int32_t server_application_type = 0; // 0 Server / 1 Client / 2 ClientAndServer / 3 DiscoveryServer
std::vector<uint8_t> server_certificate; // DER, null 허용
std::string transport_profile_uri;
uint8_t security_level = 0;
std::vector<UserTokenPolicy> user_tokens;
};
| 필드 | 유형 | 설명 |
|---|---|---|
| endpoint_url | std::string | 실제 엔드포인트 URL |
| security_policy_uri | std::string | 알고리즘 패키지 URI |
| security_mode | MessageSecurityMode | None (1) / Sign (2) / SignAndEncrypt (3) |
| server_application_uri | std::string | 서버 애플리케이션 URI |
| server_product_uri | std::string | 서버 측 제품 URI |
| server_application_name | std::string | 서버 애플리케이션 표시명 |
| server_application_type | int32_t | Server (0) / Client (1) / ClientAndServer (2) / DiscoveryServer (3) |
| server_certificate | std::vector<uint8_t> | 서버 인증서 (DER), 생략 가능 |
| transport_profile_uri | std::string | 전송 프로필 (일반적으로 UA-TCP) |
| security_level | uint8_t | 서버 측 추천 우선순위 |
| user_tokens | std::vector<UserTokenPolicy> | 지원되는 로그인 방식 |
UserTokenPolicy 필드
struct UserTokenPolicy {
std::string policy_id;
UserTokenType token_type = UserTokenType::Anonymous;
};
| 필드 | 유형 | 설명 |
|---|---|---|
| policy_id | std::string | 로그인 정책 식별자 |
| token_type | UserTokenType | Anonymous (0) / Username (1) / Certificate (2) / Issued (3) |
사용법
#include <darra/opcua/discovery.hpp>
#include <iostream>
using namespace darra::opcua;
// 다음 ModeName / TokenName 는 예제 내의 국소 보조 함수이며,
// SDK 내보내지 않음 ( API SDK 열거형 이름 변경 인터페이스가 제공되지 않음).
static const char* ModeName(MessageSecurityMode m) {
switch (m) {
case MessageSecurityMode::None: return "None";
case MessageSecurityMode::Sign: return "Sign";
case MessageSecurityMode::SignAndEncrypt: return "SignAndEncrypt";
default: return "Invalid";
}
}
static const char* TokenName(UserTokenType t) {
switch (t) {
case UserTokenType::Anonymous: return "Anonymous";
case UserTokenType::Username: return "Username";
case UserTokenType::Certificate: return "Certificate";
case UserTokenType::Issued: return "Issued";
default: return "Unknown";
}
}
Stack::Init();
auto endpoints = Discovery::GetEndpoints("opc.tcp://server:4840");
std::cout << "Server has " << endpoints.size() << " endpoints:\n";
for (auto const& ep : endpoints) {
std::cout << "\n URL: " << ep.endpoint_url
<< "\n Mode: " << static_cast<int>(ep.security_mode)
<< " (" << ModeName(ep.security_mode) << ")"
<< "\n Policy: " << ep.security_policy_uri
<< "\n Tokens:\n";
for (auto const& t : ep.user_tokens) {
std::cout << " - type=" << static_cast<int>(t.token_type)
<< " (" << TokenName(t.token_type) << ")"
<< " policy_id=" << t.policy_id << "\n";
}
}
Stack::Shutdown();
대표적인 출력
Server has 3 endpoints:
URL: opc.tcp://server:4840
Mode: 1 (None)
Policy: http://opcfoundation.org/UA/SecurityPolicy#None
Tokens:
- type=0 (Anonymous) policy_id=anonymous
URL: opc.tcp://server:4840
Mode: 2 (Sign)
Policy: http://opcfoundation.org/UA/SecurityPolicy#Basic256Sha256
Tokens:
- type=0 (Anonymous) policy_id=anonymous
- type=1 (Username) policy_id=username
URL: opc.tcp://server:4840
Mode: 3 (SignAndEncrypt)
Policy: http://opcfoundation.org/UA/SecurityPolicy#Basic256Sha256
Tokens:
- type=1 (Username) policy_id=username
- type=2 (Certificate) policy_id=certificate
서버 측에서는 일반적으로 여러 가지 조합(None / Sign / SignAndEncrypt × Anonymous / Username / Certificate)을 제공하며, 클라이언트는 필요에 따라 선택합니다.
예외
GetEndpoints 실패 처리 ( darra::opcua::Exception 상속 std::runtime_error):
| 던지는 지점 | 비정상적인 내용 |
|---|---|
| endpoint_url이 비어 있습니다 | Exception("endpoint_url 不能为空", Status::DarraInvalidArgument) |
| ‘Good’이 아닌 상태로 되돌리기 | Exception("GetEndpoints '<url>' failed", <原始状态码>) |
상태 코드는 ex.status_code() (원시형 uint32_t)을 사용하고, 읽기 쉬운 이름은 Stack::StatusName(ex.status_code()) — 이 값은 BadCommunicationError / BadTimeout / BadServiceUnsupported 와 같은 이름을 인식합니다. 반면 C++의 Status 열거형은 원시 코드의 극히 일부에만 이름을 부여하고 있으며, 이름이 지정되지 않은 값은 ex.status() 강제 변환 후 어떤 열거형 상수와도 일치하지 않습니다. 좋고 나쁨을 판단할 때는 ex.status() 대신 ex.status_code()를 사용하십시오.