-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.d.ts
More file actions
186 lines (156 loc) · 6.17 KB
/
index.d.ts
File metadata and controls
186 lines (156 loc) · 6.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* Copyright (C) 2013-2016 Trent Houliston <trent@houliston.me>, Jake Woods <jake.f.woods@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/// <reference types="node" />
/**
* NUClearNet options for connecting to the network
*/
export interface NUClearNetOptions {
/** The name of this node on the NUClearNetwork */
name: string;
/** The announce address for this network. Defaults to `239.226.152.162`. */
address?: string;
/** The announce port for this network. Defaults to `7447`. */
port?: number;
/** The MTU of the network. Used for splitting packets optimally. */
mtu?: number;
}
/**
* Data provided for sending information on the network
*/
export interface NUClearNetSend {
/**
* The type of the message to send: either a string that will be hashed,
* or a Buffer with the 64 bit hash.
*/
type: string | Buffer;
/** The data to send */
payload: Buffer;
/**
* The target to send the packet to. If `undefined`, the packet will be sent
* to everyone on the network.
*/
target?: string;
/**
* If `true`, the packet should be sent using a reliable protocol
* which handles retransmissions.
*/
reliable?: boolean;
}
/**
* Information about a peer on the NUClear network
*/
export interface NUClearNetPeer {
/** The name the peer has on the network. See `NUClearNetOptions.name` */
name: string;
/** The IP address of the peer: either a dotted decimal IPv4 string, or an IPv6 string. */
address: string;
/**
* The port the peer has connected on. This will be an ephemeral port.
* The combination of `address` + `port` will be unique for a peer.
*/
port: number;
}
/**
* A data packet that received on the NUClear network
*/
export interface NUClearNetPacket {
/** The peer the packet was sent from */
peer: NUClearNetPeer;
/** The hash code of the packet's type */
hash: Buffer;
/** The data that was sent from the peer */
payload: Buffer;
/**
* Will be set to `true`, if the peer sent the packet with reliable transmission
* (see `NUClearNetSend.reliable`).
*/
reliable: boolean;
}
/**
* A packet that is received using `on('message.type')` will always have a known string type:
* the same string that was provided in the on statement.
*/
export interface NUClearNetTypedPacket extends NUClearNetPacket {
/** The type that was provided in the on statement for this packet */
type: string;
}
/**
* When using `on('nuclear_packet')` the type as a string may or may not be known depending on
* whether another user has requested the same type. This interface represents that type of packet.
*/
export interface NUClearNetMaybeTypedPacket extends NUClearNetPacket {
/**
* The type that was provided in an on statement, or `undefined` if nobody has executed
* an on statement for this type.
*/
type: string | undefined;
}
/**
* Represents a NUClearNet network client.
*
* Usage notes:
* - Before it will provide data, the instance must be connected first via `.connect()`.
* - Join and leave callbacks should be set before calling `.connect()`. If not, join
* events from already connected peers will not be received.
* - After calling `.destroy()`, the instance should not be used again.
*/
export declare class NUClearNet {
/** Stores the `connect()` options. Is an empty object until `connect()` is called. */
options: Partial<NUClearNetOptions>;
/** Create a new NUClearNet instance. */
public constructor();
/** Emitted when a peer joins or leaves the network. */
public on(event: 'nuclear_join' | 'nuclear_leave', callback: (peer: NUClearNetPeer) => void): this;
/** Emitted when NUClearNet receives any packet */
public on(event: 'nuclear_packet', callback: (packet: NUClearNetMaybeTypedPacket) => void): this;
/** Emitted when connection to the network is lost */
public on(event: 'disconnect', callback: () => void): this;
/** Emitted when the given packet is received */
public on(event: string, callback: (packet: NUClearNetTypedPacket) => void): this;
/**
* Hash the provided string using the NUClearNet hashing method.
* These hashes will be identical to those used by NUClear
*/
public hash(data: string): Buffer;
/**
* Stop listening for the given type.
* Note that after removing the last listener for a type, the type will revert to
* being an undefined type for `NUClearNetMaybeTypedPacket`.
*/
public removeListener(event: string, listener: Function): this;
/**
* Connect this instance to the NUclear network.
* Subsequent calls to this function will disconnect from the previous network and
* reconnect to a new one.
*/
public connect(options: NUClearNetOptions): void;
/**
* Disconnect this instance from the NUClear network.
* Does not remove event listeners, therefore reconnecting will resume events.
*/
public disconnect(): void;
/**
* Disconnect and destroy this NUClearNetwork instance, clearing all event listeners.
* Attempting to use this instance after calling `destroy()` will throw an error.
*/
public destroy(): void;
/**
* Send the given packet over the NUClear network.
* Will throw if the network is not connected.
*/
public send(options: NUClearNetSend): void;
}