setting up shared seq for messages request

This commit is contained in:
Gabriel mermelstein 2025-04-11 17:46:21 +03:00
parent 30d05f13bf
commit 112008c906
No known key found for this signature in database
GPG Key ID: 82B8134785FEAE0D
4 changed files with 42 additions and 13 deletions

View File

@ -40,3 +40,14 @@ proc toSeq*[T](s: SharedSeq[T]): seq[T] =
for i in 0 ..< s.len:
ret.add(s.data[i])
return ret
proc allocSharedSeqFromCArray*[T](arr: ptr T, len: int): SharedSeq[T] =
## Creates a SharedSeq[T] from a C array pointer and length.
## The data is copied to shared memory.
## There should be a corresponding manual deallocation with deallocSharedSeq!
if arr.isNil or len <= 0:
return (nil, 0)
let data = allocShared(sizeof(T) * len)
copyMem(data, arr, sizeof(T) * len)
return (cast[ptr UncheckedArray[T]](data), len)

View File

@ -33,7 +33,8 @@ int CleanupReliabilityManager(void* ctx, SdsCallBack callback, void* userData);
int ResetReliabilityManager(void* ctx, SdsCallBack callback, void* userData);
int WrapOutgoingMessage(void* ctx,
const char* message,
void* message,
size_t messageLen,
const char* messageId,
SdsCallBack callback,
void* userData);

View File

@ -178,7 +178,8 @@ proc ResetReliabilityManager(
proc WrapOutgoingMessage(
ctx: ptr SdsContext,
message: cstring,
message: pointer,
messageLen: csize_t,
messageId: cstring,
callback: SdsCallBack,
userData: pointer,
@ -186,18 +187,29 @@ proc WrapOutgoingMessage(
initializeLibrary()
checkLibsdsParams(ctx, callback, userData)
let
msg = message.alloc()
msgId = messageId.alloc()
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()
defer:
deallocShared(msg)
deallocSharedSeq(msg)
deallocShared(msgId)
handleRequest(
ctx,
RequestType.MESSAGE,
SdsMessageRequest.createShared(SdsMessageMsgType.WRAP_MESSAGE, msg, msgId),
SdsMessageRequest.createShared(
SdsMessageMsgType.WRAP_MESSAGE, msg, messageLen, msgId
),
callback,
userData,
)

View File

@ -9,24 +9,27 @@ type SdsMessageMsgType* = enum
type SdsMessageRequest* = object
operation: SdsMessageMsgType
message: cstring
message: SharedSeq[byte]
messageLen: csize_t
messageId: cstring
proc createShared*(
T: type SdsMessageRequest,
op: SdsMessageMsgType,
message: cstring = "",
message: SharedSeq[byte],
messageLen: csize_t = 0,
messageId: cstring = "",
): ptr type T =
var ret = createShared(T)
ret[].operation = op
ret[].message = message.alloc()
ret[].messageId = messageId.alloc()
ret[].message = message # check if alloc is needed
ret[].messageLen = messageLen
ret[].messageId = messageId # check if alloc is needed
return ret
proc destroyShared(self: ptr SdsMessageRequest) =
deallocShared(self[].message)
deallocShared(self[].messageId)
#deallocShared(self[].message)
#deallocShared(self[].messageId)
deallocShared(self)
proc process*(
@ -37,6 +40,8 @@ proc process*(
case self.operation
of WRAP_MESSAGE:
let byteSeq = self.message.toSeq()
echo "------------ byteSeq: ", byteSeq
echo "------- received wrap message request"
return ok("")