@sanctumterra/raknet 1.3.80 → 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 (275) 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 +6 -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/dist/{proto/types/valid.js → server/types/connection-events.js} +0 -0
  272. /package/dist/server/{server-events.js → types/server-events.js} +0 -0
  273. /package/dist/{utils/Logger.js → shared/logger.js} +0 -0
  274. /package/dist/{proto → shared/proto}/enums/reliability.d.ts +0 -0
  275. /package/dist/{proto → shared/proto}/enums/reliability.js +0 -0
@@ -1,169 +1,130 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.Server = void 0;
7
- const emitter_1 = __importDefault(require("@serenityjs/emitter"));
8
- const server_options_1 = require("./server-options");
9
- const node_dgram_1 = require("node:dgram");
10
- const proto_1 = require("../proto");
11
- const utils_1 = require("../utils");
4
+ const shared_1 = require("../shared");
12
5
  const connection_1 = require("./connection");
13
- class Server extends emitter_1.default {
14
- options;
15
- connectionTimes = new Map();
6
+ const types_1 = require("./types");
7
+ const node_dgram_1 = require("node:dgram");
8
+ class Server extends shared_1.EventEmitter {
16
9
  socket;
17
- connections = new Map();
18
- timer;
19
- tickCount = 0;
20
- blockedConnections = new Map();
21
- packetsPerSecond = new Map();
22
- validFlagsMask = proto_1.Flags.Valid;
23
- constructor(options) {
10
+ options;
11
+ advertisement;
12
+ connections;
13
+ tickCount;
14
+ tickInterval;
15
+ constructor(options = {}) {
24
16
  super();
25
- this.options = { ...server_options_1.defaultOptions, ...options };
26
17
  this.socket = (0, node_dgram_1.createSocket)("udp4");
27
- utils_1.Logger.disabled = this.options.loggerDisabled;
28
- }
29
- async start() {
30
- this.socket.bind(this.options.port, this.options.host);
31
- this.socket.on("message", (message, remote) => {
32
- this.handle(message, remote);
33
- });
34
- utils_1.Logger.info(`Server started on ${this.options.host}:${this.options.port}`);
35
- this.tick();
18
+ this.options = { ...types_1.defaultRaknetServerOptions, ...options };
19
+ this.connections = new Map();
20
+ this.advertisement = {
21
+ gamemode: "Survival",
22
+ guid: this.options.guid,
23
+ maxPlayers: this.options.maxConnections,
24
+ message: this.options.motd,
25
+ playerCount: this.connections.size,
26
+ version: "0",
27
+ protocol: 0,
28
+ serverName: "SanctumTerra Server",
29
+ type: "MCPE",
30
+ };
31
+ this.tickCount = 0;
32
+ this.tickInterval = setInterval(this.tick.bind(this), 1000 / this.options.tickRate);
36
33
  }
37
34
  tick() {
38
- this.tickCount++;
39
35
  for (const connection of this.connections.values()) {
40
- connection.tick();
41
- }
42
- const currentTime = Date.now();
43
- // Check every 1s
44
- if (this.tickCount % this.options.tickRate === 0) {
45
- for (const [addr, blockTime] of this.blockedConnections) {
46
- if (blockTime < currentTime) {
47
- utils_1.Logger.warn(`Unblocking ${addr} for excessive packets`);
48
- this.blockedConnections.delete(addr);
49
- }
50
- }
51
- this.packetsPerSecond.clear();
36
+ connection.onTick(this.tickCount);
52
37
  }
53
- this.timer = setTimeout(() => this.tick(), 1000 / this.options.tickRate);
38
+ this.tickCount++;
54
39
  }
55
- send(message, remote) {
56
- this.socket.send(message, 0, message.length, remote.port, remote.address);
40
+ listen() {
41
+ this.socket.bind(this.options.port, this.options.address, () => {
42
+ this.emit("listening");
43
+ if (this.options.enableServerLogs) {
44
+ shared_1.Logger.info(`Server listening on ${this.options.address}:${this.options.port}`);
45
+ }
46
+ });
47
+ this.socket.on("message", this.onMessage.bind(this));
48
+ this.on("disconnect", this.onDisconnect.bind(this));
57
49
  }
58
- async handle(message, remote) {
59
- let packetId = message[0];
60
- if ((packetId & 0xf0) === 0x80)
61
- packetId = 0x80;
62
- const remoteAddr = remote.address;
63
- // console.log(`Message: ${message} from ${remoteAddr}`);
64
- if (this.blockedConnections.has(remoteAddr)) {
65
- return;
50
+ onDisconnect(connection, executedByServer = false) {
51
+ const address = connection.getAddress();
52
+ this.connections.delete(`${address.address}:${address.port}`);
53
+ this.advertisement.playerCount = this.connections.size;
54
+ if (executedByServer) {
55
+ this.emit("disconnect", connection);
66
56
  }
67
- const currentPackets = (this.packetsPerSecond.get(remoteAddr) ?? 0) + 1;
68
- this.packetsPerSecond.set(remoteAddr, currentPackets);
69
- if (currentPackets > this.options.maxPacketsPerSecond) {
70
- const blockUntil = Date.now() + this.options.blockTime;
71
- this.blockedConnections.set(remoteAddr, blockUntil);
72
- utils_1.Logger.warn(`Blocking ${remoteAddr} for ${this.options.blockTime}ms for excessive packets`);
73
- return;
57
+ if (this.options.enableServerLogs) {
58
+ shared_1.Logger.info(`Client disconnected ${address.address}:${address.port}`);
74
59
  }
75
- const connectionKey = `${remoteAddr}:${remote.port}`;
76
- const connection = this.connections.get(connectionKey);
77
- if ((packetId & this.validFlagsMask) !== 0) {
78
- if (connection) {
79
- connection.handle(message);
60
+ }
61
+ onMessage(message, rinfo) {
62
+ let id = message[0];
63
+ const isOnline = (id & 0xf0) === 0x80;
64
+ if (isOnline)
65
+ id = 0x80;
66
+ switch (id) {
67
+ case shared_1.Packets.UnconnectedPing: {
68
+ const pong = new shared_1.UnconnectedPong();
69
+ pong.guid = this.options.guid;
70
+ pong.message = (0, types_1.AdvertisementToString)(this.advertisement);
71
+ pong.timestamp = BigInt(Date.now());
72
+ this.send(pong.serialize(), rinfo.address, rinfo.port);
73
+ break;
80
74
  }
81
- return;
82
- }
83
- if ((packetId & this.validFlagsMask) === 0 && connection) {
84
- utils_1.Logger.debug(`Received Offline packet from ${connectionKey} while being connected`);
85
- return;
86
- }
87
- switch (packetId) {
88
- case proto_1.Packet.UnconnectedPing: {
89
- const ping = new proto_1.UnconnectedPing(message).deserialize();
90
- const pong = new proto_1.UnconnectedPong();
91
- pong.serverGuid = this.options.guid;
92
- pong.serverTimestamp = BigInt(Date.now());
93
- pong.message = (0, proto_1.AdvertisementToString)({
94
- type: "MCPE",
95
- gamemode: "Survival",
96
- maxPlayers: this.options.maxConnections,
97
- playerCount: this.connections.size,
98
- protocol: this.options.protocol,
99
- serverName: this.options.levelName,
100
- version: this.options.version,
101
- serverGUID: Number(this.options.guid),
102
- message: this.options.motd,
103
- });
104
- this.socket.send(pong.serialize(), remote.port, remote.address);
75
+ case shared_1.Packets.OpenConnectionRequest1: {
76
+ const reply = new shared_1.OpenConnectionReplyOne();
77
+ reply.guid = this.options.guid;
78
+ reply.mtu = this.options.mtu;
79
+ reply.security = false;
80
+ this.send(reply.serialize(), rinfo.address, rinfo.port);
105
81
  break;
106
82
  }
107
- case proto_1.Packet.OpenConnectionRequestOne: {
108
- if (!this.connectionTimes.has(`${remote.address}:${remote.port}`)) {
109
- this.connectionTimes.set(`${remote.address}:${remote.port}`, Date.now());
83
+ case shared_1.Packets.OpenConnectionRequest2: {
84
+ const request = new shared_1.OpenConnectionRequestTwo(message).deserialize();
85
+ const reply = new shared_1.OpenConnectionReplyTwo();
86
+ reply.address = new shared_1.Address("0.0.0.0", 0, 4);
87
+ reply.encryptionEnabled = false;
88
+ reply.guid = this.options.guid;
89
+ reply.mtu = this.options.mtu;
90
+ const connection = new connection_1.Connection(this, rinfo, request.mtu, request.guid);
91
+ this.connections.set(`${rinfo.address}:${rinfo.port}`, connection);
92
+ this.send(reply.serialize(), rinfo.address, rinfo.port);
93
+ break;
94
+ }
95
+ case shared_1.Packets.FrameSet: {
96
+ const frameSet = new shared_1.FrameSet(message).deserialize();
97
+ const connection = this.connections.get(`${rinfo.address}:${rinfo.port}`);
98
+ if (!connection) {
99
+ break;
110
100
  }
111
- const request = new proto_1.OpenConnectionRequestOne(message).deserialize();
112
- utils_1.Logger.debug(`Received OpenConnectionRequestOne from ${remote.address}:${remote.port} with mtu ${request.mtu}`);
113
- if (request.protocol !== this.options.protocol) {
114
- utils_1.Logger.warn(`Client ${remote.address}:${remote.port} tried to connect with invalid protocol ${request.protocol}`);
115
- const incompatible = new proto_1.IncompatibleProtocolVersion();
116
- incompatible.protocol = this.options.protocol;
117
- incompatible.guid = this.options.guid;
118
- this.socket.send(incompatible.serialize(), remote.port, remote.address);
101
+ connection.onFrameSet(frameSet);
102
+ break;
103
+ }
104
+ case shared_1.Packets.Ack: {
105
+ const connection = this.connections.get(`${rinfo.address}:${rinfo.port}`);
106
+ if (!connection) {
119
107
  break;
120
108
  }
121
- utils_1.Logger.debug(`Sending OpenConnectionReplyOne to ${remote.address}:${remote.port} with mtu ${this.options.mtu}`);
122
- const reply = new proto_1.OpenConnectionReplyOne();
123
- reply.mtu = this.options.mtu;
124
- reply.serverGuid = this.options.guid;
125
- reply.serverHasSecurity = false;
126
- this.socket.send(reply.serialize(), remote.port, remote.address);
109
+ connection.onAck(new shared_1.Ack(message).deserialize());
127
110
  break;
128
111
  }
129
- case proto_1.Packet.OpenConnectionRequestTwo: {
130
- const request = new proto_1.OpenConnectionRequestTwo(message).deserialize();
131
- utils_1.Logger.debug(`Received OpenConnectionRequestTwo from ${remote.address}:${remote.port} with mtu ${request.mtu}`);
132
- const reply = new proto_1.OpenConnectionReplyTwo();
133
- reply.mtu = this.options.mtu;
134
- reply.clientAddress = proto_1.Address.fromIdentifier(remote);
135
- reply.encryptionEnabled = false;
136
- reply.serverGuid = this.options.guid;
137
- this.socket.send(reply.serialize(), remote.port, remote.address);
138
- const connection = new connection_1.Connection(this, remote, request.clientGuid, request.mtu);
139
- this.connections.set(`${remote.address}:${remote.port}`, connection);
112
+ case shared_1.Packets.Nack: {
113
+ const connection = this.connections.get(`${rinfo.address}:${rinfo.port}`);
114
+ if (!connection) {
115
+ break;
116
+ }
117
+ connection.onNack(new shared_1.Ack(message).deserialize());
140
118
  break;
141
119
  }
142
120
  default: {
143
- // 0x80 is FrameSet with no connection so we must ignore it
144
- if (packetId === 0x80)
145
- return;
146
- utils_1.Logger.info(`Received unknown packet: ${packetId}`);
121
+ shared_1.Logger.warn(`Unknown packet type ${id}`);
122
+ break;
147
123
  }
148
124
  }
149
125
  }
150
- deleteConnection(address) {
151
- const connection = this.connections.get(address);
152
- if (connection) {
153
- this.emit("closeConnection", connection);
154
- }
155
- this.connections.delete(address);
156
- this.connectionTimes.delete(address);
157
- }
158
- close() {
159
- clearTimeout(this.timer);
160
- for (const connection of this.connections.values()) {
161
- connection.disconnect();
162
- }
163
- // just wait for all connections to disconnect
164
- setTimeout(() => {
165
- this.socket.close();
166
- }, 200);
126
+ send(buffer, address, port) {
127
+ this.socket.send(buffer, port, address);
167
128
  }
168
129
  }
169
130
  exports.Server = Server;
@@ -6,7 +6,7 @@ interface Advertisement {
6
6
  version: string;
7
7
  playerCount: number;
8
8
  maxPlayers: number;
9
- serverGUID: number;
9
+ guid: bigint;
10
10
  serverName: string;
11
11
  gamemode: string;
12
12
  }
@@ -0,0 +1 @@
1
+ {"version":3,"file":"advertisement.d.ts","sourceRoot":"","sources":["../../../src/server/types/advertisement.ts"],"names":[],"mappings":"AAAA,KAAK,iBAAiB,GAAG,MAAM,GAAG,MAAM,CAAC;AAEzC,UAAU,aAAa;IACtB,IAAI,EAAE,iBAAiB,CAAC;IACxB,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;CACjB;AAED,iBAAS,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,aAAa,CA6BlD;AAED,iBAAS,qBAAqB,CAAC,aAAa,EAAE,aAAa,GAAG,MAAM,CAYnE;AAED,OAAO,EAAE,KAAK,aAAa,EAAE,UAAU,EAAE,qBAAqB,EAAE,CAAC"}
@@ -3,17 +3,21 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.fromString = fromString;
4
4
  exports.AdvertisementToString = AdvertisementToString;
5
5
  function fromString(message) {
6
- const [type, motd, protocol, version, players, maxPlayers, guid, servername, gamemode,] = message.split(";");
6
+ const parts = message.split(";");
7
+ if (parts.length < 9) {
8
+ throw new Error(`Invalid advertisement string: expected >=9 fields, got ${parts.length}`);
9
+ }
10
+ const [type, motd, protocol, version, players, maxPlayers, guid, gamemode, serverName,] = parts;
7
11
  return {
8
12
  type: type,
9
- message: motd,
10
- protocol: Number.parseInt(protocol),
13
+ serverName,
14
+ protocol: Number.parseInt(protocol, 10),
11
15
  version,
12
- playerCount: Number.parseInt(players),
13
- maxPlayers: Number.parseInt(maxPlayers),
14
- serverGUID: Number.parseInt(guid),
15
- serverName: servername,
16
- gamemode: gamemode,
16
+ playerCount: Number.parseInt(players, 10),
17
+ maxPlayers: Number.parseInt(maxPlayers, 10),
18
+ guid: BigInt(guid),
19
+ message: motd,
20
+ gamemode,
17
21
  };
18
22
  }
19
23
  function AdvertisementToString(advertisement) {
@@ -24,8 +28,8 @@ function AdvertisementToString(advertisement) {
24
28
  advertisement.version,
25
29
  advertisement.playerCount,
26
30
  advertisement.maxPlayers,
27
- advertisement.serverGUID,
28
- advertisement.serverName,
31
+ Number(advertisement.guid),
29
32
  advertisement.gamemode,
33
+ advertisement.serverName,
30
34
  ].join(";");
31
35
  }
@@ -0,0 +1,4 @@
1
+ export type ConnectionEvents = {
2
+ encapsulated: Buffer;
3
+ };
4
+ //# sourceMappingURL=connection-events.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"connection-events.d.ts","sourceRoot":"","sources":["../../../src/server/types/connection-events.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,gBAAgB,GAAG;IAC9B,YAAY,EAAE,MAAM,CAAC;CACrB,CAAC"}
@@ -0,0 +1,5 @@
1
+ export * from "./server-events";
2
+ export * from "./server-options";
3
+ export * from "./advertisement";
4
+ export * from "./connection-events";
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/server/types/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,iBAAiB,CAAC;AAChC,cAAc,qBAAqB,CAAC"}
@@ -14,5 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./valid"), exports);
17
+ __exportStar(require("./server-events"), exports);
18
+ __exportStar(require("./server-options"), exports);
18
19
  __exportStar(require("./advertisement"), exports);
20
+ __exportStar(require("./connection-events"), exports);
@@ -0,0 +1,7 @@
1
+ import type { Connection } from "../connection";
2
+ export interface RaknetServerEvents {
3
+ listening: undefined;
4
+ connect: Connection;
5
+ disconnect: Connection;
6
+ }
7
+ //# sourceMappingURL=server-events.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server-events.d.ts","sourceRoot":"","sources":["../../../src/server/types/server-events.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAEhD,MAAM,WAAW,kBAAkB;IAClC,SAAS,EAAE,SAAS,CAAC;IACrB,OAAO,EAAE,UAAU,CAAC;IACpB,UAAU,EAAE,UAAU,CAAC;CACvB"}
@@ -0,0 +1,12 @@
1
+ export type RaknetServerOptions = {
2
+ port: number;
3
+ address: string;
4
+ guid: bigint;
5
+ motd: string;
6
+ maxConnections: number;
7
+ tickRate: number;
8
+ mtu: number;
9
+ enableServerLogs: boolean;
10
+ };
11
+ export declare const defaultRaknetServerOptions: RaknetServerOptions;
12
+ //# sourceMappingURL=server-options.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server-options.d.ts","sourceRoot":"","sources":["../../../src/server/types/server-options.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,mBAAmB,GAAG;IACjC,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,cAAc,EAAE,MAAM,CAAC;IACvB,QAAQ,EAAE,MAAM,CAAC;IACjB,GAAG,EAAE,MAAM,CAAC;IACZ,gBAAgB,EAAE,OAAO,CAAC;CAC1B,CAAC;AAEF,eAAO,MAAM,0BAA0B,EAAE,mBASxC,CAAC"}
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defaultRaknetServerOptions = void 0;
4
+ const client_1 = require("../../client");
5
+ exports.defaultRaknetServerOptions = {
6
+ port: 19132,
7
+ address: "0.0.0.0",
8
+ guid: (0, client_1.getRandomGuid)(),
9
+ motd: "SanctumTerra",
10
+ maxConnections: 100,
11
+ tickRate: 20,
12
+ mtu: 1492,
13
+ enableServerLogs: true,
14
+ };
@@ -0,0 +1,28 @@
1
+ type EventMapp = Record<string | symbol, any>;
2
+ type EventKey<T extends EventMapp> = Extract<keyof T, string | symbol>;
3
+ type EventReceiver<T> = (params: T) => void;
4
+ interface Emitter<T extends EventMapp> {
5
+ on<K extends EventKey<T>>(eventName: K, fn: EventReceiver<T[K]>): this;
6
+ off<K extends EventKey<T>>(eventName: K, fn: EventReceiver<T[K]>): this;
7
+ emit<K extends EventKey<T>>(eventName: K, ...params: T[K] extends void ? [] : [T[K]]): void;
8
+ once<K extends EventKey<T>>(eventName: K, fn: EventReceiver<T[K]>): this;
9
+ removeAllListeners<K extends EventKey<T>>(eventName?: K): this;
10
+ listenerCount<K extends EventKey<T>>(eventName: K): number;
11
+ listeners<K extends EventKey<T>>(eventName: K): EventReceiver<T[K]>[];
12
+ hasListener<K extends EventKey<T>>(eventName: K, fn?: EventReceiver<T[K]>): boolean;
13
+ hasAnyListeners<K extends EventKey<T>>(eventName: K): boolean;
14
+ }
15
+ declare class EventEmitter<T extends EventMapp> implements Emitter<T> {
16
+ private events;
17
+ on<K extends EventKey<T>>(eventName: K, fn: EventReceiver<T[K]>): this;
18
+ off<K extends EventKey<T>>(eventName: K, fn: EventReceiver<T[K]>): this;
19
+ emit<K extends EventKey<T>>(eventName: K, ...params: T[K] extends void ? [] : [T[K]]): void;
20
+ once<K extends EventKey<T>>(eventName: K, fn: EventReceiver<T[K]>): this;
21
+ removeAllListeners<K extends EventKey<T>>(eventName?: K): this;
22
+ listenerCount<K extends EventKey<T>>(eventName: K): number;
23
+ listeners<K extends EventKey<T>>(eventName: K): EventReceiver<T[K]>[];
24
+ hasListener<K extends EventKey<T>>(eventName: K, fn?: EventReceiver<T[K]>): boolean;
25
+ hasAnyListeners<K extends EventKey<T>>(eventName: K): boolean;
26
+ }
27
+ export { EventEmitter };
28
+ //# sourceMappingURL=emitter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"emitter.d.ts","sourceRoot":"","sources":["../../src/shared/emitter.ts"],"names":[],"mappings":"AAEA,KAAK,SAAS,GAAG,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,GAAG,CAAC,CAAC;AAC9C,KAAK,QAAQ,CAAC,CAAC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D,SAAS,CAAC,CAAC,SAAS,QAAQ,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,GAAG,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE;IAKrE,WAAW,CAAC,CAAC,SAAS,QAAQ,CAAC,CAAC,CAAC,EAChC,SAAS,EAAE,CAAC,EACZ,EAAE,CAAC,EAAE,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GACtB,OAAO;IAWV,eAAe,CAAC,CAAC,SAAS,QAAQ,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,GAAG,OAAO;CAI7D;AAED,OAAO,EAAE,YAAY,EAAE,CAAC"}
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EventEmitter = void 0;
4
+ class EventEmitter {
5
+ events = {};
6
+ on(eventName, fn) {
7
+ if (!this.events[eventName]) {
8
+ this.events[eventName] = new Set();
9
+ }
10
+ this.events[eventName]?.add(fn);
11
+ return this;
12
+ }
13
+ off(eventName, fn) {
14
+ const eventSet = this.events[eventName];
15
+ if (eventSet) {
16
+ eventSet.delete(fn);
17
+ if (eventSet.size === 0) {
18
+ delete this.events[eventName];
19
+ }
20
+ }
21
+ return this;
22
+ }
23
+ emit(eventName, ...params) {
24
+ const eventSet = this.events[eventName];
25
+ if (eventSet) {
26
+ for (const fn of eventSet) {
27
+ fn(params[0]);
28
+ }
29
+ }
30
+ }
31
+ once(eventName, fn) {
32
+ const onceWrapper = (params) => {
33
+ this.off(eventName, onceWrapper);
34
+ fn(params);
35
+ };
36
+ this.on(eventName, onceWrapper);
37
+ return this;
38
+ }
39
+ removeAllListeners(eventName) {
40
+ if (eventName) {
41
+ delete this.events[eventName];
42
+ }
43
+ else {
44
+ this.events = {};
45
+ }
46
+ return this;
47
+ }
48
+ listenerCount(eventName) {
49
+ const eventSet = this.events[eventName];
50
+ return eventSet ? eventSet.size : 0;
51
+ }
52
+ listeners(eventName) {
53
+ const eventSet = this.events[eventName];
54
+ return eventSet ? Array.from(eventSet) : [];
55
+ }
56
+ hasListener(eventName, fn) {
57
+ const eventSet = this.events[eventName];
58
+ if (!eventSet || eventSet.size === 0) {
59
+ return false;
60
+ }
61
+ if (fn) {
62
+ return eventSet.has(fn);
63
+ }
64
+ return eventSet.size > 0;
65
+ }
66
+ hasAnyListeners(eventName) {
67
+ const eventSet = this.events[eventName];
68
+ return eventSet ? eventSet.size > 0 : false;
69
+ }
70
+ }
71
+ exports.EventEmitter = EventEmitter;
@@ -0,0 +1,5 @@
1
+ export * from "./network_session";
2
+ export * from "./proto";
3
+ export * from "./emitter";
4
+ export * from "./logger";
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/shared/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,SAAS,CAAC;AACxB,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC"}
@@ -14,5 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./create"), exports);
18
- __exportStar(require("./serialize"), exports);
17
+ __exportStar(require("./network_session"), exports);
18
+ __exportStar(require("./proto"), exports);
19
+ __exportStar(require("./emitter"), exports);
20
+ __exportStar(require("./logger"), exports);
@@ -20,4 +20,4 @@ declare const Logger: {
20
20
  cleanup(): void;
21
21
  };
22
22
  export { Logger };
23
- //# sourceMappingURL=Logger.d.ts.map
23
+ //# sourceMappingURL=logger.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../src/shared/logger.ts"],"names":[],"mappings":"AAAA,KAAK,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,SAAS,GAAG,MAAM,GAAG,OAAO,CAAC;AAuC/E,QAAA,MAAM,MAAM;;;;;;;;;kBAMG,OAAO,EAAE,GAAG,IAAI;mBAIf,OAAO,EAAE,GAAG,IAAI;kBAIjB,OAAO,EAAE,GAAG,IAAI;mBAIf,OAAO,EAAE,GAAG,IAAI;kBAKjB,OAAO,EAAE,GAAG,IAAI;YAItB,MAAM;iBAOD,OAAO,EAAE,GAAG,IAAI;kBAKf,OAAO,EAAE,GAAG,IAAI;sBAIZ,OAAO,EAAE,GAAG,CAAC,MAAM,GAAG,OAAO,CAAC,EAAE;eAmCvC,IAAI;CAKf,CAAC;AAEF,OAAO,EAAE,MAAM,EAAE,CAAC"}
@@ -0,0 +1,34 @@
1
+ import { Ack, Frame, FrameSet, Priority } from "./proto";
2
+ export declare class NetworkSession {
3
+ mtu: number;
4
+ send: (data: Buffer) => void;
5
+ handle: (data: Buffer) => void;
6
+ outputReliableIndex: number;
7
+ outputSplitIndex: number;
8
+ protected outputSequence: number;
9
+ outputSequenceIndex: number[];
10
+ outputOrderIndex: number[];
11
+ outputFrames: Set<Frame>;
12
+ outputBackup: Map<number, Frame[]>;
13
+ receivedFrameSequences: Set<number>;
14
+ lostFrameSequences: Set<number>;
15
+ lastInputSequence: number;
16
+ fragmentsQueue: Map<number, Map<number, Frame>>;
17
+ inputHighestSequenceIndex: Array<number>;
18
+ inputOrderIndex: Array<number>;
19
+ protected inputOrderingQueue: Map<number, Map<number, Frame>>;
20
+ constructor(mtu: number);
21
+ onTick(tick: number): void;
22
+ onAck(ack: Ack): void;
23
+ onNack(nack: Ack): void;
24
+ frameAndSend(data: Buffer, priority?: Priority): void;
25
+ sendFrame(frame: Frame, priority?: Priority): void;
26
+ queueFrame(frame: Frame, priority: Priority): void;
27
+ sendQueue(amount: number): void;
28
+ onFrameSet(frameSet: FrameSet): void;
29
+ handleFrame(frame: Frame): void;
30
+ handleSplitFrame(frame: Frame): void;
31
+ handleSequenced(frame: Frame): void;
32
+ handleOrdered(frame: Frame): void;
33
+ }
34
+ //# sourceMappingURL=network_session.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"network_session.d.ts","sourceRoot":"","sources":["../../src/shared/network_session.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,GAAG,EAAE,KAAK,EAAE,QAAQ,EAAW,QAAQ,EAAe,MAAM,SAAS,CAAC;AAK/E,qBAAa,cAAc;IACnB,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAG,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;IAC9B,MAAM,EAAG,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;IAGhC,mBAAmB,SAAK;IACxB,gBAAgB,SAAK;IAC5B,SAAS,CAAC,cAAc,SAAK;IACtB,mBAAmB,EAAE,MAAM,EAAE,CAAC;IAC9B,gBAAgB,EAAE,MAAM,EAAE,CAAC;IAC3B,YAAY,EAAE,GAAG,CAAC,KAAK,CAAC,CAAa;IACrC,YAAY,uBAA8B;IAG1C,sBAAsB,EAAE,GAAG,CAAC,MAAM,CAAC,CAAa;IAChD,kBAAkB,EAAE,GAAG,CAAC,MAAM,CAAC,CAAa;IAC5C,iBAAiB,SAAM;IACvB,cAAc,EAAE,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC,CAAa;IAC5D,yBAAyB,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IACzC,eAAe,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IACtC,SAAS,CAAC,kBAAkB,EAAE,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC,CAAa;gBAE9D,GAAG,EAAE,MAAM;IAUvB,MAAM,CAAC,IAAI,EAAE,MAAM;IAuBnB,KAAK,CAAC,GAAG,EAAE,GAAG;IAMd,MAAM,CAAC,IAAI,EAAE,GAAG;IAUT,YAAY,CAAC,IAAI,EAAE,MA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