본문으로 건너뛰기

파일 전송 (OpcUaFile)

개요​

OPC UA Part 5 §10 FileType (i=11575) 6개의 메서드를 노출합니다.SDK 클래스로 OpcUaFile 캡슐화됩니다.

API​

OpcUaFile 네임스페이스의 darra::opcua 클래스(file_transfer.hpp)이며, 생성 시 를 전달합니다 Session&.Move-only, 소멸 / 예외 경로 자동 처리 Close().

방법분류읽기 및 쓰기설명
OpcUaFile(Session& s, std::string file_node_id)구조—서버 측의 File 객체 하나를 바인딩합니다.
file.IsOpen()방법읽기현재 열려 있습니까?
file.FileHandle()방법읽기서버에서 반환된 파일 핸들 uint32_t
file.FileNodeId()방법읽기std::string const&
file.Size()방법읽기파일 크기 uint64_t
file.OpenCount()방법읽기현재 열람 횟수 uint16_t
file.Writable()방법읽기쓰기 가능한지
file.UserWritable()방법읽기현재 사용자는 쓰기 권한이 있습니다.
file.Open(mode)방법쓰기열기, Status 반환
file.Read(length)방법읽기std::pair<Status, std::vector<uint8_t>> 반환
file.Write(bytes)방법쓰기Status 반환
file.GetPosition()방법읽기std::pair<Status, uint64_t> 반환
file.SetPosition(pos)방법쓰기Status 반환
file.Close()방법쓰기닫기 (자동 소멸자 호출)
file.DownloadTo(local_path, progress)고층읽기한 번의 클릭으로 다운로드, Status 반환
file.UploadFrom(local_path, progress)고층쓰기한 번의 클릭으로 업로드, Status 반환

이 모든 것은멤버 함수, 속성 필드가 아닙니다. ProgressFn = std::function<void(uint64_t done, uint64_t total)>,total == 0 총 크기가 알 수 없음을 나타냅니다.

관련 구조체 (FileOpenMode 플래그operator| , 조합 가능):

  • Read (0x01) — 읽기
  • Write (0x02) — 쓰기
  • EraseExisting (0x04) — 지우기
  • Append (0x08) — 추가

각 방법의 실패 의미:

호출열리지 않았을 때기타
Open(mode)—이미 열려 있으면 Status::DarraInvalidArgument을 반환하며, 중복해서 열지 않습니다.
Close()무응답 반환 Status::Good설정을 완료하면 자동으로 비활성화된 것으로 간주됩니다.
Read(length){Status::DarraInvalidArgument, {}}length <= 0 → {Status::Good, {}};빈 데이터 = 끝까지 읽음을 반환; 실패 시 데이터가 비어 있음
Write(bytes)Status::DarraInvalidArgumentdata 비어 있으면 바로 반환 Status::Good
GetPosition() / SetPosition(){DarraInvalidArgument, 0} / DarraInvalidArgument
DownloadTo / UploadFrom—local_path 빈 값을 반환합니다 Status::DarraInvalidArgument

코드 예시​

#include <darra/opcua/client.hpp>
#include <darra/opcua/file_transfer.hpp>
#include <cstdint>
#include <iostream>
#include <utility>
#include <vector>

using namespace darra::opcua;

Session s("opc.tcp://device:4840");
s.Connect();

// 1) 펌웨어 원클릭 다운로드 (RAII 관리 닫기)
{
OpcUaFile file(s, "ns=2;s=Firmware.Image");
std::cout << "Size = " << file.Size() << "\n";

auto progress = [](uint64_t done, uint64_t total) {
if (total == 0) return; // total=0은 알 수 없음을 의미합니다.
std::cout << "\r" << (done * 100 / total) << "% ("
<< done << "/" << total << ")" << std::flush;
};
Status rc = file.DownloadTo("C:/dl/firmware.bin", progress);
if (rc != Status::Good)
std::cerr << "\n下载失败: " << Stack::StatusName(static_cast<uint32_t>(rc)) << "\n";
}

// 2) 한 번의 클릭으로 레시피 업로드
{
OpcUaFile f2(s, "ns=2;s=Recipe.Current");
Status rc = f2.UploadFrom("C:/recipes/new.csv");
}

// 3) 수동 스트리밍 읽기
{
OpcUaFile f3(s, "ns=2;s=Logs.Current");
if (f3.Open(FileOpenMode::Read) != Status::Good) return;
while (true) {
std::pair<Status, std::vector<uint8_t>> r = f3.Read(4096);
if (r.first != Status::Good || r.second.empty()) break; // 공 = EOF
ProcessChunk(r.second);
}
f3.Close();
}

구현 설명​

  • 블록 크기 기본값 4 KB,DownloadTo ‘리단’을 (부족 4 KB)으로 읽으면 EOF 조기 종료
  • ByteString ISO-8859-1로 인코딩(C# 측과 동일한 규칙을 따름), 전체 0x00-0xFF 바이트를 손실 없이 보존한 후, Variant 노출된 후 SetByteString 절단
  • Method NodeId 해석: 우선 Method를 사용하여 TranslateBrowsePaths 인스턴스를 찾으며 NodeId, 실패할 경우 ns=0 클래스 수준으로 되돌아갑니다 NodeId(Open i=11580 / Read i=11581 / Write i=11582 / Close i=11583 / GetPosition i=11588 / SetPosition i=11591)

모범 사례​

  • RAII 라이프사이클을 관리하고, 소멸을 자동으로 처리합니다. Close()
  • 대용량 파일 분할 재시도
  • 동시에 같은 것을 사용하지 마십시오 file_handle

다국어 대조​

C#PythonJavaC++RustC
OpcUaFileOpcUaFileOpcUaFileOpcUaFileOpcUaFileDarraUa_OpcUaFile_New / _Free
DownloadTodownload_todownloadToDownloadTodownload_toDarraUa_OpcUaFile_DownloadTo
UploadFromupload_fromuploadFromUploadFromupload_fromDarraUa_OpcUaFile_UploadFrom

C의 성은 DarraUa_OpcUaFile_*, 아닙니다 DarraUa_File_*. 핸들 유형 DarraUa_OpcUaFileHandle, 진행 상황 콜백 DarraUa_OnFileProgress, 모드 열거형 DarraUa_FileOpenMode. 전체 메서드 집합:_New / _Free / _Open / _Close / _Read / _Write / _GetPosition / _SetPosition / _Size / _OpenCount / _Writable / _UserWritable / _IsOpen / _DownloadTo / _UploadFrom.

다음 단계​