File Transfer (OpcUaFile)
Overview
OPC UA Part 5 §10 FileType (i=11575) exposes 6 methods. The SDK wraps them as an OpcUaFile class.
API
OpcUaFile It is a class (file_transfer.hpp) in the namespace darra::opcua; is passed during construction Session&.Move-only, Automatic destructor/exception handling Close().
| Methods | Category | Reading and Writing | Note |
|---|---|---|---|
| OpcUaFile(Session& s, std::string file_node_id) | Structure | — | Bind a File object to the server |
| file.IsOpen() | Methods | Read | Is it currently open? |
| file.FileHandle() | Methods | Read | File handle returned by the server uint32_t |
| file.FileNodeId() | Methods | Read | std::string const& |
| file.Size() | Methods | Read | File size uint64_t |
| file.OpenCount() | Methods | Read | Current number of opens uint16_t |
| file.Writable() | Methods | Read | Is it possible to write? |
| file.UserWritable() | Methods | Read | The current user has write access |
| file.Open(mode) | Methods | Write | Open, Returns Status |
| file.Read(length) | Methods | Read | Returns std::pair<Status, std::vector<uint8_t>> |
| file.Write(bytes) | Methods | Write | Returns Status |
| file.GetPosition() | Methods | Read | Returns std::pair<Status, uint64_t> |
| file.SetPosition(pos) | Methods | Write | Returns Status |
| file.Close() | Methods | Write | Close (Destructor called automatically) |
| file.DownloadTo(local_path, progress) | High-level | Read | Download with one click, Returns Status |
| file.UploadFrom(local_path, progress) | High-level | Write | Upload with one click, then return Status |
All of the above areMember Functions, not a property field.ProgressFn = std::function<void(uint64_t done, uint64_t total)>, total == 0 indicates that the total size is unknown.
Related Structures (FileOpenMode flags,operator| combinable):
- Read (0x01) — Read
- Write (0x02) — Write
- EraseExisting (0x04) — Clear
- Append (0x08) — Append
Semantics of Method Failures:
| Call | When closed | Other |
|---|---|---|
Open(mode) | — | If it's already open, just returns Status::DarraInvalidArgument; don't open it again. |
Close() | Silent Returns Status::Good | Once the adjustment is complete, it is considered closed. |
Read(length) | {Status::DarraInvalidArgument, {}} | length <= 0 →; {Status::Good, {}} ReturnsEmpty data = Reached the end; If the operation fails, the data is empty |
Write(bytes) | Status::DarraInvalidArgument | data If it's empty, return immediately Status::Good |
GetPosition() / SetPosition() | {DarraInvalidArgument, 0} / DarraInvalidArgument | |
DownloadTo / UploadFrom | — | local_path Returns null Status::DarraInvalidArgument |
Code Example
#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) One-click firmware download (RAII Close "Management")
{
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 indicates unknown
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) Upload recipes with one click
{
OpcUaFile f2(s, "ns=2;s=Recipe.Current");
Status rc = f2.UploadFrom("C:/recipes/new.csv");
}
// 3) Manual streaming read
{
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; // Empty = EOF
ProcessChunk(r.second);
}
f3.Close();
}
Implementation Notes
- Default block size 4 KB,
DownloadToIn the short version, read (insufficient 4 KB) as "ended EOF early" - ByteString Coded as ISO-8859-1(Follows the same conventions as the C# side), preserves all
0x00-0xFFbytes without loss; etc. — exposeVariantthem first,SetByteStringthen slice them - Method NodeId resolution: first use
TranslateBrowsePathsto find the instance Method NodeId; on failure, fall back to the ns=0 class-level NodeId (Openi=11580 /Readi=11581 /Writei=11582 /Closei=11583 /GetPositioni=11588 /SetPositioni=11591)
Best Practices
- Use RAII to manage the lifecycle; destruction is automatic
Close() - Retrying in Segments for Large Files
- Do not use the same one concurrently
file_handle
Cross-Language Comparison
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| OpcUaFile | OpcUaFile | OpcUaFile | OpcUaFile | OpcUaFile | DarraUa_OpcUaFile_New / _Free |
| DownloadTo | download_to | downloadTo | DownloadTo | download_to | DarraUa_OpcUaFile_DownloadTo |
| UploadFrom | upload_from | uploadFrom | UploadFrom | upload_from | DarraUa_OpcUaFile_UploadFrom |
C's family name is DarraUa_OpcUaFile_*, No DarraUa_File_*. Handle type DarraUa_OpcUaFileHandle, progress callback DarraUa_OnFileProgress, mode enumeration DarraUa_FileOpenMode. Complete set of methods:_New / _Free / _Open / _Close / _Read / _Write / _GetPosition / _SetPosition / _Size / _OpenCount / _Writable / _UserWritable / _IsOpen / _DownloadTo / _UploadFrom.