C Integration Guide
Use the webrpc Native SDK via Link the shared library + pthread callback 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.c — Linux / macOS (POSIX). Place matching libwebrpc-*.h/.so/.dylib beside this file. */
#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 <netinet/in.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
static int read_full(int fd, void *buf, size_t n) {
char *p = (char *)buf;
size_t got = 0;
while (got < n) {
ssize_t r = read(fd, p + got, n - got);
if (r <= 0) return -1;
got += (size_t)r;
}
return 0;
}
static void handle_data_stream(GoUintptr webrpc, int fd, uint32_t session_id) {
uint32_t len_be = 0;
if (read_full(fd, &len_be, 4) != 0) return;
uint32_t len = ntohl(len_be);
char *buf = (char *)malloc(len + 1);
if (!buf) return;
if (read_full(fd, buf, len) != 0) { free(buf); return; }
buf[len] = '\0';
if (len > 0 && len < 1024) printf("Data content: %s\n", buf);
int ret = WebrpcClient_SendData(webrpc, session_id, "Received", 8, 3000);
printf("SendData ret = %d\n", ret);
free(buf);
}
static void handle_file_stream(int fd, uint32_t session_id) {
uint32_t name_len_be = 0, data_len_be = 0;
if (read_full(fd, &name_len_be, 4) != 0) return;
uint32_t name_len = ntohl(name_len_be);
char *name = (char *)malloc(name_len + 1);
if (!name) return;
if (read_full(fd, name, name_len) != 0) { free(name); return; }
name[name_len] = '\0';
if (read_full(fd, &data_len_be, 4) != 0) { free(name); return; }
uint32_t data_len = ntohl(data_len_be);
char *data = (char *)malloc(data_len ? data_len : 1);
if (!data) { free(name); return; }
if (data_len && read_full(fd, data, data_len) != 0) { free(name); free(data); return; }
printf("File stream sessionId=%u name=%s size=%u\n", session_id, name, data_len);
free(name);
free(data);
}
typedef struct { GoUintptr webrpc; int port; } CbArgs;
static void *callback_thread(void *arg) {
CbArgs *a = (CbArgs *)arg;
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) return NULL;
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons((uint16_t)a->port);
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
perror("connect callback");
close(fd);
return NULL;
}
printf("Connected to callback server, port: %d\n", a->port);
for (;;) {
uint32_t sid_be = 0;
uint8_t type = 0;
if (read_full(fd, &sid_be, 4) != 0) break;
if (read_full(fd, &type, 1) != 0) break;
uint32_t session_id = ntohl(sid_be);
if (type == 2) handle_data_stream(a->webrpc, fd, session_id);
else if (type == 1) handle_file_stream(fd, session_id);
else printf("Unknown data type: %u\n", type);
}
close(fd);
return NULL;
}
int main(void) {
GoUintptr webrpc = WebrpcClient_New("YOUR_TOKEN", "YOUR_PASSWORD", "");
if (!webrpc) {
fprintf(stderr, "WebrpcClient_New failed\n");
return 1;
}
printf("Client handle: %llu\n", (unsigned long long)webrpc);
for (;;) {
int status = WebrpcClient_LoginStatus(webrpc);
printf("Login status: %d\n", status);
if (status != 0) break;
sleep(2);
}
int port = WebrpcClient_GetReceivePort(webrpc);
printf("Callback TCP port: %d\n", port);
CbArgs args = { webrpc, port };
pthread_t tid;
pthread_create(&tid, NULL, callback_thread, &args);
pthread_detach(tid);
for (;;) {
printf("Session count = %u\n", (unsigned)WebrpcClient_SessionSize(webrpc));
sleep(3);
}
/* unreachable in this sample; call WebrpcClient_Free(webrpc) on shutdown */
return 0;
}
/*
Build (macOS example):
cc -O2 -o webrpc_demo main.c -I. -L. -lwebrpc-Mac -lpthread
Linux x86_64:
cc -O2 -o webrpc_demo main.c -I. -L. -lwebrpc-Linux -lpthread -Wl,-rpath,.
*/
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.