From 8a7b3f3263b65687c279a2f932272bc7fad1f111 Mon Sep 17 00:00:00 2001 From: Hans Date: Tue, 12 Sep 2023 16:50:07 +0800 Subject: [PATCH] fix: move constants to native, avoid platform specific value --- cpp/react-native-jsi-udp.cpp | 18 ++++++++++++ src/index.tsx | 55 +++++++++++++----------------------- src/types.d.ts | 14 +++++++++ 3 files changed, 51 insertions(+), 36 deletions(-) diff --git a/cpp/react-native-jsi-udp.cpp b/cpp/react-native-jsi-udp.cpp index 2feee9a..63083ec 100644 --- a/cpp/react-native-jsi-udp.cpp +++ b/cpp/react-native-jsi-udp.cpp @@ -172,6 +172,24 @@ UdpManager::UdpManager(Runtime &jsiRuntime, RunOnJS runOnJS) : _runtime(jsiRunti EXPOSE_FN(jsiRuntime, datagram_getOpt, 3, BIND_METHOD(UdpManager::getOpt)); EXPOSE_FN(jsiRuntime, datagram_setOpt, 5, BIND_METHOD(UdpManager::setOpt)); EXPOSE_FN(jsiRuntime, datagram_getSockName, 2, BIND_METHOD(UdpManager::getSockName)); + + { + auto global = jsiRuntime.global(); + + global.setProperty(jsiRuntime, "dgc_SOL_SOCKET", static_cast(SOL_SOCKET)); + global.setProperty(jsiRuntime, "dgc_IPPROTO_IP", static_cast(IPPROTO_IP)); + global.setProperty(jsiRuntime, "dgc_IPPROTO_IPV6", static_cast(IPPROTO_IPV6)); + global.setProperty(jsiRuntime, "dgc_SO_REUSEADDR", static_cast(SO_REUSEADDR)); + global.setProperty(jsiRuntime, "dgc_SO_REUSEPORT", static_cast(SO_REUSEPORT)); + global.setProperty(jsiRuntime, "dgc_SO_BROADCAST", static_cast(SO_BROADCAST)); + global.setProperty(jsiRuntime, "dgc_SO_RCVBUF", static_cast(SO_RCVBUF)); + global.setProperty(jsiRuntime, "dgc_SO_SNDBUF", static_cast(SO_SNDBUF)); + global.setProperty(jsiRuntime, "dgc_IP_MULTICAST_TTL", static_cast(IP_MULTICAST_TTL)); + global.setProperty(jsiRuntime, "dgc_IP_MULTICAST_LOOP", static_cast(IP_MULTICAST_LOOP)); + global.setProperty(jsiRuntime, "dgc_IP_ADD_MEMBERSHIP", static_cast(IP_ADD_MEMBERSHIP)); + global.setProperty(jsiRuntime, "dgc_IP_DROP_MEMBERSHIP", static_cast(IP_DROP_MEMBERSHIP)); + global.setProperty(jsiRuntime, "dgc_IP_TTL", static_cast(IP_TTL)); + } } UdpManager::~UdpManager() { diff --git a/src/index.tsx b/src/index.tsx index 434ef13..a17ade4 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -33,23 +33,6 @@ export enum State { export type Callback = (...args: any[]) => void; -// Level -const SOL_SOCKET = 0xffff; -const IPPROTO_IP = 0; -const IPPROTO_IPV6 = 0x29; - -// Option -const SO_REUSEADDR = 0x4; -const SO_REUSEPORT = 0x200; -const SO_BROADCAST = 0x20; -const SO_RCVBUF = 0x1002; -const SO_SNDBUF = 0x1001; -const IP_MULTICAST_TTL = 0xa; -const IP_MULTICAST_LOOP = 0xb; -const IP_ADD_MEMBERSHIP = 0xc; -const IP_DROP_MEMBERSHIP = 0xd; -const IP_TTL = 0x4; - export class Socket extends EventEmitter { private state: State; private type: 4 | 6; @@ -83,14 +66,14 @@ export class Socket extends EventEmitter { try { datagram_setOpt( this._fd, - SOL_SOCKET, - SO_REUSEADDR, + dgc_SOL_SOCKET, + dgc_SO_REUSEADDR, this.reuseAddr ? 1 : 0 ); datagram_setOpt( this._fd, - SOL_SOCKET, - SO_REUSEPORT, + dgc_SOL_SOCKET, + dgc_SO_REUSEPORT, this.reusePort ? 1 : 0 ); datagram_bind(this._fd, this.type, address ?? defaultAddr, port ?? 0); @@ -167,30 +150,30 @@ export class Socket extends EventEmitter { } setBroadcast(flag: boolean) { - datagram_setOpt(this._fd, SOL_SOCKET, SO_BROADCAST, flag ? 1 : 0); + datagram_setOpt(this._fd, dgc_SOL_SOCKET, dgc_SO_BROADCAST, flag ? 1 : 0); } getRecvBufferSize() { - return datagram_getOpt(this._fd, SOL_SOCKET, SO_RCVBUF); + return datagram_getOpt(this._fd, dgc_SOL_SOCKET, dgc_SO_RCVBUF); } setRecvBufferSize(size: number) { - datagram_setOpt(this._fd, SOL_SOCKET, SO_RCVBUF, size); + datagram_setOpt(this._fd, dgc_SOL_SOCKET, dgc_SO_RCVBUF, size); } getSendBufferSize() { - return datagram_getOpt(this._fd, SOL_SOCKET, SO_SNDBUF); + return datagram_getOpt(this._fd, dgc_SOL_SOCKET, dgc_SO_SNDBUF); } setSendBufferSize(size: number) { - datagram_setOpt(this._fd, SOL_SOCKET, SO_SNDBUF, size); + datagram_setOpt(this._fd, dgc_SOL_SOCKET, dgc_SO_SNDBUF, size); } addMembership(multicastAddress: string, multicastInterface?: string) { datagram_setOpt( this._fd, - this.type === 4 ? IPPROTO_IP : IPPROTO_IPV6, - IP_ADD_MEMBERSHIP, + this.type === 4 ? dgc_IPPROTO_IP : dgc_IPPROTO_IPV6, + dgc_IP_ADD_MEMBERSHIP, multicastAddress, multicastInterface ); @@ -199,8 +182,8 @@ export class Socket extends EventEmitter { dropMembership(multicastAddress: string, multicastInterface?: string) { datagram_setOpt( this._fd, - this.type === 4 ? IPPROTO_IP : IPPROTO_IPV6, - IP_DROP_MEMBERSHIP, + this.type === 4 ? dgc_IPPROTO_IP : dgc_IPPROTO_IPV6, + dgc_IP_DROP_MEMBERSHIP, multicastAddress, multicastInterface ); @@ -209,8 +192,8 @@ export class Socket extends EventEmitter { setMulticastTTL(ttl: number) { datagram_setOpt( this._fd, - this.type === 4 ? IPPROTO_IP : IPPROTO_IPV6, - IP_MULTICAST_TTL, + this.type === 4 ? dgc_IPPROTO_IP : dgc_IPPROTO_IPV6, + dgc_IP_MULTICAST_TTL, ttl ); } @@ -218,8 +201,8 @@ export class Socket extends EventEmitter { setMulticastLoopback(flag: boolean) { datagram_setOpt( this._fd, - this.type === 4 ? IPPROTO_IP : IPPROTO_IPV6, - IP_MULTICAST_LOOP, + this.type === 4 ? dgc_IPPROTO_IP : dgc_IPPROTO_IPV6, + dgc_IP_MULTICAST_LOOP, flag ? 1 : 0 ); } @@ -227,8 +210,8 @@ export class Socket extends EventEmitter { setTTL(ttl: number) { datagram_setOpt( this._fd, - this.type === 4 ? IPPROTO_IP : IPPROTO_IPV6, - IP_TTL, + this.type === 4 ? dgc_IPPROTO_IP : dgc_IPPROTO_IPV6, + dgc_IP_TTL, ttl ); } diff --git a/src/types.d.ts b/src/types.d.ts index eaaf8a9..57e7f6f 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -53,3 +53,17 @@ declare function datagram_getSockName( address: string; port: number; }; + +declare var dgc_SOL_SOCKET: number; +declare var dgc_IPPROTO_IP: number; +declare var dgc_IPPROTO_IPV6: number; +declare var dgc_SO_REUSEADDR: number; +declare var dgc_SO_REUSEPORT: number; +declare var dgc_SO_BROADCAST: number; +declare var dgc_SO_RCVBUF: number; +declare var dgc_SO_SNDBUF: number; +declare var dgc_IP_MULTICAST_TTL: number; +declare var dgc_IP_MULTICAST_LOOP: number; +declare var dgc_IP_ADD_MEMBERSHIP: number; +declare var dgc_IP_DROP_MEMBERSHIP: number; +declare var dgc_IP_TTL: number;