@sanctumterra/raknet 1.3.80 → 1.4.1

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.
Files changed (276) hide show
  1. package/README.md +1 -1
  2. package/dist/client/client.d.ts +19 -20
  3. package/dist/client/client.d.ts.map +1 -1
  4. package/dist/client/client.js +139 -184
  5. package/dist/client/types/client-events.d.ts +5 -20
  6. package/dist/client/types/client-events.d.ts.map +1 -1
  7. package/dist/client/types/client-options.d.ts +7 -8
  8. package/dist/client/types/client-options.d.ts.map +1 -1
  9. package/dist/client/types/client-options.js +9 -9
  10. package/dist/client/types/index.d.ts +1 -1
  11. package/dist/client/types/index.d.ts.map +1 -1
  12. package/dist/client/types/index.js +1 -1
  13. package/dist/index.d.ts +1 -3
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +1 -3
  16. package/dist/server/connection.d.ts +20 -59
  17. package/dist/server/connection.d.ts.map +1 -1
  18. package/dist/server/connection.js +53 -365
  19. package/dist/server/index.d.ts +2 -3
  20. package/dist/server/index.d.ts.map +1 -1
  21. package/dist/server/index.js +2 -3
  22. package/dist/server/server.d.ts +11 -18
  23. package/dist/server/server.d.ts.map +1 -1
  24. package/dist/server/server.js +98 -137
  25. package/dist/{proto → server}/types/advertisement.d.ts +1 -1
  26. package/dist/server/types/advertisement.d.ts.map +1 -0
  27. package/dist/{proto → server}/types/advertisement.js +14 -10
  28. package/dist/server/types/connection-events.d.ts +4 -0
  29. package/dist/server/types/connection-events.d.ts.map +1 -0
  30. package/dist/server/types/index.d.ts +5 -0
  31. package/dist/server/types/index.d.ts.map +1 -0
  32. package/dist/{proto → server}/types/index.js +3 -1
  33. package/dist/server/types/server-events.d.ts +7 -0
  34. package/dist/server/types/server-events.d.ts.map +1 -0
  35. package/dist/server/types/server-options.d.ts +12 -0
  36. package/dist/server/types/server-options.d.ts.map +1 -0
  37. package/dist/server/types/server-options.js +14 -0
  38. package/dist/shared/emitter.d.ts +28 -0
  39. package/dist/shared/emitter.d.ts.map +1 -0
  40. package/dist/shared/emitter.js +71 -0
  41. package/dist/shared/index.d.ts +5 -0
  42. package/dist/shared/index.d.ts.map +1 -0
  43. package/dist/{proto/decorators → shared}/index.js +4 -2
  44. package/dist/{utils/Logger.d.ts → shared/logger.d.ts} +1 -1
  45. package/dist/shared/logger.d.ts.map +1 -0
  46. package/dist/shared/network_session.d.ts +34 -0
  47. package/dist/shared/network_session.d.ts.map +1 -0
  48. package/dist/shared/network_session.js +247 -0
  49. package/dist/shared/proto/enums/connectionStatus.d.ts +8 -0
  50. package/dist/shared/proto/enums/connectionStatus.d.ts.map +1 -0
  51. package/dist/shared/proto/enums/connectionStatus.js +11 -0
  52. package/dist/shared/proto/enums/index.d.ts +5 -0
  53. package/dist/shared/proto/enums/index.d.ts.map +1 -0
  54. package/dist/{proto → shared/proto}/enums/index.js +2 -3
  55. package/dist/shared/proto/enums/packets.d.ts +19 -0
  56. package/dist/shared/proto/enums/packets.d.ts.map +1 -0
  57. package/dist/shared/proto/enums/packets.js +23 -0
  58. package/dist/shared/proto/enums/priority.d.ts +6 -0
  59. package/dist/shared/proto/enums/priority.d.ts.map +1 -0
  60. package/dist/shared/proto/enums/priority.js +4 -0
  61. package/dist/shared/proto/enums/reliability.d.ts.map +1 -0
  62. package/dist/shared/proto/index.d.ts +5 -0
  63. package/dist/shared/proto/index.d.ts.map +1 -0
  64. package/dist/{proto → shared/proto}/index.js +3 -3
  65. package/dist/shared/proto/offline/index.d.ts +7 -0
  66. package/dist/shared/proto/offline/index.d.ts.map +1 -0
  67. package/dist/shared/proto/offline/index.js +22 -0
  68. package/dist/shared/proto/offline/open-connection-reply-one.d.ts +9 -0
  69. package/dist/shared/proto/offline/open-connection-reply-one.d.ts.map +1 -0
  70. package/dist/shared/proto/offline/open-connection-reply-one.js +28 -0
  71. package/dist/shared/proto/offline/open-connection-reply-two.d.ts +11 -0
  72. package/dist/shared/proto/offline/open-connection-reply-two.d.ts.map +1 -0
  73. package/dist/shared/proto/offline/open-connection-reply-two.js +31 -0
  74. package/dist/shared/proto/offline/open-connection-request-one.d.ts +8 -0
  75. package/dist/shared/proto/offline/open-connection-request-one.d.ts.map +1 -0
  76. package/dist/shared/proto/offline/open-connection-request-one.js +25 -0
  77. package/dist/shared/proto/offline/open-connection-request-two.d.ts +10 -0
  78. package/dist/shared/proto/offline/open-connection-request-two.d.ts.map +1 -0
  79. package/dist/shared/proto/offline/open-connection-request-two.js +28 -0
  80. package/dist/shared/proto/offline/unconnected-ping.d.ts +8 -0
  81. package/dist/shared/proto/offline/unconnected-ping.d.ts.map +1 -0
  82. package/dist/shared/proto/offline/unconnected-ping.js +25 -0
  83. package/dist/shared/proto/offline/unconnected-pong.d.ts +9 -0
  84. package/dist/shared/proto/offline/unconnected-pong.d.ts.map +1 -0
  85. package/dist/shared/proto/offline/unconnected-pong.js +28 -0
  86. package/dist/shared/proto/online/ack.d.ts +7 -0
  87. package/dist/shared/proto/online/ack.d.ts.map +1 -0
  88. package/dist/{proto/packets → shared/proto/online}/ack.js +16 -31
  89. package/dist/shared/proto/online/connected-ping.d.ts +7 -0
  90. package/dist/shared/proto/online/connected-ping.d.ts.map +1 -0
  91. package/dist/shared/proto/online/connected-ping.js +19 -0
  92. package/dist/shared/proto/online/connected-pong.d.ts +8 -0
  93. package/dist/shared/proto/online/connected-pong.d.ts.map +1 -0
  94. package/dist/shared/proto/online/connected-pong.js +22 -0
  95. package/dist/shared/proto/online/connection-request-accepted.d.ts +12 -0
  96. package/dist/shared/proto/online/connection-request-accepted.d.ts.map +1 -0
  97. package/dist/shared/proto/online/connection-request-accepted.js +37 -0
  98. package/dist/shared/proto/online/connection-request.d.ts +9 -0
  99. package/dist/shared/proto/online/connection-request.d.ts.map +1 -0
  100. package/dist/shared/proto/online/connection-request.js +25 -0
  101. package/dist/shared/proto/online/disconnect-message.d.ts +6 -0
  102. package/dist/shared/proto/online/disconnect-message.d.ts.map +1 -0
  103. package/dist/shared/proto/online/disconnect-message.js +15 -0
  104. package/dist/shared/proto/online/frame-set.d.ts +9 -0
  105. package/dist/shared/proto/online/frame-set.d.ts.map +1 -0
  106. package/dist/shared/proto/online/frame-set.js +23 -0
  107. package/dist/shared/proto/online/index.d.ts +9 -0
  108. package/dist/shared/proto/online/index.d.ts.map +1 -0
  109. package/dist/shared/proto/online/index.js +24 -0
  110. package/dist/shared/proto/online/new-incomming-connection.d.ts +11 -0
  111. package/dist/shared/proto/online/new-incomming-connection.d.ts.map +1 -0
  112. package/dist/shared/proto/online/new-incomming-connection.js +33 -0
  113. package/dist/shared/proto/types/address.d.ts +12 -0
  114. package/dist/shared/proto/types/address.d.ts.map +1 -0
  115. package/dist/shared/proto/types/address.js +61 -0
  116. package/dist/shared/proto/types/frame.d.ts +23 -0
  117. package/dist/shared/proto/types/frame.d.ts.map +1 -0
  118. package/dist/shared/proto/types/frame.js +119 -0
  119. package/dist/{proto/packets → shared/proto}/types/index.d.ts +0 -2
  120. package/dist/shared/proto/types/index.d.ts.map +1 -0
  121. package/dist/{proto/packets → shared/proto}/types/index.js +0 -2
  122. package/dist/shared/proto/types/magic.d.ts +7 -0
  123. package/dist/shared/proto/types/magic.d.ts.map +1 -0
  124. package/dist/shared/proto/types/magic.js +15 -0
  125. package/dist/shared/proto/types/mtu.d.ts +7 -0
  126. package/dist/shared/proto/types/mtu.d.ts.map +1 -0
  127. package/dist/shared/proto/types/mtu.js +13 -0
  128. package/dist/tests/client-connect.d.ts +2 -0
  129. package/dist/tests/client-connect.d.ts.map +1 -0
  130. package/dist/tests/client-connect.js +32 -0
  131. package/dist/tests/client-once.d.ts +2 -0
  132. package/dist/tests/client-once.d.ts.map +1 -0
  133. package/dist/tests/client-once.js +11 -0
  134. package/dist/tests/server-listen.d.ts +2 -0
  135. package/dist/tests/server-listen.d.ts.map +1 -0
  136. package/dist/tests/server-listen.js +5 -0
  137. package/package.json +12 -6
  138. package/bun.lock +0 -67
  139. package/dist/client/framer.d.ts +0 -55
  140. package/dist/client/framer.d.ts.map +0 -1
  141. package/dist/client/framer.js +0 -458
  142. package/dist/proto/decorators/create.d.ts +0 -8
  143. package/dist/proto/decorators/create.d.ts.map +0 -1
  144. package/dist/proto/decorators/create.js +0 -77
  145. package/dist/proto/decorators/index.d.ts +0 -3
  146. package/dist/proto/decorators/index.d.ts.map +0 -1
  147. package/dist/proto/decorators/serialize.d.ts +0 -4
  148. package/dist/proto/decorators/serialize.d.ts.map +0 -1
  149. package/dist/proto/decorators/serialize.js +0 -12
  150. package/dist/proto/enums/flags.d.ts +0 -7
  151. package/dist/proto/enums/flags.d.ts.map +0 -1
  152. package/dist/proto/enums/flags.js +0 -10
  153. package/dist/proto/enums/index.d.ts +0 -6
  154. package/dist/proto/enums/index.d.ts.map +0 -1
  155. package/dist/proto/enums/packet.d.ts +0 -122
  156. package/dist/proto/enums/packet.d.ts.map +0 -1
  157. package/dist/proto/enums/packet.js +0 -125
  158. package/dist/proto/enums/priority.d.ts +0 -5
  159. package/dist/proto/enums/priority.d.ts.map +0 -1
  160. package/dist/proto/enums/priority.js +0 -8
  161. package/dist/proto/enums/reliability.d.ts.map +0 -1
  162. package/dist/proto/enums/status.d.ts +0 -7
  163. package/dist/proto/enums/status.d.ts.map +0 -1
  164. package/dist/proto/enums/status.js +0 -10
  165. package/dist/proto/index.d.ts +0 -5
  166. package/dist/proto/index.d.ts.map +0 -1
  167. package/dist/proto/packets/ack.d.ts +0 -10
  168. package/dist/proto/packets/ack.d.ts.map +0 -1
  169. package/dist/proto/packets/base-packet.d.ts +0 -26
  170. package/dist/proto/packets/base-packet.d.ts.map +0 -1
  171. package/dist/proto/packets/base-packet.js +0 -37
  172. package/dist/proto/packets/connected-ping.d.ts +0 -5
  173. package/dist/proto/packets/connected-ping.d.ts.map +0 -1
  174. package/dist/proto/packets/connected-ping.js +0 -27
  175. package/dist/proto/packets/connected-pong.d.ts +0 -6
  176. package/dist/proto/packets/connected-pong.d.ts.map +0 -1
  177. package/dist/proto/packets/connected-pong.js +0 -32
  178. package/dist/proto/packets/connection-request-accepted.d.ts +0 -10
  179. package/dist/proto/packets/connection-request-accepted.d.ts.map +0 -1
  180. package/dist/proto/packets/connection-request-accepted.js +0 -48
  181. package/dist/proto/packets/connection-request.d.ts +0 -7
  182. package/dist/proto/packets/connection-request.d.ts.map +0 -1
  183. package/dist/proto/packets/connection-request.js +0 -37
  184. package/dist/proto/packets/disconnect.d.ts +0 -5
  185. package/dist/proto/packets/disconnect.d.ts.map +0 -1
  186. package/dist/proto/packets/disconnect.js +0 -17
  187. package/dist/proto/packets/frame.d.ts +0 -1
  188. package/dist/proto/packets/frame.d.ts.map +0 -1
  189. package/dist/proto/packets/frame.js +0 -1
  190. package/dist/proto/packets/frameset.d.ts +0 -11
  191. package/dist/proto/packets/frameset.d.ts.map +0 -1
  192. package/dist/proto/packets/frameset.js +0 -37
  193. package/dist/proto/packets/incompatible-protocol-version.d.ts +0 -8
  194. package/dist/proto/packets/incompatible-protocol-version.d.ts.map +0 -1
  195. package/dist/proto/packets/incompatible-protocol-version.js +0 -38
  196. package/dist/proto/packets/index.d.ts +0 -19
  197. package/dist/proto/packets/index.d.ts.map +0 -1
  198. package/dist/proto/packets/index.js +0 -34
  199. package/dist/proto/packets/nack.d.ts +0 -8
  200. package/dist/proto/packets/nack.d.ts.map +0 -1
  201. package/dist/proto/packets/nack.js +0 -88
  202. package/dist/proto/packets/new-incoming-connection.d.ts +0 -9
  203. package/dist/proto/packets/new-incoming-connection.d.ts.map +0 -1
  204. package/dist/proto/packets/new-incoming-connection.js +0 -43
  205. package/dist/proto/packets/open-connection-reply-one.d.ts +0 -8
  206. package/dist/proto/packets/open-connection-reply-one.d.ts.map +0 -1
  207. package/dist/proto/packets/open-connection-reply-one.js +0 -43
  208. package/dist/proto/packets/open-connection-reply-two.d.ts +0 -10
  209. package/dist/proto/packets/open-connection-reply-two.d.ts.map +0 -1
  210. package/dist/proto/packets/open-connection-reply-two.js +0 -48
  211. package/dist/proto/packets/open-connection-request-one.d.ts +0 -7
  212. package/dist/proto/packets/open-connection-request-one.d.ts.map +0 -1
  213. package/dist/proto/packets/open-connection-request-one.js +0 -38
  214. package/dist/proto/packets/open-connection-request-two.d.ts +0 -9
  215. package/dist/proto/packets/open-connection-request-two.d.ts.map +0 -1
  216. package/dist/proto/packets/open-connection-request-two.js +0 -43
  217. package/dist/proto/packets/types/address.d.ts +0 -48
  218. package/dist/proto/packets/types/address.d.ts.map +0 -1
  219. package/dist/proto/packets/types/address.js +0 -99
  220. package/dist/proto/packets/types/data-type.d.ts +0 -21
  221. package/dist/proto/packets/types/data-type.d.ts.map +0 -1
  222. package/dist/proto/packets/types/data-type.js +0 -29
  223. package/dist/proto/packets/types/frame.d.ts +0 -23
  224. package/dist/proto/packets/types/frame.d.ts.map +0 -1
  225. package/dist/proto/packets/types/frame.js +0 -94
  226. package/dist/proto/packets/types/index.d.ts.map +0 -1
  227. package/dist/proto/packets/types/magic.d.ts +0 -7
  228. package/dist/proto/packets/types/magic.d.ts.map +0 -1
  229. package/dist/proto/packets/types/magic.js +0 -14
  230. package/dist/proto/packets/types/mtu.d.ts +0 -9
  231. package/dist/proto/packets/types/mtu.d.ts.map +0 -1
  232. package/dist/proto/packets/types/mtu.js +0 -18
  233. package/dist/proto/packets/types/sys-address.d.ts +0 -11
  234. package/dist/proto/packets/types/sys-address.d.ts.map +0 -1
  235. package/dist/proto/packets/types/sys-address.js +0 -32
  236. package/dist/proto/packets/unconnected-ping.d.ts +0 -7
  237. package/dist/proto/packets/unconnected-ping.d.ts.map +0 -1
  238. package/dist/proto/packets/unconnected-ping.js +0 -38
  239. package/dist/proto/packets/unconnected-pong.d.ts +0 -8
  240. package/dist/proto/packets/unconnected-pong.d.ts.map +0 -1
  241. package/dist/proto/packets/unconnected-pong.js +0 -43
  242. package/dist/proto/types/advertisement.d.ts.map +0 -1
  243. package/dist/proto/types/index.d.ts +0 -3
  244. package/dist/proto/types/index.d.ts.map +0 -1
  245. package/dist/proto/types/valid.d.ts +0 -4
  246. package/dist/proto/types/valid.d.ts.map +0 -1
  247. package/dist/server/server-events.d.ts +0 -9
  248. package/dist/server/server-events.d.ts.map +0 -1
  249. package/dist/server/server-options.d.ts +0 -26
  250. package/dist/server/server-options.d.ts.map +0 -1
  251. package/dist/server/server-options.js +0 -20
  252. package/dist/tests/client.d.ts +0 -2
  253. package/dist/tests/client.d.ts.map +0 -1
  254. package/dist/tests/client.js +0 -15
  255. package/dist/tests/localhost.d.ts +0 -2
  256. package/dist/tests/localhost.d.ts.map +0 -1
  257. package/dist/tests/localhost.js +0 -34
  258. package/dist/tests/server.d.ts +0 -2
  259. package/dist/tests/server.d.ts.map +0 -1
  260. package/dist/tests/server.js +0 -32
  261. package/dist/tests/test.d.ts +0 -2
  262. package/dist/tests/test.d.ts.map +0 -1
  263. package/dist/tests/test.js +0 -49
  264. package/dist/utils/Logger.d.ts.map +0 -1
  265. package/dist/utils/decorators.d.ts +0 -2
  266. package/dist/utils/decorators.d.ts.map +0 -1
  267. package/dist/utils/decorators.js +0 -17
  268. package/dist/utils/index.d.ts +0 -3
  269. package/dist/utils/index.d.ts.map +0 -1
  270. package/dist/utils/index.js +0 -18
  271. package/tsconfig.single.json +0 -9
  272. /package/dist/{proto/types/valid.js → server/types/connection-events.js} +0 -0
  273. /package/dist/server/{server-events.js → types/server-events.js} +0 -0
  274. /package/dist/{utils/Logger.js → shared/logger.js} +0 -0
  275. /package/dist/{proto → shared/proto}/enums/reliability.d.ts +0 -0
  276. /package/dist/{proto → shared/proto}/enums/reliability.js +0 -0
@@ -1,458 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Framer = void 0;
4
- const binarystream_1 = require("@serenityjs/binarystream");
5
- const proto_1 = require("../proto");
6
- const connection_request_accepted_1 = require("../proto/packets/connection-request-accepted");
7
- const utils_1 = require("../utils");
8
- // Constants for packet handling
9
- const PACKET_HEADER_MASK = 0xf0;
10
- const PACKET_HEADER_FRAMESET = 0x80;
11
- const PACKET_ENCAPSULATED = 0xfe;
12
- const PING_INTERVAL = 50;
13
- const MTU_HEADER_SIZE = 36;
14
- class Framer {
15
- client;
16
- lastInputSequence = -1;
17
- receivedFrameSequences = new Set();
18
- lostFrameSequences = new Set();
19
- inputHighestSequenceIndex = new Array(64).fill(0);
20
- inputOrderIndex = new Array(64).fill(0);
21
- inputOrderingQueue = Array.from({ length: 64 }, () => new Map());
22
- fragmentsQueue = new Map();
23
- outputOrderIndex;
24
- outputSequenceIndex;
25
- outputFrameQueue;
26
- outputSequence = 0;
27
- outputSplitIndex = 0;
28
- outputReliableIndex = 0;
29
- outputFrames = [];
30
- outputFramesByteLength = 0;
31
- outputBackup = new Map();
32
- _tickCount = 0;
33
- mtuDiff;
34
- BATCH_SIZE = 32;
35
- MAX_BATCH_INTERVAL = 50;
36
- lastBatchTime = 0;
37
- ORDERING_QUEUE_TIMEOUT = 500;
38
- constructor(client) {
39
- this.client = client;
40
- this.outputFrameQueue = new proto_1.Frameset();
41
- this.outputFrameQueue.frames = [];
42
- this.outputOrderIndex = new Array(32).fill(0);
43
- this.outputSequenceIndex = new Array(32).fill(0);
44
- this.mtuDiff = this.client.options.mtuSize - MTU_HEADER_SIZE;
45
- }
46
- tick() {
47
- this._tickCount++;
48
- const now = Date.now();
49
- if (this.client.status === proto_1.Status.Disconnected ||
50
- this.client.status === proto_1.Status.Disconnecting) {
51
- return;
52
- }
53
- // Send a ping every PING_INTERVAL ticks
54
- if (this._tickCount % PING_INTERVAL === 0) {
55
- const ping = new proto_1.ConnectedPing();
56
- ping.timestamp = BigInt(now);
57
- this.frameAndSend(ping.serialize(), proto_1.Priority.Immediate);
58
- }
59
- this.processBatchedAcksAndNacks();
60
- this.processBatchedOrderedFrames(now);
61
- if (this.outputFrames.length >= this.BATCH_SIZE ||
62
- (this.outputFrames.length > 0 &&
63
- now - this.lastBatchTime >= this.MAX_BATCH_INTERVAL)) {
64
- this.sendQueue();
65
- this.lastBatchTime = now;
66
- }
67
- }
68
- processBatchedAcksAndNacks() {
69
- if (this.receivedFrameSequences.size > 0) {
70
- const ackSeqs = Array.from(this.receivedFrameSequences);
71
- this.receivedFrameSequences.clear();
72
- const ack = new proto_1.Ack();
73
- ack.sequences = ackSeqs;
74
- this.client.send(ack.serialize());
75
- }
76
- if (this.lostFrameSequences.size > 0) {
77
- const nackSeqs = Array.from(this.lostFrameSequences);
78
- this.lostFrameSequences.clear();
79
- const nack = new proto_1.Nack();
80
- nack.sequences = nackSeqs;
81
- this.client.send(nack.serialize());
82
- }
83
- }
84
- processBatchedOrderedFrames(now) {
85
- const processedChannels = new Set();
86
- for (let channel = 0; channel < this.inputOrderingQueue.length; channel++) {
87
- if (processedChannels.has(channel))
88
- continue;
89
- const queue = this.inputOrderingQueue[channel];
90
- let processed = 0;
91
- let expected = this.inputOrderIndex[channel];
92
- while (queue.has(expected) && processed < this.BATCH_SIZE) {
93
- const queued = queue.get(expected);
94
- if (!queued) {
95
- expected++;
96
- continue;
97
- }
98
- if (now - queued.timestamp > this.ORDERING_QUEUE_TIMEOUT) {
99
- this.processFrame(queued.frame);
100
- queue.delete(expected);
101
- expected++;
102
- processed++;
103
- }
104
- else {
105
- break;
106
- }
107
- }
108
- if (processed > 0) {
109
- this.inputOrderIndex[channel] = expected;
110
- processedChannels.add(channel);
111
- }
112
- }
113
- }
114
- incomingMessage(payload, rinfo) {
115
- let header = payload.readUint8();
116
- if ((header & PACKET_HEADER_MASK) === PACKET_HEADER_FRAMESET)
117
- header = PACKET_HEADER_FRAMESET;
118
- switch (header) {
119
- case proto_1.Packet.Ack: {
120
- const ack = new proto_1.Ack(payload).deserialize();
121
- for (let i = 0, len = ack.sequences.length; i < len; i++) {
122
- this.outputBackup.delete(ack.sequences[i]);
123
- }
124
- break;
125
- }
126
- case proto_1.Packet.Nack: {
127
- const nack = new proto_1.Nack(payload).deserialize();
128
- for (let i = 0, len = nack.sequences.length; i < len; i++) {
129
- const seq = nack.sequences[i];
130
- const lostFrames = this.outputBackup.get(seq) || [];
131
- for (let j = 0, lostLen = lostFrames.length; j < lostLen; j++) {
132
- this.sendFrame(lostFrames[j], proto_1.Priority.Immediate);
133
- }
134
- }
135
- break;
136
- }
137
- case proto_1.Packet.UnconnectedPong: {
138
- const packet = new proto_1.UnconnectedPong(payload).deserialize();
139
- this.client.emit("unconnected-pong", packet);
140
- break;
141
- }
142
- case proto_1.Packet.OpenConnectionReplyOne: {
143
- const packet = new proto_1.OpenConnectionReplyOne(payload).deserialize();
144
- this.client.emit("open-connection-reply-one", packet);
145
- this.client.serverAddress = new proto_1.Address(rinfo.address, rinfo.port, rinfo.family === "IPv4" ? 4 : 6);
146
- const request = new proto_1.OpenConnectionRequestTwo();
147
- request.mtu = packet.mtu;
148
- request.address = this.client.serverAddress;
149
- request.clientGuid = this.client.options.clientId;
150
- this.client.emit("open-connection-request-two", request);
151
- this.client.send(request.serialize());
152
- break;
153
- }
154
- case proto_1.Packet.OpenConnectionReplyTwo: {
155
- const packet = new proto_1.OpenConnectionReplyTwo(payload).deserialize();
156
- this.client.emit("open-connection-reply-two", packet);
157
- this.client.options.mtuSize = packet.mtu;
158
- const conReq = new proto_1.ConnectionRequest();
159
- conReq.clientGuid = this.client.options.clientId;
160
- conReq.timestamp = BigInt(Date.now());
161
- conReq.useSecurity = false;
162
- this.client.emit("connection-request", conReq);
163
- this.frameAndSend(conReq.serialize(), proto_1.Priority.Immediate);
164
- break;
165
- }
166
- case PACKET_HEADER_FRAMESET: {
167
- const frameset = new proto_1.Frameset(payload).deserialize();
168
- this.client.emit("frameset", frameset);
169
- this.handle(frameset);
170
- break;
171
- }
172
- default: {
173
- utils_1.Logger.error(`Unknown packet header: ${header}`);
174
- }
175
- }
176
- }
177
- processFrame(frame) {
178
- const header = frame.payload[0];
179
- if (this.client.options.debug) {
180
- utils_1.Logger.debug(`[Framer] Processing frame with header ${header}`);
181
- }
182
- if (this.client.status === proto_1.Status.Connecting) {
183
- switch (header) {
184
- case proto_1.Packet.ConnectionRequest: {
185
- const packet = new proto_1.ConnectionRequest(frame.payload).deserialize();
186
- this.client.emit("connection-request", packet);
187
- break;
188
- }
189
- case proto_1.Packet.ConnectionRequestAccepted: {
190
- const packet = new connection_request_accepted_1.ConnectionRequestAccepted(frame.payload).deserialize();
191
- const newI = new proto_1.NewIncomingConnection();
192
- proto_1.SystemAddress.count = 20;
193
- if (!this.client.serverAddress) {
194
- utils_1.Logger.error("[Framer] Cannot create NewIncomingConnection: serverAddress is null");
195
- return;
196
- }
197
- newI.serverAddress = this.client.serverAddress;
198
- newI.incomingTimestamp = BigInt(Date.now());
199
- newI.serverTimestamp = packet.timestamp;
200
- this.client.emit("new-incoming-connection", newI);
201
- this.frameAndSend(newI.serialize(), proto_1.Priority.Immediate);
202
- proto_1.SystemAddress.count = 0;
203
- break;
204
- }
205
- case proto_1.Packet.DisconnectionNotification: {
206
- utils_1.Logger.info("[Framer] Received disconnection notification while connecting");
207
- this.client.disconnect();
208
- break;
209
- }
210
- }
211
- }
212
- else if (this.client.status === proto_1.Status.Connected) {
213
- switch (header) {
214
- case proto_1.Packet.ConnectedPing: {
215
- const packet = new proto_1.ConnectedPing(frame.payload).deserialize();
216
- this.client.emit("connected-ping", packet);
217
- const pong = new proto_1.ConnectedPong();
218
- pong.pongTime = BigInt(Date.now());
219
- pong.pingTime = packet.timestamp;
220
- this.frameAndSend(pong.serialize(), proto_1.Priority.Immediate);
221
- break;
222
- }
223
- case proto_1.Packet.ConnectedPong: {
224
- const packet = new proto_1.ConnectedPong(frame.payload).deserialize();
225
- if (this.client.options.debug) {
226
- utils_1.Logger.debug(`[Framer] Received pong, latency: ${Date.now() - Number(packet.pingTime)}ms`);
227
- }
228
- this.client.emit("connected-pong", packet);
229
- break;
230
- }
231
- case PACKET_ENCAPSULATED: {
232
- this.client.emit("encapsulated", frame.payload);
233
- break;
234
- }
235
- case proto_1.Packet.DisconnectionNotification: {
236
- utils_1.Logger.info("[Framer] Received disconnection notification");
237
- this.client.disconnect();
238
- break;
239
- }
240
- }
241
- }
242
- }
243
- handle(frameSet) {
244
- try {
245
- if (this.receivedFrameSequences.has(frameSet.sequence)) {
246
- if (this.client.options.debug) {
247
- utils_1.Logger.debug(`[Framer] Received duplicate frameset ${frameSet.sequence}`);
248
- }
249
- return;
250
- }
251
- this.lostFrameSequences.delete(frameSet.sequence);
252
- if (frameSet.sequence <= this.lastInputSequence) {
253
- if (this.client.options.debug) {
254
- utils_1.Logger.debug(`[Framer] Received out of order frameset ${frameSet.sequence}!`);
255
- }
256
- return;
257
- }
258
- this.receivedFrameSequences.add(frameSet.sequence);
259
- const sequenceGap = frameSet.sequence - this.lastInputSequence;
260
- if (sequenceGap > 1) {
261
- if (this.client.options.debug) {
262
- utils_1.Logger.debug(`[Framer] Detected ${sequenceGap - 1} missing sequences between ${this.lastInputSequence} and ${frameSet.sequence}`);
263
- }
264
- for (let index = this.lastInputSequence + 1; index < frameSet.sequence; index++) {
265
- this.lostFrameSequences.add(index);
266
- }
267
- }
268
- this.lastInputSequence = frameSet.sequence;
269
- const frames = frameSet.frames;
270
- for (let i = 0; i < frames.length; i += this.BATCH_SIZE) {
271
- const batch = frames.slice(i, i + this.BATCH_SIZE);
272
- for (const frame of batch) {
273
- try {
274
- this.handleFrame(frame);
275
- }
276
- catch (err) {
277
- utils_1.Logger.error("[Framer] Error handling frame", err);
278
- }
279
- }
280
- }
281
- }
282
- catch (err) {
283
- utils_1.Logger.error("[Framer] Error handling frameset", err);
284
- }
285
- }
286
- handleFrame(frame) {
287
- if (frame.isSplit) {
288
- this.handleSplit(frame);
289
- }
290
- else if (frame.isSequenced) {
291
- this.handleSequenced(frame);
292
- }
293
- else if (frame.isOrdered) {
294
- this.handleOrdered(frame);
295
- }
296
- else {
297
- this.processFrame(frame);
298
- }
299
- }
300
- handleOrdered(frame) {
301
- const channel = frame.orderChannel;
302
- const expectedOrderIndex = this.inputOrderIndex[channel];
303
- if (frame.orderedFrameIndex === expectedOrderIndex) {
304
- this.processOrderedFrames(frame);
305
- }
306
- else if (frame.orderedFrameIndex > expectedOrderIndex) {
307
- this.inputOrderingQueue[channel].set(frame.orderedFrameIndex, {
308
- frame,
309
- timestamp: Date.now(),
310
- });
311
- }
312
- }
313
- processOrderedFrames(frame) {
314
- const channel = frame.orderChannel;
315
- this.inputOrderIndex[channel] = frame.orderedFrameIndex + 1;
316
- this.inputHighestSequenceIndex[channel] = 0;
317
- this.processFrame(frame);
318
- const queue = this.inputOrderingQueue[channel];
319
- let nextOrderIndex = this.inputOrderIndex[channel];
320
- while (queue.has(nextOrderIndex)) {
321
- const queued = queue.get(nextOrderIndex);
322
- if (!queued) {
323
- nextOrderIndex++;
324
- continue;
325
- }
326
- this.processFrame(queued.frame);
327
- queue.delete(nextOrderIndex);
328
- nextOrderIndex++;
329
- }
330
- this.inputOrderIndex[channel] = nextOrderIndex;
331
- }
332
- handleSequenced(frame) {
333
- const channel = frame.orderChannel;
334
- const currentHighestSequence = this.inputHighestSequenceIndex[channel];
335
- if (frame.sequenceFrameIndex < currentHighestSequence ||
336
- frame.orderedFrameIndex === this.inputOrderIndex[channel]) {
337
- return;
338
- }
339
- this.inputHighestSequenceIndex[channel] = frame.sequenceFrameIndex + 1;
340
- this.processFrame(frame);
341
- }
342
- handleSplit(frame) {
343
- const splitId = frame.splitId;
344
- let entry = this.fragmentsQueue.get(splitId);
345
- if (!entry) {
346
- entry = { fragments: new Map(), timestamp: Date.now() };
347
- this.fragmentsQueue.set(splitId, entry);
348
- }
349
- entry.fragments.set(frame.splitFrameIndex, frame);
350
- if (entry.fragments.size === frame.splitCount) {
351
- this.reassembleAndProcessFragment(frame, entry.fragments);
352
- this.fragmentsQueue.delete(splitId);
353
- }
354
- }
355
- reassembleAndProcessFragment(frame, fragment) {
356
- const stream = new binarystream_1.BinaryStream();
357
- for (let index = 0; index < frame.splitCount; index++) {
358
- const sframe = fragment.get(index);
359
- if (!sframe) {
360
- utils_1.Logger.error(`Missing fragment at index ${index} for splitId=${frame.splitId}`);
361
- return;
362
- }
363
- stream.writeBuffer(sframe.payload);
364
- }
365
- const reassembledFrame = new proto_1.Frame();
366
- reassembledFrame.reliability = frame.reliability;
367
- reassembledFrame.reliableFrameIndex = frame.reliableFrameIndex;
368
- reassembledFrame.sequenceFrameIndex = frame.sequenceFrameIndex;
369
- reassembledFrame.orderedFrameIndex = frame.orderedFrameIndex;
370
- reassembledFrame.orderChannel = frame.orderChannel;
371
- reassembledFrame.payload = stream.getBuffer();
372
- this.handleFrame(reassembledFrame);
373
- }
374
- frameAndSend(payload, priority = proto_1.Priority.Normal) {
375
- const frame = new proto_1.Frame();
376
- frame.orderChannel = 0;
377
- frame.payload = payload;
378
- this.sendFrame(frame, priority);
379
- }
380
- sendFrame(frame, priority) {
381
- const channel = frame.orderChannel;
382
- if (frame.isSequenced) {
383
- frame.orderedFrameIndex = this.outputOrderIndex[channel];
384
- frame.sequenceFrameIndex = this.outputSequenceIndex[channel]++;
385
- }
386
- else if (frame.isOrdered) {
387
- frame.orderedFrameIndex = this.outputOrderIndex[channel]++;
388
- this.outputSequenceIndex[channel] = 0;
389
- }
390
- const maxSize = this.mtuDiff;
391
- const payloadSize = frame.payload.byteLength;
392
- if (payloadSize > maxSize) {
393
- const splitSize = Math.ceil(payloadSize / maxSize);
394
- const splitId = this.outputSplitIndex++ & 0xffff;
395
- for (let index = 0; index < payloadSize; index += maxSize) {
396
- const nframe = this.createSplitFrame(frame, index, maxSize, splitId, splitSize);
397
- this.queueFrame(nframe, priority);
398
- }
399
- }
400
- else {
401
- if (frame.isReliable) {
402
- frame.reliableFrameIndex = this.outputReliableIndex++;
403
- }
404
- this.queueFrame(frame, priority);
405
- }
406
- }
407
- createSplitFrame(originalFrame, index, maxSize, splitId, splitSize) {
408
- const nframe = new proto_1.Frame();
409
- if (originalFrame.isReliable) {
410
- nframe.reliableFrameIndex = this.outputReliableIndex++;
411
- }
412
- nframe.sequenceFrameIndex = originalFrame.sequenceFrameIndex;
413
- nframe.orderedFrameIndex = originalFrame.orderedFrameIndex;
414
- nframe.orderChannel = originalFrame.orderChannel;
415
- nframe.reliability = originalFrame.reliability;
416
- nframe.payload = originalFrame.payload.subarray(index, index + maxSize);
417
- nframe.splitFrameIndex = index / maxSize;
418
- nframe.splitId = splitId;
419
- nframe.splitCount = splitSize;
420
- return nframe;
421
- }
422
- queueFrame(frame, priority) {
423
- const frameLength = frame.getByteLength();
424
- const totalLength = 4 + this.outputFramesByteLength + frameLength;
425
- if (totalLength > this.mtuDiff ||
426
- this.outputFrames.length >= this.BATCH_SIZE) {
427
- this.sendQueue();
428
- this.lastBatchTime = Date.now();
429
- }
430
- this.outputFrames.push(frame);
431
- this.outputFramesByteLength += frameLength;
432
- if (priority === proto_1.Priority.Immediate) {
433
- this.sendQueue();
434
- this.lastBatchTime = Date.now();
435
- }
436
- }
437
- sendQueue() {
438
- if (this.outputFrames.length === 0)
439
- return;
440
- const frameset = new proto_1.Frameset();
441
- frameset.sequence = this.outputSequence++;
442
- const remainingFrames = [...this.outputFrames];
443
- this.outputFrames = [];
444
- while (remainingFrames.length > 0) {
445
- const batchSize = Math.min(this.BATCH_SIZE, remainingFrames.length);
446
- const framesToSend = remainingFrames.splice(0, batchSize);
447
- frameset.frames = framesToSend;
448
- const sentLength = framesToSend.reduce((sum, f) => sum + f.getByteLength(), 0);
449
- this.outputFramesByteLength -= sentLength;
450
- this.outputBackup.set(frameset.sequence, framesToSend);
451
- this.client.send(frameset.serialize());
452
- if (remainingFrames.length > 0) {
453
- frameset.sequence = this.outputSequence++;
454
- }
455
- }
456
- }
457
- }
458
- exports.Framer = Framer;
@@ -1,8 +0,0 @@
1
- import type { BasePacket } from "../packets";
2
- /**
3
- * Thanks to SerenityJS as an example.
4
- * @param id
5
- * @returns
6
- */
7
- export declare function Create(id: number): (target: typeof BasePacket) => void;
8
- //# sourceMappingURL=create.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"create.d.ts","sourceRoot":"","sources":["../../../src/proto/decorators/create.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAY,MAAM,YAAY,CAAC;AAQvD;;;;GAIG;AACH,wBAAgB,MAAM,CAAC,EAAE,EAAE,MAAM,IACxB,QAAQ,OAAO,UAAU,UAwEjC"}
@@ -1,77 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Create = Create;
4
- /**
5
- * Thanks to SerenityJS as an example.
6
- * @param id
7
- * @returns
8
- */
9
- function Create(id) {
10
- return (target) => {
11
- target.id = id;
12
- const packetData = Reflect.getOwnMetadata("properties", target.prototype);
13
- const properties = Object.getOwnPropertyNames(target.prototype);
14
- if (!properties.includes("serialize")) {
15
- target.prototype.serialize = function () {
16
- this.flush();
17
- if (id <= 255)
18
- this.writeUint8(id);
19
- else if (id <= 65535)
20
- this.writeUint16(id);
21
- else if (id <= 4294967295)
22
- this.writeUint32(id);
23
- else
24
- throw new Error("Packet ID cannot be greater than 4294967295.");
25
- if (!packetData)
26
- return this.getBuffer();
27
- for (const { name, type, endian, parameter } of packetData) {
28
- if (parameter) {
29
- const value = this[parameter];
30
- const dtype = type;
31
- const data = this[name];
32
- dtype.write(this, data, endian, value);
33
- }
34
- else {
35
- const dtype = type;
36
- const data = this[name];
37
- dtype.write(this, data, endian);
38
- }
39
- }
40
- return Buffer.from(this.binary);
41
- };
42
- }
43
- if (!properties.includes("deserialize")) {
44
- target.prototype.deserialize = function () {
45
- if (this.binary.length === 0)
46
- return this;
47
- if (id <= 255)
48
- target.id = this.readUint8();
49
- else if (id <= 65535)
50
- target.id = this.readUint16();
51
- else if (id <= 4294967295)
52
- target.id = this.readUint32();
53
- else
54
- throw new Error("Invalid packet ID range");
55
- if (!packetData)
56
- return this;
57
- for (const { name, type, endian, parameter } of packetData) {
58
- if (parameter) {
59
- const value = this[parameter];
60
- const dtype = type;
61
- const data = this[name];
62
- this[name] = dtype.read(this, endian, value);
63
- }
64
- else {
65
- const dtype = type;
66
- const data = this[name];
67
- this[name] = dtype.read(this, endian);
68
- }
69
- }
70
- return this;
71
- };
72
- }
73
- if (!properties.includes("getId")) {
74
- target.prototype.getId = () => target.id;
75
- }
76
- };
77
- }
@@ -1,3 +0,0 @@
1
- export * from "./create";
2
- export * from "./serialize";
3
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/proto/decorators/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC"}
@@ -1,4 +0,0 @@
1
- import { Endianness } from "@serenityjs/binarystream";
2
- import type { ValidTypes } from "../types";
3
- export declare function Serialize(type: ValidTypes, endian?: Endianness | boolean, parameter?: string): (target: object, propertyKey: string) => void;
4
- //# sourceMappingURL=serialize.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"serialize.d.ts","sourceRoot":"","sources":["../../../src/proto/decorators/serialize.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAEtD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAG3C,wBAAgB,SAAS,CACxB,IAAI,EAAE,UAAU,EAChB,MAAM,GAAE,UAAU,GAAG,OAAwB,EAC7C,SAAS,CAAC,EAAE,MAAM,IAGV,QAAQ,MAAM,EAAE,aAAa,MAAM,UAK3C"}
@@ -1,12 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Serialize = Serialize;
4
- function Serialize(type, endian = 0 /* Endianness.Big */, parameter) {
5
- if (!type)
6
- throw new Error("@Serialize() must be given a type.");
7
- return (target, propertyKey) => {
8
- const properties = Reflect.getMetadata("properties", target) || [];
9
- properties.push({ name: propertyKey, type, endian, parameter });
10
- Reflect.defineMetadata("properties", properties, target);
11
- };
12
- }
@@ -1,7 +0,0 @@
1
- export declare enum Flags {
2
- Split = 16,
3
- Valid = 128,
4
- Ack = 64,
5
- Nak = 32
6
- }
7
- //# sourceMappingURL=flags.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"flags.d.ts","sourceRoot":"","sources":["../../../src/proto/enums/flags.ts"],"names":[],"mappings":"AAAA,oBAAY,KAAK;IAChB,KAAK,KAAO;IACZ,KAAK,MAAO;IACZ,GAAG,KAAO;IACV,GAAG,KAAO;CACV"}
@@ -1,10 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Flags = void 0;
4
- var Flags;
5
- (function (Flags) {
6
- Flags[Flags["Split"] = 16] = "Split";
7
- Flags[Flags["Valid"] = 128] = "Valid";
8
- Flags[Flags["Ack"] = 64] = "Ack";
9
- Flags[Flags["Nak"] = 32] = "Nak";
10
- })(Flags || (exports.Flags = Flags = {}));
@@ -1,6 +0,0 @@
1
- export * from "./packet";
2
- export * from "./flags";
3
- export * from "./reliability";
4
- export * from "./priority";
5
- export * from "./status";
6
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/proto/enums/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC;AAC3B,cAAc,UAAU,CAAC"}