C++ Integration Guide
Use the webrpc Native SDK via C++17 + std::thread, matching the official Go sample flow: login → callback port → send/receive.
Overview
- The SDK exposes a C ABI (
libwebrpc-*.h+ shared library per platform). - The sample below is meant to be buildable/runnable after you replace placeholders and place the correct platform library files.
- Get token/password from My Tokens in the console.
Dependencies & platforms
| Platform | Header | Link library (typical) |
|---|---|---|
| macOS | libwebrpc-Mac.h | -lwebrpc-Mac |
| Linux x86_64 | libwebrpc-Linux.h | -lwebrpc-Linux |
| Linux arm64 | libwebrpc-Linux-arm64.h | -lwebrpc-Linux-arm64 |
| Windows | libwebrpc-Windows.h | webrpc-Windows (see .lib / .dll in the SDK zip) |
Download the matching SDK from the console, place the header and shared library next to your project (or configure include/lib paths).
Replace
YOUR_TOKEN/YOUR_PASSWORDwith real values from My Tokens.
Integration flow
Typical lifecycle:
1. WebrpcClient_New
2. Poll WebrpcClient_LoginStatus until non-zero
3. WebrpcClient_GetReceivePort
4. Background thread connects to the callback port and parses frames
5. Optional: WebrpcClient_OpenSession to dial a peer
6. Main loop may print WebrpcClient_SessionSize
7. WebrpcClient_Free before exit
Native API reference
| API | Description |
|---|---|
WebrpcClient_New(token, passwd, permission) | Create a client handle (uintptr). permission may be an empty string. |
WebrpcClient_LoginStatus(handle) | Login status; non-zero means ready. |
WebrpcClient_GetReceivePort(handle) | Local callback TCP port on 127.0.0.1. |
WebrpcClient_OpenSession(handle, toToken, permission) | Open a session to a peer token; returns sessionId. |
WebrpcClient_SessionSize(handle) | Current session count. |
WebrpcClient_TarTokenBySession(handle, sessionId) | Peer token for a session. |
WebrpcClient_CloseSession(handle, sessionId) | Close a session. |
WebrpcClient_SendData(handle, sessionId, data, dataLen, timeOutMs) | Send bytes; timeOutMs is timeout in milliseconds. |
WebrpcClient_SendFile(handle, sessionId, filePath) | Send a file. |
WebrpcClient_Free(handle) | Free the client. Always call before exit. |
Callback TCP protocol
After the client is created, the SDK opens a local TCP callback port. Your process should connect("127.0.0.1", port) and read frames in a loop:
sessionId : uint32 big-endian (4 bytes)
type : uint8 (1 byte)
- 2 = data stream
- 1 = file stream
Data stream (type=2)
dataLen : uint32 BE
payload : dataLen bytes
File stream (type=1)
nameLen : uint32 BE
fileName : nameLen bytes
dataLen : uint32 BE
fileData : dataLen bytes
On a data stream, the sample replies with WebrpcClient_SendData(..., "Received", ..., 3000).
Complete example
// main.cpp — Linux / macOS
#if defined(__APPLE__)
#include "libwebrpc-Mac.h"
#elif defined(__linux__) && defined(__aarch64__)
#include "libwebrpc-Linux-arm64.h"
#elif defined(__linux__)
#include "libwebrpc-Linux.h"
#else
#error "unsupported platform"
#endif
#include <arpa/inet.h>
#include <atomic>
#include <chrono>
#include <cstdint>
#include <cstring>
#include <iostream>
#include <netinet/in.h>
#include <string>
#include <sys/socket.h>
#include <thread>
#include <unistd.h>
#include <vector>
static bool readFull(int fd, void *buf, size_t n) {
char *p = static_cast<char *>(buf);
size_t got = 0;
while (got < n) {
ssize_t r = ::read(fd, p + got, n - got);
if (r <= 0) return false;
got += static_cast<size_t>(r);
}
return true;
}
static void handleDataStream(GoUintptr webrpc, int fd, uint32_t sessionId) {
uint32_t lenBe = 0;
if (!readFull(fd, &lenBe, 4)) return;
uint32_t len = ntohl(lenBe);
std::vector<char> data(len);
if (len && !readFull(fd, data.data(), len)) return;
if (len > 0 && len < 1024) {
std::cout << "Data content: " << std::string(data.begin(), data.end()) << std::endl;
}
int ret = WebrpcClient_SendData(webrpc, sessionId, const_cast<char *>("Received"), 8, 3000);
std::cout << "SendData ret = " << ret << std::endl;
}
static void handleFileStream(int fd, uint32_t sessionId) {
uint32_t nameLenBe = 0, dataLenBe = 0;
if (!readFull(fd, &nameLenBe, 4)) return;
uint32_t nameLen = ntohl(nameLenBe);
std::vector<char> name(nameLen);
if (nameLen && !readFull(fd, name.data(), nameLen)) return;
if (!readFull(fd, &dataLenBe, 4)) return;
uint32_t dataLen = ntohl(dataLenBe);
std::vector<char> file(dataLen);
if (dataLen && !readFull(fd, file.data(), dataLen)) return;
std::cout << "File stream sessionId=" << sessionId
<< " name=" << std::string(name.begin(), name.end())
<< " size=" << dataLen << std::endl;
}
static void callbackLoop(GoUintptr webrpc, int port) {
int fd = ::socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) return;
sockaddr_in addr{};
addr.sin_family = AF_INET;
addr.sin_port = htons(static_cast<uint16_t>(port));
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
if (::connect(fd, reinterpret_cast<sockaddr *>(&addr), sizeof(addr)) != 0) {
perror("connect callback");
::close(fd);
return;
}
std::cout << "Connected to callback server, port: " << port << std::endl;
for (;;) {
uint32_t sidBe = 0;
uint8_t type = 0;
if (!readFull(fd, &sidBe, 4) || !readFull(fd, &type, 1)) break;
uint32_t sessionId = ntohl(sidBe);
if (type == 2) handleDataStream(webrpc, fd, sessionId);
else if (type == 1) handleFileStream(fd, sessionId);
else std::cout << "Unknown data type: " << static_cast<int>(type) << std::endl;
}
::close(fd);
}
int main() {
GoUintptr webrpc = WebrpcClient_New(const_cast<char *>("YOUR_TOKEN"),
const_cast<char *>("YOUR_PASSWORD"),
const_cast<char *>(""));
if (!webrpc) {
std::cerr << "WebrpcClient_New failed\n";
return 1;
}
std::cout << "Client handle: " << webrpc << std::endl;
for (;;) {
int status = WebrpcClient_LoginStatus(webrpc);
std::cout << "Login status: " << status << std::endl;
if (status != 0) break;
std::this_thread::sleep_for(std::chrono::seconds(2));
}
int port = WebrpcClient_GetReceivePort(webrpc);
std::cout << "Callback TCP port: " << port << std::endl;
std::thread(callbackLoop, webrpc, port).detach();
for (;;) {
std::cout << "Session count = " << WebrpcClient_SessionSize(webrpc) << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3));
}
// WebrpcClient_Free(webrpc);
return 0;
}
/*
Build (macOS):
c++ -std=c++17 -O2 -o webrpc_demo main.cpp -I. -L. -lwebrpc-Mac -lpthread
*/
FAQ
1. Cannot find the shared library
Ensure -L. / LD_LIBRARY_PATH / DYLD_LIBRARY_PATH / java.library.path points at the SDK library folder.
2. LoginStatus stays 0
Verify token/password, network access, and that you loaded the correct platform SDK.
3. Callback connect fails
Connect to 127.0.0.1:port after GetReceivePort. Do not hard-code the port.
4. Dial a peer
Call WebrpcClient_OpenSession(handle, peerToken, permission), then SendData with the returned sessionId.
5. Windows
The C/C++ samples use POSIX sockets. On Windows prefer Go / Java / Python / Rust, or adapt the TCP code to Winsock.