WebSocketManager Controller (Native C Worker)
A pure-C port of the WebSocketManager controller
for the NativeDllEngine. Demonstrates how to implement the worker ABI in C
without the C# runtime or any FlatBuffers C++ runtime dependency.
Topic: command.websocketmanagercontroller
Group: websocketmanagercontroller
MIME type: application/x-nativedll
Architecture
Command Event (CloudEvent on command.websocketmanagercontroller)
↓
Dapr Pubsub → WorkManager → NativeDllEngine → Process (C export)
↓
main.c entry → command dispatch
↓
cloudevent_fb.c (hand-rolled FlatBuffer reader)
↓
commands.c handlers (9 command types)
↓
host->gateway_call (C ABI host callback, raw function pointer)
↓
Returns response FlatBuffer (WorkerResponse)
Building
The build script:
1. Compiles all .c files in src/ with cc -shared -fPIC -O2.
2. Outputs bin/native/worker.zip containing the shared library:
The manifest.json declares abi_version: 1 and library: "controller".
Running
WORKER_B64=$(base64 -i bin/native/worker.zip | tr -d '\n')
curl -X POST http://localhost:25001/v1/workers \
-H "Content-Type: application/json" \
-d "{
\"code_source\": {\"content\": \"$WORKER_B64\"},
\"mime_type\": \"application/x-nativedll\",
\"topic\": \"command.websocketmanagercontroller\",
\"group\": \"websocketmanagercontroller\"
}" | python3 -c "import sys,json; print(json.load(sys.stdin)['id'])" | xargs -I{} curl -X POST http://localhost:25001/v1/workers/{}/start
Files
| File | Purpose |
|---|---|
src/main.c |
Process and FreeResult exports + command dispatch |
src/commands.c |
9 command handlers (create, subscribe, unsubscribe, publish, stop_publish, send_raw, list, disconnect, stop) |
src/cloudevent_fb.c/.h |
Hand-rolled FlatBuffer reader for CloudEvent input |
src/worker_resp_fb.c/.h |
Hand-rolled FlatBuffer writer for WorkerResponse output |
src/json.c/.h |
Minimal JSON parser/builder for command envelope and base64 |
manifest.json |
Worker manifest (abi_version: 1, library: "controller") |
Key Differences from the C# AOT Version
| Aspect | C# NativeAOT | C |
|---|---|---|
| CloudEvent decode | Vendored C# bindings (CloudEventCodec) |
Hand-rolled FlatBuffer reader |
| JSON parsing | System.Text.Json |
Hand-rolled JSON parser |
| Gateway calls | NativeApiGateway.InvokeAsync |
Direct host->gateway_call |
| Binary size | ~1-2 MB (AOT runtime) | ~30 KB |
| Dependencies | Virtufin.Worker.DevKit NuGet |
libc only |
| Compile time | ~30 s (NativeAOT) | <1 s (cc -shared) |
Limitations
This minimal C implementation focuses on demonstrating the ABI. A production C worker would need:
- A proper FlatBuffer writer for
result_event(currently only builds empty WorkerResponse or error_message). - Proper connection state tracking (currently
handle_stopis a no-op). - More robust JSON parsing with full escape handling.
- Thread-safe connection state if the engine calls Process concurrently from multiple threads.
See Also
- C# NativeAOT variant.
- Out-of-process DotNet DLL variant.
docs/v1/native-dll-workers.md— ABI specification.