@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
@@ -33,6 +33,11 @@ export type EnetHooks = {
33
33
  */
34
34
  established(): void
35
35
 
36
+ /**
37
+ * Queued messages went on the wire
38
+ */
39
+ relieved(): void
40
+
36
41
  /**
37
42
  * The connection is over
38
43
  */
@@ -235,6 +240,16 @@ export class EnetPeer {
235
240
  */
236
241
  private queued: number
237
242
 
243
+ /**
244
+ * Payload bytes waiting in the queues
245
+ */
246
+ private waiting: number
247
+
248
+ /**
249
+ * Payload bytes that left the queues so far
250
+ */
251
+ private departed: number
252
+
238
253
  /**
239
254
  * Unacked reliable bytes on the wire
240
255
  */
@@ -389,6 +404,8 @@ export class EnetPeer {
389
404
  this.batch = []
390
405
  this.filled = 0
391
406
  this.queued = 0
407
+ this.waiting = 0
408
+ this.departed = 0
392
409
  this.transit = 0
393
410
  this.buffered = 0
394
411
  this.group = 0
@@ -468,6 +485,20 @@ export class EnetPeer {
468
485
  return this.count
469
486
  }
470
487
 
488
+ /**
489
+ * Payload bytes waiting to go on the wire
490
+ */
491
+ public get queuedBytes(): number {
492
+ return this.waiting
493
+ }
494
+
495
+ /**
496
+ * Payload bytes put on the wire so far
497
+ */
498
+ public get sentBytes(): number {
499
+ return this.departed
500
+ }
501
+
471
502
  /**
472
503
  * Whether messages can be sent
473
504
  */
@@ -753,6 +784,19 @@ export class EnetPeer {
753
784
  this.finish(this.ending)
754
785
  }
755
786
 
787
+ /**
788
+ * Forgets every message still waiting to go on the wire
789
+ */
790
+ public discard(): void {
791
+ for (const lane of this.order) {
792
+ this.queued -= lane.reliables.length + lane.unreliables.length
793
+ lane.reliables.length = 0
794
+ lane.unreliables.length = 0
795
+ }
796
+
797
+ this.waiting = 0
798
+ }
799
+
756
800
  /**
757
801
  * Hangs up at once
758
802
  * @param reason reserved byte the other end is shown
@@ -932,6 +976,7 @@ export class EnetPeer {
932
976
  else lane.unreliables.push(command)
933
977
 
934
978
  this.queued += 1
979
+ this.waiting += command.payload.length
935
980
  }
936
981
 
937
982
  /**
@@ -1269,10 +1314,12 @@ export class EnetPeer {
1269
1314
  return
1270
1315
  }
1271
1316
 
1317
+ const before = this.departed
1272
1318
  this.idle(now)
1273
1319
  this.fill(now, late)
1274
1320
  this.emit(now)
1275
1321
  this.wake(this.next())
1322
+ if (this.departed !== before) this.hooks.relieved()
1276
1323
  }
1277
1324
 
1278
1325
  /**
@@ -1375,11 +1422,13 @@ export class EnetPeer {
1375
1422
  this.flights.set(flight.key, flight)
1376
1423
  this.transit += command.size
1377
1424
  this.queued -= 1
1425
+ this.leave(command)
1378
1426
  lane.flying += 1
1379
1427
  this.load(command, now)
1380
1428
  }
1381
1429
 
1382
1430
  for (const command of lane.unreliables) {
1431
+ this.leave(command)
1383
1432
  this.load(command, now)
1384
1433
  }
1385
1434
 
@@ -1387,6 +1436,15 @@ export class EnetPeer {
1387
1436
  lane.unreliables.length = 0
1388
1437
  }
1389
1438
 
1439
+ /**
1440
+ * Counts a command's payload as gone from the queues
1441
+ * @param command what left
1442
+ */
1443
+ private leave(command: Command): void {
1444
+ this.waiting -= command.payload.length
1445
+ this.departed += command.payload.length
1446
+ }
1447
+
1390
1448
  /**
1391
1449
  * Whether the wire has room for another reliable command of a channel
1392
1450
  * @param lane the channel
@@ -1520,6 +1578,7 @@ export class EnetPeer {
1520
1578
  this.acks = []
1521
1579
  this.batch = []
1522
1580
  this.order = []
1581
+ this.waiting = 0
1523
1582
  this.lanes.clear()
1524
1583
  this.flights.clear()
1525
1584
  this.hooks.closed()
@@ -1,6 +1,5 @@
1
1
  import type { Transport } from '@netifyjs/netify'
2
2
 
3
- import { Frame } from '../../frame/index.ts'
4
3
  import type { EnetPeer } from '../peer/index.ts'
5
4
 
6
5
  /**
@@ -8,11 +7,6 @@ import type { EnetPeer } from '../peer/index.ts'
8
7
  * @class
9
8
  */
10
9
  export class EnetTransport implements Transport {
11
- /**
12
- * Every write is a message of its own
13
- */
14
- public readonly datagram: boolean = true
15
-
16
10
  /**
17
11
  * Where the other end's datagrams come from
18
12
  */
@@ -23,11 +17,6 @@ export class EnetTransport implements Transport {
23
17
  */
24
18
  public readonly remotePort: number
25
19
 
26
- /**
27
- * Whether the channel asked for reads to stop
28
- */
29
- public paused: boolean
30
-
31
20
  /**
32
21
  * The connection underneath
33
22
  */
@@ -45,19 +34,14 @@ export class EnetTransport implements Transport {
45
34
  ) {
46
35
  this.remoteAddress = remoteAddress
47
36
  this.remotePort = remotePort
48
- this.paused = false
49
37
  this.peer = peer
50
38
  }
51
39
 
52
40
  /**
53
- * Sends raw bytes as one reliable message on channel 0
54
- * @param bytes a whole message
41
+ * Never called
55
42
  */
56
- public write(bytes: Uint8Array): number {
57
- if (!this.peer.open) return -1
58
-
59
- this.peer.send(Frame.reliable(bytes))
60
- return bytes.length
43
+ public write(): number {
44
+ return -1
61
45
  }
62
46
 
63
47
  /**
@@ -68,16 +52,16 @@ export class EnetTransport implements Transport {
68
52
  }
69
53
 
70
54
  /**
71
- * Notes that the channel asked for reads to stop
55
+ * Says reads on a shared socket can't be held back
72
56
  */
73
- public pause(): void {
74
- this.paused = true
57
+ public pause(): boolean {
58
+ return false
75
59
  }
76
60
 
77
61
  /**
78
- * Notes that the channel takes reads again
62
+ * Nothing to start again
79
63
  */
80
64
  public resume(): void {
81
- this.paused = false
65
+ return
82
66
  }
83
67
  }
@@ -0,0 +1,353 @@
1
+ import { ErrorCode } from '../../balancing/code/error/index.ts'
2
+ import { JoinMode } from '../../balancing/code/join/index.ts'
3
+ import { ErrorMessage } from '../../balancing/code/message/index.ts'
4
+ import { RoomOptionFlag } from '../../balancing/code/room/index.ts'
5
+ import type { ExcludedUser } from '../../registry/index.ts'
6
+ import type { Actor } from '../actor/index.ts'
7
+ import type { EntrySetup } from '../entry/index.ts'
8
+ import { Fault } from '../fault/index.ts'
9
+ import type { History } from '../history/index.ts'
10
+ import type { Lifecycle } from '../lifecycle/index.ts'
11
+ import type { GamePeer } from '../peer/index.ts'
12
+ import type { RoomProperties } from '../properties/index.ts'
13
+ import type { Seats } from '../seats/index.ts'
14
+ import type { RoomServices } from '../services/index.ts'
15
+
16
+ /**
17
+ * Decides who gets a seat in one room and which one
18
+ * @class
19
+ */
20
+ export class Admission {
21
+ /**
22
+ * How a GUID user id looks
23
+ */
24
+ private static readonly GUID: RegExp =
25
+ /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i
26
+
27
+ /**
28
+ * Who sits in the room
29
+ */
30
+ private readonly seats: Seats
31
+
32
+ /**
33
+ * Limits and options of the room
34
+ */
35
+ private readonly properties: RoomProperties
36
+
37
+ /**
38
+ * Whether the caches overflowed
39
+ */
40
+ private readonly history: History
41
+
42
+ /**
43
+ * Keeps the room alive and times the empty seats
44
+ */
45
+ private readonly lifecycle: Lifecycle
46
+
47
+ /**
48
+ * What every room shares
49
+ */
50
+ private readonly services: RoomServices
51
+
52
+ /**
53
+ * @param seats who sits in the room
54
+ * @param properties limits and options of the room
55
+ * @param history whether the caches overflowed
56
+ * @param lifecycle keeps the room alive and times the empty seats
57
+ * @param services what every room shares
58
+ */
59
+ public constructor(
60
+ seats: Seats,
61
+ properties: RoomProperties,
62
+ history: History,
63
+ lifecycle: Lifecycle,
64
+ services: RoomServices,
65
+ ) {
66
+ this.seats = seats
67
+ this.properties = properties
68
+ this.history = history
69
+ this.lifecycle = lifecycle
70
+ this.services = services
71
+ }
72
+
73
+ /**
74
+ * The refusal for a user the room keeps out
75
+ * @param ban who's kept out and why
76
+ */
77
+ private static banned(ban: ExcludedUser): Fault {
78
+ switch (ban.reason) {
79
+ case 1:
80
+ return Fault.of(
81
+ ErrorCode.JOIN_FAILED_FOUND_EXCLUDED_USER_ID,
82
+ 'User {0} is banned in this game',
83
+ ban.userId,
84
+ )
85
+ case 2:
86
+ return Fault.of(
87
+ ErrorCode.JOIN_FAILED_FOUND_EXCLUDED_USER_ID,
88
+ 'User {0} is global banned',
89
+ ban.userId,
90
+ )
91
+ default:
92
+ return Fault.of(
93
+ ErrorCode.JOIN_FAILED_FOUND_EXCLUDED_USER_ID,
94
+ 'Unknown reason {0} for excluding ',
95
+ ban.reason,
96
+ )
97
+ }
98
+ }
99
+
100
+ /**
101
+ * Why a connection can't get into the room, or null
102
+ * @param peer who's asking
103
+ * @param setup the request as the room needs it
104
+ */
105
+ public refusal(peer: GamePeer, setup: EntrySetup): Fault | null {
106
+ const userId = peer.userId
107
+ const awaited = this.seats.expected.includes(userId)
108
+
109
+ if (setup.rejoining) {
110
+ const fault = this.rejoin(peer, setup)
111
+ if (fault !== undefined) return fault
112
+ if (awaited) {
113
+ return Fault.denied(ErrorMessage.REJOIN_OR_JOIN_WITH_EXPECTED_PLAYER)
114
+ }
115
+ }
116
+
117
+ if (!this.properties.isOpen) {
118
+ return Fault.of(
119
+ ErrorCode.GAME_CLOSED,
120
+ this.history.discarded
121
+ ? ErrorMessage.GAME_CLOSED_CACHE_DISCARDED
122
+ : ErrorMessage.GAME_CLOSED,
123
+ )
124
+ }
125
+
126
+ const limit = this.properties.maxPlayers
127
+ const taken = this.seats.taken + this.seats.awaited
128
+ if (limit > 0 && taken >= limit && !awaited) {
129
+ return Fault.of(ErrorCode.GAME_FULL, ErrorMessage.GAME_FULL)
130
+ }
131
+
132
+ return null
133
+ }
134
+
135
+ /**
136
+ * The seat a connection comes back to, null for a new one, or why it can't sit
137
+ * @param peer who's asking
138
+ * @param setup the request as the room needs it
139
+ */
140
+ public seat(peer: GamePeer, setup: EntrySetup): Actor | Fault | null {
141
+ if (this.seats.playing(peer) !== null) {
142
+ return Fault.of(
143
+ ErrorCode.JOIN_FAILED_PEER_ALREADY_JOINED,
144
+ 'Join failed: Peer already joined the specified game.',
145
+ )
146
+ }
147
+
148
+ const found = this.properties.has(RoomOptionFlag.CHECK_USER_ON_JOIN)
149
+ ? this.seatByUser(peer, setup)
150
+ : this.seatByNumber(peer, setup)
151
+ if (found !== null) return found
152
+
153
+ return setup.mode === JoinMode.REJOIN_ONLY
154
+ ? Fault.of(
155
+ ErrorCode.JOIN_FAILED_WITH_REJOINER_NOT_FOUND,
156
+ 'Rejoin failed: userid={0} not found, actor nr={1}.',
157
+ peer.userId,
158
+ setup.actorNumber,
159
+ )
160
+ : null
161
+ }
162
+
163
+ /**
164
+ * How a rejoin request fares, undefined to go on as a plain join
165
+ * @param peer who's asking
166
+ * @param setup the request as the room needs it
167
+ */
168
+ private rejoin(peer: GamePeer, setup: EntrySetup): Fault | null | undefined {
169
+ if (this.history.discarded) {
170
+ return Fault.of(
171
+ ErrorCode.EVENT_CACHE_EXCEEDED,
172
+ ErrorMessage.REJOINING_BLOCKED_CACHE_EXCEEDED,
173
+ )
174
+ }
175
+
176
+ const lasting = this.properties.playerTtl !== 0
177
+ const only = setup.mode === JoinMode.REJOIN_ONLY
178
+ if (!lasting && only) {
179
+ return Fault.denied(ErrorMessage.REJOIN_NOT_SUPPORTED)
180
+ }
181
+
182
+ const named = this.properties.has(RoomOptionFlag.CHECK_USER_ON_JOIN)
183
+ const actor = named
184
+ ? this.seats.user(peer.userId)
185
+ : this.seats.numbered(setup.actorNumber)
186
+ if (actor !== null) {
187
+ return lasting || only
188
+ ? null
189
+ : Fault.denied(ErrorMessage.REJOIN_NOT_SUPPORTED)
190
+ }
191
+
192
+ if (!only) return undefined
193
+
194
+ return named
195
+ ? Fault.of(
196
+ ErrorCode.JOIN_FAILED_WITH_REJOINER_NOT_FOUND,
197
+ ErrorMessage.USER_NOT_FOUND,
198
+ )
199
+ : Fault.of(
200
+ ErrorCode.JOIN_FAILED_WITH_REJOINER_NOT_FOUND,
201
+ ErrorMessage.ACTOR_NOT_FOUND,
202
+ setup.actorNumber,
203
+ )
204
+ }
205
+
206
+ /**
207
+ * The seat of a connection's user id, null for none, or why it can't sit
208
+ * @param peer who's asking
209
+ * @param setup the request as the room needs it
210
+ */
211
+ private seatByUser(peer: GamePeer, setup: EntrySetup): Actor | Fault | null {
212
+ const userId = peer.userId
213
+ if (userId === '') {
214
+ return Fault.invalid(
215
+ 'Join failed: UserId is not set, checkUserIdOnJoin=true expects a UserId.',
216
+ )
217
+ }
218
+
219
+ const ban = this.seats.excluded.find(
220
+ (user: ExcludedUser): boolean => user.userId === userId,
221
+ )
222
+ if (ban !== undefined) return Admission.banned(ban)
223
+
224
+ const actor = this.seats.user(userId)
225
+ if (actor === null) return null
226
+
227
+ if (actor.active) {
228
+ return this.takeover(actor, peer, setup, setup.forceRejoin)
229
+ ? actor
230
+ : Fault.of(
231
+ ErrorCode.JOIN_FAILED_FOUND_ACTIVE_JOINER,
232
+ ErrorMessage.USER_ALREADY_JOINED,
233
+ userId,
234
+ setup.mode,
235
+ )
236
+ }
237
+
238
+ if (this.lifecycle.expired(actor)) {
239
+ return Fault.of(
240
+ ErrorCode.JOIN_FAILED_WITH_REJOINER_NOT_FOUND,
241
+ "Join failed: UserId '{0}' is expired (JoinMode={1}).",
242
+ userId,
243
+ setup.mode,
244
+ )
245
+ }
246
+
247
+ return setup.rejoining
248
+ ? actor
249
+ : Fault.of(
250
+ ErrorCode.JOIN_FAILED_FOUND_INACTIVE_JOINER,
251
+ "Found inactive UserId '{0}', but not rejoining (JoinMode={1}).",
252
+ userId,
253
+ setup.mode,
254
+ )
255
+ }
256
+
257
+ /**
258
+ * The seat a request names by number, null for none, or why it can't sit
259
+ * @param peer who's asking
260
+ * @param setup the request as the room needs it
261
+ */
262
+ private seatByNumber(
263
+ peer: GamePeer,
264
+ setup: EntrySetup,
265
+ ): Actor | Fault | null {
266
+ const number = setup.actorNumber
267
+ if (number <= 0) return null
268
+
269
+ const actor = this.seats.numbered(number)
270
+ if (actor === null) {
271
+ return Fault.of(
272
+ ErrorCode.JOIN_FAILED_WITH_REJOINER_NOT_FOUND,
273
+ 'Rejoin failed: actor nr={0} not found.',
274
+ number,
275
+ )
276
+ }
277
+
278
+ if (actor.active) {
279
+ return this.takeover(actor, peer, setup, false)
280
+ ? actor
281
+ : Fault.of(
282
+ ErrorCode.JOIN_FAILED_FOUND_ACTIVE_JOINER,
283
+ ErrorMessage.USER_WITH_ACTOR_ID_ALREADY_JOINED,
284
+ number,
285
+ setup.mode,
286
+ )
287
+ }
288
+
289
+ const strangers =
290
+ actor.userId !== peer.userId &&
291
+ !(Admission.GUID.test(actor.userId) && Admission.GUID.test(peer.userId))
292
+ if (strangers) {
293
+ return Fault.internal(
294
+ "Rejoin failed: userId of peer doesn't match userid of actor.",
295
+ )
296
+ }
297
+
298
+ if (this.lifecycle.expired(actor)) {
299
+ return Fault.of(
300
+ ErrorCode.JOIN_FAILED_WITH_REJOINER_NOT_FOUND,
301
+ "Join failed: ActorNr '{0}' is expired.",
302
+ number,
303
+ )
304
+ }
305
+
306
+ return setup.rejoining
307
+ ? actor
308
+ : Fault.internal(
309
+ "Found inactive ActorNr '{0}', but not rejoining (JoinMode={1}).",
310
+ number,
311
+ setup.mode,
312
+ )
313
+ }
314
+
315
+ /**
316
+ * Frees a taken seat for a second connection of the same login
317
+ * @param actor the seat
318
+ * @param peer who's asking
319
+ * @param setup the request as the room needs it
320
+ * @param forced whether the request insists
321
+ */
322
+ private takeover(
323
+ actor: Actor,
324
+ peer: GamePeer,
325
+ setup: EntrySetup,
326
+ forced: boolean,
327
+ ): boolean {
328
+ const holder = actor.peer
329
+ if (
330
+ holder === null ||
331
+ this.properties.playerTtl === 0 ||
332
+ !setup.rejoining
333
+ ) {
334
+ return false
335
+ }
336
+
337
+ const first = holder.token
338
+ const second = peer.token
339
+ const same =
340
+ first !== null &&
341
+ second !== null &&
342
+ this.services.authenticator.same(first, second)
343
+ if (!forced && !same) return false
344
+
345
+ this.seats.suspend(actor, this.services.clock.now())
346
+
347
+ this.lifecycle.forget(holder)
348
+ holder.room = null
349
+ holder.drop()
350
+
351
+ return true
352
+ }
353
+ }
@@ -66,6 +66,19 @@ export class PropertyBag {
66
66
  return this.table.size
67
67
  }
68
68
 
69
+ /**
70
+ * How many properties it would hold after a merge
71
+ * @param properties what would be written
72
+ */
73
+ public grown(properties: PhotonHashtable): number {
74
+ let size = this.table.size
75
+ for (const [key] of properties.entries()) {
76
+ if (!this.table.has(key)) size += 1
77
+ }
78
+
79
+ return size
80
+ }
81
+
69
82
  /**
70
83
  * The value under a key, or null
71
84
  * @param key typed key
@@ -0,0 +1,79 @@
1
+ import type { Channel } from '@netifyjs/netify'
2
+
3
+ import { SharedFrames } from '../shared/index.ts'
4
+
5
+ /**
6
+ * Sends one packet to several connections
7
+ */
8
+ export type Broadcast = {
9
+ /**
10
+ * Sends a packet to every connection given
11
+ * @param channels who gets it
12
+ * @param packet a typed packet
13
+ */
14
+ spread(channels: readonly Channel[], packet: unknown): void
15
+ }
16
+
17
+ /**
18
+ * Sends a room's broadcasts, encoding once for clients that read the same bytes
19
+ * @class
20
+ */
21
+ export class Broadcaster implements Broadcast {
22
+ /**
23
+ * Encodes an event once, or null
24
+ */
25
+ private readonly shared: SharedFrames | null
26
+
27
+ /**
28
+ * Takes broadcasts over, or null
29
+ */
30
+ private route: Broadcast | null
31
+
32
+ /**
33
+ * @param shared encodes an event once, or null
34
+ */
35
+ public constructor(shared: SharedFrames | null) {
36
+ this.shared = shared
37
+ this.route = null
38
+ }
39
+
40
+ /**
41
+ * Hands every later broadcast to something else
42
+ * @param route takes broadcasts over
43
+ */
44
+ public reroute(route: Broadcast): void {
45
+ this.route = route
46
+ }
47
+
48
+ /**
49
+ * Sends a packet to every connection given
50
+ * @param channels who gets it
51
+ * @param packet a typed packet
52
+ */
53
+ public spread(channels: readonly Channel[], packet: unknown): void {
54
+ if (this.route !== null) {
55
+ this.route.spread(channels, packet)
56
+ return
57
+ }
58
+
59
+ const shared = channels.length > 1 ? this.shared : null
60
+ let frame: Uint8Array | null = null
61
+ let framed = false
62
+
63
+ for (const channel of channels) {
64
+ if (!channel.isActive) continue
65
+ if (shared === null || !SharedFrames.reads(channel)) {
66
+ channel.writeAndFlush(packet)
67
+ continue
68
+ }
69
+
70
+ if (!framed) {
71
+ frame = shared.frame(packet)
72
+ framed = true
73
+ }
74
+
75
+ if (frame === null) channel.writeAndFlush(packet)
76
+ else channel.send(frame)
77
+ }
78
+ }
79
+ }