forked from LeenkxTeam/LNXRNT
Kore Update
This commit is contained in:
@ -4,7 +4,12 @@
|
||||
#include "socket_optimization.h"
|
||||
#include <kinc/log.h>
|
||||
#include <string.h>
|
||||
#include <emmintrin.h> // SSE2
|
||||
// SSE2 for x86/x64, SSE2 to NEON translation for ARM64
|
||||
#if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
|
||||
#include <emmintrin.h>
|
||||
#elif defined(__aarch64__) || defined(__arm64__) || defined(_M_ARM64)
|
||||
#include "sse2neon.h"
|
||||
#endif
|
||||
#include <unordered_map>
|
||||
#include <thread>
|
||||
#include <atomic>
|
||||
@ -67,6 +72,7 @@ namespace WebSocketWrapper {
|
||||
|
||||
void initialize() {
|
||||
if (!winsock_initialized.exchange(true)) {
|
||||
#ifdef _WIN32
|
||||
WSADATA wsaData;
|
||||
int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
|
||||
if (result != 0) {
|
||||
@ -77,16 +83,19 @@ namespace WebSocketWrapper {
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "WebSocket client support initialized successfully");
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
void cleanup() {
|
||||
active_websockets.clear();
|
||||
if (winsock_initialized.exchange(false)) {
|
||||
#ifdef _WIN32
|
||||
WSACleanup();
|
||||
#ifdef DEBUG_NETWORK
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "WebSocket cleanup complete");
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@ -347,7 +356,7 @@ namespace WebSocketWrapper {
|
||||
int sock = static_cast<int>(reinterpret_cast<intptr_t>(ws_));
|
||||
int sendResult;
|
||||
|
||||
if (is_ssl_ && ssl_context_initialized_) {
|
||||
if (is_ssl_ && isSSLReady()) {
|
||||
sendResult = webSocketSSLSend(frame.data(), static_cast<int>(frame.size()));
|
||||
} else {
|
||||
sendResult = ::send(sock, reinterpret_cast<const char*>(frame.data()), static_cast<int>(frame.size()), 0);
|
||||
@ -397,7 +406,7 @@ namespace WebSocketWrapper {
|
||||
int sock = static_cast<int>(reinterpret_cast<intptr_t>(ws_));
|
||||
int sendResult;
|
||||
|
||||
if (is_ssl_ && ssl_context_initialized_) {
|
||||
if (is_ssl_ && isSSLReady()) {
|
||||
sendResult = webSocketSSLSend(frame.data(), static_cast<int>(frame.size()));
|
||||
} else {
|
||||
sendResult = ::send(sock, reinterpret_cast<const char*>(frame.data()), static_cast<int>(frame.size()), 0);
|
||||
@ -651,6 +660,7 @@ namespace WebSocketWrapper {
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "WebSocket: webSocketSSLReceive called with bufferSize=%d", bufferSize);
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
if (!ssl_context_initialized_) {
|
||||
#ifdef DEBUG_NETWORK
|
||||
kinc_log(KINC_LOG_LEVEL_ERROR, "WebSocket: SSL not initialized for receive");
|
||||
@ -881,6 +891,32 @@ namespace WebSocketWrapper {
|
||||
ssl_buffer_.clear();
|
||||
return -1;
|
||||
}
|
||||
#else
|
||||
// LibreSSL/OpenSSL implementation for macOS/Linux
|
||||
if (!ssl_initialized_ || !ssl_) {
|
||||
#ifdef DEBUG_NETWORK
|
||||
kinc_log(KINC_LOG_LEVEL_ERROR, "WebSocket: SSL not initialized for receive");
|
||||
#endif
|
||||
return -1;
|
||||
}
|
||||
|
||||
int result = SSL_read(ssl_, buffer, bufferSize);
|
||||
if (result <= 0) {
|
||||
int error = SSL_get_error(ssl_, result);
|
||||
if (error == SSL_ERROR_WANT_READ || error == SSL_ERROR_WANT_WRITE) {
|
||||
return 0; // need more data
|
||||
}
|
||||
#ifdef DEBUG_NETWORK
|
||||
kinc_log(KINC_LOG_LEVEL_ERROR, "WebSocket: SSL_read failed with error %d", error);
|
||||
#endif
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifdef DEBUG_NETWORK
|
||||
kinc_log(KINC_LOG_LEVEL_INFO, "WebSocket: SSL_read returned %d bytes", result);
|
||||
#endif
|
||||
return result;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool WebSocketClient::connectToServer(const std::string& host, int port, const std::string& path) {
|
||||
@ -1010,7 +1046,7 @@ namespace WebSocketWrapper {
|
||||
#endif
|
||||
|
||||
int sendResult;
|
||||
if (is_ssl_ && ssl_context_initialized_) {
|
||||
if (is_ssl_ && isSSLReady()) {
|
||||
sendResult = webSocketSSLSend(handshakeRequest.c_str(), static_cast<int>(handshakeRequest.length()));
|
||||
} else {
|
||||
sendResult = ::send(sock, handshakeRequest.c_str(), static_cast<int>(handshakeRequest.length()), 0);
|
||||
@ -1025,7 +1061,7 @@ namespace WebSocketWrapper {
|
||||
|
||||
char buffer[SocketOptimization::SMALL_BUFFER_SIZE];
|
||||
int bytesReceived;
|
||||
if (is_ssl_ && ssl_context_initialized_) {
|
||||
if (is_ssl_ && isSSLReady()) {
|
||||
bytesReceived = webSocketSSLReceive(buffer, sizeof(buffer) - 1);
|
||||
} else {
|
||||
bytesReceived = recv(sock, buffer, sizeof(buffer) - 1, 0);
|
||||
@ -1158,7 +1194,7 @@ namespace WebSocketWrapper {
|
||||
{
|
||||
std::vector<uint8_t> pongFrame = createPongFrame(payload);
|
||||
int sock = static_cast<int>(reinterpret_cast<intptr_t>(ws_));
|
||||
if (is_ssl_ && ssl_context_initialized_) {
|
||||
if (is_ssl_ && isSSLReady()) {
|
||||
webSocketSSLSend(pongFrame.data(), static_cast<int>(pongFrame.size()));
|
||||
} else {
|
||||
::send(sock, reinterpret_cast<const char*>(pongFrame.data()), static_cast<int>(pongFrame.size()), 0);
|
||||
@ -1230,7 +1266,7 @@ namespace WebSocketWrapper {
|
||||
}
|
||||
|
||||
int bytesReceived;
|
||||
if (is_ssl_ && ssl_context_initialized_) {
|
||||
if (is_ssl_ && isSSLReady()) {
|
||||
bytesReceived = webSocketSSLReceive(reinterpret_cast<char*>(writePtr), static_cast<int>(contiguousSpace));
|
||||
} else {
|
||||
bytesReceived = recv(sock, reinterpret_cast<char*>(writePtr), static_cast<int>(contiguousSpace), 0);
|
||||
@ -1431,7 +1467,7 @@ namespace WebSocketWrapper {
|
||||
int sock = static_cast<int>(reinterpret_cast<intptr_t>(ws_));
|
||||
|
||||
int sendResult;
|
||||
if (is_ssl_ && ssl_context_initialized_) {
|
||||
if (is_ssl_ && isSSLReady()) {
|
||||
sendResult = webSocketSSLSend(pongFrame.data(), static_cast<int>(pongFrame.size()));
|
||||
} else {
|
||||
sendResult = ::send(sock, reinterpret_cast<const char*>(pongFrame.data()), static_cast<int>(pongFrame.size()), 0);
|
||||
@ -1706,18 +1742,7 @@ namespace WebSocketWrapper {
|
||||
}
|
||||
|
||||
int WebSocketClient::webSocketSSLSend(const void* data, int len) {
|
||||
#ifdef WITH_SSL
|
||||
#ifdef _WIN32
|
||||
if (!ssl_context_initialized_) {
|
||||
return ::send(ssl_socket_, reinterpret_cast<const char*>(data), len, 0);
|
||||
}
|
||||
return webSocketSSLSend(reinterpret_cast<const char*>(data), len);
|
||||
#else
|
||||
return ::send(ssl_socket_, reinterpret_cast<const char*>(data), len, 0);
|
||||
#endif
|
||||
#else
|
||||
return ::send(ssl_socket_, reinterpret_cast<const char*>(data), len, 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef WITH_SSL
|
||||
@ -2862,4 +2887,4 @@ void createWebSocketEventClasses(Isolate* isolate, Local<ObjectTemplate>& global
|
||||
global->Set(isolate, "CloseEvent", closeEventTpl);
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user