@dcl/sdk 7.24.6-29505165911.commit-d270434 → 7.24.6-29580060460.commit-06f0b7e

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 (72) hide show
  1. package/composite-provider.js +4 -9
  2. package/internal/utf8.d.ts +15 -0
  3. package/internal/utf8.js +172 -0
  4. package/network/binary-message-bus.d.ts +3 -6
  5. package/network/binary-message-bus.js +5 -9
  6. package/network/index.d.ts +2 -8
  7. package/network/index.js +3 -16
  8. package/network/message-bus-sync.d.ts +1 -14
  9. package/network/message-bus-sync.js +103 -161
  10. package/network/state.js +5 -3
  11. package/package.json +6 -7
  12. package/src/composite-provider.ts +3 -10
  13. package/src/internal/utf8.ts +167 -0
  14. package/src/network/binary-message-bus.ts +4 -9
  15. package/src/network/index.ts +3 -40
  16. package/src/network/message-bus-sync.ts +110 -174
  17. package/src/network/state.ts +4 -3
  18. package/src/text-codec.ts +59 -12
  19. package/text-codec.d.ts +23 -8
  20. package/text-codec.js +55 -13
  21. package/atom.d.ts +0 -19
  22. package/atom.js +0 -83
  23. package/future.d.ts +0 -8
  24. package/future.js +0 -26
  25. package/network/chunking.d.ts +0 -5
  26. package/network/chunking.js +0 -38
  27. package/network/events/implementation.d.ts +0 -93
  28. package/network/events/implementation.js +0 -221
  29. package/network/events/index.d.ts +0 -42
  30. package/network/events/index.js +0 -43
  31. package/network/events/protocol.d.ts +0 -27
  32. package/network/events/protocol.js +0 -66
  33. package/network/events/registry.d.ts +0 -8
  34. package/network/events/registry.js +0 -3
  35. package/network/server/index.d.ts +0 -14
  36. package/network/server/index.js +0 -219
  37. package/network/server/utils.d.ts +0 -18
  38. package/network/server/utils.js +0 -135
  39. package/server/env-var.d.ts +0 -15
  40. package/server/env-var.js +0 -31
  41. package/server/index.d.ts +0 -2
  42. package/server/index.js +0 -3
  43. package/server/storage/constants.d.ts +0 -23
  44. package/server/storage/constants.js +0 -2
  45. package/server/storage/index.d.ts +0 -22
  46. package/server/storage/index.js +0 -29
  47. package/server/storage/player.d.ts +0 -43
  48. package/server/storage/player.js +0 -92
  49. package/server/storage/scene.d.ts +0 -38
  50. package/server/storage/scene.js +0 -90
  51. package/server/storage-url.d.ts +0 -10
  52. package/server/storage-url.js +0 -29
  53. package/server/utils.d.ts +0 -35
  54. package/server/utils.js +0 -56
  55. package/src/atom.ts +0 -98
  56. package/src/ethereum-provider/text-encoding.d.ts +0 -4
  57. package/src/future.ts +0 -38
  58. package/src/network/chunking.ts +0 -45
  59. package/src/network/events/implementation.ts +0 -271
  60. package/src/network/events/index.ts +0 -48
  61. package/src/network/events/protocol.ts +0 -94
  62. package/src/network/events/registry.ts +0 -18
  63. package/src/network/server/index.ts +0 -301
  64. package/src/network/server/utils.ts +0 -189
  65. package/src/server/env-var.ts +0 -36
  66. package/src/server/index.ts +0 -2
  67. package/src/server/storage/constants.ts +0 -22
  68. package/src/server/storage/index.ts +0 -44
  69. package/src/server/storage/player.ts +0 -156
  70. package/src/server/storage/scene.ts +0 -149
  71. package/src/server/storage-url.ts +0 -34
  72. package/src/server/utils.ts +0 -73
@@ -0,0 +1,167 @@
1
+ /**
2
+ * Self-contained UTF-8 codec implementing the WHATWG Encoding Standard
3
+ * algorithms (https://encoding.spec.whatwg.org/), so SDK code never depends
4
+ * on the host providing `TextEncoder` / `TextDecoder` globals.
5
+ */
6
+
7
+ const REPLACEMENT_CHARACTER = 0xfffd
8
+
9
+ export type DecodeUtf8Options = {
10
+ fatal?: boolean
11
+ ignoreBOM?: boolean
12
+ }
13
+
14
+ export function decodeUtf8(input: Uint8Array, options: DecodeUtf8Options = {}): string {
15
+ const fatal = options.fatal === true
16
+ const start =
17
+ !options.ignoreBOM && input.length >= 3 && input[0] === 0xef && input[1] === 0xbb && input[2] === 0xbf ? 3 : 0
18
+
19
+ let result = ''
20
+ let pending: number[] = []
21
+ let codePoint = 0
22
+ let bytesNeeded = 0
23
+ let bytesSeen = 0
24
+ let lowerBoundary = 0x80
25
+ let upperBoundary = 0xbf
26
+
27
+ function emit(value: number) {
28
+ if (value <= 0xffff) {
29
+ pending.push(value)
30
+ } else {
31
+ const offset = value - 0x10000
32
+ pending.push(0xd800 + (offset >> 10), 0xdc00 + (offset & 0x3ff))
33
+ }
34
+ if (pending.length >= 4096) {
35
+ result += String.fromCharCode(...pending)
36
+ pending = []
37
+ }
38
+ }
39
+
40
+ function malformed() {
41
+ if (fatal) throw new TypeError('decodeUtf8: the encoded data is not valid utf-8')
42
+ emit(REPLACEMENT_CHARACTER)
43
+ }
44
+
45
+ for (let i = start; i < input.length; i++) {
46
+ const byte = input[i]
47
+ if (bytesNeeded === 0) {
48
+ if (byte <= 0x7f) {
49
+ emit(byte)
50
+ } else if (byte >= 0xc2 && byte <= 0xdf) {
51
+ bytesNeeded = 1
52
+ codePoint = byte & 0x1f
53
+ } else if (byte >= 0xe0 && byte <= 0xef) {
54
+ if (byte === 0xe0) lowerBoundary = 0xa0
55
+ if (byte === 0xed) upperBoundary = 0x9f
56
+ bytesNeeded = 2
57
+ codePoint = byte & 0xf
58
+ } else if (byte >= 0xf0 && byte <= 0xf4) {
59
+ if (byte === 0xf0) lowerBoundary = 0x90
60
+ if (byte === 0xf4) upperBoundary = 0x8f
61
+ bytesNeeded = 3
62
+ codePoint = byte & 0x7
63
+ } else {
64
+ malformed()
65
+ }
66
+ } else if (byte < lowerBoundary || byte > upperBoundary) {
67
+ codePoint = 0
68
+ bytesNeeded = 0
69
+ bytesSeen = 0
70
+ lowerBoundary = 0x80
71
+ upperBoundary = 0xbf
72
+ malformed()
73
+ // the spec prepends the byte back to the stream: it starts a new sequence
74
+ i--
75
+ } else {
76
+ lowerBoundary = 0x80
77
+ upperBoundary = 0xbf
78
+ codePoint = (codePoint << 6) | (byte & 0x3f)
79
+ if (++bytesSeen === bytesNeeded) {
80
+ emit(codePoint)
81
+ codePoint = 0
82
+ bytesNeeded = 0
83
+ bytesSeen = 0
84
+ }
85
+ }
86
+ }
87
+ if (bytesNeeded !== 0) malformed()
88
+
89
+ if (pending.length > 0) result += String.fromCharCode(...pending)
90
+ return result
91
+ }
92
+
93
+ export function encodeUtf8(input: string): Uint8Array {
94
+ const output = new Uint8Array(utf8ByteLength(input))
95
+ let offset = 0
96
+ for (let i = 0; i < input.length; i++) {
97
+ const codePoint = codePointAt(input, i)
98
+ if (codePoint > 0xffff) i++
99
+ offset = writeCodePoint(output, offset, codePoint)
100
+ }
101
+ return output
102
+ }
103
+
104
+ export function encodeUtf8Into(source: string, destination: Uint8Array): { read: number; written: number } {
105
+ let read = 0
106
+ let written = 0
107
+ for (let i = 0; i < source.length; i++) {
108
+ const codePoint = codePointAt(source, i)
109
+ const size = byteSize(codePoint)
110
+ if (written + size > destination.length) break
111
+ written = writeCodePoint(destination, written, codePoint)
112
+ if (codePoint > 0xffff) {
113
+ i++
114
+ read += 2
115
+ } else {
116
+ read += 1
117
+ }
118
+ }
119
+ return { read, written }
120
+ }
121
+
122
+ // Reads the code point at index, replacing unpaired surrogates with U+FFFD
123
+ // (the USVString conversion `TextEncoder` mandates).
124
+ function codePointAt(input: string, index: number): number {
125
+ const first = input.charCodeAt(index)
126
+ if (first >= 0xd800 && first <= 0xdbff) {
127
+ const second = index + 1 < input.length ? input.charCodeAt(index + 1) : 0
128
+ if (second >= 0xdc00 && second <= 0xdfff) return 0x10000 + ((first - 0xd800) << 10) + (second - 0xdc00)
129
+ return REPLACEMENT_CHARACTER
130
+ }
131
+ if (first >= 0xdc00 && first <= 0xdfff) return REPLACEMENT_CHARACTER
132
+ return first
133
+ }
134
+
135
+ function byteSize(codePoint: number): number {
136
+ return codePoint <= 0x7f ? 1 : codePoint <= 0x7ff ? 2 : codePoint <= 0xffff ? 3 : 4
137
+ }
138
+
139
+ function utf8ByteLength(input: string): number {
140
+ let length = 0
141
+ for (let i = 0; i < input.length; i++) {
142
+ const codePoint = codePointAt(input, i)
143
+ if (codePoint > 0xffff) i++
144
+ length += byteSize(codePoint)
145
+ }
146
+ return length
147
+ }
148
+
149
+ function writeCodePoint(output: Uint8Array, offset: number, codePoint: number): number {
150
+ let next = offset
151
+ if (codePoint <= 0x7f) {
152
+ output[next++] = codePoint
153
+ } else if (codePoint <= 0x7ff) {
154
+ output[next++] = 0xc0 | (codePoint >> 6)
155
+ output[next++] = 0x80 | (codePoint & 0x3f)
156
+ } else if (codePoint <= 0xffff) {
157
+ output[next++] = 0xe0 | (codePoint >> 12)
158
+ output[next++] = 0x80 | ((codePoint >> 6) & 0x3f)
159
+ output[next++] = 0x80 | (codePoint & 0x3f)
160
+ } else {
161
+ output[next++] = 0xf0 | (codePoint >> 18)
162
+ output[next++] = 0x80 | ((codePoint >> 12) & 0x3f)
163
+ output[next++] = 0x80 | ((codePoint >> 6) & 0x3f)
164
+ output[next++] = 0x80 | (codePoint & 0x3f)
165
+ }
166
+ return next
167
+ }
@@ -1,12 +1,9 @@
1
1
  import { ReadWriteByteBuffer } from '@dcl/ecs/dist/serialization/ByteBuffer'
2
2
 
3
3
  export enum CommsMessage {
4
- CRDT = 7,
5
- REQ_CRDT_STATE = 8,
6
- RES_CRDT_STATE = 9,
7
- CRDT_SERVER = 4,
8
- CRDT_AUTHORITATIVE = 5,
9
- CUSTOM_EVENT = 6
4
+ CRDT = 1,
5
+ REQ_CRDT_STATE = 2,
6
+ RES_CRDT_STATE = 3
10
7
  }
11
8
 
12
9
  export function BinaryMessageBus<T extends CommsMessage>(
@@ -23,9 +20,7 @@ export function BinaryMessageBus<T extends CommsMessage>(
23
20
  __processMessages: (messages: Uint8Array[]) => {
24
21
  for (const message of messages) {
25
22
  const commsMsg = decodeCommsMessage<T>(message)
26
- if (!commsMsg) {
27
- continue
28
- }
23
+ if (!commsMsg) continue
29
24
  const { sender, messageType, data } = commsMsg
30
25
  const fn = mapping.get(messageType)
31
26
  if (fn) fn(data, sender)
@@ -2,46 +2,9 @@ import { sendBinary } from '~system/CommunicationsController'
2
2
  import { engine } from '@dcl/ecs'
3
3
  import { addSyncTransport } from './message-bus-sync'
4
4
  import { getUserData } from '~system/UserIdentity'
5
- import { isServer as isServerApi } from '~system/EngineApi'
6
- import { Atom } from '../atom'
7
-
8
- // Create isServer atom for consistent state
9
- const isServerAtom = Atom<boolean>(false)
10
- void isServerApi({}).then((response) => {
11
- isServerAtom.swap(!!response.isServer)
12
- })
13
-
14
- // Helper function to check if running on server
15
- export function isServer(): boolean {
16
- return isServerAtom.getOrNull() ?? false
17
- }
18
5
 
19
6
  // initialize sync transport for sdk engine
20
- const {
21
- getChildren,
22
- syncEntity,
23
- parentEntity,
24
- getParent,
25
- myProfile,
26
- removeParent,
27
- getFirstChild,
28
- isStateSyncronized,
29
- binaryMessageBus,
30
- eventBus
31
- } = addSyncTransport(engine, sendBinary, getUserData, isServerApi, 'network')
32
-
33
- // Re-export the room messaging system
34
- export { registerMessages, getRoom } from './events'
7
+ const { getChildren, syncEntity, parentEntity, getParent, myProfile, removeParent, getFirstChild, isStateSyncronized } =
8
+ addSyncTransport(engine, sendBinary, getUserData)
35
9
 
36
- export {
37
- getFirstChild,
38
- getChildren,
39
- syncEntity,
40
- parentEntity,
41
- getParent,
42
- myProfile,
43
- removeParent,
44
- isStateSyncronized,
45
- binaryMessageBus,
46
- eventBus
47
- }
10
+ export { getFirstChild, getChildren, syncEntity, parentEntity, getParent, myProfile, removeParent, isStateSyncronized }
@@ -1,53 +1,27 @@
1
- import { IEngine, Transport, RealmInfo } from '@dcl/ecs'
1
+ import { IEngine, Transport, RealmInfo, PlayerIdentityData } from '@dcl/ecs'
2
2
  import { type SendBinaryRequest, type SendBinaryResponse } from '~system/CommunicationsController'
3
3
 
4
4
  import { syncFilter } from './filter'
5
5
  import { engineToCrdt } from './state'
6
- import { BinaryMessageBus, CommsMessage } from './binary-message-bus'
6
+ import { BinaryMessageBus, CommsMessage, decodeString, encodeString } from './binary-message-bus'
7
7
  import { fetchProfile } from './utils'
8
8
  import { entityUtils } from './entities'
9
- import { createServerValidator } from './server'
10
9
  import { GetUserDataRequest, GetUserDataResponse } from '~system/UserIdentity'
11
10
  import { definePlayerHelper } from '../players'
12
11
  import { serializeCrdtMessages } from '../internal/transports/logger'
13
- import { IsServerRequest, IsServerResponse } from '~system/EngineApi'
14
- import { Atom } from '../atom'
15
- import { setGlobalRoom, Room } from './events/implementation'
16
12
 
17
13
  export type IProfile = { networkId: number; userId: string }
18
14
  // user that we asked for the inital crdt state
19
- export const AUTH_SERVER_PEER_ID = 'authoritative-server'
20
- export const DEBUG_NETWORK_MESSAGES = () => (globalThis as any).DEBUG_NETWORK_MESSAGES ?? false
21
-
22
- // Test environment detection without 'as any'
23
- const isTestEnvironment = (): boolean => {
24
- try {
25
- if (typeof globalThis === 'undefined') return false
26
- const globalWithProcess = globalThis as unknown as { process?: { env?: { NODE_ENV?: string } } }
27
- return globalWithProcess.process?.env?.NODE_ENV === 'test'
28
- } catch {
29
- return false
30
- }
31
- }
32
-
33
15
  export function addSyncTransport(
34
16
  engine: IEngine,
35
17
  sendBinary: (msg: SendBinaryRequest) => Promise<SendBinaryResponse>,
36
- getUserData: (value: GetUserDataRequest) => Promise<GetUserDataResponse>,
37
- isServerFn: (request: IsServerRequest) => Promise<IsServerResponse>,
38
- name: string
18
+ getUserData: (value: GetUserDataRequest) => Promise<GetUserDataResponse>
39
19
  ) {
20
+ const DEBUG_NETWORK_MESSAGES = () => (globalThis as any).DEBUG_NETWORK_MESSAGES ?? false
40
21
  // Profile Info
41
22
  const myProfile: IProfile = {} as IProfile
42
23
  fetchProfile(myProfile!, getUserData)
43
24
 
44
- const isServerAtom = Atom<boolean>()
45
- const isRoomReadyAtom = Atom<boolean>(false)
46
-
47
- void isServerFn({}).then(($: IsServerResponse) => {
48
- return isServerAtom.swap(!!$.isServer)
49
- })
50
-
51
25
  // Entity utils
52
26
  const entityDefinitions = entityUtils(engine, myProfile)
53
27
 
@@ -66,190 +40,108 @@ export function addSyncTransport(
66
40
  const players = definePlayerHelper(engine)
67
41
 
68
42
  let stateIsSyncronized = false
43
+ let transportInitialzed = false
69
44
 
70
- /**
71
- * We need to wait till 2 ticks that is when the engine is ready to send new messages.
72
- * The first tick is for the client engine processing the CRDT messages,
73
- * and the second one are the messages created by the main() function.
74
- * So to avoid sending those messages, that all the clients have, through the network we put this validation here.
75
- */
76
- let tick = 0
77
- const TRANSPORT_INITIALIZED_NUMBER = isTestEnvironment() ? 0 : 2
78
45
  // Add Sync Transport
79
46
  const transport: Transport = {
80
47
  filter: syncFilter(engine),
81
48
  send: async (messages) => {
82
- if (tick <= TRANSPORT_INITIALIZED_NUMBER) tick++
83
- for (const message of tick > TRANSPORT_INITIALIZED_NUMBER ? [messages].flat() : []) {
84
- if (message.byteLength) {
49
+ for (const message of [messages].flat()) {
50
+ if (message.byteLength && transportInitialzed) {
85
51
  DEBUG_NETWORK_MESSAGES() &&
86
52
  console.log(...Array.from(serializeCrdtMessages('[NetworkMessage sent]:', message, engine)))
87
-
88
- // Convert regular messages to network messages for broadcasting with chunking
89
- for (const chunk of serverValidator.convertRegularToNetworkMessage(message)) {
90
- binaryMessageBus.emit(CommsMessage.CRDT, chunk)
91
- }
53
+ binaryMessageBus.emit(CommsMessage.CRDT, message)
92
54
  }
93
55
  }
94
56
  const peerMessages = getMessagesToSend()
57
+ let totalSize = 0
58
+ for (const message of peerMessages) {
59
+ for (const data of message.data) {
60
+ totalSize += data.byteLength
61
+ }
62
+ }
63
+ if (totalSize) {
64
+ DEBUG_NETWORK_MESSAGES() && console.log('Sending network messages: ', totalSize / 1024, 'KB')
65
+ }
95
66
  const response = await sendBinary({ data: [], peerData: peerMessages })
96
67
  binaryMessageBus.__processMessages(response.data)
68
+ transportInitialzed = true
97
69
  },
98
- type: name
70
+ type: 'network'
99
71
  }
100
-
101
- // Server validation setup
102
- const serverValidator = createServerValidator({
103
- engine,
104
- binaryMessageBus
105
- })
106
-
107
- // Initialize Event Bus with registered schemas
108
- const eventBus = new Room(engine, binaryMessageBus, isServerAtom, isRoomReadyAtom)
109
-
110
- // Set global eventBus instance
111
- setGlobalRoom(eventBus)
112
-
113
72
  engine.addTransport(transport)
114
73
  // End add sync transport
115
74
 
116
75
  // Receive & Process CRDT_STATE
117
- binaryMessageBus.on(CommsMessage.REQ_CRDT_STATE, async (data, sender) => {
118
- DEBUG_NETWORK_MESSAGES() && console.log('[REQ_CRDT_STATE]', sender, Date.now())
119
- const chunks = engineToCrdt(engine)
120
- if (chunks.length === 0) {
121
- DEBUG_NETWORK_MESSAGES() && console.log('[Emiting empty state:]', sender, Date.now())
122
- binaryMessageBus.emit(CommsMessage.RES_CRDT_STATE, new Uint8Array(), [sender])
123
- } else {
124
- for (const chunk of chunks) {
125
- DEBUG_NETWORK_MESSAGES() && console.log('[Emiting:]', sender, Date.now())
126
- binaryMessageBus.emit(CommsMessage.RES_CRDT_STATE, chunk, [sender])
127
- }
128
- }
129
- })
130
- binaryMessageBus.on(CommsMessage.RES_CRDT_STATE, async (data, sender) => {
131
- requestingState = false
132
- elapsedTimeSinceRequest = 0
133
- if (isServerAtom.getOrNull() || sender !== AUTH_SERVER_PEER_ID) return
76
+ binaryMessageBus.on(CommsMessage.RES_CRDT_STATE, (value) => {
77
+ const { sender, data } = decodeCRDTState(value)
78
+ if (sender !== myProfile.userId) return
134
79
  DEBUG_NETWORK_MESSAGES() && console.log('[Processing CRDT State]', data.byteLength / 1024, 'KB')
135
- if (data.byteLength > 0) {
136
- transport.onmessage!(serverValidator.processClientMessages(data, sender))
137
- }
80
+ transport.onmessage!(data)
138
81
  stateIsSyncronized = true
82
+ })
83
+
84
+ // Answer to REQ_CRDT_STATE
85
+ binaryMessageBus.on(CommsMessage.REQ_CRDT_STATE, async (_, userId) => {
86
+ DEBUG_NETWORK_MESSAGES() && console.log(`Sending CRDT State to: ${userId}`)
139
87
 
140
- // IMPORTANT: Only mark room as ready AFTER state is synchronized
141
- // This ensures comms is truly connected and working
142
- const realmInfo = RealmInfo.getOrNull(engine.RootEntity)
143
- if (realmInfo) {
144
- DEBUG_NETWORK_MESSAGES() && console.log('[isRoomReady] Marking room as ready after state sync')
145
- isRoomReadyAtom.swap(true)
88
+ for (const chunk of engineToCrdt(engine)) {
89
+ binaryMessageBus.emit(CommsMessage.RES_CRDT_STATE, encodeCRDTState(userId, chunk), [userId])
146
90
  }
147
91
  })
148
92
 
149
- // received message from the network
150
- binaryMessageBus.on(CommsMessage.CRDT, (value, sender) => {
151
- const isServer = isServerAtom.getOrNull()
93
+ // Process CRDT messages here
94
+ binaryMessageBus.on(CommsMessage.CRDT, (value) => {
152
95
  DEBUG_NETWORK_MESSAGES() &&
153
- console.log(
154
- transport.type,
155
- ...Array.from(serializeCrdtMessages('[NetworkMessage received]:', value, engine)),
156
- isServer
157
- )
158
- if (isServer) {
159
- transport.onmessage!(serverValidator.processServerMessages(value, sender))
160
- } else if (sender === AUTH_SERVER_PEER_ID) {
161
- // Process network messages from server and convert to regular messages
162
- transport.onmessage!(serverValidator.processClientMessages(value, sender))
163
- }
96
+ console.log(Array.from(serializeCrdtMessages('[NetworkMessage received]:', value, engine)))
97
+ transport.onmessage!(value)
164
98
  })
165
99
 
166
- // received authoritative message from server - force apply to fix invalid local state
167
- binaryMessageBus.on(CommsMessage.CRDT_AUTHORITATIVE, (value, sender) => {
168
- // Only accept authoritative messages from authoritative server
169
- if (sender !== AUTH_SERVER_PEER_ID) return
100
+ async function requestState(retryCount: number = 1) {
101
+ let players = Array.from(engine.getEntitiesWith(PlayerIdentityData))
102
+ DEBUG_NETWORK_MESSAGES() && console.log(`Requesting state. Players connected: ${players.length - 1}`)
103
+
104
+ if (!RealmInfo.getOrNull(engine.RootEntity)?.isConnectedSceneRoom) {
105
+ DEBUG_NETWORK_MESSAGES() && console.log(`Aborting Requesting state?. Disconnected`)
106
+ return
107
+ }
170
108
 
171
- // DEBUG_NETWORK_MESSAGES() &&
172
- console.log('[AUTHORITATIVE] Received authoritative message from server:', value.byteLength, 'bytes')
109
+ binaryMessageBus.emit(CommsMessage.REQ_CRDT_STATE, new Uint8Array())
173
110
 
174
- // Process authoritative messages by forcing them through normal CRDT processing
175
- // but with a timestamp that's guaranteed to be accepted
176
- const authoritativeBuffer = serverValidator.processClientMessages(value, sender, true)
177
- if (authoritativeBuffer.byteLength > 0) {
178
- // Apply authoritative message through normal transport, but the server's messages
179
- // should be processed as authoritative with special timestamp handling
180
- transport.onmessage!(authoritativeBuffer)
111
+ // Wait ~5s for the response.
112
+ await sleep(5000)
181
113
 
182
- DEBUG_NETWORK_MESSAGES() && console.log('[AUTHORITATIVE] Applied server authoritative message to local state')
114
+ players = Array.from(engine.getEntitiesWith(PlayerIdentityData))
115
+
116
+ if (!stateIsSyncronized) {
117
+ if (players.length > 1 && retryCount <= 2) {
118
+ DEBUG_NETWORK_MESSAGES() &&
119
+ console.log(`Requesting state again ${retryCount} (no response). Players connected: ${players.length - 1}`)
120
+ void requestState(retryCount + 1)
121
+ } else {
122
+ DEBUG_NETWORK_MESSAGES() && console.log('No active players. State syncronized')
123
+ stateIsSyncronized = true
124
+ }
183
125
  }
184
- })
126
+ }
185
127
 
186
128
  players.onEnterScene((player) => {
187
129
  DEBUG_NETWORK_MESSAGES() && console.log('[onEnterScene]', player.userId)
188
- if (!isServerAtom.getOrNull() && myProfile.userId === player.userId) {
189
- requestState()
190
- }
191
130
  })
192
131
 
193
132
  // Asks for the REQ_CRDT_STATE when its connected to comms
194
133
  RealmInfo.onChange(engine.RootEntity, (value) => {
195
- const isServer = isServerAtom.getOrNull()
196
-
197
134
  if (!value?.isConnectedSceneRoom) {
198
- // Only react when actually transitioning from ready to not ready
199
- if (isRoomReadyAtom.getOrNull() === true) {
200
- DEBUG_NETWORK_MESSAGES() && console.log('Disconnected from comms')
201
- isRoomReadyAtom.swap(false)
202
- if (!isServer) {
203
- stateIsSyncronized = false
204
- }
205
- }
135
+ DEBUG_NETWORK_MESSAGES() && console.log('Disconnected from comms')
136
+ stateIsSyncronized = false
206
137
  }
207
138
 
208
139
  if (value?.isConnectedSceneRoom) {
209
- requestState()
210
-
211
- // For servers, mark as ready immediately when connected
212
- // (servers don't need to sync state from anyone)
213
- if (isServer && isRoomReadyAtom.getOrNull() === false) {
214
- DEBUG_NETWORK_MESSAGES() && console.log('[isRoomReady] Server marking room as ready')
215
- isRoomReadyAtom.swap(true)
216
- }
217
- // For clients, room will be marked ready after receiving CRDT state (above)
218
- }
219
- })
220
-
221
- let requestingState = false
222
- let elapsedTimeSinceRequest = 0
223
- const STATE_REQUEST_RETRY_INTERVAL = 2.0 // seconds
224
-
225
- /**
226
- * Why we have to request the state if we have a server that can send us the state when we joined?
227
- * The thing is that when the server detects a new JOIN_PARTICIPANT on livekit room, it sends automatically the state to that peer.
228
- * But in unity, it takes more time, so that message is not being delivered to the client.
229
- * So instead, when we are finally connected to the room, we request the state, and then the server answers with the state :)
230
- *
231
- * If no response is received within 2 seconds, the request is automatically retried.
232
- */
233
- function requestState() {
234
- if (isServerAtom.getOrNull()) return
235
- if (RealmInfo.getOrNull(engine.RootEntity)?.isConnectedSceneRoom && !requestingState) {
236
- requestingState = true
237
- elapsedTimeSinceRequest = 0
238
- DEBUG_NETWORK_MESSAGES() && console.log('Requesting state...')
239
- binaryMessageBus.emit(CommsMessage.REQ_CRDT_STATE, new Uint8Array())
140
+ DEBUG_NETWORK_MESSAGES() && console.log('Connected to comms')
240
141
  }
241
- }
242
142
 
243
- // System to retry state request if no response is received within the retry interval
244
- engine.addSystem((dt: number) => {
245
- if (requestingState && !stateIsSyncronized) {
246
- elapsedTimeSinceRequest += dt
247
- if (elapsedTimeSinceRequest >= STATE_REQUEST_RETRY_INTERVAL) {
248
- DEBUG_NETWORK_MESSAGES() && console.log('State request timed out, retrying...')
249
- elapsedTimeSinceRequest = 0
250
- requestingState = false
251
- requestState()
252
- }
143
+ if (value?.isConnectedSceneRoom && !stateIsSyncronized) {
144
+ void requestState()
253
145
  }
254
146
  })
255
147
 
@@ -261,12 +153,56 @@ export function addSyncTransport(
261
153
  return stateIsSyncronized
262
154
  }
263
155
 
156
+ function sleep(ms: number) {
157
+ return new Promise<void>((resolve) => {
158
+ let timer = 0
159
+ function sleepSystem(dt: number) {
160
+ timer += dt
161
+ if (timer * 1000 >= ms) {
162
+ engine.removeSystem(sleepSystem)
163
+ resolve()
164
+ }
165
+ }
166
+ engine.addSystem(sleepSystem)
167
+ })
168
+ }
169
+
264
170
  return {
265
171
  ...entityDefinitions,
266
172
  myProfile,
267
- isStateSyncronized,
268
- binaryMessageBus,
269
- eventBus,
270
- isRoomReadyAtom
173
+ isStateSyncronized
271
174
  }
272
175
  }
176
+
177
+ /**
178
+ * Messages Protocol Encoding
179
+ *
180
+ * CRDT: Plain Uint8Array
181
+ *
182
+ * CRDT_STATE_RES { sender: string, data: Uint8Array}
183
+ */
184
+ function decodeCRDTState(data: Uint8Array) {
185
+ let offset = 0
186
+ const r = new Uint8Array(data)
187
+ const view = new DataView(r.buffer)
188
+ const senderLength = view.getUint8(offset)
189
+ offset += 1
190
+ const sender = decodeString(data.subarray(1, senderLength + 1))
191
+ offset += senderLength
192
+ const state = r.subarray(offset)
193
+
194
+ return { sender, data: state }
195
+ }
196
+
197
+ function encodeCRDTState(address: string, data: Uint8Array) {
198
+ // address to uint8array
199
+ const addressBuffer = encodeString(address)
200
+ const addressOffset = 1
201
+ const messageLength = addressOffset + addressBuffer.byteLength + data.byteLength
202
+
203
+ const serializedMessage = new Uint8Array(messageLength)
204
+ serializedMessage.set(new Uint8Array([addressBuffer.byteLength]), 0)
205
+ serializedMessage.set(addressBuffer, 1)
206
+ serializedMessage.set(data, addressBuffer.byteLength + 1)
207
+ return serializedMessage
208
+ }
@@ -28,7 +28,7 @@ import {
28
28
  TriggerAreaResult,
29
29
  ComponentDefinition
30
30
  } from '@dcl/ecs'
31
- import { LIVEKIT_MAX_SIZE } from './server'
31
+ import { LIVEKIT_MAX_SIZE } from '@dcl/ecs/dist/systems/crdt'
32
32
 
33
33
  export const NOT_SYNC_COMPONENTS: ComponentDefinition<unknown>[] = [
34
34
  VideoEvent,
@@ -79,9 +79,9 @@ export function engineToCrdt(engine: IEngine): Uint8Array[] {
79
79
  if (!shouldSyncComponent(itComponentDefinition)) {
80
80
  continue
81
81
  }
82
-
83
82
  itComponentDefinition.dumpCrdtStateToBuffer(crdtBuffer, (entity) => {
84
- return NetworkEntity.has(entity)
83
+ const isNetworkEntity = NetworkEntity.has(entity)
84
+ return isNetworkEntity
85
85
  })
86
86
  }
87
87
 
@@ -103,6 +103,7 @@ export function engineToCrdt(engine: IEngine): Uint8Array[] {
103
103
  }
104
104
 
105
105
  // If the message itself is larger than the limit, we need to handle it specially
106
+ // For now, we'll skip it to prevent infinite loops
106
107
  if (messageSize / 1024 > LIVEKIT_MAX_SIZE) {
107
108
  console.error(
108
109
  `Message too large (${messageSize} bytes), skipping component ${message.componentId} for entity ${message.entityId}`