From 679ee643a77d50a806bf24d69fca34b9c30dcb22 Mon Sep 17 00:00:00 2001 From: Hans Date: Thu, 28 Nov 2024 05:00:04 +0800 Subject: [PATCH] chore: format code --- cpp/helper.h | 31 ++- cpp/react-native-jsi-udp.cpp | 424 ++++++++++++++++------------------- cpp/react-native-jsi-udp.h | 177 ++++++++------- ios/JsiUdp.mm | 45 ++-- 4 files changed, 327 insertions(+), 350 deletions(-) diff --git a/cpp/helper.h b/cpp/helper.h index 86f3a6f..eb8a790 100644 --- a/cpp/helper.h +++ b/cpp/helper.h @@ -1,23 +1,18 @@ #pragma once -#define JSI_HOST_FUNCTION(NAME) \ - facebook::jsi::Value NAME( \ - facebook::jsi::Runtime &runtime, \ - const facebook::jsi::Value &thisValue, \ - const facebook::jsi::Value *arguments, \ - size_t count \ - ) +#define JSI_HOST_FUNCTION(NAME) \ + facebook::jsi::Value NAME( \ + facebook::jsi::Runtime &runtime, const facebook::jsi::Value &thisValue, \ + const facebook::jsi::Value *arguments, size_t count) -#define EXPOSE_FN(RUNTIME, NAME, ARGC, FUNCTION) \ - { \ - auto NAME = Function::createFromHostFunction( \ - (RUNTIME), \ - facebook::jsi::PropNameID::forAscii((RUNTIME), #NAME), \ - ARGC, \ - FUNCTION \ - ); \ - (RUNTIME).global().setProperty((RUNTIME), #NAME, std::move(NAME)); \ +#define EXPOSE_FN(RUNTIME, NAME, ARGC, FUNCTION) \ + { \ + auto NAME = Function::createFromHostFunction( \ + (RUNTIME), facebook::jsi::PropNameID::forAscii((RUNTIME), #NAME), \ + ARGC, FUNCTION); \ + (RUNTIME).global().setProperty((RUNTIME), #NAME, std::move(NAME)); \ } -#define BIND_METHOD(METHOD) \ - std::bind(&METHOD, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4) +#define BIND_METHOD(METHOD) \ + std::bind(&METHOD, this, std::placeholders::_1, std::placeholders::_2, \ + std::placeholders::_3, std::placeholders::_4) diff --git a/cpp/react-native-jsi-udp.cpp b/cpp/react-native-jsi-udp.cpp index fcddf21..2936653 100644 --- a/cpp/react-native-jsi-udp.cpp +++ b/cpp/react-native-jsi-udp.cpp @@ -1,20 +1,20 @@ #include "react-native-jsi-udp.h" #include "helper.h" +#include #include -#include -#include -#include #include #include -#include #include -#include -#include +#include +#include +#include #include #include -#include -#include +#include +#include +#include #include +#include #if __APPLE__ @@ -24,8 +24,8 @@ #endif #ifndef IPV6_ADD_MEMBERSHIP -#define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP -#define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP +#define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP +#define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP #endif #define MAX_PACK_SIZE 65535 @@ -37,70 +37,70 @@ namespace jsiudp { std::string error_name(int err) { switch (err) { - case EACCES: - return "EACCES"; - case EADDRINUSE: - return "EADDRINUSE"; - case EADDRNOTAVAIL: - return "EADDRNOTAVAIL"; - case EAFNOSUPPORT: - return "EAFNOSUPPORT"; - case EAGAIN: - return "EAGAIN"; - case EALREADY: - return "EALREADY"; - case EBADF: - return "EBADF"; - case ECONNREFUSED: - return "ECONNREFUSED"; - case EFAULT: - return "EFAULT"; - case EINPROGRESS: - return "EINPROGRESS"; - case EINTR: - return "EINTR"; - case EISCONN: - return "EISCONN"; - case ENETUNREACH: - return "ENETUNREACH"; - case ENOTSOCK: - return "ENOTSOCK"; - case ETIMEDOUT: - return "ETIMEDOUT"; - case ENOPROTOOPT: - return "ENOPROTOOPT"; - case EINVAL: - return "EINVAL"; - case EDOM: - return "EDOM"; - case ENOMEM: - return "ENOMEM"; - case ENOBUFS: - return "ENOBUFS"; - case EOPNOTSUPP: - return "EOPNOTSUPP"; - case ENETDOWN: - return "ENETDOWN"; - case ECONNABORTED: - return "ECONNABORTED"; - case ECONNRESET: - return "ECONNRESET"; - case ENOTCONN: - return "ENOTCONN"; - case EHOSTUNREACH: - return "EHOSTUNREACH"; - case EPERM: - return "EPERM"; - case EPIPE: - return "EPIPE"; - default: - LOGE("unknown error %d", err); - return "UNKNOWN"; + case EACCES: + return "EACCES"; + case EADDRINUSE: + return "EADDRINUSE"; + case EADDRNOTAVAIL: + return "EADDRNOTAVAIL"; + case EAFNOSUPPORT: + return "EAFNOSUPPORT"; + case EAGAIN: + return "EAGAIN"; + case EALREADY: + return "EALREADY"; + case EBADF: + return "EBADF"; + case ECONNREFUSED: + return "ECONNREFUSED"; + case EFAULT: + return "EFAULT"; + case EINPROGRESS: + return "EINPROGRESS"; + case EINTR: + return "EINTR"; + case EISCONN: + return "EISCONN"; + case ENETUNREACH: + return "ENETUNREACH"; + case ENOTSOCK: + return "ENOTSOCK"; + case ETIMEDOUT: + return "ETIMEDOUT"; + case ENOPROTOOPT: + return "ENOPROTOOPT"; + case EINVAL: + return "EINVAL"; + case EDOM: + return "EDOM"; + case ENOMEM: + return "ENOMEM"; + case ENOBUFS: + return "ENOBUFS"; + case EOPNOTSUPP: + return "EOPNOTSUPP"; + case ENETDOWN: + return "ENETDOWN"; + case ECONNABORTED: + return "ECONNABORTED"; + case ECONNRESET: + return "ECONNRESET"; + case ENOTCONN: + return "ENOTCONN"; + case EHOSTUNREACH: + return "EHOSTUNREACH"; + case EPERM: + return "EPERM"; + case EPIPE: + return "EPIPE"; + default: + LOGE("unknown error %d", err); + return "UNKNOWN"; } } int setupIface(int fd, struct sockaddr_in &addr) { - #if __APPLE__ +#if __APPLE__ struct ifaddrs *ifaddr, *ifa; if (getifaddrs(&ifaddr) == -1) { return -1; @@ -108,16 +108,11 @@ int setupIface(int fd, struct sockaddr_in &addr) { auto isAny = addr.sin_addr.s_addr == INADDR_ANY; auto isLoopback = addr.sin_addr.s_addr == htonl(INADDR_LOOPBACK); for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { - if ( - ifa->ifa_addr != NULL && - ifa->ifa_addr->sa_family == AF_INET && - !((ifa->ifa_flags & IFF_LOOPBACK) ^ isLoopback) && - (ifa->ifa_flags & IFF_UP) && - ( - isAny || - reinterpret_cast(ifa->ifa_addr)->sin_addr.s_addr == addr.sin_addr.s_addr - ) - ) { + if (ifa->ifa_addr != NULL && ifa->ifa_addr->sa_family == AF_INET && + !((ifa->ifa_flags & IFF_LOOPBACK) ^ isLoopback) && + (ifa->ifa_flags & IFF_UP) && + (isAny || reinterpret_cast(ifa->ifa_addr) + ->sin_addr.s_addr == addr.sin_addr.s_addr)) { auto index = if_nametoindex(ifa->ifa_name); if (setsockopt(fd, IPPROTO_IP, IP_BOUND_IF, &index, sizeof(index)) != 0) { return -1; @@ -127,12 +122,12 @@ int setupIface(int fd, struct sockaddr_in &addr) { } } freeifaddrs(ifaddr); - #endif +#endif return 0; } int setupIface(int fd, struct sockaddr_in6 &addr) { - #if __APPLE__ +#if __APPLE__ struct ifaddrs *ifaddr, *ifa; if (getifaddrs(&ifaddr) == -1) { return -1; @@ -141,22 +136,17 @@ int setupIface(int fd, struct sockaddr_in6 &addr) { auto isAny = memcmp(&(addr.sin6_addr), &in6addr_any, size) == 0; auto isLoopback = memcmp(&(addr.sin6_addr), &in6addr_loopback, size) == 0; for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { - if ( - ifa->ifa_addr != NULL && - ifa->ifa_addr->sa_family == AF_INET6 && - !((ifa->ifa_flags & IFF_LOOPBACK) ^ isLoopback) && - (ifa->ifa_flags & IFF_UP) && - ( - isAny || - memcmp( - &(addr.sin6_addr), - &(reinterpret_cast(ifa->ifa_addr)->sin6_addr), - size - ) == 0 - ) - ) { + if (ifa->ifa_addr != NULL && ifa->ifa_addr->sa_family == AF_INET6 && + !((ifa->ifa_flags & IFF_LOOPBACK) ^ isLoopback) && + (ifa->ifa_flags & IFF_UP) && + (isAny || + memcmp(&(addr.sin6_addr), + &(reinterpret_cast(ifa->ifa_addr) + ->sin6_addr), + size) == 0)) { auto index = if_nametoindex(ifa->ifa_name); - if (setsockopt(fd, IPPROTO_IPV6, IPV6_BOUND_IF, &index, sizeof(index)) != 0) { + if (setsockopt(fd, IPPROTO_IPV6, IPV6_BOUND_IF, &index, sizeof(index)) != + 0) { return -1; } LOGI("bound to %s for %d", ifa->ifa_name, fd); @@ -164,11 +154,13 @@ int setupIface(int fd, struct sockaddr_in6 &addr) { } } freeifaddrs(ifaddr); - #endif +#endif return 0; } -UdpManager::UdpManager(Runtime *jsiRuntime, std::shared_ptr callInvoker): _runtime(jsiRuntime), _callInvoker(callInvoker) { +UdpManager::UdpManager(Runtime *jsiRuntime, + std::shared_ptr callInvoker) + : _runtime(jsiRuntime), _callInvoker(callInvoker) { eventThread = std::thread(&UdpManager::receiveEvent, this); EXPOSE_FN(*_runtime, datagram_create, 1, BIND_METHOD(UdpManager::create)); @@ -177,21 +169,30 @@ UdpManager::UdpManager(Runtime *jsiRuntime, std::shared_ptr callInv EXPOSE_FN(*_runtime, datagram_close, 1, BIND_METHOD(UdpManager::close)); EXPOSE_FN(*_runtime, datagram_getOpt, 3, BIND_METHOD(UdpManager::getOpt)); EXPOSE_FN(*_runtime, datagram_setOpt, 5, BIND_METHOD(UdpManager::setOpt)); - EXPOSE_FN(*_runtime, datagram_getSockName, 2, BIND_METHOD(UdpManager::getSockName)); + EXPOSE_FN(*_runtime, datagram_getSockName, 2, + BIND_METHOD(UdpManager::getSockName)); auto global = _runtime->global(); global.setProperty(*_runtime, "dgc_SOL_SOCKET", static_cast(SOL_SOCKET)); global.setProperty(*_runtime, "dgc_IPPROTO_IP", static_cast(IPPROTO_IP)); - global.setProperty(*_runtime, "dgc_IPPROTO_IPV6", static_cast(IPPROTO_IPV6)); - global.setProperty(*_runtime, "dgc_SO_REUSEADDR", static_cast(SO_REUSEADDR)); - global.setProperty(*_runtime, "dgc_SO_REUSEPORT", static_cast(SO_REUSEPORT)); - global.setProperty(*_runtime, "dgc_SO_BROADCAST", static_cast(SO_BROADCAST)); + global.setProperty(*_runtime, "dgc_IPPROTO_IPV6", + static_cast(IPPROTO_IPV6)); + global.setProperty(*_runtime, "dgc_SO_REUSEADDR", + static_cast(SO_REUSEADDR)); + global.setProperty(*_runtime, "dgc_SO_REUSEPORT", + static_cast(SO_REUSEPORT)); + global.setProperty(*_runtime, "dgc_SO_BROADCAST", + static_cast(SO_BROADCAST)); global.setProperty(*_runtime, "dgc_SO_RCVBUF", static_cast(SO_RCVBUF)); global.setProperty(*_runtime, "dgc_SO_SNDBUF", static_cast(SO_SNDBUF)); - global.setProperty(*_runtime, "dgc_IP_MULTICAST_TTL", static_cast(IP_MULTICAST_TTL)); - global.setProperty(*_runtime, "dgc_IP_MULTICAST_LOOP", static_cast(IP_MULTICAST_LOOP)); - global.setProperty(*_runtime, "dgc_IP_ADD_MEMBERSHIP", static_cast(IP_ADD_MEMBERSHIP)); - global.setProperty(*_runtime, "dgc_IP_DROP_MEMBERSHIP", static_cast(IP_DROP_MEMBERSHIP)); + global.setProperty(*_runtime, "dgc_IP_MULTICAST_TTL", + static_cast(IP_MULTICAST_TTL)); + global.setProperty(*_runtime, "dgc_IP_MULTICAST_LOOP", + static_cast(IP_MULTICAST_LOOP)); + global.setProperty(*_runtime, "dgc_IP_ADD_MEMBERSHIP", + static_cast(IP_ADD_MEMBERSHIP)); + global.setProperty(*_runtime, "dgc_IP_DROP_MEMBERSHIP", + static_cast(IP_DROP_MEMBERSHIP)); global.setProperty(*_runtime, "dgc_IP_TTL", static_cast(IP_TTL)); global.setProperty(*_runtime, "datagram_callbacks", Object(*_runtime)); @@ -202,11 +203,13 @@ UdpManager::~UdpManager() { _invalidate = true; fdQueueCond.notify_all(); cond.notify_all(); - if (eventThread.joinable()) eventThread.join(); + if (eventThread.joinable()) + eventThread.join(); for (auto &worker : workerPool) { - if (worker.joinable()) worker.join(); + if (worker.joinable()) + worker.join(); } - for (const auto& [id, fd] : idToFdMap) { + for (const auto &[id, fd] : idToFdMap) { ::close(fd); } } @@ -221,7 +224,8 @@ void UdpManager::createWorker() { char buffer[MAX_PACK_SIZE]; while (!_invalidate) { std::unique_lock lock(fdQueueMutex); - fdQueueCond.wait(lock, [this] { return !fdQueue.empty() || _invalidate; }); + fdQueueCond.wait(lock, + [this] { return !fdQueue.empty() || _invalidate; }); if (_invalidate) { break; } @@ -230,21 +234,17 @@ void UdpManager::createWorker() { lock.unlock(); struct sockaddr_in in_addr; socklen_t in_len = sizeof(in_addr); - auto recvn = recvfrom(fd, buffer, sizeof(buffer), 0, (struct sockaddr*)&in_addr, &in_len); + auto recvn = recvfrom(fd, buffer, sizeof(buffer), 0, + (struct sockaddr *)&in_addr, &in_len); if (recvn < 0) { if (errno != EAGAIN && errno != EWOULDBLOCK) { - sendEvent({ fd, ERROR, error_name(errno), 0, "", 0 }); + sendEvent({fd, ERROR, error_name(errno), 0, "", 0}); continue; } } else { - sendEvent({ - fd, - MESSAGE, - std::string(buffer, recvn), - in_addr.sin_family, - inet_ntoa(in_addr.sin_addr), - ntohs(in_addr.sin_port) - }); + sendEvent({fd, MESSAGE, std::string(buffer, recvn), + in_addr.sin_family, inet_ntoa(in_addr.sin_addr), + ntohs(in_addr.sin_port)}); } emplaceFd(fd); } @@ -253,7 +253,8 @@ void UdpManager::createWorker() { } void UdpManager::emplaceFd(int fd) { - if (_invalidate) return; + if (_invalidate) + return; std::lock_guard lock(fdQueueMutex); fdQueue.emplace(fd); fdQueueCond.notify_one(); @@ -272,7 +273,7 @@ void UdpManager::removeFd(int fd) { } void UdpManager::closeAll() { - for (const auto& [id, fd] : idToFdMap) { + for (const auto &[id, fd] : idToFdMap) { removeFd(fd); ::close(fd); } @@ -327,7 +328,8 @@ JSI_HOST_FUNCTION(UdpManager::bind) { if (setupIface(fd, addr) != 0) { throw JSError(runtime, error_name(errno)); } - ret = ::bind(fd, reinterpret_cast(&addr), sizeof(addr)); + ret = + ::bind(fd, reinterpret_cast(&addr), sizeof(addr)); } } else { struct sockaddr_in6 addr; @@ -338,7 +340,8 @@ JSI_HOST_FUNCTION(UdpManager::bind) { if (setupIface(fd, addr) != 0) { throw JSError(runtime, error_name(errno)); } - ret = ::bind(fd, reinterpret_cast(&addr), sizeof(addr)); + ret = + ::bind(fd, reinterpret_cast(&addr), sizeof(addr)); } } @@ -382,7 +385,8 @@ JSI_HOST_FUNCTION(UdpManager::setOpt) { case IP_ADD_MEMBERSHIP: case IP_DROP_MEMBERSHIP: { struct ip_mreq mreq; - mreq.imr_multiaddr.s_addr = inet_addr(arguments[3].asString(runtime).utf8(runtime).c_str()); + mreq.imr_multiaddr.s_addr = + inet_addr(arguments[3].asString(runtime).utf8(runtime).c_str()); if (arguments[4].isString()) { auto value = arguments[4].asString(runtime).utf8(runtime); mreq.imr_interface.s_addr = inet_addr(value.c_str()); @@ -468,14 +472,8 @@ JSI_HOST_FUNCTION(UdpManager::send) { addr.sin_port = htons(port); ret = inet_pton(AF_INET, host.c_str(), &(addr.sin_addr)); if (ret == 1) { - ret = sendto( - fd, - data.data(runtime), - data.size(runtime), - MSG_DONTWAIT, - reinterpret_cast(&addr), - sizeof(addr) - ); + ret = sendto(fd, data.data(runtime), data.size(runtime), MSG_DONTWAIT, + reinterpret_cast(&addr), sizeof(addr)); } } else { struct sockaddr_in6 addr; @@ -483,14 +481,8 @@ JSI_HOST_FUNCTION(UdpManager::send) { addr.sin6_port = htons(port); ret = inet_pton(AF_INET6, host.c_str(), &(addr.sin6_addr)); if (ret == 1) { - ret = sendto( - fd, - data.data(runtime), - data.size(runtime), - MSG_DONTWAIT, - reinterpret_cast(&addr), - sizeof(addr) - ); + ret = sendto(fd, data.data(runtime), data.size(runtime), MSG_DONTWAIT, + reinterpret_cast(&addr), sizeof(addr)); } } @@ -516,21 +508,11 @@ JSI_HOST_FUNCTION(UdpManager::getSockName) { } auto host = inet_ntoa(addr.sin_addr); auto port = ntohs(addr.sin_port); - result.setProperty( - runtime, - "address", - String::createFromAscii(runtime, host) - ); - result.setProperty( - runtime, - "port", - static_cast(port) - ); - result.setProperty( - runtime, - "family", - String::createFromAscii(runtime, "IPv4") - ); + result.setProperty(runtime, "address", + String::createFromAscii(runtime, host)); + result.setProperty(runtime, "port", static_cast(port)); + result.setProperty(runtime, "family", + String::createFromAscii(runtime, "IPv4")); } else { struct sockaddr_in6 addr; socklen_t len = sizeof(addr); @@ -541,21 +523,11 @@ JSI_HOST_FUNCTION(UdpManager::getSockName) { char host[INET6_ADDRSTRLEN]; inet_ntop(AF_INET6, &addr.sin6_addr, host, INET6_ADDRSTRLEN); auto port = ntohs(addr.sin6_port); - result.setProperty( - runtime, - "address", - String::createFromAscii(runtime, host) - ); - result.setProperty( - runtime, - "port", - static_cast(port) - ); - result.setProperty( - runtime, - "family", - String::createFromAscii(runtime, "IPv6") - ); + result.setProperty(runtime, "address", + String::createFromAscii(runtime, host)); + result.setProperty(runtime, "port", static_cast(port)); + result.setProperty(runtime, "family", + String::createFromAscii(runtime, "IPv6")); } return result; } @@ -570,55 +542,50 @@ void UdpManager::receiveEvent() { auto event = events.front(); events.pop(); lock.unlock(); - int id = std::find_if(idToFdMap.begin(), idToFdMap.end(), [&event](const auto& pair) { - return pair.second == event.fd; - })->first; + int id = std::find_if( + idToFdMap.begin(), idToFdMap.end(), + [&event](const auto &pair) { return pair.second == event.fd; }) + ->first; runOnJS([this, id, event = std::move(event)]() { try { - auto callback = _runtime->global() - .getPropertyAsObject(*_runtime, "datagram_callbacks") - .getPropertyAsFunction(*_runtime, std::to_string(id).c_str()); + auto callback = + _runtime->global() + .getPropertyAsObject(*_runtime, "datagram_callbacks") + .getPropertyAsFunction(*_runtime, std::to_string(id).c_str()); auto eventObj = Object(*_runtime); - eventObj.setProperty( - *_runtime, - "type", - String::createFromAscii( - *_runtime, - event.type == MESSAGE ? "message" : event.type == ERROR ? "error" : "close" - ) - ); + eventObj.setProperty(*_runtime, "type", + String::createFromAscii( + *_runtime, event.type == MESSAGE ? "message" + : event.type == ERROR ? "error" + : "close")); if (event.type == MESSAGE) { - auto ArrayBuffer = _runtime->global().getPropertyAsFunction(*_runtime, "ArrayBuffer"); - auto arrayBufferObj = ArrayBuffer - .callAsConstructor(*_runtime, static_cast(event.data.size())) - .getObject(*_runtime); + auto ArrayBuffer = _runtime->global().getPropertyAsFunction( + *_runtime, "ArrayBuffer"); + auto arrayBufferObj = + ArrayBuffer + .callAsConstructor(*_runtime, + static_cast(event.data.size())) + .getObject(*_runtime); auto arrayBuffer = arrayBufferObj.getArrayBuffer(*_runtime); - memcpy(arrayBuffer.data(*_runtime), event.data.c_str(), event.data.size()); - eventObj.setProperty( - *_runtime, - "data", - std::move(arrayBuffer) - ); - eventObj.setProperty( - *_runtime, - "family", - String::createFromAscii(*_runtime, event.family == AF_INET ? "IPv4" : "IPv6") - ); + memcpy(arrayBuffer.data(*_runtime), event.data.c_str(), + event.data.size()); + eventObj.setProperty(*_runtime, "data", std::move(arrayBuffer)); eventObj.setProperty( - *_runtime, - "address", - String::createFromAscii(*_runtime, event.address) - ); + *_runtime, "family", + String::createFromAscii( + *_runtime, event.family == AF_INET ? "IPv4" : "IPv6")); eventObj.setProperty( - *_runtime, - "port", - static_cast(event.port) - ); + *_runtime, "address", + String::createFromAscii(*_runtime, event.address)); + eventObj.setProperty(*_runtime, "port", static_cast(event.port)); } else if (event.type == ERROR) { - auto Error = _runtime->global().getPropertyAsFunction(*_runtime, "Error"); - auto errorObj = Error - .callAsConstructor(*_runtime, String::createFromAscii(*_runtime, event.data)) - .getObject(*_runtime); + auto Error = + _runtime->global().getPropertyAsFunction(*_runtime, "Error"); + auto errorObj = + Error + .callAsConstructor( + *_runtime, String::createFromAscii(*_runtime, event.data)) + .getObject(*_runtime); eventObj.setProperty(*_runtime, "error", errorObj); } callback.call(*_runtime, eventObj); @@ -630,7 +597,8 @@ void UdpManager::receiveEvent() { } void UdpManager::sendEvent(Event event) { - if (_invalidate) return; + if (_invalidate) + return; std::lock_guard lock(mutex); events.push(event); cond.notify_one(); @@ -643,7 +611,7 @@ void UdpManager::suspendAll() { fdQueue.pop(); } - for (const auto& [id, fd] : idToFdMap) { + for (const auto &[id, fd] : idToFdMap) { SocketState state; state.id = id; @@ -653,7 +621,7 @@ void UdpManager::suspendAll() { state.address = inet_ntoa(addr.sin_addr); state.port = ntohs(addr.sin_port); state.type = addr.sin_family == AF_INET ? 4 : 6; - + int value; socklen_t optlen = sizeof(value); if (getsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &value, &optlen) == 0) { @@ -671,7 +639,7 @@ void UdpManager::suspendAll() { removeFd(fd); ::close(fd); } - + idToFdMap.clear(); cond.notify_all(); } @@ -679,13 +647,13 @@ void UdpManager::suspendAll() { void UdpManager::resumeAll() { std::lock_guard lock(mutex); - for (const auto& state : suspendedSockets) { + for (const auto &state : suspendedSockets) { auto newFd = socket(state.type == 4 ? AF_INET : AF_INET6, SOCK_DGRAM, 0); if (newFd <= 0) { sendEvent({ - .fd = state.id, - .type = ERROR, - .data = error_name(errno), + .fd = state.id, + .type = ERROR, + .data = error_name(errno), }); continue; } @@ -708,9 +676,10 @@ void UdpManager::resumeAll() { addr.sin_family = AF_INET; addr.sin_port = htons(state.port); inet_pton(AF_INET, state.address.c_str(), &(addr.sin_addr)); - - if (setupIface(newFd, addr) == 0 && - ::bind(newFd, reinterpret_cast(&addr), sizeof(addr)) == 0) { + + if (setupIface(newFd, addr) == 0 && + ::bind(newFd, reinterpret_cast(&addr), + sizeof(addr)) == 0) { idToFdMap[state.id] = newFd; emplaceFd(newFd); } else { @@ -722,8 +691,9 @@ void UdpManager::resumeAll() { addr.sin6_port = htons(state.port); inet_pton(AF_INET6, state.address.c_str(), &(addr.sin6_addr)); - if (setupIface(newFd, addr) == 0 && - ::bind(newFd, reinterpret_cast(&addr), sizeof(addr)) == 0) { + if (setupIface(newFd, addr) == 0 && + ::bind(newFd, reinterpret_cast(&addr), + sizeof(addr)) == 0) { idToFdMap[state.id] = newFd; emplaceFd(newFd); } else { diff --git a/cpp/react-native-jsi-udp.h b/cpp/react-native-jsi-udp.h index 43e5f61..3f3d02e 100644 --- a/cpp/react-native-jsi-udp.h +++ b/cpp/react-native-jsi-udp.h @@ -1,24 +1,36 @@ #pragma once -#include +#include "helper.h" #include +#include +#include +#include +#include +#include #include +#include +#include #include #include -#include -#include #include -#include -#include -#include -#include -#include "helper.h" #if __APPLE__ -#define LOGI(...) printf("[JsiUdp] INFO: "); printf(__VA_ARGS__); printf("\n") -#define LOGD(...) printf("[JsiUdp] DEBUG: "); printf(__VA_ARGS__); printf("\n") -#define LOGW(...) printf("[JsiUdp] WARN: "); printf(__VA_ARGS__); printf("\n") -#define LOGE(...) printf("[JsiUdp] ERROR: "); printf(__VA_ARGS__); printf("\n") +#define LOGI(...) \ + printf("[JsiUdp] INFO: "); \ + printf(__VA_ARGS__); \ + printf("\n") +#define LOGD(...) \ + printf("[JsiUdp] DEBUG: "); \ + printf(__VA_ARGS__); \ + printf("\n") +#define LOGW(...) \ + printf("[JsiUdp] WARN: "); \ + printf(__VA_ARGS__); \ + printf("\n") +#define LOGE(...) \ + printf("[JsiUdp] ERROR: "); \ + printf(__VA_ARGS__); \ + printf("\n") #else @@ -32,75 +44,72 @@ #endif namespace jsiudp { - enum EventType { - MESSAGE, - ERROR, - CLOSE - }; - - struct Event { - int fd; - EventType type; - std::string data; - int family; - std::string address; - int port; - }; - - struct SocketState { - int id; - std::string address; - int port; - int type; - bool reuseAddr; - bool reusePort; - bool broadcast; - }; - - class UdpManager { - public: - UdpManager(facebook::jsi::Runtime *jsiRuntime, std::shared_ptr callInvoker); - ~UdpManager(); - - void closeAll(); - void suspendAll(); - void resumeAll(); - - protected: - facebook::jsi::Runtime *_runtime; - std::shared_ptr _callInvoker; - std::atomic _invalidate = false; - std::thread eventThread; - - JSI_HOST_FUNCTION(create); - JSI_HOST_FUNCTION(send); - JSI_HOST_FUNCTION(bind); - JSI_HOST_FUNCTION(setOpt); - JSI_HOST_FUNCTION(getOpt); - JSI_HOST_FUNCTION(close); - JSI_HOST_FUNCTION(getSockName); - - void runOnJS(std::function &&f); - - void sendEvent(Event event); - void receiveEvent(); - - // receive worker - void emplaceFd(int fd); - void removeFd(int fd); - void createWorker(); - - private: - std::condition_variable cond; - std::mutex mutex; - std::queue events; - std::vector workerPool; - std::queue fdQueue; - std::mutex fdQueueMutex; - std::condition_variable fdQueueCond; - std::map idToFdMap; - int nextId = 1; - - std::vector suspendedSockets; - }; -} +enum EventType { MESSAGE, ERROR, CLOSE }; + +struct Event { + int fd; + EventType type; + std::string data; + int family; + std::string address; + int port; +}; + +struct SocketState { + int id; + std::string address; + int port; + int type; + bool reuseAddr; + bool reusePort; + bool broadcast; +}; + +class UdpManager { +public: + UdpManager(facebook::jsi::Runtime *jsiRuntime, + std::shared_ptr callInvoker); + ~UdpManager(); + + void closeAll(); + void suspendAll(); + void resumeAll(); + +protected: + facebook::jsi::Runtime *_runtime; + std::shared_ptr _callInvoker; + std::atomic _invalidate = false; + std::thread eventThread; + + JSI_HOST_FUNCTION(create); + JSI_HOST_FUNCTION(send); + JSI_HOST_FUNCTION(bind); + JSI_HOST_FUNCTION(setOpt); + JSI_HOST_FUNCTION(getOpt); + JSI_HOST_FUNCTION(close); + JSI_HOST_FUNCTION(getSockName); + + void runOnJS(std::function &&f); + + void sendEvent(Event event); + void receiveEvent(); + + // receive worker + void emplaceFd(int fd); + void removeFd(int fd); + void createWorker(); + +private: + std::condition_variable cond; + std::mutex mutex; + std::queue events; + std::vector workerPool; + std::queue fdQueue; + std::mutex fdQueueMutex; + std::condition_variable fdQueueCond; + std::map idToFdMap; + int nextId = 1; + + std::vector suspendedSockets; +}; +} // namespace jsiudp diff --git a/ios/JsiUdp.mm b/ios/JsiUdp.mm index 14f2ac8..28a53b6 100644 --- a/ios/JsiUdp.mm +++ b/ios/JsiUdp.mm @@ -27,18 +27,17 @@ + (BOOL)requiresMainQueueSetup { return YES; } -void installApi( - std::shared_ptr callInvoker, - facebook::jsi::Runtime *runtime -) { - _manager = std::make_shared(runtime, std::move(callInvoker)); +void installApi(std::shared_ptr callInvoker, + facebook::jsi::Runtime *runtime) { + _manager = + std::make_shared(runtime, std::move(callInvoker)); } -RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(install) -{ +RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(install) { RCTCxxBridge *cxxBridge = (RCTCxxBridge *)_bridge; if (cxxBridge.runtime != nullptr) { - installApi(cxxBridge.jsCallInvoker, (facebook::jsi::Runtime *)cxxBridge.runtime); + installApi(cxxBridge.jsCallInvoker, + (facebook::jsi::Runtime *)cxxBridge.runtime); return @(true); } return @(false); @@ -47,10 +46,10 @@ void installApi( // Don't compile this code when we build for the old architecture. #ifdef RCT_NEW_ARCH_ENABLED - (std::shared_ptr)getTurboModule: - (const facebook::react::ObjCTurboModule::InitParams &)params -{ + (const facebook::react::ObjCTurboModule::InitParams &)params { RCTCxxBridge *cxxBridge = (RCTCxxBridge *)_bridge; - installApi(cxxBridge.jsCallInvoker, (facebook::jsi::Runtime *)cxxBridge.runtime); + installApi(cxxBridge.jsCallInvoker, + (facebook::jsi::Runtime *)cxxBridge.runtime); return std::make_shared(params); } @@ -62,24 +61,28 @@ - (void)dealloc { - (instancetype)init { if (self = [super init]) { - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(handleAppStateChange:) - name:UIApplicationWillResignActiveNotification - object:nil]; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(handleAppStateChange:) - name:UIApplicationDidBecomeActiveNotification - object:nil]; + [[NSNotificationCenter defaultCenter] + addObserver:self + selector:@selector(handleAppStateChange:) + name:UIApplicationWillResignActiveNotification + object:nil]; + [[NSNotificationCenter defaultCenter] + addObserver:self + selector:@selector(handleAppStateChange:) + name:UIApplicationDidBecomeActiveNotification + object:nil]; } return self; } - (void)handleAppStateChange:(NSNotification *)notification { - if ([notification.name isEqualToString:UIApplicationWillResignActiveNotification]) { + if ([notification.name + isEqualToString:UIApplicationWillResignActiveNotification]) { if (_manager) { _manager->suspendAll(); } - } else if ([notification.name isEqualToString:UIApplicationDidBecomeActiveNotification]) { + } else if ([notification.name + isEqualToString:UIApplicationDidBecomeActiveNotification]) { if (_manager) { _manager->resumeAll(); }