mirror of
https://github.com/logos-messaging/nim-ffi.git
synced 2026-06-21 08:49:34 +00:00
Adds a standalone IPC example: the library serving itself over a CBOR socket. examples/timer/ipc_chronos/serve.nim compiles into libmy_timer only under -d:ffiIpcServe (every other build untouched) and runs a chronos socket server that, per request, decodes CBOR at the socket edge and calls the library's own async procs directly — native, in-process, zero serialization between the socket and the logic, no FFI boundary, no callback bridge. Exposed as my_timer_serve(address). CBOR (not the native struct ABI) is correct at the wire here: a relay's data is serialized regardless, so native would only relocate the decode and add marshalling for no gain — native locally, CBOR for IPC. serve_host.nim starts it; client.nim is a lib-free chronos client. Both use chronos sockets, so the example builds and runs on Linux, macOS and Windows over TCP (unix sockets are a POSIX bonus). CI: tests/e2e/ipc/run_roundtrip.nim builds the dylib + host + client, spawns the server and round-trips over loopback TCP asserting the replies; wired as `nimble test_ipc` and a 3-OS CI matrix (ubuntu/macos/windows). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
20 lines
781 B
Nim
20 lines
781 B
Nim
## Cross-platform host for the in-library CBOR server.
|
|
##
|
|
## The library *is* the server: `my_timer_serve` (compiled into libmy_timer with
|
|
## -d:ffiIpcServe) runs the chronos socket loop and dispatches each decoded
|
|
## request to the library's own procs directly. This host just links the lib and
|
|
## starts it. Written in Nim so the dylib link is handled portably (the C host
|
|
## `serve_host.c` is the POSIX-only equivalent).
|
|
##
|
|
## serve_host tcp:0.0.0.0:9099 # any platform
|
|
## serve_host unix:/tmp/timer.sock # POSIX
|
|
import std/os
|
|
|
|
proc my_timer_serve(address: cstring): cint {.importc, cdecl.}
|
|
|
|
when isMainModule:
|
|
if paramCount() != 1:
|
|
stderr.writeLine "usage: serve_host <tcp:host:port | unix:path>"
|
|
quit(2)
|
|
quit(int(my_timer_serve(cstring(paramStr(1)))))
|