From 1602cd79dd98422faaa1e5f742cc62b0a3cd5536 Mon Sep 17 00:00:00 2001 From: Morgan Pretty Date: Fri, 15 Mar 2024 11:29:06 +1100 Subject: [PATCH] Added missing 'sendRequest' function --- .../SessionUtil/SessionUtil.swift | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/SessionMessagingKit/SessionUtil/SessionUtil.swift b/SessionMessagingKit/SessionUtil/SessionUtil.swift index 2ed9b3c69..faf839c78 100644 --- a/SessionMessagingKit/SessionUtil/SessionUtil.swift +++ b/SessionMessagingKit/SessionUtil/SessionUtil.swift @@ -532,3 +532,49 @@ public extension SessionUtil { return String(cString: cFullUrl) } } + + +public extension SessionUtil { + static func sendRequest( + ed25519SecretKey: [UInt8]?, + targetPubkey: String, + targetIp: String, + targetPort: UInt16, + endpoint: String, + payload: Data, + callback: @escaping (Bool, Int16, Data?) -> Void + ) { + class CWrapper { + let callback: (Bool, Int16, Data?) -> Void + + public init(_ callback: @escaping (Bool, Int16, Data?) -> Void) { + self.callback = callback + } + } + + let callbackWrapper: CWrapper = CWrapper(callback) + let cWrapperPtr: UnsafeMutableRawPointer = Unmanaged.passRetained(callbackWrapper).toOpaque() + let cEd25519SecretKey: [UInt8] = ed25519SecretKey! + let cRemoteAddress: remote_address = remote_address( + pubkey: targetPubkey.toLibSession(), + ip: targetIp.toLibSession(), + port: targetPort + ) + let cEndpoint: [CChar] = endpoint.cArray + let cPayload: [UInt8] = payload.cArray + + network_send_request( + cEd25519SecretKey, + cRemoteAddress, + cEndpoint, + cEndpoint.count, + cPayload, + cPayload.count, + { success, statusCode, dataPtr, dataLen, ctx in + let data: Data? = dataPtr.map { Data(bytes: $0, count: dataLen) } + Unmanaged.fromOpaque(ctx!).takeRetainedValue().callback(success, statusCode, data) + }, + cWrapperPtr + ) + } +}