nim-sds/library/libsds.nim

247 lines
7.3 KiB
Nim
Raw Normal View History

2025-04-08 19:28:26 +03:00
{.pragma: exported, exportc, cdecl, raises: [].}
{.pragma: callback, cdecl, raises: [], gcsafe.}
{.passc: "-fPIC".}
when defined(linux):
{.passl: "-Wl,-soname,libsds.so".}
2025-04-09 17:49:20 +03:00
import std/[locks, typetraits, tables, atomics] # Added tables
import chronos
2025-03-13 22:58:55 +05:30
import results
2025-04-09 13:39:12 +03:00
import
./sds_thread/sds_thread,
./alloc,
./ffi_types,
2025-04-09 17:49:20 +03:00
./sds_thread/inter_thread_communication/sds_thread_request,
2025-04-10 16:14:44 +03:00
./sds_thread/inter_thread_communication/requests/
[sds_lifecycle_request, sds_message_request],
2025-04-09 13:39:12 +03:00
../src/[reliability, reliability_utils, message]
2025-04-08 19:28:26 +03:00
################################################################################
### Wrapper around the reliability manager
################################################################################
2025-04-08 14:46:27 +03:00
2025-04-08 19:28:26 +03:00
################################################################################
### Not-exported components
2025-04-07 17:19:44 +05:30
2025-04-08 19:28:26 +03:00
template checkLibsdsParams*(
ctx: ptr SdsContext, callback: SdsCallBack, userData: pointer
2025-04-08 14:46:27 +03:00
) =
2025-04-08 19:28:26 +03:00
ctx[].userData = userData
2025-04-07 17:19:44 +05:30
2025-04-08 19:28:26 +03:00
if isNil(callback):
return RET_MISSING_CALLBACK
2025-04-07 17:19:44 +05:30
2025-04-08 19:28:26 +03:00
template callEventCallback(ctx: ptr SdsContext, eventName: string, body: untyped) =
if isNil(ctx[].eventCallback):
error eventName & " - eventCallback is nil"
2025-04-08 14:46:27 +03:00
return
2025-04-08 19:28:26 +03:00
if isNil(ctx[].eventUserData):
error eventName & " - eventUserData is nil"
2025-04-08 14:46:27 +03:00
return
2025-04-08 19:28:26 +03:00
foreignThreadGc:
try:
let event = body
cast[SdsCallBack](ctx[].eventCallback)(
RET_OK, unsafeAddr event[0], cast[csize_t](len(event)), ctx[].eventUserData
)
except Exception, CatchableError:
let msg =
"Exception " & eventName & " when calling 'eventCallBack': " &
getCurrentExceptionMsg()
cast[SdsCallBack](ctx[].eventCallback)(
RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), ctx[].eventUserData
)
proc handleRequest(
ctx: ptr SdsContext,
requestType: RequestType,
content: pointer,
callback: SdsCallBack,
userData: pointer,
): cint =
sds_thread.sendRequestToSdsThread(ctx, requestType, content, callback, userData).isOkOr:
let msg = "libsds error: " & $error
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR
return RET_OK
### End of not-exported components
################################################################################
################################################################################
### Library setup
# Every Nim library must have this function called - the name is derived from
# the `--nimMainPrefix` command line option
proc libsdsNimMain() {.importc.}
# To control when the library has been initialized
var initialized: Atomic[bool]
if defined(android):
# Redirect chronicles to Android System logs
when compiles(defaultChroniclesStream.outputs[0].writer):
defaultChroniclesStream.outputs[0].writer = proc(
logLevel: LogLevel, msg: LogOutputStr
) {.raises: [].} =
echo logLevel, msg
proc initializeLibrary() {.exported.} =
if not initialized.exchange(true):
## Every Nim library needs to call `<yourprefix>NimMain` once exactly, to initialize the Nim runtime.
## Being `<yourprefix>` the value given in the optional compilation flag --nimMainPrefix:yourprefix
libsdsNimMain()
when declared(setupForeignThreadGc):
setupForeignThreadGc()
when declared(nimGC_setStackBottom):
var locals {.volatile, noinit.}: pointer
locals = addr(locals)
nimGC_setStackBottom(locals)
### End of library setup
################################################################################
2025-04-09 17:58:42 +03:00
################################################################################
### Exported procs
proc NewReliabilityManager(
2025-04-09 18:31:43 +03:00
channelId: cstring, callback: SdsCallBack, userData: pointer
2025-04-09 17:58:42 +03:00
): pointer {.dynlib, exportc, cdecl.} =
initializeLibrary()
2025-04-10 12:52:04 +03:00
## Creates a new instance of the Reliability Manager.
2025-04-09 17:58:42 +03:00
if isNil(callback):
echo "error: missing callback in NewReliabilityManager"
return nil
## Create the SDS thread that will keep waiting for req from the main thread.
var ctx = sds_thread.createSdsThread().valueOr:
let msg = "Error in createSdsThread: " & $error
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return nil
ctx.userData = userData
let retCode = handleRequest(
ctx,
RequestType.LIFECYCLE,
SdsLifecycleRequest.createShared(
SdsLifecycleMsgType.CREATE_RELIABILITY_MANAGER, channelId
),
callback,
userData,
)
if retCode == RET_ERR:
return nil
return ctx
2025-04-10 12:41:48 +03:00
proc SetEventCallback(
ctx: ptr SdsContext, callback: SdsCallBack, userData: pointer
) {.dynlib, exportc.} =
initializeLibrary()
ctx[].eventCallback = cast[pointer](callback)
ctx[].eventUserData = userData
2025-04-10 12:52:04 +03:00
proc CleanupReliabilityManager(
ctx: ptr SdsContext, callback: SdsCallBack, userData: pointer
): cint {.dynlib, exportc.} =
initializeLibrary()
checkLibsdsParams(ctx, callback, userData)
sds_thread.destroySdsThread(ctx).isOkOr:
let msg = "libsds error: " & $error
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR
## always need to invoke the callback although we don't retrieve value to the caller
callback(RET_OK, nil, 0, userData)
return RET_OK
2025-04-10 14:25:53 +03:00
proc ResetReliabilityManager(
ctx: ptr SdsContext, callback: SdsCallBack, userData: pointer
): cint {.dynlib, exportc.} =
checkLibsdsParams(ctx, callback, userData)
handleRequest(
ctx,
RequestType.LIFECYCLE,
SdsLifecycleRequest.createShared(SdsLifecycleMsgType.RESET_RELIABILITY_MANAGER),
callback,
userData,
)
2025-04-10 16:14:44 +03:00
proc WrapOutgoingMessage(
ctx: ptr SdsContext,
message: pointer,
messageLen: csize_t,
2025-04-10 16:14:44 +03:00
messageId: cstring,
callback: SdsCallBack,
userData: pointer,
): cint {.dynlib, exportc.} =
initializeLibrary()
checkLibsdsParams(ctx, callback, userData)
if message == nil and messageLen > 0:
let msg = "libsds error: " & "message pointer is NULL but length > 0"
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR
if messageId == nil:
let msg = "libsds error: " & "message ID pointer is NULL"
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR
var msg = allocSharedSeqFromCArray(cast[ptr byte](message), messageLen.int)
let msgId = messageId.alloc()
2025-04-10 16:14:44 +03:00
defer:
deallocSharedSeq(msg)
2025-04-10 16:14:44 +03:00
deallocShared(msgId)
handleRequest(
ctx,
RequestType.MESSAGE,
SdsMessageRequest.createShared(
SdsMessageMsgType.WRAP_MESSAGE, msg, messageLen, msgId
),
2025-04-10 16:14:44 +03:00
callback,
userData,
)
2025-04-15 16:23:06 +03:00
proc UnwrapReceivedMessage(
ctx: ptr SdsContext,
message: pointer,
messageLen: csize_t,
callback: SdsCallBack,
userData: pointer,
): cint {.dynlib, exportc.} =
initializeLibrary()
checkLibsdsParams(ctx, callback, userData)
if message == nil and messageLen > 0:
let msg = "libsds error: " & "message pointer is NULL but length > 0"
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR
var msg = allocSharedSeqFromCArray(cast[ptr byte](message), messageLen.int)
defer:
deallocSharedSeq(msg)
handleRequest(
ctx,
RequestType.MESSAGE,
SdsMessageRequest.createShared(SdsMessageMsgType.UNWRAP_MESSAGE, msg, messageLen),
callback,
userData,
)
2025-04-09 17:58:42 +03:00
### End of exported procs
################################################################################