enet-js 1.2.4 → 1.2.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +219 -75
- package/dist/constants.d.ts +4 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +3 -0
- package/dist/enums.d.ts +2 -2
- package/dist/enums.d.ts.map +1 -0
- package/dist/enums.js +1 -7
- package/dist/global.d.ts +1 -0
- package/dist/global.d.ts.map +1 -0
- package/dist/global.js +4 -8
- package/dist/host.d.ts +8 -4
- package/dist/host.d.ts.map +1 -0
- package/dist/host.js +69 -67
- package/dist/index.d.ts +7 -5
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -32
- package/dist/native/index.d.ts +17 -14
- package/dist/native/index.d.ts.map +1 -0
- package/dist/native/index.js +21 -42
- package/dist/native/structs.d.ts +13 -326
- package/dist/native/structs.d.ts.map +1 -0
- package/dist/native/structs.js +97 -143
- package/dist/packet.d.ts +3 -4
- package/dist/packet.d.ts.map +1 -0
- package/dist/packet.js +10 -23
- package/dist/peer.d.ts +5 -2
- package/dist/peer.d.ts.map +1 -0
- package/dist/peer.js +9 -6
- package/dist/structs.d.ts +22 -11
- package/dist/structs.d.ts.map +1 -0
- package/dist/structs.js +1 -2
- package/dist/util.d.ts +1 -0
- package/dist/util.d.ts.map +1 -0
- package/dist/util.js +3 -7
- package/package.json +34 -30
package/README.md
CHANGED
|
@@ -3,112 +3,256 @@
|
|
|
3
3
|
Modern Node.js bindings for [ENet](http://enet.bespin.org/), the reliable UDP
|
|
4
4
|
networking library.
|
|
5
5
|
|
|
6
|
-
This package uses
|
|
7
|
-
native C library
|
|
6
|
+
This package uses [Koffi](https://koffi.dev/) to provide a foreign function
|
|
7
|
+
interface for the native C library
|
|
8
8
|
|
|
9
9
|
Note that some ENet functions have not been covered yet, so feel free to
|
|
10
10
|
contribute the ones you need
|
|
11
11
|
|
|
12
12
|
## Versioning
|
|
13
13
|
|
|
14
|
-
[](
|
|
15
|
-
https://www.npmjs.com/package/enet-js
|
|
16
|
-
)
|
|
14
|
+
[](https://www.npmjs.com/package/enet-js)
|
|
17
15
|
|
|
18
16
|
The `<major>.<minor>` version matches the supported enet version
|
|
19
17
|
|
|
20
18
|
## Install
|
|
21
19
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
```sh
|
|
21
|
+
npm install --save-exact enet-js
|
|
22
|
+
```
|
|
25
23
|
|
|
26
|
-
|
|
24
|
+
**Note:** This package requires Node.js 18 or later.
|
|
25
|
+
|
|
26
|
+
Before importing enet-js, set the `ENET_LIB_PATH` environment variable to the
|
|
27
|
+
full path of the ENet shared library (including extension, e.g. `.dll`,
|
|
28
|
+
`.dylib`, or `.so`).
|
|
27
29
|
|
|
28
30
|
```sh
|
|
29
|
-
|
|
31
|
+
export ENET_LIB_PATH="/path/to/libenet.0.dylib"
|
|
30
32
|
```
|
|
31
33
|
|
|
32
|
-
|
|
33
|
-
is located. If you have binaries for multiple platforms, omit the extension
|
|
34
|
+
Or set it when running your application:
|
|
34
35
|
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
"enetLibPath": "path/to/enet(.dll|.so)"
|
|
38
|
-
}
|
|
36
|
+
```sh
|
|
37
|
+
ENET_LIB_PATH=/path/to/libenet.0.dylib node your-app.js
|
|
39
38
|
```
|
|
40
39
|
|
|
40
|
+
To get the dynamic library, compile enet following the instructions at
|
|
41
|
+
<http://enet.bespin.org/Installation.html>.
|
|
42
|
+
|
|
41
43
|
## Usage
|
|
42
44
|
|
|
43
|
-
|
|
45
|
+
### Initialization
|
|
46
|
+
|
|
47
|
+
<http://enet.bespin.org/Tutorial.html#Initialization>
|
|
44
48
|
|
|
45
49
|
```ts
|
|
46
|
-
import
|
|
47
|
-
import { ENET_HOST_ANY, ENetEventType, enet } from "enet-js";
|
|
50
|
+
import { enet } from "enet-js";
|
|
48
51
|
|
|
49
|
-
const
|
|
50
|
-
if (enet.initialize()
|
|
51
|
-
|
|
52
|
+
const start = (): void => {
|
|
53
|
+
if (enet.initialize() === 0) {
|
|
54
|
+
process.on("SIGINT", (): void => {
|
|
55
|
+
enet.deinitialize();
|
|
56
|
+
process.exit();
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// ...
|
|
52
60
|
} else {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
port: 2600,
|
|
56
|
-
};
|
|
57
|
-
const peerCount = 32;
|
|
58
|
-
const host: IENetHost | null = enet.host.create(address, peerCount, 0, 0);
|
|
59
|
-
|
|
60
|
-
if (host === null) {
|
|
61
|
-
console.error("Unable to create host");
|
|
62
|
-
} else {
|
|
63
|
-
console.log("Server running in port", address.port);
|
|
64
|
-
|
|
65
|
-
while (true) {
|
|
66
|
-
const timeout = 0;
|
|
67
|
-
const event: IENetEvent | null = enet.host.service(host, timeout);
|
|
68
|
-
|
|
69
|
-
if (event) {
|
|
70
|
-
switch (event.type) {
|
|
71
|
-
case ENetEventType.none:
|
|
72
|
-
break;
|
|
73
|
-
|
|
74
|
-
case ENetEventType.connect:
|
|
75
|
-
console.log(
|
|
76
|
-
"Client connected",
|
|
77
|
-
event.peer.address.host,
|
|
78
|
-
event.peer.address.port
|
|
79
|
-
);
|
|
80
|
-
break;
|
|
81
|
-
|
|
82
|
-
case ENetEventType.disconnect:
|
|
83
|
-
console.log(
|
|
84
|
-
"Client disconnected",
|
|
85
|
-
event.peer.address.host,
|
|
86
|
-
event.peer.address.port
|
|
87
|
-
);
|
|
88
|
-
break;
|
|
89
|
-
|
|
90
|
-
case ENetEventType.receive:
|
|
91
|
-
console.log(
|
|
92
|
-
"Packet received from channel",
|
|
93
|
-
event.channelID,
|
|
94
|
-
event.packet
|
|
95
|
-
);
|
|
96
|
-
break;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
61
|
+
console.error("Unable to initialize ENet");
|
|
62
|
+
process.exit(1);
|
|
101
63
|
}
|
|
102
64
|
};
|
|
103
65
|
|
|
104
|
-
|
|
66
|
+
start();
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Creating an ENet server
|
|
70
|
+
|
|
71
|
+
<http://enet.bespin.org/Tutorial.html#CreateServer>
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
const address: IENetAddress = {
|
|
75
|
+
// Bind the server to the default localhost.
|
|
76
|
+
host: ENET_HOST_ANY,
|
|
77
|
+
port: 1234,
|
|
78
|
+
};
|
|
79
|
+
const host: IENetHost | null = enet.host.create(
|
|
80
|
+
// the address to bind the server host to
|
|
81
|
+
address,
|
|
82
|
+
// allow up to 32 clients and/or outgoing connections
|
|
83
|
+
32,
|
|
84
|
+
// assume any amount of incoming bandwidth
|
|
85
|
+
0,
|
|
86
|
+
// assume any amount of outgoing bandwidth
|
|
87
|
+
0,
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
if (host === null) {
|
|
91
|
+
console.error("Unable to create host");
|
|
92
|
+
process.exit(1);
|
|
93
|
+
} else {
|
|
94
|
+
console.log("Server running on port", address.port);
|
|
95
|
+
|
|
96
|
+
// ...
|
|
97
|
+
|
|
98
|
+
enet.host.destroy(host);
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Creating an ENet client
|
|
103
|
+
|
|
104
|
+
<http://enet.bespin.org/Tutorial.html#CreateClient>
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
const host: IENetHost | null = enet.host.create(
|
|
108
|
+
// create a client host
|
|
109
|
+
null,
|
|
110
|
+
// only allow 1 outgoing connection
|
|
111
|
+
1,
|
|
112
|
+
// assume any amount of incoming bandwidth
|
|
113
|
+
0,
|
|
114
|
+
// assume any amount of outgoing bandwidth
|
|
115
|
+
0,
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
if (host === null) {
|
|
119
|
+
console.error("Unable to create host");
|
|
120
|
+
process.exit(1);
|
|
121
|
+
} else {
|
|
122
|
+
// ...
|
|
123
|
+
|
|
124
|
+
enet.host.destroy(host);
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### Managing an ENet host
|
|
129
|
+
|
|
130
|
+
<http://enet.bespin.org/Tutorial.html#ManageHost>
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
while (true) {
|
|
134
|
+
// Wait up to 1000 milliseconds for an event.
|
|
135
|
+
const event: IENetEvent = enet.host.service(host, 1000);
|
|
136
|
+
|
|
137
|
+
switch (event.type) {
|
|
138
|
+
case ENetEventType.none:
|
|
139
|
+
break;
|
|
140
|
+
|
|
141
|
+
case ENetEventType.connect:
|
|
142
|
+
console.log(
|
|
143
|
+
"Client connected",
|
|
144
|
+
event.peer.address.host,
|
|
145
|
+
event.peer.address.port,
|
|
146
|
+
);
|
|
147
|
+
break;
|
|
148
|
+
|
|
149
|
+
case ENetEventType.disconnect:
|
|
150
|
+
console.log(
|
|
151
|
+
"Client disconnected",
|
|
152
|
+
event.peer.address.host,
|
|
153
|
+
event.peer.address.port,
|
|
154
|
+
);
|
|
155
|
+
break;
|
|
156
|
+
|
|
157
|
+
case ENetEventType.receive:
|
|
158
|
+
console.log(
|
|
159
|
+
"Packet received from channel",
|
|
160
|
+
event.channelID,
|
|
161
|
+
event.packet.data,
|
|
162
|
+
);
|
|
163
|
+
// Clean up the packet now that we're done using it.
|
|
164
|
+
enet.packet.destroy(event.packet);
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### Sending a packet to an ENet peer
|
|
171
|
+
|
|
172
|
+
<http://enet.bespin.org/Tutorial.html#SendingPacket>
|
|
173
|
+
|
|
174
|
+
```ts
|
|
175
|
+
// Create a reliable packet of size 7 containing "packet\0"
|
|
176
|
+
const packet: IENetPacket | null = enet.packet.create(
|
|
177
|
+
Buffer.from("packet\0"),
|
|
178
|
+
ENetPacketFlag.reliable,
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
/* Send the packet to the peer over channel id 0.
|
|
182
|
+
* One could also broadcast the packet
|
|
183
|
+
* using enet.host.broadcast(host, 0, packet);
|
|
184
|
+
*/
|
|
185
|
+
if (packet) {
|
|
186
|
+
enet.peer.send(peer, 0, packet);
|
|
187
|
+
// One could just use enet.host.service() instead.
|
|
188
|
+
enet.host.flush(host);
|
|
189
|
+
}
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### Disconnecting an ENet peer
|
|
193
|
+
|
|
194
|
+
<http://enet.bespin.org/Tutorial.html#Disconnecting>
|
|
195
|
+
|
|
196
|
+
```ts
|
|
197
|
+
enet.peer.disconnect(peer, 0);
|
|
198
|
+
/* Allow up to 3 seconds for the disconnect to succeed
|
|
199
|
+
* and drop any received packets.
|
|
200
|
+
*/
|
|
201
|
+
const event: IENetEvent = enet.host.service(host, 3000);
|
|
202
|
+
|
|
203
|
+
switch (event.type) {
|
|
204
|
+
case ENetEventType.disconnect:
|
|
205
|
+
console.log("Disconnection succeeded.");
|
|
206
|
+
return;
|
|
207
|
+
|
|
208
|
+
case ENetEventType.receive:
|
|
209
|
+
enet.packet.destroy(packet);
|
|
210
|
+
break;
|
|
211
|
+
}
|
|
212
|
+
/* We've arrived here, so the disconnect attempt didn't
|
|
213
|
+
* succeed yet. Force the connection down.
|
|
214
|
+
*/
|
|
215
|
+
enet.peer.reset(peer);
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
### Connecting to an ENet host
|
|
219
|
+
|
|
220
|
+
<http://enet.bespin.org/Tutorial.html#Connecting>
|
|
221
|
+
|
|
222
|
+
```ts
|
|
223
|
+
// Connect to 127.0.0.1:1234.
|
|
224
|
+
const address: IENetAddress = { host: "127.0.0.1", port: 1234 };
|
|
225
|
+
|
|
226
|
+
// Initiate the connection, allocating the two channels 0 and 1.
|
|
227
|
+
const peer: IENetPeer | null = enet.host.connect(host, address, 2);
|
|
228
|
+
|
|
229
|
+
if (peer === null) {
|
|
230
|
+
console.error("No available peers for initiating an ENet connection");
|
|
231
|
+
process.exit(1);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// Wait up to 5 seconds for the connection attempt to succeed.
|
|
235
|
+
const event: IENetEvent = enet.host.service(host, 5000);
|
|
236
|
+
|
|
237
|
+
if (event.type === ENetEventType.connect) {
|
|
238
|
+
console.log("Connection to 127.0.0.1:1234 succeeded.");
|
|
239
|
+
|
|
240
|
+
// ...
|
|
241
|
+
} else {
|
|
242
|
+
/* Either the 5 seconds are up or a disconnect event was
|
|
243
|
+
* received. Reset the peer in the event the 5 seconds
|
|
244
|
+
* had run out without any significant event.
|
|
245
|
+
*/
|
|
246
|
+
enet.peer.reset(peer);
|
|
247
|
+
console.error("Connection to 127.0.0.1:1234 failed.");
|
|
248
|
+
}
|
|
105
249
|
```
|
|
106
250
|
|
|
107
251
|
## Docs
|
|
108
252
|
|
|
109
|
-
This package aims to serve only as a compatibility layer without
|
|
110
|
-
functionality, which means the functions and data structures mirror the
|
|
111
|
-
|
|
253
|
+
This package aims to serve only as a compatibility layer without expanding the
|
|
254
|
+
functionality, which means the functions and data structures mirror the native
|
|
255
|
+
ones, whose docs can be found at <http://enet.bespin.org/>.
|
|
112
256
|
|
|
113
257
|
This package also provides [TypeScript](https://www.typescriptlang.org/) type
|
|
114
|
-
definitions to help ensure proper usage
|
|
258
|
+
definitions to help ensure proper usage.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,QAAA,MAAM,aAAa,YAAY,CAAC;AAChC,QAAA,MAAM,mBAAmB,oBAAoB,CAAC;AAE9C,OAAO,EAAE,aAAa,EAAE,mBAAmB,EAAE,CAAC"}
|
package/dist/enums.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"enums.d.ts","sourceRoot":"","sources":["../src/enums.ts"],"names":[],"mappings":"AAAA,aAAK,aAAa;IAChB,IAAI,IAAI;IACR,OAAO,IAAI;IACX,UAAU,IAAI;IACd,OAAO,IAAI;CACZ;AAED,aAAK,cAAc;IACjB,IAAI,IAAI;IACR,QAAQ,IAAI;IACZ,WAAW,IAAI;IACf,UAAU,IAAI;CACf;AAED,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,CAAC"}
|
package/dist/enums.js
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ENetPacketFlag = exports.ENetEventType = exports.ENET_HOST_ANY = void 0;
|
|
4
1
|
var ENetEventType;
|
|
5
2
|
(function (ENetEventType) {
|
|
6
3
|
ENetEventType[ENetEventType["none"] = 0] = "none";
|
|
@@ -8,7 +5,6 @@ var ENetEventType;
|
|
|
8
5
|
ENetEventType[ENetEventType["disconnect"] = 2] = "disconnect";
|
|
9
6
|
ENetEventType[ENetEventType["receive"] = 3] = "receive";
|
|
10
7
|
})(ENetEventType || (ENetEventType = {}));
|
|
11
|
-
exports.ENetEventType = ENetEventType;
|
|
12
8
|
var ENetPacketFlag;
|
|
13
9
|
(function (ENetPacketFlag) {
|
|
14
10
|
ENetPacketFlag[ENetPacketFlag["none"] = 0] = "none";
|
|
@@ -16,6 +12,4 @@ var ENetPacketFlag;
|
|
|
16
12
|
ENetPacketFlag[ENetPacketFlag["unsequenced"] = 2] = "unsequenced";
|
|
17
13
|
ENetPacketFlag[ENetPacketFlag["noAllocate"] = 4] = "noAllocate";
|
|
18
14
|
})(ENetPacketFlag || (ENetPacketFlag = {}));
|
|
19
|
-
|
|
20
|
-
const ENET_HOST_ANY = "0.0.0.0";
|
|
21
|
-
exports.ENET_HOST_ANY = ENET_HOST_ANY;
|
|
15
|
+
export { ENetEventType, ENetPacketFlag };
|
package/dist/global.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"global.d.ts","sourceRoot":"","sources":["../src/global.ts"],"names":[],"mappings":"AAEA,QAAA,MAAM,YAAY,QAAO,IAExB,CAAC;AACF,QAAA,MAAM,UAAU,QAAO,MAAqC,CAAC;AAE7D,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,CAAC"}
|
package/dist/global.js
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.initialize = exports.deinitialize = void 0;
|
|
4
|
-
const native_1 = require("./native");
|
|
1
|
+
import { enet_deinitialize, enet_initialize } from "./native/index.js";
|
|
5
2
|
const deinitialize = () => {
|
|
6
|
-
|
|
3
|
+
enet_deinitialize();
|
|
7
4
|
};
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
exports.initialize = initialize;
|
|
5
|
+
const initialize = () => enet_initialize();
|
|
6
|
+
export { deinitialize, initialize };
|
package/dist/host.d.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
import type { IENetAddress, IENetEvent, IENetHost, IENetPacket } from "./structs";
|
|
1
|
+
import type { IENetAddress, IENetEvent, IENetHost, IENetPacket, IENetPeer } from "./structs.js";
|
|
2
2
|
declare const broadcast: (host: IENetHost, channelID: number, packet: IENetPacket) => void;
|
|
3
|
-
declare const
|
|
4
|
-
declare const
|
|
5
|
-
|
|
3
|
+
declare const connect: (host: IENetHost, address: IENetAddress, channelCount: number) => IENetPeer | null;
|
|
4
|
+
declare const create: (address: IENetAddress | null, peerCount: number, incomingBandwidth: number, outgoingBandwidth: number) => IENetHost | null;
|
|
5
|
+
declare const destroy: (host: IENetHost) => void;
|
|
6
|
+
declare const flush: (host: IENetHost) => void;
|
|
7
|
+
declare const service: (host: IENetHost, timeout: number) => IENetEvent;
|
|
8
|
+
export { broadcast, connect, create, destroy, flush, service };
|
|
9
|
+
//# sourceMappingURL=host.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"host.d.ts","sourceRoot":"","sources":["../src/host.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EACV,YAAY,EACZ,UAAU,EACV,SAAS,EACT,WAAW,EACX,SAAS,EACV,MAAM,cAAc,CAAC;AAGtB,QAAA,MAAM,SAAS,GACb,MAAM,SAAS,EACf,WAAW,MAAM,EACjB,QAAQ,WAAW,KAClB,IAEF,CAAC;AAyBF,QAAA,MAAM,OAAO,GACX,MAAM,SAAS,EACf,SAAS,YAAY,EACrB,cAAc,MAAM,KACnB,SAAS,GAAG,IAYd,CAAC;AAEF,QAAA,MAAM,MAAM,GACV,SAAS,YAAY,GAAG,IAAI,EAC5B,WAAW,MAAM,EACjB,mBAAmB,MAAM,EACzB,mBAAmB,MAAM,KACxB,SAAS,GAAG,IAad,CAAC;AAEF,QAAA,MAAM,OAAO,GAAI,MAAM,SAAS,KAAG,IAElC,CAAC;AAEF,QAAA,MAAM,KAAK,GAAI,MAAM,SAAS,KAAG,IAEhC,CAAC;AA2DF,QAAA,MAAM,OAAO,GAAI,MAAM,SAAS,EAAE,SAAS,MAAM,KAAG,UAKnD,CAAC;AAEF,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC"}
|
package/dist/host.js
CHANGED
|
@@ -1,84 +1,86 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.service = exports.create = exports.broadcast = void 0;
|
|
7
|
-
const ref_napi_1 = __importDefault(require("ref-napi"));
|
|
8
|
-
const native_1 = require("./native");
|
|
9
|
-
const structs_1 = require("./native/structs");
|
|
10
|
-
const util_1 = require("./util");
|
|
1
|
+
import koffi from "koffi";
|
|
2
|
+
import { ENetEventType } from "./enums.js";
|
|
3
|
+
import { enetPacket, enetPeer, enet_host_broadcast, enet_host_connect, enet_host_create, enet_host_destroy, enet_host_flush, enet_host_service, } from "./native/index.js";
|
|
4
|
+
import { ipFromLong, ipToLong } from "./util.js";
|
|
11
5
|
const broadcast = (host, channelID, packet) => {
|
|
12
|
-
|
|
6
|
+
enet_host_broadcast(host.native, channelID, packet.native);
|
|
13
7
|
};
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const
|
|
21
|
-
if (ref_napi_1.default.isNull(host)) {
|
|
22
|
-
return null;
|
|
23
|
-
}
|
|
24
|
-
const hostAttributes = ref_napi_1.default.deref(host);
|
|
8
|
+
const formatAddress = (address) => ({
|
|
9
|
+
host: ipToLong(address.host),
|
|
10
|
+
port: address.port,
|
|
11
|
+
});
|
|
12
|
+
const formatPeer = (peer) => {
|
|
13
|
+
const peerAttributes = koffi.decode(peer, enetPeer);
|
|
14
|
+
const peerAddress = peerAttributes.address;
|
|
25
15
|
return {
|
|
26
|
-
|
|
27
|
-
|
|
16
|
+
address: {
|
|
17
|
+
host: ipFromLong(peerAddress.host),
|
|
18
|
+
port: peerAddress.port,
|
|
19
|
+
},
|
|
20
|
+
mtu: peerAttributes.mtu,
|
|
21
|
+
native: peer,
|
|
28
22
|
};
|
|
29
23
|
};
|
|
30
|
-
|
|
31
|
-
const
|
|
32
|
-
if (
|
|
24
|
+
const connect = (host, address, channelCount) => {
|
|
25
|
+
const peer = enet_host_connect(host.native, formatAddress(address), channelCount);
|
|
26
|
+
if (!peer) {
|
|
33
27
|
return null;
|
|
34
28
|
}
|
|
35
|
-
|
|
36
|
-
const dataLength = packetAttributes.dataLength;
|
|
37
|
-
Object.assign(packetAttributes.data.type, { size: dataLength });
|
|
38
|
-
return {
|
|
39
|
-
data: packetAttributes.data,
|
|
40
|
-
dataLength,
|
|
41
|
-
flags: packetAttributes.flags,
|
|
42
|
-
native: packetInstance,
|
|
43
|
-
referenceCount: packetAttributes.referenceCount,
|
|
44
|
-
};
|
|
29
|
+
return formatPeer(peer);
|
|
45
30
|
};
|
|
46
|
-
const
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
31
|
+
const create = (address, peerCount, incomingBandwidth, outgoingBandwidth) => {
|
|
32
|
+
const host = enet_host_create(address ? formatAddress(address) : null, peerCount, incomingBandwidth, outgoingBandwidth);
|
|
33
|
+
if (!host) {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
return { native: host };
|
|
37
|
+
};
|
|
38
|
+
const destroy = (host) => {
|
|
39
|
+
enet_host_destroy(host.native);
|
|
40
|
+
};
|
|
41
|
+
const flush = (host) => {
|
|
42
|
+
enet_host_flush(host.native);
|
|
43
|
+
};
|
|
44
|
+
const formatPacket = (packet) => {
|
|
45
|
+
const packetAttributes = koffi.decode(packet, enetPacket);
|
|
46
|
+
const dataView = koffi.view(packetAttributes.data, packetAttributes.dataLength);
|
|
47
|
+
return {
|
|
48
|
+
...packetAttributes,
|
|
49
|
+
data: Buffer.from(dataView),
|
|
50
|
+
native: packet,
|
|
56
51
|
};
|
|
57
|
-
Object.defineProperty(peer, "mtu", {
|
|
58
|
-
get: () => peerAttributes.mtu,
|
|
59
|
-
set: (value) => {
|
|
60
|
-
Object.assign(peerAttributes, { mtu: value });
|
|
61
|
-
},
|
|
62
|
-
});
|
|
63
|
-
return peer;
|
|
64
52
|
};
|
|
65
|
-
const formatEvent = (
|
|
66
|
-
const eventAttributes =
|
|
53
|
+
const formatEvent = (event) => {
|
|
54
|
+
const eventAttributes = event;
|
|
55
|
+
if (eventAttributes.type === ENetEventType.none) {
|
|
56
|
+
return {
|
|
57
|
+
...eventAttributes,
|
|
58
|
+
native: event,
|
|
59
|
+
packet: null,
|
|
60
|
+
peer: null,
|
|
61
|
+
type: ENetEventType.none,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
if (eventAttributes.type === ENetEventType.receive) {
|
|
65
|
+
return {
|
|
66
|
+
...eventAttributes,
|
|
67
|
+
native: event,
|
|
68
|
+
packet: formatPacket(eventAttributes.packet),
|
|
69
|
+
peer: formatPeer(eventAttributes.peer),
|
|
70
|
+
type: ENetEventType.receive,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
67
73
|
return {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
packet: formatPacket(eventAttributes.packet),
|
|
74
|
+
...eventAttributes,
|
|
75
|
+
native: event,
|
|
76
|
+
packet: null,
|
|
72
77
|
peer: formatPeer(eventAttributes.peer),
|
|
73
78
|
type: eventAttributes.type,
|
|
74
79
|
};
|
|
75
80
|
};
|
|
76
81
|
const service = (host, timeout) => {
|
|
77
|
-
const event =
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
return formatEvent(event);
|
|
81
|
-
}
|
|
82
|
-
return null;
|
|
82
|
+
const event = {};
|
|
83
|
+
enet_host_service(host.native, event, timeout);
|
|
84
|
+
return formatEvent(event);
|
|
83
85
|
};
|
|
84
|
-
|
|
86
|
+
export { broadcast, connect, create, destroy, flush, service };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import * as host from "./host";
|
|
2
|
-
import * as packet from "./packet";
|
|
3
|
-
import * as peer from "./peer";
|
|
1
|
+
import * as host from "./host.js";
|
|
2
|
+
import * as packet from "./packet.js";
|
|
3
|
+
import * as peer from "./peer.js";
|
|
4
4
|
declare const enet: {
|
|
5
5
|
host: typeof host;
|
|
6
6
|
packet: typeof packet;
|
|
@@ -8,6 +8,8 @@ declare const enet: {
|
|
|
8
8
|
deinitialize: () => void;
|
|
9
9
|
initialize: () => number;
|
|
10
10
|
};
|
|
11
|
-
export * from "./
|
|
12
|
-
export * from "./
|
|
11
|
+
export * from "./constants.js";
|
|
12
|
+
export * from "./enums.js";
|
|
13
|
+
export type * from "./structs.js";
|
|
13
14
|
export { enet };
|
|
15
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC;AACtC,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAElC,QAAA,MAAM,IAAI;;;;;;CAKT,CAAC;AAEF,cAAc,gBAAgB,CAAC;AAC/B,cAAc,YAAY,CAAC;AAC3B,mBAAmB,cAAc,CAAC;AAClC,OAAO,EAAE,IAAI,EAAE,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,38 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}) : (function(o, m, k, k2) {
|
|
6
|
-
if (k2 === undefined) k2 = k;
|
|
7
|
-
o[k2] = m[k];
|
|
8
|
-
}));
|
|
9
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
-
}) : function(o, v) {
|
|
12
|
-
o["default"] = v;
|
|
13
|
-
});
|
|
14
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
-
if (mod && mod.__esModule) return mod;
|
|
16
|
-
var result = {};
|
|
17
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
-
__setModuleDefault(result, mod);
|
|
19
|
-
return result;
|
|
20
|
-
};
|
|
21
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
22
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
23
|
-
};
|
|
24
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
-
exports.enet = void 0;
|
|
26
|
-
const global = __importStar(require("./global"));
|
|
27
|
-
const host = __importStar(require("./host"));
|
|
28
|
-
const packet = __importStar(require("./packet"));
|
|
29
|
-
const peer = __importStar(require("./peer"));
|
|
1
|
+
import * as global from "./global.js";
|
|
2
|
+
import * as host from "./host.js";
|
|
3
|
+
import * as packet from "./packet.js";
|
|
4
|
+
import * as peer from "./peer.js";
|
|
30
5
|
const enet = {
|
|
31
6
|
...global,
|
|
32
7
|
host,
|
|
33
8
|
packet,
|
|
34
9
|
peer,
|
|
35
10
|
};
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
11
|
+
export * from "./constants.js";
|
|
12
|
+
export * from "./enums.js";
|
|
13
|
+
export { enet };
|