Skip to content

Commit

Permalink
remove magic numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
gonpombo8 committed Jan 21, 2025
1 parent 2169828 commit 649683b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
19 changes: 12 additions & 7 deletions packages/@dcl/ecs/src/systems/crdt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,21 +243,25 @@ export function crdtSceneSystem(engine: PreEngine, onProcessEntityComponentChang
}

// Send CRDT messages to transports
const transportBuffers: Uint8Array[] = []
const transportBuffer = new ReadWriteByteBuffer()
for (const index in transports) {
// NetworkMessages can only have a MAX_SIZE of 13kb. So we need to send it in chunks.
const LIVEKIT_MAX_SIZE = 13
const __NetworkMessagesBuffer: Uint8Array[] = []

const transportIndex = Number(index)
const transport = transports[transportIndex]
const isRendererTransport = transport.type === 'renderer'
const isNetworkTransport = transport.type === 'network'

// Reset Buffer for each Transport
transportBuffer.resetBuffer()
transportBuffers.length = 0
const buffer = new ReadWriteByteBuffer()

// Then we send all the new crdtMessages that the transport needs to process
for (const message of crdtMessages) {
if (isNetworkTransport && transportBuffer.toBinary().byteLength / 1024 > 13) {
transportBuffers.push(transportBuffer.toBinary())
if (isNetworkTransport && transportBuffer.toBinary().byteLength / 1024 > LIVEKIT_MAX_SIZE) {
__NetworkMessagesBuffer.push(transportBuffer.toBinary())
transportBuffer.resetBuffer()
}
// Avoid echo messages
Expand Down Expand Up @@ -316,10 +320,11 @@ export function crdtSceneSystem(engine: PreEngine, onProcessEntityComponentChang
// Common message
transportBuffer.writeBuffer(message.messageBuffer, false)
}
if (transportBuffer.currentWriteOffset()) {
transportBuffers.push(transportBuffer.toBinary())

if (isNetworkTransport && transportBuffer.currentWriteOffset()) {
__NetworkMessagesBuffer.push(transportBuffer.toBinary())
}
const message = isNetworkTransport ? transportBuffers : transportBuffer.toBinary()
const message = isNetworkTransport ? __NetworkMessagesBuffer : transportBuffer.toBinary()
await transport.send(message)
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/@dcl/ecs/src/systems/crdt/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export type TransportMessage = Omit<ReceiveMessage, 'data'>
* @public
*/
export type Transport = {
/**
* For Network messages its an Uint8Array[]. Due too the LiveKit MAX_SIZE = 13kb
* For Renderer & Other transports we send a single Uint8Array
*/
send(message: Uint8Array | Uint8Array[]): Promise<void>
onmessage?(message: Uint8Array): void
filter(message: Omit<TransportMessage, 'messageBuffer'>): boolean
Expand Down

0 comments on commit 649683b

Please sign in to comment.