diff --git a/AppleNative/AneAwesomeUtils/AneAwesomeUtils.xcodeproj/project.pbxproj b/AppleNative/AneAwesomeUtils/AneAwesomeUtils.xcodeproj/project.pbxproj index 3c41d51..a18a53e 100644 --- a/AppleNative/AneAwesomeUtils/AneAwesomeUtils.xcodeproj/project.pbxproj +++ b/AppleNative/AneAwesomeUtils/AneAwesomeUtils.xcodeproj/project.pbxproj @@ -59,7 +59,6 @@ AneAwesomeUtilsSupport.cpp, DeviceUtils.m, log.cpp, - WebSocketClient.cpp, ); target = 57B755732CD02AFF0000FE66 /* AneAwesomeUtils-IOS */; }; diff --git a/AppleNative/AneAwesomeUtils/AneAwesomeUtils.xcodeproj/project.xcworkspace/xcuserdata/joaovitorborges.xcuserdatad/UserInterfaceState.xcuserstate b/AppleNative/AneAwesomeUtils/AneAwesomeUtils.xcodeproj/project.xcworkspace/xcuserdata/joaovitorborges.xcuserdatad/UserInterfaceState.xcuserstate index f637556..5add524 100644 Binary files a/AppleNative/AneAwesomeUtils/AneAwesomeUtils.xcodeproj/project.xcworkspace/xcuserdata/joaovitorborges.xcuserdatad/UserInterfaceState.xcuserstate and b/AppleNative/AneAwesomeUtils/AneAwesomeUtils.xcodeproj/project.xcworkspace/xcuserdata/joaovitorborges.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/AppleNative/AneAwesomeUtils/AneAwesomeUtils/AneAwesomeUtilsCsharp.h b/AppleNative/AneAwesomeUtils/AneAwesomeUtils/AneAwesomeUtilsCsharp.h index 788aa71..0e9be6d 100644 --- a/AppleNative/AneAwesomeUtils/AneAwesomeUtils/AneAwesomeUtilsCsharp.h +++ b/AppleNative/AneAwesomeUtils/AneAwesomeUtils/AneAwesomeUtilsCsharp.h @@ -1,14 +1,13 @@ // -// AneAwesomeUtilsCsharp.h -// AneAwesomeUtils -// -// Created by João Vitor Borges on 28/10/24. +// Created by User on 29/10/2024. // -#ifndef AneAwesomeUtilsCsharp_h -#define AneAwesomeUtilsCsharp_h +#ifndef ANEAWESOMEUTILSCSHARP_H +#define ANEAWESOMEUTILSCSHARP_H +#include "DataArray.h" + extern "C" { - __cdecl int csharpLibrary_awesomeUtils_initialize( + int __cdecl csharpLibrary_awesomeUtils_initialize( const void* urlLoaderSuccessCallBack, const void *urlLoaderProgressCallBack, const void *urlLoaderFailureCallBack, @@ -16,15 +15,16 @@ extern "C" { const void *webSocketErrorCallBack, const void *webSocketDataCallBack, const void *writeLogCallBack); - __cdecl char* csharpLibrary_awesomeUtils_uuid(); - __cdecl char* csharpLibrary_awesomeUtils_deviceUniqueId(); - __cdecl char* csharpLibrary_awesomeUtils_loadUrl(const char* url, const char* method, const char* variables, const char* headers); - __cdecl char* csharpLibrary_awesomeUtils_createWebSocket(); - __cdecl int csharpLibrary_awesomeUtils_connectWebSocket(const void* guidPointer, const char* host); - __cdecl int csharpLibrary_awesomeUtils_sendWebSocketMessage(const void* guidPointer, const void* data, int length); - __cdecl int csharpLibrary_awesomeUtils_closeWebSocket(const void* guidPointer, int closeCode); - __cdecl void csharpLibrary_awesomeUtils_addStaticHost(const char* host, const char* ip); - __cdecl void csharpLibrary_awesomeUtils_removeStaticHost(const char* host); + char* __cdecl csharpLibrary_awesomeUtils_uuid(); + char* __cdecl csharpLibrary_awesomeUtils_deviceUniqueId(); + char* __cdecl csharpLibrary_awesomeUtils_loadUrl(const char* url, const char* method, const char* variables, const char* headers); + DataArray __cdecl csharpLibrary_awesomeUtils_getLoaderResult(const void* guidPointer); + char* __cdecl csharpLibrary_awesomeUtils_createWebSocket(); + int __cdecl csharpLibrary_awesomeUtils_connectWebSocket(const void* guidPointer, const char* host); + int __cdecl csharpLibrary_awesomeUtils_sendWebSocketMessage(const void* guidPointer, const void* data, int length); + int __cdecl csharpLibrary_awesomeUtils_closeWebSocket(const void* guidPointer, int closeCode); + DataArray __cdecl csharpLibrary_awesomeUtils_getWebSocketMessage(const void* guidPointer); + void __cdecl csharpLibrary_awesomeUtils_addStaticHost(const char* host, const char* ip); + void __cdecl csharpLibrary_awesomeUtils_removeStaticHost(const char* host); } - -#endif /* AneAwesomeUtilsCsharp_h */ +#endif //ANEAWESOMEUTILSCSHARP_H diff --git a/AppleNative/AneAwesomeUtils/AneAwesomeUtils/AneAwesomeUtilsSupport.cpp b/AppleNative/AneAwesomeUtils/AneAwesomeUtils/AneAwesomeUtilsSupport.cpp index 4b282a7..a4d805b 100644 --- a/AppleNative/AneAwesomeUtils/AneAwesomeUtils/AneAwesomeUtilsSupport.cpp +++ b/AppleNative/AneAwesomeUtils/AneAwesomeUtils/AneAwesomeUtilsSupport.cpp @@ -1,12 +1,6 @@ #include "AneAwesomeUtilsCsharp.h" -#include "WebSocketClient.hpp" #include #include -#include -#include -#include -#include -#include typedef void* NSWindow; // don't need this.. #include // Adobe AIR runtime includes #include "log.hpp" @@ -16,69 +10,30 @@ typedef void* NSWindow; // don't need this.. #endif static bool alreadyInitialized = false; -static FRENamedFunction* exportedFunctions = new FRENamedFunction[11]; -static std::unordered_map wsClientMap; -static std::mutex wsClientMapMutex; -static std::unordered_map> loaderResultMap; -static std::mutex loaderResultMapMutex; +static FRENamedFunction *exportedFunctions = new FRENamedFunction[11]; static FREContext context; -// Helper function to safely retrieve WebSocketClient from the map -static WebSocketClient* getWebSocketClient(std::string guidString) { - std::lock_guard guard(wsClientMapMutex); - auto it = wsClientMap.find(guidString); - if (it != wsClientMap.end()) { - return it->second; - } - return nullptr; -} - -// Helper function to safely insert WebSocketClient into the map -static void setWebSocketClient(std::string guidString, WebSocketClient* wsClient) { - std::lock_guard guard(wsClientMapMutex); - wsClientMap[guidString] = wsClient; -} - -// Helper function to safely remove WebSocketClient from the map -static void removeWebSocketClient(std::string guidString) { - std::lock_guard guard(wsClientMapMutex); - wsClientMap.erase(guidString); -} - -static void dispatchWebSocketEvent(const char* guid, const char* code, const char* level) { +static void dispatchWebSocketEvent(const char *guid, const char *code, const char *level) { std::string fullCode = std::string("web-socket;") + code + std::string(";") + guid; FREDispatchStatusEventAsync(context, reinterpret_cast(fullCode.c_str()), reinterpret_cast(level)); } -static void dispatchUrlLoaderEvent(const char* guid, const char* code, const char* level) { +static void dispatchUrlLoaderEvent(const char *guid, const char *code, const char *level) { std::string fullCode = std::string("url-loader;") + code + std::string(";") + guid; FREDispatchStatusEventAsync(context, reinterpret_cast(fullCode.c_str()), reinterpret_cast(level)); } -__cdecl static void webSocketConnectCallBack(char* guid) { +static void __cdecl webSocketConnectCallBack(char *guid) { writeLog("connectCallback called"); dispatchWebSocketEvent(guid, "connected", ""); } -__cdecl static void webSocketDataCallBack(char* guid, const uint8_t *data, int length) { +static void __cdecl webSocketDataCallBack(char *guid) { writeLog("dataCallback called"); - - WebSocketClient* wsClient = getWebSocketClient(guid); - - if(wsClient == nullptr){ - writeLog("wsClient not found"); - return; - } - - auto dataCopyVector = std::vector(); - dataCopyVector.resize(length); - std::copy_n(data, length, dataCopyVector.begin()); - wsClient->enqueueMessage(dataCopyVector); - dispatchWebSocketEvent(guid, "nextMessage", ""); } -__cdecl static void webSocketErrorCallBack(char* guid, int closeCode, const char *reason) { +static void __cdecl webSocketErrorCallBack(char *guid, int closeCode, const char *reason) { writeLog("disconnectCallback called"); auto closeCodeReason = std::to_string(closeCode) + ";" + std::string(reason); @@ -86,29 +41,28 @@ __cdecl static void webSocketErrorCallBack(char* guid, int closeCode, const char writeLog(closeCodeReason.c_str()); dispatchWebSocketEvent(guid, "disconnected", closeCodeReason.c_str()); - removeWebSocketClient(guid); } -__cdecl void urlLoaderSuccessCallBack(const char *id, uint8_t *result, int32_t length) { +static void __cdecl urlLoaderSuccessCallBack(const char *id) { writeLog("Calling SuccessCallback"); + // Prepare data outside the lock to minimize locked duration std::string id_str(id); + writeLog(("ID: " + id_str).c_str()); - writeLog(("Result Length: " + std::to_string(length)).c_str()); - std::lock_guard lock(loaderResultMapMutex); - loaderResultMap.insert({id_str, std::vector(result, result + length)}); + dispatchUrlLoaderEvent(id_str.c_str(), "success", ""); - dispatchUrlLoaderEvent(id, "success", ""); + // Notify and log the success event outside the lock writeLog("Dispatched success event"); } -__cdecl void urlLoaderProgressCallBack(const char *id, const char *message) { - dispatchUrlLoaderEvent(id, "progress", message); +static void __cdecl urlLoaderProgressCallBack(const char *id, const char *message) { + dispatchUrlLoaderEvent(std::string(id).c_str(), "progress", message); } -__cdecl void urlLoaderErrorCallBack(const char *id, const char *message) { - dispatchUrlLoaderEvent(id, "error", message); +static void __cdecl urlLoaderErrorCallBack(const char *id, const char *message) { + dispatchUrlLoaderEvent(std::string(id).c_str(), "error", message); } static void writeLogCallback(const char *message) { @@ -119,15 +73,15 @@ static void writeLogCallback(const char *message) { static FREObject awesomeUtils_initialize(FREContext ctx, void *funcData, uint32_t argc, FREObject argv[]) { writeLog("initialize called"); auto initResult = csharpLibrary_awesomeUtils_initialize( - (void*)&urlLoaderSuccessCallBack, - (void*)&urlLoaderProgressCallBack, - (void*)&urlLoaderErrorCallBack, - (void*)&webSocketConnectCallBack, - (void*)&webSocketErrorCallBack, - (void*)&webSocketDataCallBack, - (void*)&writeLogCallback - ); - + (void *) &urlLoaderSuccessCallBack, + (void *) &urlLoaderProgressCallBack, + (void *) &urlLoaderErrorCallBack, + (void *) &webSocketConnectCallBack, + (void *) &webSocketErrorCallBack, + (void *) &webSocketDataCallBack, + (void *) &writeLogCallback + ); + FREObject resultBool; FRENewObjectFromBool(initResult == 1, &resultBool); return resultBool; @@ -135,12 +89,12 @@ static FREObject awesomeUtils_initialize(FREContext ctx, void *funcData, uint32_ static FREObject awesomeUtils_createWebSocket(FREContext ctx, void *funcData, uint32_t argc, FREObject argv[]) { writeLog("createWebSocket called"); - auto idWebSocket = csharpLibrary_awesomeUtils_createWebSocket(); - auto wsClient = new WebSocketClient(idWebSocket); - setWebSocketClient(std::string(idWebSocket), wsClient); - + + // Capture idWebSocket immediately as a string + const char *idWebSocketPtr = csharpLibrary_awesomeUtils_createWebSocket(); + FREObject resultStr; - FRENewObjectFromUTF8((uint32_t)strlen(idWebSocket), (const uint8_t *)idWebSocket, &resultStr); + FRENewObjectFromUTF8(strlen(idWebSocketPtr), reinterpret_cast(idWebSocketPtr), &resultStr); return resultStr; } @@ -162,16 +116,7 @@ static FREObject awesomeUtils_connectWebSocket(FREContext ctx, void *funcData, u writeLog("Calling connect to uri: "); writeLog(uriChar); - WebSocketClient* wsClient = getWebSocketClient(idChar); - - if(wsClient == nullptr){ - writeLog("wsClient not found"); - return nullptr; - } - - if (wsClient != nullptr) { - wsClient->connect(reinterpret_cast(uri)); - } + csharpLibrary_awesomeUtils_connectWebSocket(idChar, uriChar); return nullptr; } @@ -185,19 +130,10 @@ static FREObject awesomeUtils_closeWebSocket(FREContext ctx, void *funcData, uin FREGetObjectAsUTF8(argv[0], &idLength, &id); auto idChar = reinterpret_cast(id); - WebSocketClient* wsClient = getWebSocketClient(idChar); - - if(wsClient == nullptr){ - writeLog("wsClient not found"); - return nullptr; - } - uint32_t closeCode = 1000; // Default close code - if (argc > 0) { - FREGetObjectAsUint32(argv[0], &closeCode); - } + FREGetObjectAsUint32(argv[0], &closeCode); - wsClient->close(closeCode); + csharpLibrary_awesomeUtils_closeWebSocket(idChar, static_cast(closeCode)); return nullptr; } @@ -210,8 +146,6 @@ static FREObject awesomeUtils_sendWebSocketMessage(FREContext ctx, void *funcDat FREGetObjectAsUTF8(argv[0], &idLength, &id); auto idChar = reinterpret_cast(id); - WebSocketClient* wsClient = getWebSocketClient(idChar); - uint32_t messageType; FREGetObjectAsUint32(argv[1], &messageType); @@ -224,7 +158,7 @@ static FREObject awesomeUtils_sendWebSocketMessage(FREContext ctx, void *funcDat FREByteArray byteArray; FREAcquireByteArray(argv[2], &byteArray); - wsClient->sendMessage(byteArray.bytes, static_cast(byteArray.length)); + csharpLibrary_awesomeUtils_sendWebSocketMessage(idChar, byteArray.bytes, static_cast(byteArray.length)); FREReleaseByteArray(argv[2]); } @@ -235,7 +169,7 @@ static FREObject awesomeUtils_sendWebSocketMessage(FREContext ctx, void *funcDat static FREObject awesomeUtils_getWebSocketByteArrayMessage(FREContext ctx, void *funcData, uint32_t argc, FREObject argv[]) { writeLog("getByteArrayMessage called"); - + if (argc < 1) return nullptr; uint32_t idLength; @@ -243,29 +177,22 @@ static FREObject awesomeUtils_getWebSocketByteArrayMessage(FREContext ctx, void FREGetObjectAsUTF8(argv[0], &idLength, &id); auto idChar = reinterpret_cast(id); - WebSocketClient* wsClient = getWebSocketClient(idChar); - - auto nextMessageResult = wsClient->getNextMessage(); + auto nextMessageResult = csharpLibrary_awesomeUtils_getWebSocketMessage(idChar); - if (!nextMessageResult.has_value()) { + if (nextMessageResult.Size == 0) { writeLog("no messages found"); return nullptr; } - auto vectorData = nextMessageResult.value(); - - if (vectorData.empty()) { - writeLog("message it's empty"); - return nullptr; - } - FREObject byteArrayObject = nullptr; FREByteArray byteArray; - byteArray.length = (uint32_t)vectorData.size(); - byteArray.bytes = vectorData.data(); + byteArray.length = nextMessageResult.Size; + byteArray.bytes = nextMessageResult.DataPointer; FRENewByteArray(&byteArray, &byteArrayObject); + free(nextMessageResult.DataPointer); + return byteArrayObject; } @@ -275,74 +202,64 @@ static FREObject awesomeUtils_loadUrl(FREContext ctx, void *funcData, uint32_t a uint32_t stringLength; const uint8_t *url; FREGetObjectAsUTF8(argv[0], &stringLength, &url); - writeLog(("URL: " + std::string((const char *)url)).c_str()); + writeLog(("URL: " + std::string(reinterpret_cast(url))).c_str()); uint32_t methodLength; const uint8_t *method; FREGetObjectAsUTF8(argv[1], &methodLength, &method); - writeLog(("Method: " + std::string((const char *)method)).c_str()); + writeLog(("Method: " + std::string(reinterpret_cast(method))).c_str()); uint32_t variableLength; const uint8_t *variable; FREGetObjectAsUTF8(argv[2], &variableLength, &variable); - writeLog(("Variable: " + std::string((const char *)variable)).c_str()); + writeLog(("Variable: " + std::string(reinterpret_cast(variable))).c_str()); uint32_t headersLength; const uint8_t *headers; FREGetObjectAsUTF8(argv[3], &headersLength, &headers); - writeLog(("Headers: " + std::string((const char *)headers)).c_str()); + writeLog(("Headers: " + std::string(reinterpret_cast(headers))).c_str()); - char *result = csharpLibrary_awesomeUtils_loadUrl((const char *)url, (const char *)method, (const char *)variable, (const char *)headers); + char *result = csharpLibrary_awesomeUtils_loadUrl(reinterpret_cast(url), reinterpret_cast(method), reinterpret_cast(variable), reinterpret_cast(headers)); if (!result) { writeLog("startLoader returned null"); return nullptr; } - writeLog(("Result: " + std::string(result)).c_str()); + auto resultString = std::string(result); + + writeLog(("Result: " + resultString).c_str()); FREObject resultStr; - FRENewObjectFromUTF8((uint32_t)strlen(result), (const uint8_t *)result, &resultStr); + FRENewObjectFromUTF8(resultString.length(), reinterpret_cast(resultString.c_str()), &resultStr); free(result); return resultStr; } - static FREObject awesomeUtils_getLoaderResult(FREContext ctx, void *functionData, uint32_t argc, FREObject argv[]) { writeLog("Calling getResult"); + // Extract UUID from arguments uint32_t uuidLength; const uint8_t *uuid; FREGetObjectAsUTF8(argv[0], &uuidLength, &uuid); - std::string uuidStr(reinterpret_cast(uuid), uuidLength); - writeLog(("GetResult ID: " + uuidStr).c_str()); - - std::vector result; - std::lock_guard lock(loaderResultMapMutex); - auto it = loaderResultMap.find(uuidStr); - if (it != loaderResultMap.end()) { - result = it->second; - loaderResultMap.erase(it); - writeLog("Result found"); - } + auto uuidChar = reinterpret_cast(uuid); + auto result = csharpLibrary_awesomeUtils_getLoaderResult(uuidChar); + + // Handle the result outside the lock FREObject byteArrayObject = nullptr; - if (!result.empty()) { - writeLog("Creating AS3 ByteArray"); + if (result.Size > 0) { FREByteArray byteArray; - byteArray.length = (uint32_t)result.size(); - byteArray.bytes = result.data(); - + byteArray.length = result.Size; + byteArray.bytes = result.DataPointer; FRENewByteArray(&byteArray, &byteArrayObject); - writeLog("AS3 ByteArray created"); - } else { - writeLog("Result is empty"); + free(result.DataPointer); } - - result.clear(); return byteArrayObject; } + static FREObject awesomeUtils_addStaticHost(FREContext ctx, void *funcData, uint32_t argc, FREObject argv[]) { writeLog("addStaticHost called"); if (argc < 2) return nullptr; @@ -402,35 +319,35 @@ static FREObject awesomeUtils_getDeviceUniqueId(FREContext ctx, void *funcData, } static void AneAwesomeUtilsSupportInitializer( - void* extData, - const uint8_t* ctxType, - FREContext ctx, - uint32_t* numFunctionsToSet, - const FRENamedFunction** functionsToSet + void *extData, + const uint8_t *ctxType, + FREContext ctx, + uint32_t *numFunctionsToSet, + const FRENamedFunction **functionsToSet ) { - if(!alreadyInitialized){ + if (!alreadyInitialized) { alreadyInitialized = true; - exportedFunctions[0].name = (const uint8_t*)"awesomeUtils_initialize"; + exportedFunctions[0].name = reinterpret_cast("awesomeUtils_initialize"); exportedFunctions[0].function = awesomeUtils_initialize; - exportedFunctions[1].name = (const uint8_t*)"awesomeUtils_createWebSocket"; + exportedFunctions[1].name = reinterpret_cast("awesomeUtils_createWebSocket"); exportedFunctions[1].function = awesomeUtils_createWebSocket; - exportedFunctions[2].name = (const uint8_t*)"awesomeUtils_sendWebSocketMessage"; + exportedFunctions[2].name = reinterpret_cast("awesomeUtils_sendWebSocketMessage"); exportedFunctions[2].function = awesomeUtils_sendWebSocketMessage; - exportedFunctions[3].name = (const uint8_t*)"awesomeUtils_closeWebSocket"; + exportedFunctions[3].name = reinterpret_cast("awesomeUtils_closeWebSocket"); exportedFunctions[3].function = awesomeUtils_closeWebSocket; - exportedFunctions[4].name = (const uint8_t*)"awesomeUtils_connectWebSocket"; + exportedFunctions[4].name = (const uint8_t *) "awesomeUtils_connectWebSocket"; exportedFunctions[4].function = awesomeUtils_connectWebSocket; - exportedFunctions[5].name = (const uint8_t*)"awesomeUtils_addStaticHost"; + exportedFunctions[5].name = (const uint8_t *) "awesomeUtils_addStaticHost"; exportedFunctions[5].function = awesomeUtils_addStaticHost; - exportedFunctions[6].name = (const uint8_t*)"awesomeUtils_removeStaticHost"; + exportedFunctions[6].name = (const uint8_t *) "awesomeUtils_removeStaticHost"; exportedFunctions[6].function = awesomeUtils_removeStaticHost; - exportedFunctions[7].name = (const uint8_t*)"awesomeUtils_loadUrl"; + exportedFunctions[7].name = (const uint8_t *) "awesomeUtils_loadUrl"; exportedFunctions[7].function = awesomeUtils_loadUrl; - exportedFunctions[8].name = (const uint8_t*)"awesomeUtils_getLoaderResult"; + exportedFunctions[8].name = (const uint8_t *) "awesomeUtils_getLoaderResult"; exportedFunctions[8].function = awesomeUtils_getLoaderResult; - exportedFunctions[9].name = (const uint8_t*)"awesomeUtils_getWebSocketByteArrayMessage"; + exportedFunctions[9].name = (const uint8_t *) "awesomeUtils_getWebSocketByteArrayMessage"; exportedFunctions[9].function = awesomeUtils_getWebSocketByteArrayMessage; - exportedFunctions[10].name = (const uint8_t*)"awesomeUtils_getDeviceUniqueId"; + exportedFunctions[10].name = (const uint8_t *) "awesomeUtils_getDeviceUniqueId"; exportedFunctions[10].function = awesomeUtils_getDeviceUniqueId; context = ctx; } @@ -439,11 +356,9 @@ static void AneAwesomeUtilsSupportInitializer( } static void AneAwesomeUtilsSupportFinalizer(FREContext ctx) { - } extern "C" { - __attribute__ ((visibility ("default"))) void InitExtension(void** extDataToSet, FREContextInitializer* ctxInitializerToSet, FREContextFinalizer* ctxFinalizerToSet) { writeLog("InitExtension called"); diff --git a/AppleNative/AneAwesomeUtils/AneAwesomeUtils/DataArray.h b/AppleNative/AneAwesomeUtils/AneAwesomeUtils/DataArray.h new file mode 100644 index 0000000..adc3f24 --- /dev/null +++ b/AppleNative/AneAwesomeUtils/AneAwesomeUtils/DataArray.h @@ -0,0 +1,14 @@ +// +// Created by User on 11/11/2024. +// + +#ifndef DATAARRAY_H +#define DATAARRAY_H +#include + +struct DataArray +{ + uint8_t* DataPointer; + int Size; +}; +#endif //DATAARRAY_H diff --git a/AppleNative/AneAwesomeUtils/AneAwesomeUtils/WebSocketClient.cpp b/AppleNative/AneAwesomeUtils/AneAwesomeUtils/WebSocketClient.cpp deleted file mode 100644 index aaba20e..0000000 --- a/AppleNative/AneAwesomeUtils/AneAwesomeUtils/WebSocketClient.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include "AneAwesomeUtilsCsharp.h" -#include "WebSocketClient.hpp" -#include "log.hpp" - -WebSocketClient::WebSocketClient(char* guid) { - writeLog("WebSocketClient created"); - m_guidPointer = guid; - writeLog(m_guidPointer); -} - -WebSocketClient::~WebSocketClient() { -} - -void WebSocketClient::connect(const char* uri) { - csharpLibrary_awesomeUtils_connectWebSocket(m_guidPointer, uri); -} - -void WebSocketClient::close(uint32_t closeCode) { - csharpLibrary_awesomeUtils_closeWebSocket(m_guidPointer, static_cast(closeCode)); -} - -void WebSocketClient::sendMessage(uint8_t* bytes, int lenght) { - csharpLibrary_awesomeUtils_sendWebSocketMessage(m_guidPointer, bytes, lenght); -} - -std::optional > WebSocketClient::getNextMessage() { - std::lock_guard guard(m_lock_receive_queue); - if (m_received_message_queue.empty()) { - return std::nullopt; - } - - // Pega a próxima mensagem da fila - std::vector message = std::move(m_received_message_queue.front()); - m_received_message_queue.pop(); - - return message; -} - -void WebSocketClient::enqueueMessage(const std::vector &message) { - std::lock_guard guard(m_lock_receive_queue); - m_received_message_queue.push(message); -} diff --git a/AppleNative/AneAwesomeUtils/AneAwesomeUtils/WebSocketClient.hpp b/AppleNative/AneAwesomeUtils/AneAwesomeUtils/WebSocketClient.hpp deleted file mode 100644 index 839f395..0000000 --- a/AppleNative/AneAwesomeUtils/AneAwesomeUtils/WebSocketClient.hpp +++ /dev/null @@ -1,37 +0,0 @@ -// -// WebSocketClient.hpp -// WebSocketANE -// -// Created by João Vitor Borges on 23/08/24. -// - -#ifndef WebSocketClient_hpp -#define WebSocketClient_hpp - -#include -#include -#include -#include -#include -#include -#include - -class WebSocketClient { -public: - WebSocketClient(char* guid); - - ~WebSocketClient(); - - void connect(const char* uri); - void close(uint32_t closeCode); - void sendMessage(uint8_t* bytes, int lenght); - std::optional> getNextMessage(); - void enqueueMessage(const std::vector& message); - -private: - std::mutex m_lock_receive_queue; - std::queue> m_received_message_queue; - char* m_guidPointer; -}; - -#endif /* WebSocketClient_hpp */