logos-messaging-nim/library/channels_api/channel_lifecycle_api.nim
Ivan FB c64d156f2a
FFI: migrate liblogosdelivery root to nim-ffi v0.2.0-rc.3
Rewrite the FFI root over the new per-layer APIs using nim-ffi v0.2.0 typed
{.ffiCtor.}/{.ffiDtor.}/{.ffi.}/{.ffiEvent.} + CBOR, replacing the
hand-written cstring/JSON bridge. Events are fed by internal nim-broker
listeners (no AppCallbacks). Adds the messaging_api/channels_api groups and
the broker-listener event modules, and drops the v0.1 scaffolding
(declare_lib, node_api, node_lifecycle_api, logos_delivery_api/*, json_*).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-25 04:18:35 +02:00

25 lines
1.2 KiB
Nim

## Opaque handle to a live reliable channel. Holds the owning manager + the
## channel id so the channel ops (send / close) need no other context. Only its
## uint64 id crosses the FFI boundary; the object stays in the ctx registry.
type ReliableChannelHandle {.ffiHandle.} = ref object
manager: ReliableChannelManager
channelId: ChannelId
proc channel_create*(
self: LogosDelivery, channelId: string, contentTopic: string, senderId: string
): Future[Result[ReliableChannelHandle, string]] {.ffi.} =
## Creates a reliable channel and returns a handle to it. The send handler and
## rng come from the manager; encryption providers are installed separately.
let id = self.reliableChannelManager.createReliableChannel(
ChannelId(channelId), ContentTopic(contentTopic), SdsParticipantID(senderId)
).valueOr:
return err(error)
return ok(ReliableChannelHandle(manager: self.reliableChannelManager, channelId: id))
proc channel_close*(ch: ReliableChannelHandle): Future[Result[string, string]] {.ffi.} =
## Stops the channel's SDS loops and deregisters it from the manager.
## Persisted SDS state survives, so re-creating the channel restores it.
(await ch.manager.closeChannel(ch.channelId)).isOkOr:
return err(error)
return ok("")