Skip to content

Commit 3aab2fb

Browse files
feat: adopt nim-ffi v0.1.5 (macro-based snake_case FFI) (#81)
1 parent c88205d commit 3aab2fb

6 files changed

Lines changed: 229 additions & 462 deletions

File tree

CLAUDE.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -95,21 +95,23 @@ flake.nix / Makefile # Reproducible cross-platform build system
9595

9696
## FFI API (`library/libsds.nim`)
9797

98-
The C API wraps `ReliabilityManager` behind an opaque `SdsContext` handle:
98+
The C API (snake_case, generated by the nim-ffi `{.ffiCtor.}`/`{.ffi.}`/`{.ffiDtor.}`
99+
macros) wraps `ReliabilityManager` behind an opaque context handle. Requests and
100+
responses are marshalled as JSON; see `library/libsds.h`.
99101

100102
| Export | Maps to |
101103
|---|---|
102-
| `SdsNewReliabilityManager` | Create context |
103-
| `SdsWrapOutgoingMessage` | `wrapOutgoingMessage` |
104-
| `SdsUnwrapReceivedMessage` | `unwrapReceivedMessage` |
105-
| `SdsMarkDependenciesMet` | Notify buffered-message dependencies satisfied |
106-
| `SdsSetEventCallback` | Register event handler (JSON payloads) |
107-
| `SdsSetRetrievalHintProvider` | Register hint-provider callback |
108-
| `SdsStartPeriodicTasks` | Start periodic sync loop |
109-
| `SdsCleanupReliabilityManager` | Free context |
110-
| `SdsResetReliabilityManager` | Reset state without freeing |
111-
112-
Each `SdsContext` runs a dedicated Chronos async loop on a worker thread; application threads communicate with it via SPSC channels.
104+
| `sds_create` | Create context (`{.ffiCtor.}`; `configJson` carries `participantId`) |
105+
| `sds_set_event_callback` | Register event handler (JSON payloads) |
106+
| `sds_set_retrieval_hint_provider` | Register hint-provider callback (hand-written) |
107+
| `sds_wrap_outgoing_message` | `wrapOutgoingMessage` |
108+
| `sds_unwrap_received_message` | `unwrapReceivedMessage` |
109+
| `sds_mark_dependencies_met` | Notify buffered-message dependencies satisfied |
110+
| `sds_start_periodic_tasks` | Start periodic sync loop |
111+
| `sds_reset` | Reset state without freeing |
112+
| `sds_destroy` | Recycle/free context (`{.ffiDtor.}`) |
113+
114+
Each context runs a dedicated Chronos async loop on a worker thread; application threads communicate with it via SPSC channels.
113115

114116
---
115117

library/libsds.h

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11

2-
// Generated manually and inspired by the one generated by the Nim Compiler.
3-
// In order to see the header file generated by Nim just run `make libsds`
4-
// from the root repo folder and the header should be created in
5-
// nimcache/release/libsds/libsds.h
2+
// C API for libsds, built on the nim-ffi framework.
3+
//
4+
// Parameters and results are marshalled as JSON: each request/response struct
5+
// in library/libsds.nim is a JSON object, passed in via the `*Json` cstring
6+
// argument and returned to the callback as a JSON string. Binary fields
7+
// (message bytes) are JSON arrays of byte values.
68
#ifndef __libsds__
79
#define __libsds__
810

@@ -18,51 +20,52 @@
1820
extern "C" {
1921
#endif
2022

23+
// Result/event callback. `msg` is the (JSON) payload of length `len`.
24+
// callerRet is one of the RET_* codes above.
2125
typedef void (*SdsCallBack) (int callerRet, const char* msg, size_t len, void* userData);
2226

27+
// Synchronous provider invoked by SDS-R to fetch a retrieval hint for a
28+
// message id. The implementation allocates `*hint` (and sets `*hintLen`); the
29+
// library takes ownership and frees it with deallocShared.
2330
typedef void (*SdsRetrievalHintProvider) (const char* messageId, char** hint, size_t* hintLen, void* userData);
2431

2532

2633
// --- Core API Functions ---
2734

2835

29-
void* SdsNewReliabilityManager(SdsCallBack callback, void* userData);
36+
// Create a context + ReliabilityManager. configJson: {"participantId":"..."}
37+
// (empty participantId disables SDS-R). Returns the context handle, or NULL on
38+
// failure. The callback also fires on async completion.
39+
void* sds_create(const char* configJson, SdsCallBack callback, void* userData);
3040

31-
void SdsSetEventCallback(void* ctx, SdsCallBack callback, void* userData);
41+
// Register the event callback (message_ready, message_sent,
42+
// missing_dependencies, periodic_sync, repair_ready). Payloads are JSON.
43+
void sds_set_event_callback(void* ctx, SdsCallBack callback, void* userData);
3244

33-
void SdsSetRetrievalHintProvider(void* ctx, SdsRetrievalHintProvider callback, void* userData);
45+
// Register the retrieval-hint provider used by SDS-R.
46+
int sds_set_retrieval_hint_provider(void* ctx, SdsRetrievalHintProvider callback, void* userData);
3447

35-
int SdsCleanupReliabilityManager(void* ctx, SdsCallBack callback, void* userData);
48+
// reqJson: {"message":[..bytes..],"messageId":"..","channelId":".."}
49+
// Result JSON: {"message":[..bytes..]}
50+
int sds_wrap_outgoing_message(void* ctx, SdsCallBack callback, void* userData, const char* reqJson);
3651

37-
int SdsResetReliabilityManager(void* ctx, SdsCallBack callback, void* userData);
52+
// reqJson: {"message":[..bytes..]}
53+
// Result JSON: {"message":[..],"channelId":"..","missingDeps":[{"messageId":"..","retrievalHint":"<base64>"}]}
54+
int sds_unwrap_received_message(void* ctx, SdsCallBack callback, void* userData, const char* reqJson);
3855

39-
int SdsWrapOutgoingMessage(void* ctx,
40-
void* message,
41-
size_t messageLen,
42-
const char* messageId,
43-
const char* channelId,
44-
SdsCallBack callback,
45-
void* userData);
56+
// reqJson: {"messageIds":["..",".."],"channelId":".."}
57+
int sds_mark_dependencies_met(void* ctx, SdsCallBack callback, void* userData, const char* reqJson);
4658

47-
int SdsUnwrapReceivedMessage(void* ctx,
48-
void* message,
49-
size_t messageLen,
50-
SdsCallBack callback,
51-
void* userData);
59+
int sds_reset(void* ctx, SdsCallBack callback, void* userData);
5260

53-
int SdsMarkDependenciesMet(void* ctx,
54-
char** messageIDs,
55-
size_t count,
56-
const char* channelId,
57-
SdsCallBack callback,
58-
void* userData);
59-
60-
int SdsStartPeriodicTasks(void* ctx, SdsCallBack callback, void* userData);
61+
int sds_start_periodic_tasks(void* ctx, SdsCallBack callback, void* userData);
6162

63+
// Tear down the context created by sds_create.
64+
int sds_destroy(void* ctx, SdsCallBack callback, void* userData);
6265

6366

6467
#ifdef __cplusplus
6568
}
6669
#endif
6770

68-
#endif /* __libsds__ */
71+
#endif /* __libsds__ */

0 commit comments

Comments
 (0)