@netifyjs/protocol 1.0.0-alpha.3 → 1.0.0-beta.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 (175) hide show
  1. package/package.json +2 -2
  2. package/src/codec/index.ts +51 -221
  3. package/src/dispatch/dispatcher/index.ts +89 -47
  4. package/src/dispatch/session/index.ts +6 -24
  5. package/src/index.ts +2 -4
  6. package/src/photon/balancing/codec/index.ts +27 -53
  7. package/src/photon/balancing/format/authenticate/request/index.ts +52 -6
  8. package/src/photon/balancing/format/friends/request/index.ts +21 -5
  9. package/src/photon/balancing/format/game/create/request/index.ts +45 -8
  10. package/src/photon/balancing/format/game/debug/request/index.ts +19 -4
  11. package/src/photon/balancing/format/game/join/request/index.ts +65 -13
  12. package/src/photon/balancing/format/game/list/request/index.ts +29 -3
  13. package/src/photon/balancing/format/game/random/request/index.ts +58 -10
  14. package/src/photon/balancing/format/groups/request/index.ts +19 -3
  15. package/src/photon/balancing/format/index.ts +7 -0
  16. package/src/photon/balancing/format/leave/request/index.ts +19 -3
  17. package/src/photon/balancing/format/lobby/join/request/index.ts +23 -3
  18. package/src/photon/balancing/format/lobby/stats/request/index.ts +19 -3
  19. package/src/photon/balancing/format/once/request/index.ts +31 -8
  20. package/src/photon/balancing/format/properties/get/request/index.ts +31 -4
  21. package/src/photon/balancing/format/properties/set/request/index.ts +36 -7
  22. package/src/photon/balancing/format/raise/request/index.ts +76 -10
  23. package/src/photon/balancing/format/reader/index.ts +75 -6
  24. package/src/photon/balancing/format/regions/request/index.ts +16 -1
  25. package/src/photon/balancing/format/rpc/request/index.ts +26 -3
  26. package/src/photon/balancing/format/settings/request/index.ts +15 -2
  27. package/src/photon/balancing/format/shared/credentials/index.ts +77 -33
  28. package/src/photon/balancing/format/shared/lobby/index.ts +41 -5
  29. package/src/photon/balancing/format/shared/options/index.ts +56 -24
  30. package/src/photon/balancing/format/table/index.ts +399 -0
  31. package/src/photon/balancing/index.ts +11 -1
  32. package/src/photon/balancing/travel/index.ts +15 -12
  33. package/src/photon/client/connection/index.ts +6 -6
  34. package/src/photon/cluster/back/index.ts +32 -45
  35. package/src/photon/cluster/dresser/index.ts +7 -8
  36. package/src/photon/cluster/front/index.ts +25 -49
  37. package/src/photon/cluster/grid/index.ts +1 -1
  38. package/src/photon/cluster/mover/index.ts +12 -12
  39. package/src/photon/cluster/press/index.ts +2 -18
  40. package/src/photon/cluster/route/game/index.ts +9 -11
  41. package/src/photon/cluster/route/master/index.ts +15 -34
  42. package/src/photon/cluster/route/name/index.ts +4 -21
  43. package/src/photon/cluster/seat/game/index.ts +6 -26
  44. package/src/photon/cluster/seat/master/index.ts +1 -1
  45. package/src/photon/cluster/sessions/index.ts +5 -6
  46. package/src/photon/cluster/spread/index.ts +8 -147
  47. package/src/photon/enet/client/index.ts +1 -8
  48. package/src/photon/enet/gateway/index.ts +5 -15
  49. package/src/photon/enet/index.ts +1 -1
  50. package/src/photon/enet/link/index.ts +34 -15
  51. package/src/photon/enet/outbound/index.ts +279 -0
  52. package/src/photon/enet/peer/index.ts +59 -0
  53. package/src/photon/enet/transport/index.ts +8 -24
  54. package/src/photon/game/admission/index.ts +353 -0
  55. package/src/photon/game/bag/index.ts +13 -0
  56. package/src/photon/game/broadcast/index.ts +79 -0
  57. package/src/photon/game/editor/index.ts +446 -0
  58. package/src/photon/game/gate/index.ts +9 -8
  59. package/src/photon/game/handler/index.ts +30 -3
  60. package/src/photon/game/history/index.ts +234 -0
  61. package/src/photon/game/hooked/index.ts +16 -15
  62. package/src/photon/game/index.ts +12 -1
  63. package/src/photon/game/lifecycle/index.ts +193 -0
  64. package/src/photon/game/peer/index.ts +1 -10
  65. package/src/photon/game/properties/index.ts +342 -0
  66. package/src/photon/game/room/index.ts +435 -1793
  67. package/src/photon/game/rooms/index.ts +2 -1
  68. package/src/photon/game/seats/index.ts +237 -0
  69. package/src/photon/game/server/index.ts +12 -2
  70. package/src/photon/game/services/index.ts +41 -0
  71. package/src/photon/game/settings/index.ts +22 -0
  72. package/src/photon/game/snapshot/index.ts +133 -0
  73. package/src/photon/game/throttle/index.ts +60 -0
  74. package/src/photon/game/touch/index.ts +2 -5
  75. package/src/photon/game/venue/index.ts +31 -0
  76. package/src/photon/master/application/index.ts +7 -0
  77. package/src/photon/master/gate/index.ts +12 -8
  78. package/src/photon/master/handler/index.ts +36 -748
  79. package/src/photon/master/index.ts +5 -0
  80. package/src/photon/master/list/sql/index.ts +37 -3
  81. package/src/photon/master/listing/index.ts +133 -0
  82. package/src/photon/master/lobby/index.ts +9 -0
  83. package/src/photon/master/login/index.ts +424 -0
  84. package/src/photon/master/matchmaking/index.ts +282 -0
  85. package/src/photon/master/query/index.ts +45 -0
  86. package/src/photon/master/redirect/index.ts +8 -7
  87. package/src/photon/master/services/index.ts +35 -0
  88. package/src/photon/master/settings/index.ts +44 -0
  89. package/src/photon/master/sql/cost/index.ts +263 -0
  90. package/src/photon/master/sql/index.ts +1 -0
  91. package/src/photon/message/client/index.ts +14 -11
  92. package/src/photon/message/handler/index.ts +22 -64
  93. package/src/photon/message/pipeline/index.ts +12 -12
  94. package/src/photon/message/server/index.ts +87 -43
  95. package/src/photon/message/watch/index.ts +14 -37
  96. package/src/photon/server/deferral/index.ts +57 -0
  97. package/src/photon/server/host/index.ts +32 -10
  98. package/src/photon/server/index.ts +1 -0
  99. package/src/photon/tcp/client/index.ts +1 -1
  100. package/src/photon/tcp/codec/index.ts +13 -42
  101. package/src/photon/tcp/server/index.ts +4 -4
  102. package/src/photon/websocket/bridge/index.ts +16 -20
  103. package/src/photon/websocket/message/index.ts +27 -39
  104. package/src/photon/websocket/path/index.ts +2 -5
  105. package/src/scale/fanout/index.ts +6 -6
  106. package/src/scale/index.ts +2 -4
  107. package/src/scale/outbound/index.ts +192 -0
  108. package/src/scale/relay/index.ts +20 -31
  109. package/src/scale/sink/index.ts +6 -11
  110. package/src/scale/transport/index.ts +27 -44
  111. package/src/scale/tunnel/index.ts +37 -91
  112. package/src/smartfox/common/bluebox/gateway/index.ts +401 -0
  113. package/src/smartfox/common/bluebox/index.ts +4 -0
  114. package/src/smartfox/{x2 → common}/bluebox/transport/index.ts +36 -23
  115. package/src/smartfox/common/index.ts +5 -0
  116. package/src/smartfox/common/strikes/index.ts +74 -0
  117. package/src/smartfox/pro/bluebox/index.ts +176 -4
  118. package/src/smartfox/pro/codec/index.ts +8 -7
  119. package/src/smartfox/pro/escalation/index.ts +28 -20
  120. package/src/smartfox/pro/flood/index.ts +2 -2
  121. package/src/smartfox/pro/format/extension/raw/index.ts +12 -2
  122. package/src/smartfox/pro/handler/chat/index.ts +11 -8
  123. package/src/smartfox/pro/handler/login/index.ts +2 -25
  124. package/src/smartfox/pro/handler/shared/index.ts +4 -3
  125. package/src/smartfox/pro/index.ts +11 -5
  126. package/src/smartfox/pro/login/gate/index.ts +166 -0
  127. package/src/smartfox/pro/login/index.ts +1 -0
  128. package/src/smartfox/pro/moderation/index.ts +220 -0
  129. package/src/smartfox/pro/options/index.ts +5 -5
  130. package/src/smartfox/pro/protocol/index.ts +62 -8
  131. package/src/smartfox/pro/web/index.ts +33 -26
  132. package/src/smartfox/pro/zone/index.ts +59 -282
  133. package/src/smartfox/pro/zones/index.ts +3 -3
  134. package/src/smartfox/x2/bluebox/index.ts +149 -4
  135. package/src/smartfox/x2/chat/index.ts +9 -19
  136. package/src/smartfox/x2/codec/index.ts +31 -39
  137. package/src/smartfox/x2/config/index.ts +6 -1
  138. package/src/smartfox/x2/data/array/index.ts +96 -60
  139. package/src/smartfox/x2/data/object/index.ts +144 -82
  140. package/src/smartfox/x2/data/serializer/index.ts +41 -23
  141. package/src/smartfox/x2/flood/gate/index.ts +19 -13
  142. package/src/smartfox/x2/format/buddy/init/response/index.ts +4 -3
  143. package/src/smartfox/x2/format/find/rooms/response/index.ts +4 -3
  144. package/src/smartfox/x2/format/find/users/response/index.ts +4 -3
  145. package/src/smartfox/x2/format/group/subscribe/response/index.ts +4 -3
  146. package/src/smartfox/x2/format/login/response/index.ts +4 -3
  147. package/src/smartfox/x2/format/mmo/position/response/index.ts +8 -6
  148. package/src/smartfox/x2/format/room/join/response/index.ts +4 -3
  149. package/src/smartfox/x2/games/index.ts +35 -8
  150. package/src/smartfox/x2/hall/index.ts +0 -6
  151. package/src/smartfox/x2/handler/session/index.ts +1 -1
  152. package/src/smartfox/x2/index.ts +8 -3
  153. package/src/smartfox/x2/member/index.ts +10 -3
  154. package/src/smartfox/x2/packet/login/response/index.ts +12 -0
  155. package/src/smartfox/x2/protocol/index.ts +11 -1
  156. package/src/smartfox/x2/rooms/index.ts +1 -2
  157. package/src/smartfox/x2/sessions/index.ts +6 -0
  158. package/src/smartfox/x2/udp/index.ts +61 -24
  159. package/src/smartfox/x2/web/index.ts +35 -17
  160. package/src/smartfox/x2/websocket/index.ts +18 -36
  161. package/src/smartfox/x2/zone/index.ts +77 -28
  162. package/src/smartfox/x2/zones/index.ts +17 -5
  163. package/src/photon/enet/handler/index.ts +0 -65
  164. package/src/scale/hosted/index.ts +0 -68
  165. package/src/scale/outlet/index.ts +0 -121
  166. package/src/smartfox/pro/bluebox/gateway/index.ts +0 -310
  167. package/src/smartfox/pro/bluebox/session/index.ts +0 -63
  168. package/src/smartfox/pro/bluebox/transport/index.ts +0 -104
  169. package/src/smartfox/x2/bluebox/gateway/index.ts +0 -315
  170. /package/src/smartfox/{x2 → common}/bluebox/session/index.ts +0 -0
  171. /package/src/smartfox/{pro → common}/counters/index.ts +0 -0
  172. /package/src/smartfox/{pro → common}/counters/memory/index.ts +0 -0
  173. /package/src/smartfox/{pro → common}/counters/offender/index.ts +0 -0
  174. /package/src/smartfox/{pro → common}/counters/store/index.ts +0 -0
  175. /package/src/smartfox/{pro → common}/sequence/index.ts +0 -0
@@ -40,16 +40,16 @@ export class WebSocketBridge extends DuplexHandlerAdapter {
40
40
  * @param context the connection
41
41
  * @param event whatever happened
42
42
  */
43
- public override async userEventTriggered(
43
+ public override userEventTriggered(
44
44
  context: HandlerContext,
45
45
  event: unknown,
46
- ): Promise<void> {
46
+ ): void {
47
47
  if (event instanceof WebSocketHandshakeComplete) {
48
- await this.setup(context.channel)
49
- await context.fireChannelActive()
48
+ this.setup(context.channel)
49
+ context.fireChannelActive()
50
50
  }
51
51
 
52
- await context.fireUserEventTriggered(event)
52
+ context.fireUserEventTriggered(event)
53
53
  }
54
54
 
55
55
  /**
@@ -57,21 +57,21 @@ export class WebSocketBridge extends DuplexHandlerAdapter {
57
57
  * @param context the connection
58
58
  * @param message a frame, or anything else
59
59
  */
60
- public override channelRead(
61
- context: HandlerContext,
62
- message: unknown,
63
- ): void | Promise<void> {
60
+ public override channelRead(context: HandlerContext, message: unknown): void {
64
61
  if (
65
62
  message instanceof BinaryWebSocketFrame ||
66
63
  message instanceof ContinuationWebSocketFrame
67
64
  ) {
68
- return context.fireChannelRead(message.content)
65
+ context.fireChannelRead(message.content)
66
+ return
69
67
  }
70
68
 
71
- if (message instanceof FullHttpRequest)
72
- return this.missing(context, message)
69
+ if (message instanceof FullHttpRequest) {
70
+ this.missing(context, message)
71
+ return
72
+ }
73
73
 
74
- return context.fireChannelRead(message)
74
+ context.fireChannelRead(message)
75
75
  }
76
76
 
77
77
  /**
@@ -79,16 +79,13 @@ export class WebSocketBridge extends DuplexHandlerAdapter {
79
79
  * @param context the HTTP connection
80
80
  * @param request what's being answered
81
81
  */
82
- private async missing(
83
- context: HandlerContext,
84
- request: FullHttpRequest,
85
- ): Promise<void> {
82
+ private missing(context: HandlerContext, request: FullHttpRequest): void {
86
83
  const response = new FullHttpResponse(HttpResponseStatus.NOT_FOUND, '')
87
84
  response.setContentLength(0)
88
85
  response.setKeepAlive(request.isKeepAlive())
89
86
 
90
- await context.writeAndFlush(response)
91
- if (!request.isKeepAlive()) await context.close()
87
+ context.writeAndFlush(response)
88
+ if (!request.isKeepAlive()) void context.close()
92
89
  }
93
90
 
94
91
  /**
@@ -96,24 +93,9 @@ export class WebSocketBridge extends DuplexHandlerAdapter {
96
93
  * @param context the connection
97
94
  * @param message bytes heading out, or anything else
98
95
  */
99
- public override write(
100
- context: HandlerContext,
101
- message: unknown,
102
- ): Promise<void> {
103
- return context.write(
96
+ public override write(context: HandlerContext, message: unknown): void {
97
+ context.write(
104
98
  message instanceof ByteBuf ? new BinaryWebSocketFrame(message) : message,
105
99
  )
106
100
  }
107
-
108
- /**
109
- * Sends each framed packet as one binary message, without a promise
110
- * @param context the connection
111
- * @param message bytes heading out, or anything else
112
- */
113
- public writeSync(context: HandlerContext, message: unknown): boolean {
114
- if (!(message instanceof ByteBuf)) return false
115
-
116
- context.writeSync(new BinaryWebSocketFrame(message))
117
- return true
118
- }
119
101
  }
@@ -9,16 +9,17 @@ import {
9
9
  import type { Session } from '../../../dispatch/session/index.ts'
10
10
  import type { Protocol } from '../../../protocol/index.ts'
11
11
  import type { Fanout } from '../../../scale/fanout/index.ts'
12
- import { Offender } from '../../pro/counters/offender/index.ts'
13
- import type { CounterStore } from '../../pro/counters/store/index.ts'
14
- import { Sequence } from '../../pro/sequence/index.ts'
12
+ import { Offender } from '../../common/counters/offender/index.ts'
13
+ import type { CounterStore } from '../../common/counters/store/index.ts'
14
+ import { Sequence } from '../../common/sequence/index.ts'
15
15
  import { Bans } from '../ban/list/index.ts'
16
16
  import { MemoryBanStore } from '../ban/memory/index.ts'
17
17
  import type { BanModeId } from '../ban/record/index.ts'
18
- import { BlueBox } from '../bluebox/gateway/index.ts'
18
+ import { BlueBox } from '../bluebox/index.ts'
19
19
  import { Buddies } from '../buddies/index.ts'
20
20
  import { Chat } from '../chat/index.ts'
21
21
  import { SmartFox2XCodec } from '../codec/index.ts'
22
+ import { SfsObject } from '../data/object/index.ts'
22
23
  import type {
23
24
  Authenticator,
24
25
  ClusterBalancer,
@@ -179,6 +180,11 @@ export class Zone {
179
180
  */
180
181
  public readonly maxJoinedRooms: number
181
182
 
183
+ /**
184
+ * Invitations one user may have waiting for an answer
185
+ */
186
+ public readonly maxInvitations: number
187
+
182
188
  /**
183
189
  * Every group clients may subscribe to
184
190
  */
@@ -359,6 +365,7 @@ export class Zone {
359
365
  this.maxUserVariables = config.maxUserVariables ?? 5
360
366
  this.maxRoomVariables = config.maxRoomVariables ?? 5
361
367
  this.maxJoinedRooms = config.maxJoinedRooms ?? 10
368
+ this.maxInvitations = config.maxInvitations ?? 20
362
369
  this.groups = new Set<string>(config.groups ?? [Room.defaultGroup])
363
370
  this.defaultGroups = config.defaultGroups ?? [Room.defaultGroup]
364
371
  this.members = new Map<number, Member>()
@@ -579,8 +586,8 @@ export class Zone {
579
586
 
580
587
  frame ??= this.frame(packet)
581
588
  if (frame === null) member.send(packet)
582
- else if (Zone.direct(channel)) channel.writeAndFlushEncodedSync(frame)
583
- else channel.writeAndFlushSync(ByteBuf.wrap(frame))
589
+ else if (Zone.direct(channel)) channel.send(frame)
590
+ else channel.writeAndFlush(ByteBuf.wrap(frame))
584
591
  }
585
592
  }
586
593
 
@@ -605,11 +612,7 @@ export class Zone {
605
612
  * @param packet what to send
606
613
  */
607
614
  private frame(packet: object): Uint8Array | null {
608
- try {
609
- return this.protocol?.encode(packet) ?? null
610
- } catch {
611
- return null
612
- }
615
+ return this.protocol?.encode(packet) ?? null
613
616
  }
614
617
 
615
618
  /**
@@ -782,6 +785,7 @@ export class Zone {
782
785
  )
783
786
 
784
787
  const verdict = await this.judge(attempt)
788
+ if (Zone.hungUp(session)) return
785
789
  if (verdict.error !== null) {
786
790
  session.send(LoginResponse.failure(verdict.error))
787
791
  return
@@ -793,7 +797,12 @@ export class Zone {
793
797
  ? `${this.guestPrefix}${String(id)}`
794
798
  : (verdict.name ?? attempt.name)
795
799
 
796
- const error = await this.refusal(session, name, guest)
800
+ const early =
801
+ this.refusal(session, name, guest) ??
802
+ (await this.barring(session.channel.remoteAddress, name))
803
+ if (Zone.hungUp(session)) return
804
+
805
+ const error = early ?? this.crowding(name)
797
806
  if (error !== null) {
798
807
  session.send(LoginResponse.failure(error))
799
808
  return
@@ -825,11 +834,33 @@ export class Zone {
825
834
  this.name,
826
835
  this.rooms.listing(member.groups),
827
836
  this.reconnectionSeconds,
828
- verdict.data,
837
+ Zone.extras(verdict.data, member),
829
838
  ),
830
839
  )
831
840
  }
832
841
 
842
+ /**
843
+ * Whether a connection closed while its login was being checked
844
+ * @param session the connection logging in
845
+ */
846
+ private static hungUp(session: Session<Member>): boolean {
847
+ return !session.channel.isActive
848
+ }
849
+
850
+ /**
851
+ * The login's extra data with the user's UDP key added
852
+ * @param data what the login check sent back, or null
853
+ * @param member who just logged in
854
+ */
855
+ private static extras(data: SfsObject | null, member: Member): SfsObject {
856
+ const extras = SfsObject.of()
857
+ for (const [key, entry] of data?.entries() ?? []) {
858
+ extras.put(key, entry.type, entry.value)
859
+ }
860
+
861
+ return extras.putLong(LoginResponse.udpKey, member.udpKey)
862
+ }
863
+
833
864
  /**
834
865
  * Takes a user out of the zone and tells them so
835
866
  * @param session the connection
@@ -937,7 +968,7 @@ export class Zone {
937
968
  return
938
969
  }
939
970
 
940
- channel.writeAndFlushSync(event)
971
+ channel.writeAndFlush(event)
941
972
  void channel.close()
942
973
  }
943
974
 
@@ -1059,18 +1090,16 @@ export class Zone {
1059
1090
  }
1060
1091
 
1061
1092
  /**
1062
- * Why a login can't go ahead, or null
1093
+ * Why a login can't go ahead before the ban lists are asked, or null
1063
1094
  * @param session the connection
1064
1095
  * @param name name the user would play under
1065
1096
  * @param guest whether they came without a name
1066
1097
  */
1067
- private async refusal(
1098
+ private refusal(
1068
1099
  session: Session<Member>,
1069
1100
  name: string,
1070
1101
  guest: boolean,
1071
- ): Promise<SfsError | null> {
1072
- const address = session.channel.remoteAddress
1073
-
1102
+ ): SfsError | null {
1074
1103
  if (session.user !== null) {
1075
1104
  return this.error(ErrorCode.LOGIN_ALREADY_LOGGED, name, this.name)
1076
1105
  }
@@ -1079,6 +1108,28 @@ export class Zone {
1079
1108
  return this.error(ErrorCode.LOGIN_GUEST_NOT_ALLOWED, this.name)
1080
1109
  }
1081
1110
 
1111
+ const filtered =
1112
+ this.words?.names === true && !guest ? this.words.apply(name) : null
1113
+ if (filtered !== null && filtered.found.length > 0) {
1114
+ return this.error(
1115
+ ErrorCode.LOGIN_NAME_CONTAINS_BAD_WORDS,
1116
+ name,
1117
+ filtered.text ?? '',
1118
+ )
1119
+ }
1120
+
1121
+ return null
1122
+ }
1123
+
1124
+ /**
1125
+ * The ban that keeps a login out, or null
1126
+ * @param address where the user connected from
1127
+ * @param name name the user would play under
1128
+ */
1129
+ private async barring(
1130
+ address: string,
1131
+ name: string,
1132
+ ): Promise<SfsError | null> {
1082
1133
  const barred = await this.bans.addressed(address)
1083
1134
  if (barred === 'unreachable') {
1084
1135
  return this.error(ErrorCode.LOGIN_INACTIVE_ZONE, this.name)
@@ -1093,16 +1144,14 @@ export class Zone {
1093
1144
 
1094
1145
  if (listed !== null) return this.error(ErrorCode.LOGIN_BANNED_USER, name)
1095
1146
 
1096
- const filtered =
1097
- this.words?.names === true && !guest ? this.words.apply(name) : null
1098
- if (filtered !== null && filtered.found.length > 0) {
1099
- return this.error(
1100
- ErrorCode.LOGIN_NAME_CONTAINS_BAD_WORDS,
1101
- name,
1102
- filtered.text ?? '',
1103
- )
1104
- }
1147
+ return null
1148
+ }
1105
1149
 
1150
+ /**
1151
+ * Makes room for a login and says why there's none, or null
1152
+ * @param name name the user would play under
1153
+ */
1154
+ private crowding(name: string): SfsError | null {
1106
1155
  if (this.members.size >= this.maxUsers) {
1107
1156
  return this.error(ErrorCode.LOGIN_ZONE_FULL, this.name)
1108
1157
  }
@@ -9,14 +9,13 @@ import {
9
9
  } from '@netifyjs/netify'
10
10
 
11
11
  import { PacketDispatcher } from '../../../dispatch/dispatcher/index.ts'
12
- import type { Session } from '../../../dispatch/session/index.ts'
12
+ import { Session } from '../../../dispatch/session/index.ts'
13
13
  import { Layout } from '../../../scale/layout/index.ts'
14
14
  import { Mesh } from '../../../scale/mesh/index.ts'
15
15
  import type { PlacementRule } from '../../../scale/placement/index.ts'
16
16
  import type { Portable, Reach } from '../../../scale/portable/index.ts'
17
- import { Tunnel } from '../../../scale/tunnel/index.ts'
18
- import { Sequence } from '../../pro/sequence/index.ts'
19
- import { BlueBox } from '../bluebox/gateway/index.ts'
17
+ import { Sequence } from '../../common/sequence/index.ts'
18
+ import { BlueBox } from '../bluebox/index.ts'
20
19
  import { PacketCipher } from '../cipher/index.ts'
21
20
  import { SmartFox2XCodec } from '../codec/index.ts'
22
21
  import type { ZoneConfig, ZonesConfig } from '../config/index.ts'
@@ -511,6 +510,19 @@ export class Zones implements SessionLookup, Portable<SessionSeed> {
511
510
  return null
512
511
  }
513
512
 
513
+ /**
514
+ * The key a logged in user binds UDP with, or null
515
+ * @param id their user id
516
+ */
517
+ public udpKey(id: number): bigint | null {
518
+ for (const zone of this.zones.values()) {
519
+ const member = zone.find(id)
520
+ if (member !== null) return member.udpKey
521
+ }
522
+
523
+ return null
524
+ }
525
+
514
526
  /**
515
527
  * Adds the handlers for every system request to a dispatcher
516
528
  * @param dispatcher where the game's own handlers go too
@@ -559,7 +571,7 @@ export class Zones implements SessionLookup, Portable<SessionSeed> {
559
571
  */
560
572
  private async lost(token: string): Promise<true | null> {
561
573
  const channel = this.tokens.get(token)
562
- if (channel !== undefined && Tunnel.carries(channel)) {
574
+ if (channel !== undefined && Session.of<Member>(channel).moved) {
563
575
  await this.mesh?.tunnel.vanished(channel)
564
576
  }
565
577
 
@@ -1,65 +0,0 @@
1
- import { type HandlerContext, OutboundHandlerAdapter } from '@netifyjs/netify'
2
-
3
- import { Frame } from '../../frame/index.ts'
4
- import type { EnetPeer } from '../peer/index.ts'
5
-
6
- /**
7
- * Turns the frames an application writes into commands on the wire
8
- * @class
9
- */
10
- export class EnetFrameHandler extends OutboundHandlerAdapter {
11
- /**
12
- * The connection frames leave on
13
- */
14
- private readonly peer: EnetPeer
15
-
16
- /**
17
- * @param peer the connection frames leave on
18
- */
19
- public constructor(peer: EnetPeer) {
20
- super()
21
-
22
- this.peer = peer
23
- }
24
-
25
- /**
26
- * Queues a frame
27
- * @param context where to pass other messages
28
- * @param message what the application wrote
29
- */
30
- public override write(
31
- context: HandlerContext,
32
- message: unknown,
33
- ): Promise<void> {
34
- if (!(message instanceof Frame)) return context.write(message)
35
-
36
- try {
37
- this.peer.send(message)
38
- } catch (error) {
39
- return Promise.reject(
40
- error instanceof Error
41
- ? error
42
- : new Error('Write failed', { cause: error }),
43
- )
44
- }
45
-
46
- return Promise.resolve()
47
- }
48
-
49
- /**
50
- * Queues a frame nobody waits on
51
- * @param context the connection's pipeline position
52
- * @param message what the application wrote
53
- */
54
- public writeSync(context: HandlerContext, message: unknown): boolean {
55
- if (!(message instanceof Frame)) return false
56
-
57
- try {
58
- this.peer.send(message)
59
- } catch (error) {
60
- context.channel.handleWriteFailure(error)
61
- }
62
-
63
- return true
64
- }
65
- }
@@ -1,68 +0,0 @@
1
- import { ByteBuf, Channel, type Transport } from '@netifyjs/netify'
2
-
3
- import type { OutletSink } from '../outlet/index.ts'
4
-
5
- /**
6
- * The connection of a session whose client is on another shard
7
- * @class
8
- */
9
- export class HostedChannel extends Channel {
10
- /**
11
- * Takes bytes that skip the pipeline
12
- */
13
- private readonly sink: OutletSink
14
-
15
- /**
16
- * @param transport stands in for the socket
17
- * @param sink takes bytes that skip the pipeline
18
- */
19
- public constructor(transport: Transport, sink: OutletSink) {
20
- super(transport)
21
-
22
- this.sink = sink
23
- }
24
-
25
- /**
26
- * Sends encoded bytes to the client's shard at once
27
- * @param bytes ready for the wire
28
- */
29
- public override writeEncoded(bytes: Uint8Array | ByteBuf): Promise<void> {
30
- this.writeEncodedSync(bytes)
31
-
32
- return Promise.resolve()
33
- }
34
-
35
- /**
36
- * Sends encoded bytes to the client's shard at once, without a promise
37
- * @param bytes ready for the wire
38
- */
39
- public override writeEncodedSync(bytes: Uint8Array | ByteBuf): void {
40
- if (bytes instanceof ByteBuf) {
41
- this.sink(
42
- bytes.array,
43
- bytes.arrayOffset + bytes.readerIndex,
44
- bytes.readableBytes,
45
- )
46
- } else {
47
- this.sink(bytes, 0, bytes.length)
48
- }
49
- }
50
-
51
- /**
52
- * Sends encoded bytes to the client's shard at once, without a promise
53
- * @param bytes ready for the wire
54
- */
55
- public override writeAndFlushEncodedSync(bytes: Uint8Array | ByteBuf): void {
56
- this.writeEncodedSync(bytes)
57
- }
58
-
59
- /**
60
- * Sends encoded bytes to the client's shard at once
61
- * @param bytes ready for the wire
62
- */
63
- public override writeAndFlushEncoded(
64
- bytes: Uint8Array | ByteBuf,
65
- ): Promise<void> {
66
- return this.writeEncoded(bytes)
67
- }
68
- }
@@ -1,121 +0,0 @@
1
- import {
2
- ByteBuf,
3
- OutboundHandlerAdapter,
4
- type HandlerContext,
5
- } from '@netifyjs/netify'
6
-
7
- import type { Staging } from '../../codec/index.ts'
8
-
9
- /**
10
- * Takes each frame a moved session writes
11
- * @param source holds the frame, borrowed for the call
12
- * @param start where it begins
13
- * @param size how many bytes it takes
14
- */
15
- export type OutletSink = (
16
- source: Uint8Array,
17
- start: number,
18
- size: number,
19
- ) => void
20
-
21
- /**
22
- * Catches a moved session's frames before they reach its outbox
23
- * @class
24
- */
25
- export class TunnelOutlet extends OutboundHandlerAdapter implements Staging {
26
- /**
27
- * Room codecs write into, shared by every outlet
28
- */
29
- private static scratch: Uint8Array = new Uint8Array(4096)
30
-
31
- /**
32
- * Where frames go
33
- */
34
- private readonly sink: OutletSink
35
-
36
- /**
37
- * Bytes the last reserve asked for
38
- */
39
- private reserved: number
40
-
41
- /**
42
- * @param sink where frames go
43
- */
44
- public constructor(sink: OutletSink) {
45
- super()
46
-
47
- this.sink = sink
48
- this.reserved = 0
49
- }
50
-
51
- /**
52
- * Where reserved bytes get written
53
- */
54
- public get stagingBuffer(): Uint8Array {
55
- return TunnelOutlet.scratch
56
- }
57
-
58
- /**
59
- * Makes room for a frame and gives where it starts
60
- * @param size bytes the frame may take
61
- */
62
- public reserve(size: number): number {
63
- if (TunnelOutlet.scratch.length < size) {
64
- TunnelOutlet.scratch = new Uint8Array(
65
- Math.max(size, TunnelOutlet.scratch.length * 2),
66
- )
67
- }
68
-
69
- this.reserved = size
70
- return 0
71
- }
72
-
73
- /**
74
- * Sends what was written into the reserved room
75
- * @param unused bytes of the room left over
76
- */
77
- public commitStaged(unused = 0): void {
78
- const size = this.reserved - unused
79
-
80
- this.reserved = 0
81
- if (size > 0) this.sink(TunnelOutlet.scratch, 0, size)
82
- }
83
-
84
- /**
85
- * Sends a frame on its way and lets the rest through
86
- * @param context the connection
87
- * @param message a frame, or anything else
88
- */
89
- public override write(
90
- context: HandlerContext,
91
- message: unknown,
92
- ): Promise<void> {
93
- if (!(message instanceof ByteBuf)) return context.write(message)
94
-
95
- this.drain(message)
96
- return Promise.resolve()
97
- }
98
-
99
- /**
100
- * Sends a frame on its way without a promise
101
- * @param context the connection
102
- * @param message a frame, or anything else
103
- */
104
- public writeSync(context: HandlerContext, message: unknown): boolean {
105
- if (!(message instanceof ByteBuf)) return false
106
-
107
- this.drain(message)
108
- return true
109
- }
110
-
111
- /**
112
- * Hands the frame's unread bytes to the sink and marks them read
113
- * @param message the frame
114
- */
115
- private drain(message: ByteBuf): void {
116
- const size = message.readableBytes
117
-
118
- this.sink(message.array, message.arrayOffset + message.readerIndex, size)
119
- message.skipBytes(size)
120
- }
121
- }