Skip to content

Commit

Permalink
fix: move constants to native, avoid platform specific value
Browse files Browse the repository at this point in the history
  • Loading branch information
hans00 committed Sep 12, 2023
1 parent 32b7bf6 commit 8a7b3f3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 36 deletions.
18 changes: 18 additions & 0 deletions cpp/react-native-jsi-udp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(SOL_SOCKET));
global.setProperty(jsiRuntime, "dgc_IPPROTO_IP", static_cast<int>(IPPROTO_IP));
global.setProperty(jsiRuntime, "dgc_IPPROTO_IPV6", static_cast<int>(IPPROTO_IPV6));
global.setProperty(jsiRuntime, "dgc_SO_REUSEADDR", static_cast<int>(SO_REUSEADDR));
global.setProperty(jsiRuntime, "dgc_SO_REUSEPORT", static_cast<int>(SO_REUSEPORT));
global.setProperty(jsiRuntime, "dgc_SO_BROADCAST", static_cast<int>(SO_BROADCAST));
global.setProperty(jsiRuntime, "dgc_SO_RCVBUF", static_cast<int>(SO_RCVBUF));
global.setProperty(jsiRuntime, "dgc_SO_SNDBUF", static_cast<int>(SO_SNDBUF));
global.setProperty(jsiRuntime, "dgc_IP_MULTICAST_TTL", static_cast<int>(IP_MULTICAST_TTL));
global.setProperty(jsiRuntime, "dgc_IP_MULTICAST_LOOP", static_cast<int>(IP_MULTICAST_LOOP));
global.setProperty(jsiRuntime, "dgc_IP_ADD_MEMBERSHIP", static_cast<int>(IP_ADD_MEMBERSHIP));
global.setProperty(jsiRuntime, "dgc_IP_DROP_MEMBERSHIP", static_cast<int>(IP_DROP_MEMBERSHIP));
global.setProperty(jsiRuntime, "dgc_IP_TTL", static_cast<int>(IP_TTL));
}
}

UdpManager::~UdpManager() {
Expand Down
55 changes: 19 additions & 36 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
);
Expand All @@ -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
);
Expand All @@ -209,26 +192,26 @@ 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
);
}

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
);
}

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
);
}
Expand Down
14 changes: 14 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 8a7b3f3

Please sign in to comment.