logos-messaging-nim/library/channels_api/channel_lifecycle_api.nim
Ivan FB 46666044f2
channels_api: reliable-channel FFI (create/send/close + events)
Add library/channels_api over the reliable-channel logic: channel_create
returns a {.ffiHandle.} ReliableChannelHandle, channel_send/channel_close
operate on it, and the channel message received/sent/error events are fed by
the channel-layer broker events. Wire them into the FFI root.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-25 13:14:41 +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("")