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