diff --git a/.gitignore b/.gitignore index b8be30d..6f5bb9f 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ nph docs for_reference do_not_commit +build/* +sds.nims diff --git a/Makefile b/Makefile index 443bddc..3ab8b78 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,19 @@ .PHONY: libsds -libsds: - nim c --app:lib --mm:refc --outdir:build library/libsds.nim \ No newline at end of file +sds.nims: + ln -s sds.nimble $@ + +deps: | sds.nims + + +STATIC ?= 0 + +libsds: deps + rm -f build/libwaku* +ifeq ($(STATIC), 1) + echo -e $(BUILD_MSG) "build/$@.a" && \ + $(ENV_SCRIPT) nim libsdsStatic $(NIM_PARAMS) sds.nims +else + echo -e $(BUILD_MSG) "build/$@.so" && \ + $(ENV_SCRIPT) nim libsdsDynamic $(NIM_PARAMS) sds.nims +endif \ No newline at end of file diff --git a/library/libsds.h b/library/libsds.h index 67fd179..f34daf4 100644 --- a/library/libsds.h +++ b/library/libsds.h @@ -1,56 +1,24 @@ -#ifndef BINDINGS_H -#define BINDINGS_H -#include // For size_t -#include // For standard integer types -#include // For bool type +// Generated manually and inspired by the one generated by the Nim Compiler. +// In order to see the header file generated by Nim just run `make libsds` +// from the root repo folder and the header should be created in +// nimcache/release/libsds/libsds.h +#ifndef __libsds__ +#define __libsds__ + +#include +#include + +// The possible returned values for the functions that return int +#define RET_OK 0 +#define RET_ERR 1 +#define RET_MISSING_CALLBACK 2 #ifdef __cplusplus extern "C" { #endif - -// Opaque struct declaration (handle replaces direct pointer usage) -typedef struct ReliabilityManager ReliabilityManager; // Keep forward declaration - -// Define MessageID as a C string -typedef const char* MessageID; // Keep const for the typedef itself - -// --- Result Types --- - -typedef struct { - bool is_ok; - char* error_message; -} CResult; - -typedef struct { - CResult base_result; - unsigned char* message; - size_t message_len; - MessageID* missing_deps; - size_t missing_deps_count; -} CUnwrapResult; - -typedef struct { - CResult base_result; - unsigned char* message; - size_t message_len; -} CWrapResult; - - -// --- Callback Function Pointer Types --- - -// Define event types (enum or constants) -typedef enum { - EVENT_MESSAGE_READY = 1, - EVENT_MESSAGE_SENT = 2, - EVENT_MISSING_DEPENDENCIES = 3, - EVENT_PERIODIC_SYNC = 4 -} CEventType; - -// Single callback type for all events -// Nim will call this, passing the handle and event-specific data -typedef void (*CEventCallback)(void* handle, CEventType eventType, void* data1, void* data2, size_t data3); +typedef void (*SdsCallBack) (int callerRet, const char* msg, size_t len, void* userData); // --- Core API Functions --- @@ -60,73 +28,16 @@ typedef void (*CEventCallback)(void* handle, CEventType eventType, void* data1, * @param channelId A unique identifier for the communication channel. * @return An opaque handle (void*) representing the instance, or NULL on failure. */ -void* NewReliabilityManager(char* channelId); +void* sds_reliability_manager_new(char* channelId); -/** - * @brief Cleans up resources associated with a ReliabilityManager instance. - * @param handle The opaque handle (void*) of the instance to clean up. - */ -void CleanupReliabilityManager(void* handle); - -/** - * @brief Resets the ReliabilityManager instance. - * @param handle The opaque handle (void*) of the instance. - * @return CResult indicating success or failure. - */ -CResult ResetReliabilityManager(void* handle); -/** - * @brief Wraps an outgoing message. - * @param handle The opaque handle (void*) of the instance. - * @param message Pointer to the raw message content. - * @param messageLen Length of the raw message content. - * @param messageId A unique identifier for this message. - * @return CWrapResult containing the wrapped message or an error. - */ -CWrapResult WrapOutgoingMessage(void* handle, void* message, size_t messageLen, char* messageId); -/** - * @brief Unwraps a received message. - * @param handle The opaque handle (void*) of the instance. - * @param message Pointer to the received message data. - * @param messageLen Length of the received message data. - * @return CUnwrapResult containing the unwrapped content, missing dependencies, or an error. - */ -CUnwrapResult UnwrapReceivedMessage(void* handle, void* message, size_t messageLen); - -/** - * @brief Marks specified message dependencies as met. - * @param handle The opaque handle (void*) of the instance. - * @param messageIDs An array of message IDs to mark as met. - * @param count The number of message IDs in the array. - * @return CResult indicating success or failure. - */ -CResult MarkDependenciesMet(void* handle, char** messageIDs, size_t count); // Reverted to char** - -/** - * @brief Registers callback functions. - * @param handle The opaque handle (void*) of the instance. - * @param messageReady Callback for when a message is ready. - * @param messageSent Callback for when an outgoing message is acknowledged. - * @param eventCallback The single callback function to handle all events. - * @param user_data A pointer to user-defined data (optional, could be managed in Go). - */ -void RegisterCallback(void* handle, CEventCallback eventCallback, void* user_data); // Renamed and simplified - -/** - * @brief Starts the background periodic tasks. - * @param handle The opaque handle (void*) of the instance. - */ -void StartPeriodicTasks(void* handle); - - -// --- Memory Freeing Functions --- - -void FreeCResultError(CResult result); -void FreeCWrapResult(CWrapResult result); -void FreeCUnwrapResult(CUnwrapResult result); +/* void* waku_new( + const char* configJson, + WakuCallBack callback, + void* userData); */ #ifdef __cplusplus -} // extern "C" +} #endif -#endif // BINDINGS_H +#endif /* __libsds__ */ \ No newline at end of file diff --git a/reliability.nimble b/reliability.nimble deleted file mode 100644 index da7f7e6..0000000 --- a/reliability.nimble +++ /dev/null @@ -1,29 +0,0 @@ -# Package -version = "0.1.0" -author = "Waku Team" -description = "E2E Reliability Protocol API" -license = "MIT" -srcDir = "src" - -# Dependencies -requires "nim >= 2.0.8" -requires "chronicles" -requires "libp2p" - -# Tasks -task test, "Run the test suite": - exec "nim c -r tests/test_bloom.nim" - exec "nim c -r tests/test_reliability.nim" - -task bindings, "Generate bindings": - proc compile(libName: string, flags = "") = - exec "nim c -f " & flags & " -d:release --app:lib --mm:arc --tlsEmulation:off --out:" & libName & " --outdir:build library/libsds.nim" - - when defined(windows): - compile "reliability.dll" - elif defined(macosx): - compile "libsds.dylib.arm", "--cpu:arm64 -l:'-target arm64-apple-macos11' -t:'-target arm64-apple-macos11'" - compile "libsds.dylib.x64", "--cpu:amd64 -l:'-target x86_64-apple-macos10.12' -t:'-target x86_64-apple-macos10.12'" - exec "lipo build/libsds.dylib.arm build/libsds.dylib.x64 -output build/libsds.dylib -create" - else: - compile "libsds.so" \ No newline at end of file diff --git a/sds.nimble b/sds.nimble new file mode 100644 index 0000000..2362c28 --- /dev/null +++ b/sds.nimble @@ -0,0 +1,39 @@ +# Package +version = "0.1.0" +author = "Waku Team" +description = "E2E Reliability Protocol API" +license = "MIT" +srcDir = "src" + +# Dependencies +requires "nim >= 2.0.8" +requires "chronicles" +requires "libp2p" + +proc buildLibrary(name: string, srcDir = "./", params = "", `type` = "static") = + if not dirExists "build": + mkDir "build" + # allow something like "nim nimbus --verbosity:0 --hints:off nimbus.nims" + var extra_params = params + for i in 2 ..< paramCount(): + extra_params &= " " & paramStr(i) + if `type` == "static": + exec "nim c" & " --out:build/" & name & + ".a --threads:on --app:staticlib --opt:size --noMain --mm:refc --header --undef:metrics --nimMainPrefix:libsds --skipParentCfg:on " & + extra_params & " " & srcDir & name & ".nim" + else: + exec "nim c" & " --out:build/" & name & + ".so --threads:on --app:lib --opt:size --noMain --mm:refc --header --undef:metrics --nimMainPrefix:libsds --skipParentCfg:on " & + extra_params & " " & srcDir & name & ".nim" + +# Tasks +task test, "Run the test suite": + exec "nim c -r tests/test_bloom.nim" + exec "nim c -r tests/test_reliability.nim" + +task libsdsDynamic, "Generate bindings": + let name = "libsds" + buildLibrary name, + "library/", + "", + "dynamic" \ No newline at end of file diff --git a/sds_wrapper.go b/sds_wrapper.go index 389f763..1150a46 100644 --- a/sds_wrapper.go +++ b/sds_wrapper.go @@ -2,7 +2,7 @@ package main /* #cgo CFLAGS: -I${SRCDIR}/library -#cgo LDFLAGS: -L${SRCDIR}/build -lbindings +#cgo LDFLAGS: -L${SRCDIR}/build -llibsds #cgo LDFLAGS: -Wl,-rpath,${SRCDIR}/build #include // For C.free