@netifyjs/netify 0.0.0-stage → 1.0.0-alpha.0
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/LICENSE +21 -0
- package/README.md +105 -2
- package/package.json +65 -4
- package/src/bootstrap/client/index.ts +191 -0
- package/src/bootstrap/dispatcher/index.ts +204 -0
- package/src/bootstrap/index.ts +22 -0
- package/src/bootstrap/reconnect/index.ts +334 -0
- package/src/bootstrap/server/index.ts +284 -0
- package/src/buffer/bits/index.ts +2 -0
- package/src/buffer/bits/reader/index.ts +64 -0
- package/src/buffer/bits/writer/index.ts +79 -0
- package/src/buffer/index.ts +1375 -0
- package/src/buffer/pool/index.ts +46 -0
- package/src/buffer/quantizer/index.ts +101 -0
- package/src/channel/attribute/index.ts +89 -0
- package/src/channel/context/index.ts +621 -0
- package/src/channel/group/index.ts +256 -0
- package/src/channel/handler/duplex/index.ts +105 -0
- package/src/channel/handler/inbound/index.ts +78 -0
- package/src/channel/handler/index.ts +135 -0
- package/src/channel/handler/outbound/index.ts +33 -0
- package/src/channel/index.ts +619 -0
- package/src/channel/initializer/index.ts +77 -0
- package/src/channel/outbound/index.ts +611 -0
- package/src/channel/pipeline/head/index.ts +100 -0
- package/src/channel/pipeline/index.ts +414 -0
- package/src/channel/pipeline/tail/index.ts +45 -0
- package/src/channel/pipeline/tracker/index.ts +120 -0
- package/src/channel/queue/index.ts +203 -0
- package/src/channel/resolved/index.ts +34 -0
- package/src/channel/server/index.ts +164 -0
- package/src/channel/shutdown/index.ts +25 -0
- package/src/channel/simple/index.ts +38 -0
- package/src/channel/transport/index.ts +74 -0
- package/src/channel/transport/socket/index.ts +80 -0
- package/src/cluster/index.ts +360 -0
- package/src/codec/binary/index.ts +102 -0
- package/src/codec/decoder/index.ts +479 -0
- package/src/codec/delimiter/index.ts +189 -0
- package/src/codec/encoder/index.ts +48 -0
- package/src/codec/error/index.ts +33 -0
- package/src/codec/fixed/index.ts +45 -0
- package/src/codec/http/aggregator/index.ts +236 -0
- package/src/codec/http/client/index.ts +120 -0
- package/src/codec/http/decoder/index.ts +4 -0
- package/src/codec/http/decoder/object/index.ts +644 -0
- package/src/codec/http/decoder/request/index.ts +49 -0
- package/src/codec/http/decoder/response/index.ts +59 -0
- package/src/codec/http/encoder/index.ts +3 -0
- package/src/codec/http/encoder/object/index.ts +196 -0
- package/src/codec/http/encoder/request/index.ts +24 -0
- package/src/codec/http/encoder/response/index.ts +26 -0
- package/src/codec/http/headers/index.ts +287 -0
- package/src/codec/http/index.ts +33 -0
- package/src/codec/http/message/content/index.ts +36 -0
- package/src/codec/http/message/full/index.ts +10 -0
- package/src/codec/http/message/full/request/index.ts +45 -0
- package/src/codec/http/message/full/response/index.ts +43 -0
- package/src/codec/http/message/head/index.ts +79 -0
- package/src/codec/http/message/index.ts +16 -0
- package/src/codec/http/message/last/index.ts +27 -0
- package/src/codec/http/message/request/index.ts +39 -0
- package/src/codec/http/message/response/index.ts +31 -0
- package/src/codec/http/method/index.ts +101 -0
- package/src/codec/http/server/index.ts +81 -0
- package/src/codec/http/status/index.ts +170 -0
- package/src/codec/http/version/index.ts +70 -0
- package/src/codec/index.ts +83 -0
- package/src/codec/length/index.ts +330 -0
- package/src/codec/line/index.ts +45 -0
- package/src/codec/message/decoder/index.ts +84 -0
- package/src/codec/message/encoder/index.ts +56 -0
- package/src/codec/message/index.ts +2 -0
- package/src/codec/msgpack/index.ts +51 -0
- package/src/codec/prepender/index.ts +296 -0
- package/src/codec/protobuf/index.ts +57 -0
- package/src/codec/string/decoder/index.ts +47 -0
- package/src/codec/string/encoder/index.ts +26 -0
- package/src/codec/string/index.ts +2 -0
- package/src/codec/websocket/client/index.ts +235 -0
- package/src/codec/websocket/decoder/index.ts +371 -0
- package/src/codec/websocket/encoder/index.ts +170 -0
- package/src/codec/websocket/error/index.ts +42 -0
- package/src/codec/websocket/frame/base/index.ts +55 -0
- package/src/codec/websocket/frame/binary/index.ts +24 -0
- package/src/codec/websocket/frame/close/index.ts +82 -0
- package/src/codec/websocket/frame/continuation/index.ts +31 -0
- package/src/codec/websocket/frame/index.ts +10 -0
- package/src/codec/websocket/frame/opcode/index.ts +35 -0
- package/src/codec/websocket/frame/ping/index.ts +24 -0
- package/src/codec/websocket/frame/pong/index.ts +24 -0
- package/src/codec/websocket/frame/text/index.ts +31 -0
- package/src/codec/websocket/handshake/index.ts +38 -0
- package/src/codec/websocket/index.ts +25 -0
- package/src/codec/websocket/key/index.ts +41 -0
- package/src/codec/websocket/protocol/index.ts +147 -0
- package/src/codec/websocket/server/index.ts +233 -0
- package/src/codec/websocket/status/index.ts +97 -0
- package/src/codec/websocket/upgrade/index.ts +116 -0
- package/src/datagram/bootstrap/index.ts +269 -0
- package/src/datagram/channel/index.ts +103 -0
- package/src/datagram/index.ts +10 -0
- package/src/datagram/packet/index.ts +72 -0
- package/src/datagram/transport/index.ts +239 -0
- package/src/handler/flush/index.ts +187 -0
- package/src/handler/idle/index.ts +295 -0
- package/src/handler/index.ts +22 -0
- package/src/handler/logging/index.ts +195 -0
- package/src/handler/metrics/index.ts +300 -0
- package/src/handler/offload/index.ts +141 -0
- package/src/handler/timeout/error/index.ts +29 -0
- package/src/handler/timeout/index.ts +3 -0
- package/src/handler/timeout/read/index.ts +44 -0
- package/src/handler/timeout/write/index.ts +89 -0
- package/src/handler/traffic/index.ts +259 -0
- package/src/index.ts +258 -0
- package/src/logger/index.ts +47 -0
- package/src/reliable/bootstrap/index.ts +154 -0
- package/src/reliable/cipher/index.ts +227 -0
- package/src/reliable/cookie/index.ts +78 -0
- package/src/reliable/handler/index.ts +1202 -0
- package/src/reliable/handshake/index.ts +144 -0
- package/src/reliable/index.ts +38 -0
- package/src/reliable/lane/index.ts +420 -0
- package/src/reliable/message/index.ts +102 -0
- package/src/reliable/packet/index.ts +117 -0
- package/src/reliable/peer/index.ts +929 -0
- package/src/reliable/registry/index.ts +107 -0
- package/src/serve/bootstrap/index.ts +606 -0
- package/src/serve/channel/index.ts +246 -0
- package/src/serve/index.ts +17 -0
- package/src/serve/outbox/index.ts +292 -0
- package/src/serve/transport/index.ts +96 -0
- package/src/serve/writer/index.ts +119 -0
- package/src/shard/envelope/index.ts +44 -0
- package/src/shard/index.ts +10 -0
- package/src/shard/link/index.ts +743 -0
- package/src/shard/map/index.ts +91 -0
- package/src/worker/group/index.ts +175 -0
- package/src/worker/index.ts +10 -0
- package/src/worker/pool/index.ts +235 -0
- package/src/worker/service/index.ts +55 -0
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ConnectField,
|
|
3
|
+
PacketKind,
|
|
4
|
+
acceptSize,
|
|
5
|
+
challengeSize,
|
|
6
|
+
connectSize,
|
|
7
|
+
cookieSize,
|
|
8
|
+
dataHeaderSize,
|
|
9
|
+
encryptFlag,
|
|
10
|
+
keySize,
|
|
11
|
+
protocolMagic,
|
|
12
|
+
} from '../packet/index.ts'
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A padded connect packet
|
|
16
|
+
* @param salt the client's random number
|
|
17
|
+
* @param time cookie timestamp, 0 without one
|
|
18
|
+
* @param cookie the server's cookie, or null
|
|
19
|
+
* @param publicKey our key when encrypting, or null
|
|
20
|
+
*/
|
|
21
|
+
export const connectPacket = (
|
|
22
|
+
salt: number,
|
|
23
|
+
time: number,
|
|
24
|
+
cookie: Uint8Array | null,
|
|
25
|
+
publicKey: Uint8Array | null,
|
|
26
|
+
): Uint8Array => {
|
|
27
|
+
const bytes = new Uint8Array(connectSize)
|
|
28
|
+
bytes[0] = PacketKind.connect
|
|
29
|
+
writeUint32(bytes, 1, protocolMagic)
|
|
30
|
+
writeUint32(bytes, ConnectField.salt, salt)
|
|
31
|
+
bytes[ConnectField.flags] = publicKey === null ? 0 : encryptFlag
|
|
32
|
+
writeUint32(bytes, ConnectField.time, time)
|
|
33
|
+
if (cookie !== null) bytes.set(cookie, ConnectField.cookie)
|
|
34
|
+
if (publicKey !== null) bytes.set(publicKey, ConnectField.key)
|
|
35
|
+
|
|
36
|
+
return bytes
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The server's cookie for a client to send back
|
|
41
|
+
* @param salt the client's random number
|
|
42
|
+
* @param time when the cookie was issued
|
|
43
|
+
* @param cookie the cookie
|
|
44
|
+
*/
|
|
45
|
+
export const challengePacket = (
|
|
46
|
+
salt: number,
|
|
47
|
+
time: number,
|
|
48
|
+
cookie: Uint8Array,
|
|
49
|
+
): Uint8Array => {
|
|
50
|
+
const bytes = new Uint8Array(challengeSize)
|
|
51
|
+
bytes[0] = PacketKind.challenge
|
|
52
|
+
writeUint32(bytes, 1, protocolMagic)
|
|
53
|
+
writeUint32(bytes, 5, salt)
|
|
54
|
+
writeUint32(bytes, 9, time)
|
|
55
|
+
bytes.set(cookie.subarray(0, cookieSize), 13)
|
|
56
|
+
|
|
57
|
+
return bytes
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The server letting a client in
|
|
62
|
+
* @param salt the client's random number
|
|
63
|
+
* @param session the new session id
|
|
64
|
+
* @param publicKey the server's key when encrypting, or null
|
|
65
|
+
*/
|
|
66
|
+
export const acceptPacket = (
|
|
67
|
+
salt: number,
|
|
68
|
+
session: number,
|
|
69
|
+
publicKey: Uint8Array | null,
|
|
70
|
+
): Uint8Array => {
|
|
71
|
+
const bytes = new Uint8Array(acceptSize + (publicKey === null ? 0 : keySize))
|
|
72
|
+
bytes[0] = PacketKind.accept
|
|
73
|
+
writeUint32(bytes, 1, protocolMagic)
|
|
74
|
+
writeUint32(bytes, 5, salt)
|
|
75
|
+
writeUint32(bytes, 9, session)
|
|
76
|
+
if (publicKey !== null) bytes.set(publicKey, acceptSize)
|
|
77
|
+
|
|
78
|
+
return bytes
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* A hang up, as long as a data header so it can be sealed
|
|
83
|
+
* @param session the session id
|
|
84
|
+
*/
|
|
85
|
+
export const disconnectPacket = (session: number): Uint8Array => {
|
|
86
|
+
const bytes = new Uint8Array(dataHeaderSize)
|
|
87
|
+
bytes[0] = PacketKind.disconnect
|
|
88
|
+
writeUint32(bytes, 1, session)
|
|
89
|
+
|
|
90
|
+
return bytes
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Salt for key derivation, ties the keys to this session
|
|
95
|
+
* @param salt the client's random number
|
|
96
|
+
* @param session the session id
|
|
97
|
+
*/
|
|
98
|
+
export const keySalt = (salt: number, session: number): Uint8Array => {
|
|
99
|
+
const bytes = new Uint8Array(8)
|
|
100
|
+
writeUint32(bytes, 0, salt)
|
|
101
|
+
writeUint32(bytes, 4, session)
|
|
102
|
+
|
|
103
|
+
return bytes
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Whether a packet starts with our magic number
|
|
108
|
+
* @param bytes the packet
|
|
109
|
+
*/
|
|
110
|
+
export const isOurs = (bytes: Uint8Array): boolean => {
|
|
111
|
+
return readUint32(bytes, 1) === protocolMagic
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Reads a big endian 32 bit number
|
|
116
|
+
* @param bytes where to read
|
|
117
|
+
* @param offset where it starts
|
|
118
|
+
*/
|
|
119
|
+
export const readUint32 = (bytes: Uint8Array, offset: number): number => {
|
|
120
|
+
return (
|
|
121
|
+
(((bytes[offset] ?? 0) << 24) |
|
|
122
|
+
((bytes[offset + 1] ?? 0) << 16) |
|
|
123
|
+
((bytes[offset + 2] ?? 0) << 8) |
|
|
124
|
+
(bytes[offset + 3] ?? 0)) >>>
|
|
125
|
+
0
|
|
126
|
+
)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Writes a big endian 32 bit number
|
|
131
|
+
* @param bytes where to write
|
|
132
|
+
* @param offset where it starts
|
|
133
|
+
* @param value the number
|
|
134
|
+
*/
|
|
135
|
+
export const writeUint32 = (
|
|
136
|
+
bytes: Uint8Array,
|
|
137
|
+
offset: number,
|
|
138
|
+
value: number,
|
|
139
|
+
): void => {
|
|
140
|
+
bytes[offset] = value >>> 24
|
|
141
|
+
bytes[offset + 1] = (value >>> 16) & 0xff
|
|
142
|
+
bytes[offset + 2] = (value >>> 8) & 0xff
|
|
143
|
+
bytes[offset + 3] = value & 0xff
|
|
144
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export { ReliableBootstrap } from './bootstrap/index.ts'
|
|
2
|
+
export type {
|
|
3
|
+
ReliableClient,
|
|
4
|
+
ReliableServer,
|
|
5
|
+
ReliableSettings,
|
|
6
|
+
} from './bootstrap/index.ts'
|
|
7
|
+
export { PacketCipher } from './cipher/index.ts'
|
|
8
|
+
export type { KeyPair } from './cipher/index.ts'
|
|
9
|
+
export { CookieJar } from './cookie/index.ts'
|
|
10
|
+
export { ReliableHandler } from './handler/index.ts'
|
|
11
|
+
export {
|
|
12
|
+
acceptPacket,
|
|
13
|
+
challengePacket,
|
|
14
|
+
connectPacket,
|
|
15
|
+
disconnectPacket,
|
|
16
|
+
isOurs,
|
|
17
|
+
keySalt,
|
|
18
|
+
readUint32,
|
|
19
|
+
writeUint32,
|
|
20
|
+
} from './handshake/index.ts'
|
|
21
|
+
export type {
|
|
22
|
+
EncryptionOptions,
|
|
23
|
+
ReliableOptions,
|
|
24
|
+
ReliableRole,
|
|
25
|
+
SimulateOptions,
|
|
26
|
+
} from './handler/index.ts'
|
|
27
|
+
export { Lane } from './lane/index.ts'
|
|
28
|
+
export type { Deliver, Delivery, LaneOptions, Outgoing } from './lane/index.ts'
|
|
29
|
+
export {
|
|
30
|
+
PeerConnected,
|
|
31
|
+
PeerDisconnected,
|
|
32
|
+
ReliableMessage,
|
|
33
|
+
} from './message/index.ts'
|
|
34
|
+
export type { DisconnectReason } from './message/index.ts'
|
|
35
|
+
export { ReliablePeer } from './peer/index.ts'
|
|
36
|
+
export { PeerRegistry } from './registry/index.ts'
|
|
37
|
+
export type { PeerOptions, PeerState, PeerStats } from './peer/index.ts'
|
|
38
|
+
export { PacketKind, isNewer } from './packet/index.ts'
|
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
import { isNewer } from '../packet/index.ts'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* How messages on a lane get delivered
|
|
5
|
+
*/
|
|
6
|
+
export type Delivery =
|
|
7
|
+
| 'reliable-ordered'
|
|
8
|
+
| 'reliable-unordered'
|
|
9
|
+
| 'unreliable-sequenced'
|
|
10
|
+
| 'unreliable'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Settings for one lane
|
|
14
|
+
*/
|
|
15
|
+
export type LaneOptions = {
|
|
16
|
+
/**
|
|
17
|
+
* Delivery guarantee for everything sent on this lane
|
|
18
|
+
*/
|
|
19
|
+
readonly delivery: Delivery
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* A message, or one fragment of one, on its way out
|
|
24
|
+
*/
|
|
25
|
+
export type Outgoing = {
|
|
26
|
+
/**
|
|
27
|
+
* Which lane it's on
|
|
28
|
+
*/
|
|
29
|
+
readonly lane: number
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Its id on that lane
|
|
33
|
+
*/
|
|
34
|
+
readonly id: number
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Fragment position, 0 when it isn't fragmented
|
|
38
|
+
*/
|
|
39
|
+
readonly index: number
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Fragments in the whole message, 1 when it isn't fragmented
|
|
43
|
+
*/
|
|
44
|
+
readonly count: number
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The bytes
|
|
48
|
+
*/
|
|
49
|
+
readonly payload: Uint8Array
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* When it last went out, minus infinity before the first send
|
|
53
|
+
*/
|
|
54
|
+
lastSent: number
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* How many times it's been sent
|
|
58
|
+
*/
|
|
59
|
+
sends: number
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Set once a packet carrying it is acked
|
|
63
|
+
*/
|
|
64
|
+
acked: boolean
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Gets each message once it's ready to hand over
|
|
69
|
+
*/
|
|
70
|
+
export type Deliver = (content: Uint8Array) => void
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* A fragment held until the rest of its message turns up
|
|
74
|
+
*/
|
|
75
|
+
type Piece = {
|
|
76
|
+
/**
|
|
77
|
+
* Position in its message
|
|
78
|
+
*/
|
|
79
|
+
readonly index: number
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Fragments in the whole message
|
|
83
|
+
*/
|
|
84
|
+
readonly count: number
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* The bytes
|
|
88
|
+
*/
|
|
89
|
+
readonly payload: Uint8Array
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* One lane's send queue and receive state for one peer
|
|
94
|
+
* @class
|
|
95
|
+
*/
|
|
96
|
+
export class Lane {
|
|
97
|
+
/**
|
|
98
|
+
* Delivery guarantee
|
|
99
|
+
*/
|
|
100
|
+
public readonly delivery: Delivery
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* True for the two reliable modes
|
|
104
|
+
*/
|
|
105
|
+
public readonly reliable: boolean
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Waiting for room in the window, or for the next flush when unreliable
|
|
109
|
+
*/
|
|
110
|
+
public readonly queue: Outgoing[]
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Sent but not acked yet, reliable lanes only
|
|
114
|
+
*/
|
|
115
|
+
public readonly inFlight: Map<number, Outgoing>
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Bytes sitting in queue and inFlight
|
|
119
|
+
*/
|
|
120
|
+
public queuedBytes: number
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Next id to hand out
|
|
124
|
+
*/
|
|
125
|
+
private nextId: number
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Ordered lanes, id we're waiting to deliver next
|
|
129
|
+
*/
|
|
130
|
+
private nextDeliver: number
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Ordered lanes, arrivals held until the gap before them fills
|
|
134
|
+
*/
|
|
135
|
+
private readonly held: Map<number, Piece>
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Unordered lanes, which ids have already been delivered
|
|
139
|
+
*/
|
|
140
|
+
private readonly seen: Uint8Array
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Unordered lanes, fragments by the id of their first piece
|
|
144
|
+
*/
|
|
145
|
+
private readonly groups: Map<number, Piece[]>
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Sequenced lanes, newest id delivered
|
|
149
|
+
*/
|
|
150
|
+
private latest: number
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Sequenced lanes, whether anything has arrived yet
|
|
154
|
+
*/
|
|
155
|
+
private started: boolean
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Furthest ahead an id may be before it's treated as junk
|
|
159
|
+
*/
|
|
160
|
+
private readonly window: number
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Oldest id still waiting for an ack
|
|
164
|
+
*/
|
|
165
|
+
private oldest: number
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Next id to go in flight
|
|
169
|
+
*/
|
|
170
|
+
private launched: number
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Fresh state for one lane
|
|
174
|
+
* @param options delivery mode
|
|
175
|
+
* @param window ids that can be in flight or held at once
|
|
176
|
+
*/
|
|
177
|
+
public constructor(options: LaneOptions, window: number) {
|
|
178
|
+
this.delivery = options.delivery
|
|
179
|
+
this.reliable =
|
|
180
|
+
options.delivery === 'reliable-ordered' ||
|
|
181
|
+
options.delivery === 'reliable-unordered'
|
|
182
|
+
this.queue = []
|
|
183
|
+
this.inFlight = new Map<number, Outgoing>()
|
|
184
|
+
this.queuedBytes = 0
|
|
185
|
+
this.nextId = 0
|
|
186
|
+
this.nextDeliver = 0
|
|
187
|
+
this.held = new Map<number, Piece>()
|
|
188
|
+
this.seen = new Uint8Array(
|
|
189
|
+
options.delivery === 'reliable-unordered' ? 65536 : 0,
|
|
190
|
+
)
|
|
191
|
+
this.groups = new Map<number, Piece[]>()
|
|
192
|
+
this.latest = 0
|
|
193
|
+
this.started = false
|
|
194
|
+
this.window = window
|
|
195
|
+
this.oldest = 0
|
|
196
|
+
this.launched = 0
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Whether another message fits in the send window
|
|
201
|
+
* @param limit congestion window, can be smaller than ours
|
|
202
|
+
*/
|
|
203
|
+
public hasRoom(limit: number): boolean {
|
|
204
|
+
return (
|
|
205
|
+
((this.launched - this.oldest) & 0xffff) < Math.min(this.window, limit)
|
|
206
|
+
)
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Moves a queued message into flight
|
|
211
|
+
* @param message the next one in the queue
|
|
212
|
+
*/
|
|
213
|
+
public launch(message: Outgoing): void {
|
|
214
|
+
this.inFlight.set(message.id, message)
|
|
215
|
+
this.launched = (message.id + 1) & 0xffff
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Queues a message, already cut into fragments if it needed it
|
|
220
|
+
* @param lane this lane's number
|
|
221
|
+
* @param pieces the message, one piece unless it was fragmented
|
|
222
|
+
*/
|
|
223
|
+
public enqueue(lane: number, pieces: readonly Uint8Array[]): void {
|
|
224
|
+
const count = pieces.length
|
|
225
|
+
|
|
226
|
+
for (let index = 0; index < count; index++) {
|
|
227
|
+
const payload = pieces[index] ?? new Uint8Array(0)
|
|
228
|
+
this.queue.push({
|
|
229
|
+
lane,
|
|
230
|
+
id: this.nextId,
|
|
231
|
+
index: count === 1 ? 0 : index,
|
|
232
|
+
count,
|
|
233
|
+
payload,
|
|
234
|
+
lastSent: Number.NEGATIVE_INFINITY,
|
|
235
|
+
sends: 0,
|
|
236
|
+
acked: false,
|
|
237
|
+
})
|
|
238
|
+
this.nextId = (this.nextId + 1) & 0xffff
|
|
239
|
+
this.queuedBytes += payload.length
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Forgets a message once the peer has it
|
|
245
|
+
* @param message the acked message
|
|
246
|
+
*/
|
|
247
|
+
public acknowledge(message: Outgoing): void {
|
|
248
|
+
if (message.acked) return
|
|
249
|
+
message.acked = true
|
|
250
|
+
|
|
251
|
+
this.inFlight.delete(message.id)
|
|
252
|
+
this.queuedBytes -= message.payload.length
|
|
253
|
+
while (this.oldest !== this.launched && !this.inFlight.has(this.oldest)) {
|
|
254
|
+
this.oldest = (this.oldest + 1) & 0xffff
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Takes in an arrival and hands back whatever can be delivered now
|
|
260
|
+
* @param id its id on this lane
|
|
261
|
+
* @param index fragment position
|
|
262
|
+
* @param count fragments in the whole message
|
|
263
|
+
* @param payload the bytes, a view into the packet
|
|
264
|
+
* @param deliver gets each message that's ready
|
|
265
|
+
*/
|
|
266
|
+
public receive(
|
|
267
|
+
id: number,
|
|
268
|
+
index: number,
|
|
269
|
+
count: number,
|
|
270
|
+
payload: Uint8Array,
|
|
271
|
+
deliver: Deliver,
|
|
272
|
+
): void {
|
|
273
|
+
switch (this.delivery) {
|
|
274
|
+
case 'unreliable':
|
|
275
|
+
deliver(payload)
|
|
276
|
+
return
|
|
277
|
+
case 'unreliable-sequenced':
|
|
278
|
+
this.sequenced(id, payload, deliver)
|
|
279
|
+
return
|
|
280
|
+
case 'reliable-unordered':
|
|
281
|
+
this.unordered(id, index, count, payload, deliver)
|
|
282
|
+
return
|
|
283
|
+
case 'reliable-ordered':
|
|
284
|
+
this.ordered(id, index, count, payload, deliver)
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Keeps only the newest, older arrivals are dropped
|
|
290
|
+
* @param id its id
|
|
291
|
+
* @param payload the bytes
|
|
292
|
+
* @param deliver gets it if it's the newest
|
|
293
|
+
*/
|
|
294
|
+
private sequenced(id: number, payload: Uint8Array, deliver: Deliver): void {
|
|
295
|
+
if (this.started && !isNewer(id, this.latest)) return
|
|
296
|
+
|
|
297
|
+
this.started = true
|
|
298
|
+
this.latest = id
|
|
299
|
+
deliver(payload)
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Delivers each message once, as soon as it's whole
|
|
304
|
+
* @param id its id
|
|
305
|
+
* @param index fragment position
|
|
306
|
+
* @param count fragments in the whole message
|
|
307
|
+
* @param payload the bytes
|
|
308
|
+
* @param deliver gets it once it's whole
|
|
309
|
+
*/
|
|
310
|
+
private unordered(
|
|
311
|
+
id: number,
|
|
312
|
+
index: number,
|
|
313
|
+
count: number,
|
|
314
|
+
payload: Uint8Array,
|
|
315
|
+
deliver: Deliver,
|
|
316
|
+
): void {
|
|
317
|
+
if (this.seen[id] === 1) return
|
|
318
|
+
if (count === 1) {
|
|
319
|
+
this.mark(id)
|
|
320
|
+
deliver(payload)
|
|
321
|
+
return
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
const base = (id - index) & 0xffff
|
|
325
|
+
const group = this.groups.get(base)
|
|
326
|
+
if (group === undefined && this.groups.size >= this.window) return
|
|
327
|
+
this.mark(id)
|
|
328
|
+
|
|
329
|
+
const pieces = group ?? []
|
|
330
|
+
pieces.push({ index, count, payload: payload.slice() })
|
|
331
|
+
if (pieces.length < count) {
|
|
332
|
+
this.groups.set(base, pieces)
|
|
333
|
+
return
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
this.groups.delete(base)
|
|
337
|
+
pieces.sort(
|
|
338
|
+
(first: Piece, second: Piece): number => first.index - second.index,
|
|
339
|
+
)
|
|
340
|
+
deliver(Lane.join(pieces))
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Notes an id as delivered and frees the one half a lap away
|
|
345
|
+
* @param id its id
|
|
346
|
+
*/
|
|
347
|
+
private mark(id: number): void {
|
|
348
|
+
this.seen[id] = 1
|
|
349
|
+
this.seen[(id + 32768) & 0xffff] = 0
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Holds arrivals until everything before them is in, then delivers in order
|
|
354
|
+
* @param id its id
|
|
355
|
+
* @param index fragment position
|
|
356
|
+
* @param count fragments in the whole message
|
|
357
|
+
* @param payload the bytes
|
|
358
|
+
* @param deliver gets each message in order
|
|
359
|
+
*/
|
|
360
|
+
private ordered(
|
|
361
|
+
id: number,
|
|
362
|
+
index: number,
|
|
363
|
+
count: number,
|
|
364
|
+
payload: Uint8Array,
|
|
365
|
+
deliver: Deliver,
|
|
366
|
+
): void {
|
|
367
|
+
const ahead = (id - this.nextDeliver) & 0xffff
|
|
368
|
+
if (ahead >= this.window || this.held.has(id)) return
|
|
369
|
+
|
|
370
|
+
if (ahead === 0 && count === 1) {
|
|
371
|
+
deliver(payload)
|
|
372
|
+
this.nextDeliver = (this.nextDeliver + 1) & 0xffff
|
|
373
|
+
} else {
|
|
374
|
+
this.held.set(id, { index, count, payload: payload.slice() })
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
for (;;) {
|
|
378
|
+
const head = this.held.get(this.nextDeliver)
|
|
379
|
+
if (head === undefined) break
|
|
380
|
+
if (head.count === 1) {
|
|
381
|
+
this.held.delete(this.nextDeliver)
|
|
382
|
+
deliver(head.payload)
|
|
383
|
+
this.nextDeliver = (this.nextDeliver + 1) & 0xffff
|
|
384
|
+
continue
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
const pieces: Piece[] = []
|
|
388
|
+
for (let offset = 0; offset < head.count; offset++) {
|
|
389
|
+
const piece = this.held.get((this.nextDeliver + offset) & 0xffff)
|
|
390
|
+
if (piece === undefined) return
|
|
391
|
+
pieces.push(piece)
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
for (let offset = 0; offset < head.count; offset++) {
|
|
395
|
+
this.held.delete((this.nextDeliver + offset) & 0xffff)
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
deliver(Lane.join(pieces))
|
|
399
|
+
this.nextDeliver = (this.nextDeliver + head.count) & 0xffff
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* Glues fragments back together in order
|
|
405
|
+
* @param pieces every fragment of one message, sorted
|
|
406
|
+
*/
|
|
407
|
+
private static join(pieces: readonly Piece[]): Uint8Array {
|
|
408
|
+
let size = 0
|
|
409
|
+
for (const piece of pieces) size += piece.payload.length
|
|
410
|
+
|
|
411
|
+
const whole = new Uint8Array(size)
|
|
412
|
+
let offset = 0
|
|
413
|
+
for (const piece of pieces) {
|
|
414
|
+
whole.set(piece.payload, offset)
|
|
415
|
+
offset += piece.payload.length
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
return whole
|
|
419
|
+
}
|
|
420
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { ByteBuf } from '../../buffer/index.ts'
|
|
2
|
+
import type { ReliablePeer } from '../peer/index.ts'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Why a peer went away
|
|
6
|
+
*/
|
|
7
|
+
export type DisconnectReason = 'closed' | 'timeout' | 'resends' | 'local'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A whole message from a peer, or one to send to it
|
|
11
|
+
* @class
|
|
12
|
+
*/
|
|
13
|
+
export class ReliableMessage {
|
|
14
|
+
/**
|
|
15
|
+
* Who it's from, or who it's for
|
|
16
|
+
*/
|
|
17
|
+
public readonly peer: ReliablePeer
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Lane number it travels on
|
|
21
|
+
*/
|
|
22
|
+
public readonly lane: number
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The bytes
|
|
26
|
+
*/
|
|
27
|
+
public readonly content: ByteBuf
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Strings and raw bytes get wrapped in a ByteBuf for you
|
|
31
|
+
* @param peer who it's from or for
|
|
32
|
+
* @param lane lane number
|
|
33
|
+
* @param content the bytes
|
|
34
|
+
*/
|
|
35
|
+
public constructor(
|
|
36
|
+
peer: ReliablePeer,
|
|
37
|
+
lane: number,
|
|
38
|
+
content: ByteBuf | Uint8Array | string,
|
|
39
|
+
) {
|
|
40
|
+
this.peer = peer
|
|
41
|
+
this.lane = lane
|
|
42
|
+
this.content =
|
|
43
|
+
content instanceof ByteBuf
|
|
44
|
+
? content
|
|
45
|
+
: typeof content === 'string'
|
|
46
|
+
? ByteBuf.fromString(content)
|
|
47
|
+
: ByteBuf.wrap(content)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Sends something back on the same lane
|
|
52
|
+
* @param content what to send
|
|
53
|
+
*/
|
|
54
|
+
public reply(content: ByteBuf | Uint8Array | string): void {
|
|
55
|
+
this.peer.send(this.lane, content)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Fired as a user event once a peer finishes the handshake
|
|
61
|
+
* @class
|
|
62
|
+
*/
|
|
63
|
+
export class PeerConnected {
|
|
64
|
+
/**
|
|
65
|
+
* The new peer
|
|
66
|
+
*/
|
|
67
|
+
public readonly peer: ReliablePeer
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Wraps the peer
|
|
71
|
+
* @param peer the new peer
|
|
72
|
+
*/
|
|
73
|
+
public constructor(peer: ReliablePeer) {
|
|
74
|
+
this.peer = peer
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Fired as a user event once a peer is gone
|
|
80
|
+
* @class
|
|
81
|
+
*/
|
|
82
|
+
export class PeerDisconnected {
|
|
83
|
+
/**
|
|
84
|
+
* The peer that left
|
|
85
|
+
*/
|
|
86
|
+
public readonly peer: ReliablePeer
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Why it left
|
|
90
|
+
*/
|
|
91
|
+
public readonly reason: DisconnectReason
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Wraps the peer and the reason
|
|
95
|
+
* @param peer the peer that left
|
|
96
|
+
* @param reason why it left
|
|
97
|
+
*/
|
|
98
|
+
public constructor(peer: ReliablePeer, reason: DisconnectReason) {
|
|
99
|
+
this.peer = peer
|
|
100
|
+
this.reason = reason
|
|
101
|
+
}
|
|
102
|
+
}
|