@dcl/ecs 1.0.0-20220601102612.commit-8b31ac4 → 1.0.0-2448628808.commit-1a55b7a
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.
- package/dist/index.d.ts +1676 -3
- package/dist/index.js +5815 -3
- package/dist/index.min.js +1 -0
- package/dist/index.min.js.map +1 -0
- package/package.json +8 -5
- package/dist/Math.d.ts +0 -2
- package/dist/Math.js +0 -2
- package/dist/built-in-types/ArrayType.d.ts +0 -5
- package/dist/built-in-types/ArrayType.js +0 -21
- package/dist/built-in-types/EcsType.d.ts +0 -8
- package/dist/built-in-types/EcsType.js +0 -1
- package/dist/built-in-types/MapType.d.ts +0 -18
- package/dist/built-in-types/MapType.js +0 -22
- package/dist/built-in-types/Optional.d.ts +0 -5
- package/dist/built-in-types/Optional.js +0 -22
- package/dist/built-in-types/basic/Boolean.d.ts +0 -5
- package/dist/built-in-types/basic/Boolean.js +0 -11
- package/dist/built-in-types/basic/Enum.d.ts +0 -5
- package/dist/built-in-types/basic/Enum.js +0 -13
- package/dist/built-in-types/basic/Float.d.ts +0 -9
- package/dist/built-in-types/basic/Float.js +0 -22
- package/dist/built-in-types/basic/Integer.d.ts +0 -17
- package/dist/built-in-types/basic/Integer.js +0 -44
- package/dist/built-in-types/basic/String.d.ts +0 -9
- package/dist/built-in-types/basic/String.js +0 -15
- package/dist/built-in-types/index.d.ts +0 -9
- package/dist/built-in-types/index.js +0 -9
- package/dist/built-in-types/typing.d.ts +0 -16
- package/dist/built-in-types/typing.js +0 -1
- package/dist/components/generated/AudioSource.d.ts +0 -1
- package/dist/components/generated/AudioSource.js +0 -19
- package/dist/components/generated/AudioStream.d.ts +0 -1
- package/dist/components/generated/AudioStream.js +0 -19
- package/dist/components/generated/BoxShape.d.ts +0 -1
- package/dist/components/generated/BoxShape.js +0 -19
- package/dist/components/generated/CylinderShape.d.ts +0 -1
- package/dist/components/generated/CylinderShape.js +0 -19
- package/dist/components/generated/PlaneShape.d.ts +0 -1
- package/dist/components/generated/PlaneShape.js +0 -19
- package/dist/components/generated/SphereShape.d.ts +0 -1
- package/dist/components/generated/SphereShape.js +0 -19
- package/dist/components/generated/index.d.ts +0 -9
- package/dist/components/generated/index.js +0 -16
- package/dist/components/generated/pb/AudioSource.d.ts +0 -8
- package/dist/components/generated/pb/AudioSource.js +0 -74
- package/dist/components/generated/pb/AudioStream.d.ts +0 -5
- package/dist/components/generated/pb/AudioStream.js +0 -49
- package/dist/components/generated/pb/BoxShape.d.ts +0 -6
- package/dist/components/generated/pb/BoxShape.js +0 -70
- package/dist/components/generated/pb/CylinderShape.d.ts +0 -7
- package/dist/components/generated/pb/CylinderShape.js +0 -67
- package/dist/components/generated/pb/PlaneShape.d.ts +0 -6
- package/dist/components/generated/pb/PlaneShape.js +0 -70
- package/dist/components/generated/pb/SphereShape.d.ts +0 -5
- package/dist/components/generated/pb/SphereShape.js +0 -49
- package/dist/components/index.d.ts +0 -15
- package/dist/components/index.js +0 -7
- package/dist/components/legacy/Transform.d.ts +0 -14
- package/dist/components/legacy/Transform.js +0 -28
- package/dist/components/legacy/index.d.ts +0 -11
- package/dist/components/legacy/index.js +0 -9
- package/dist/components/legacy/types.d.ts +0 -51
- package/dist/components/legacy/types.js +0 -53
- package/dist/components/types.d.ts +0 -3
- package/dist/components/types.js +0 -4
- package/dist/engine/component.d.ts +0 -34
- package/dist/engine/component.js +0 -98
- package/dist/engine/entity-utils.d.ts +0 -7
- package/dist/engine/entity-utils.js +0 -18
- package/dist/engine/entity.d.ts +0 -20
- package/dist/engine/entity.js +0 -75
- package/dist/engine/index.d.ts +0 -11
- package/dist/engine/index.js +0 -132
- package/dist/engine/types.d.ts +0 -34
- package/dist/engine/types.js +0 -1
- package/dist/engine/utils.d.ts +0 -4
- package/dist/engine/utils.js +0 -11
- package/dist/proto-definitions/AudioSource.proto +0 -16
- package/dist/proto-definitions/AudioStream.proto +0 -13
- package/dist/proto-definitions/BoxShape.proto +0 -14
- package/dist/proto-definitions/CylinderShape.proto +0 -15
- package/dist/proto-definitions/PlaneShape.proto +0 -14
- package/dist/proto-definitions/README.md +0 -0
- package/dist/proto-definitions/SphereShape.proto +0 -13
- package/dist/proto-definitions/Transform.md +0 -23
- package/dist/serialization/ByteBuffer/index.d.ts +0 -130
- package/dist/serialization/ByteBuffer/index.js +0 -281
- package/dist/serialization/crdt/componentOperation.d.ts +0 -25
- package/dist/serialization/crdt/componentOperation.js +0 -39
- package/dist/serialization/wireMessage.d.ts +0 -39
- package/dist/serialization/wireMessage.js +0 -53
- package/dist/systems/crdt/index.d.ts +0 -6
- package/dist/systems/crdt/index.js +0 -147
- package/dist/systems/crdt/transport.d.ts +0 -8
- package/dist/systems/crdt/transport.js +0 -21
- package/dist/systems/crdt/types.d.ts +0 -10
- package/dist/systems/crdt/types.js +0 -1
- package/dist/systems/crdt/utils.d.ts +0 -11
- package/dist/systems/crdt/utils.js +0 -51
|
@@ -1,281 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Take the max between currentSize and intendedSize and then plus 1024. Then,
|
|
3
|
-
* find the next nearer multiple of 1024.
|
|
4
|
-
* @param currentSize - number
|
|
5
|
-
* @param intendedSize - number
|
|
6
|
-
* @returns the calculated number
|
|
7
|
-
*/
|
|
8
|
-
function getNextSize(currentSize, intendedSize) {
|
|
9
|
-
const minNewSize = Math.max(currentSize, intendedSize) + 1024;
|
|
10
|
-
return Math.ceil(minNewSize / 1024) * 1024;
|
|
11
|
-
}
|
|
12
|
-
const defaultInitialCapacity = 10240;
|
|
13
|
-
/**
|
|
14
|
-
* ByteBuffer is a wrapper of DataView which also adds a read and write offset.
|
|
15
|
-
* Also in a write operation it resizes the buffer is being used if it needs.
|
|
16
|
-
*
|
|
17
|
-
* - Use read and write function to generate or consume data.
|
|
18
|
-
* - Use set and get only if you are sure that you're doing.
|
|
19
|
-
*/
|
|
20
|
-
export function createByteBuffer(options = {}) {
|
|
21
|
-
const initialROffset = options.reading?.currentOffset || 0;
|
|
22
|
-
let initialBuffer = null;
|
|
23
|
-
let initialWOffset = 0;
|
|
24
|
-
if (options.writing) {
|
|
25
|
-
initialBuffer = options.writing.buffer;
|
|
26
|
-
if (options.writing.currentOffset) {
|
|
27
|
-
initialWOffset = options.writing.currentOffset;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
else if (options.reading) {
|
|
31
|
-
initialBuffer = options.reading.buffer;
|
|
32
|
-
initialWOffset = options.reading.length || options.reading.buffer.length;
|
|
33
|
-
}
|
|
34
|
-
else {
|
|
35
|
-
initialBuffer = new Uint8Array(options.initialCapacity || defaultInitialCapacity);
|
|
36
|
-
}
|
|
37
|
-
let buffer = initialBuffer;
|
|
38
|
-
let view = new DataView(buffer.buffer, buffer.byteOffset);
|
|
39
|
-
let woffset = initialWOffset;
|
|
40
|
-
let roffset = initialROffset;
|
|
41
|
-
/**
|
|
42
|
-
* Increement the write offset and resize the buffer if it needs.
|
|
43
|
-
*/
|
|
44
|
-
const woAdd = (amount) => {
|
|
45
|
-
if (woffset + amount > buffer.byteLength) {
|
|
46
|
-
const newsize = getNextSize(buffer.byteLength, woffset + amount);
|
|
47
|
-
const newBuffer = new Uint8Array(newsize);
|
|
48
|
-
newBuffer.set(buffer);
|
|
49
|
-
buffer = newBuffer;
|
|
50
|
-
view = new DataView(buffer.buffer);
|
|
51
|
-
}
|
|
52
|
-
woffset += amount;
|
|
53
|
-
return woffset - amount;
|
|
54
|
-
};
|
|
55
|
-
/**
|
|
56
|
-
* Increment the read offset and throw an error if it's trying to read
|
|
57
|
-
* outside the bounds.
|
|
58
|
-
*/
|
|
59
|
-
const roAdd = (amount) => {
|
|
60
|
-
if (roffset + amount > woffset) {
|
|
61
|
-
throw new Error('Outside of the bounds of writen data.');
|
|
62
|
-
}
|
|
63
|
-
roffset += amount;
|
|
64
|
-
return roffset - amount;
|
|
65
|
-
};
|
|
66
|
-
return {
|
|
67
|
-
/**
|
|
68
|
-
* @returns The entire current Uint8Array.
|
|
69
|
-
*
|
|
70
|
-
* WARNING: if the buffer grows, the view had changed itself,
|
|
71
|
-
* and the reference will be a invalid one.
|
|
72
|
-
*/
|
|
73
|
-
buffer() {
|
|
74
|
-
return buffer;
|
|
75
|
-
},
|
|
76
|
-
/**
|
|
77
|
-
* @returns The capacity of the current buffer
|
|
78
|
-
*/
|
|
79
|
-
bufferLength() {
|
|
80
|
-
return buffer.length;
|
|
81
|
-
},
|
|
82
|
-
/**
|
|
83
|
-
* Resets byteBuffer to avoid creating a new one
|
|
84
|
-
*/
|
|
85
|
-
resetBuffer() {
|
|
86
|
-
roffset = 0;
|
|
87
|
-
woffset = 0;
|
|
88
|
-
},
|
|
89
|
-
/**
|
|
90
|
-
* @returns The current read offset
|
|
91
|
-
*/
|
|
92
|
-
currentReadOffset() {
|
|
93
|
-
return roffset;
|
|
94
|
-
},
|
|
95
|
-
/**
|
|
96
|
-
* @returns The current write offset
|
|
97
|
-
*/
|
|
98
|
-
currentWriteOffset() {
|
|
99
|
-
return woffset;
|
|
100
|
-
},
|
|
101
|
-
/**
|
|
102
|
-
* Reading purpose
|
|
103
|
-
* Returns the previuos offsset size before incrementing
|
|
104
|
-
*/
|
|
105
|
-
incrementReadOffset(amount) {
|
|
106
|
-
return roAdd(amount);
|
|
107
|
-
},
|
|
108
|
-
/**
|
|
109
|
-
* @returns How many bytes are available to read.
|
|
110
|
-
*/
|
|
111
|
-
remainingBytes() {
|
|
112
|
-
return woffset - roffset;
|
|
113
|
-
},
|
|
114
|
-
readFloat32() {
|
|
115
|
-
return view.getFloat32(roAdd(4));
|
|
116
|
-
},
|
|
117
|
-
readFloat64() {
|
|
118
|
-
return view.getFloat64(roAdd(8));
|
|
119
|
-
},
|
|
120
|
-
readInt8() {
|
|
121
|
-
return view.getInt8(roAdd(1));
|
|
122
|
-
},
|
|
123
|
-
readInt16() {
|
|
124
|
-
return view.getInt16(roAdd(2));
|
|
125
|
-
},
|
|
126
|
-
readInt32() {
|
|
127
|
-
return view.getInt32(roAdd(4));
|
|
128
|
-
},
|
|
129
|
-
readInt64() {
|
|
130
|
-
return view.getBigInt64(roAdd(8));
|
|
131
|
-
},
|
|
132
|
-
readUint8() {
|
|
133
|
-
return view.getUint8(roAdd(1));
|
|
134
|
-
},
|
|
135
|
-
readUint16() {
|
|
136
|
-
return view.getUint16(roAdd(2));
|
|
137
|
-
},
|
|
138
|
-
readUint32() {
|
|
139
|
-
return view.getUint32(roAdd(4));
|
|
140
|
-
},
|
|
141
|
-
readUint64() {
|
|
142
|
-
return view.getBigUint64(roAdd(8));
|
|
143
|
-
},
|
|
144
|
-
readBuffer() {
|
|
145
|
-
const length = view.getUint32(roAdd(4));
|
|
146
|
-
return buffer.subarray(roAdd(length), roAdd(0));
|
|
147
|
-
},
|
|
148
|
-
/**
|
|
149
|
-
* Writing purpose
|
|
150
|
-
*/
|
|
151
|
-
/**
|
|
152
|
-
* Increment offset
|
|
153
|
-
* @param amount - how many bytes
|
|
154
|
-
* @returns The offset when this reserving starts.
|
|
155
|
-
*/
|
|
156
|
-
incrementWriteOffset(amount) {
|
|
157
|
-
return woAdd(amount);
|
|
158
|
-
},
|
|
159
|
-
/**
|
|
160
|
-
* @returns The total number of bytes writen in the buffer.
|
|
161
|
-
*/
|
|
162
|
-
size() {
|
|
163
|
-
return woffset;
|
|
164
|
-
},
|
|
165
|
-
/**
|
|
166
|
-
* @returns The subarray from 0 to offset.
|
|
167
|
-
*/
|
|
168
|
-
toBinary() {
|
|
169
|
-
return buffer.subarray(0, woffset);
|
|
170
|
-
},
|
|
171
|
-
writeBuffer(value, writeLength = true) {
|
|
172
|
-
if (writeLength) {
|
|
173
|
-
this.writeUint32(value.byteLength);
|
|
174
|
-
}
|
|
175
|
-
// TODO: lean
|
|
176
|
-
const o = woAdd(value.byteLength);
|
|
177
|
-
buffer.set(value, o);
|
|
178
|
-
},
|
|
179
|
-
writeFloat32(value) {
|
|
180
|
-
const o = woAdd(4);
|
|
181
|
-
view.setFloat32(o, value);
|
|
182
|
-
},
|
|
183
|
-
writeFloat64(value) {
|
|
184
|
-
const o = woAdd(8);
|
|
185
|
-
view.setFloat64(o, value);
|
|
186
|
-
},
|
|
187
|
-
writeInt8(value) {
|
|
188
|
-
const o = woAdd(1);
|
|
189
|
-
view.setInt8(o, value);
|
|
190
|
-
},
|
|
191
|
-
writeInt16(value) {
|
|
192
|
-
const o = woAdd(2);
|
|
193
|
-
view.setInt16(o, value);
|
|
194
|
-
},
|
|
195
|
-
writeInt32(value) {
|
|
196
|
-
const o = woAdd(4);
|
|
197
|
-
view.setInt32(o, value);
|
|
198
|
-
},
|
|
199
|
-
writeInt64(value) {
|
|
200
|
-
const o = woAdd(8);
|
|
201
|
-
view.setBigInt64(o, value);
|
|
202
|
-
},
|
|
203
|
-
writeUint8(value) {
|
|
204
|
-
const o = woAdd(1);
|
|
205
|
-
view.setUint8(o, value);
|
|
206
|
-
},
|
|
207
|
-
writeUint16(value) {
|
|
208
|
-
const o = woAdd(2);
|
|
209
|
-
view.setUint16(o, value);
|
|
210
|
-
},
|
|
211
|
-
writeUint32(value) {
|
|
212
|
-
const o = woAdd(4);
|
|
213
|
-
view.setUint32(o, value);
|
|
214
|
-
},
|
|
215
|
-
writeUint64(value) {
|
|
216
|
-
const o = woAdd(8);
|
|
217
|
-
view.setBigUint64(o, value);
|
|
218
|
-
},
|
|
219
|
-
// Dataview Proxy
|
|
220
|
-
getFloat32(offset) {
|
|
221
|
-
return view.getFloat32(offset);
|
|
222
|
-
},
|
|
223
|
-
getFloat64(offset) {
|
|
224
|
-
return view.getFloat64(offset);
|
|
225
|
-
},
|
|
226
|
-
getInt8(offset) {
|
|
227
|
-
return view.getInt8(offset);
|
|
228
|
-
},
|
|
229
|
-
getInt16(offset) {
|
|
230
|
-
return view.getInt16(offset);
|
|
231
|
-
},
|
|
232
|
-
getInt32(offset) {
|
|
233
|
-
return view.getInt32(offset);
|
|
234
|
-
},
|
|
235
|
-
getInt64(offset) {
|
|
236
|
-
return view.getBigInt64(offset);
|
|
237
|
-
},
|
|
238
|
-
getUint8(offset) {
|
|
239
|
-
return view.getUint8(offset);
|
|
240
|
-
},
|
|
241
|
-
getUint16(offset) {
|
|
242
|
-
return view.getUint16(offset);
|
|
243
|
-
},
|
|
244
|
-
getUint32(offset) {
|
|
245
|
-
return view.getUint32(offset);
|
|
246
|
-
},
|
|
247
|
-
getUint64(offset) {
|
|
248
|
-
return view.getBigUint64(offset);
|
|
249
|
-
},
|
|
250
|
-
setFloat32(offset, value) {
|
|
251
|
-
view.setFloat32(offset, value);
|
|
252
|
-
},
|
|
253
|
-
setFloat64(offset, value) {
|
|
254
|
-
view.setFloat64(offset, value);
|
|
255
|
-
},
|
|
256
|
-
setInt8(offset, value) {
|
|
257
|
-
view.setInt8(offset, value);
|
|
258
|
-
},
|
|
259
|
-
setInt16(offset, value) {
|
|
260
|
-
view.setInt16(offset, value);
|
|
261
|
-
},
|
|
262
|
-
setInt32(offset, value) {
|
|
263
|
-
view.setInt32(offset, value);
|
|
264
|
-
},
|
|
265
|
-
setInt64(offset, value) {
|
|
266
|
-
view.setBigInt64(offset, value);
|
|
267
|
-
},
|
|
268
|
-
setUint8(offset, value) {
|
|
269
|
-
view.setUint8(offset, value);
|
|
270
|
-
},
|
|
271
|
-
setUint16(offset, value) {
|
|
272
|
-
view.setUint16(offset, value);
|
|
273
|
-
},
|
|
274
|
-
setUint32(offset, value) {
|
|
275
|
-
view.setUint32(offset, value);
|
|
276
|
-
},
|
|
277
|
-
setUint64(offset, value) {
|
|
278
|
-
view.setBigUint64(offset, value);
|
|
279
|
-
}
|
|
280
|
-
};
|
|
281
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { ComponentDefinition } from '../../engine/component';
|
|
2
|
-
import { Entity } from '../../engine/entity';
|
|
3
|
-
import { ByteBuffer } from '../ByteBuffer';
|
|
4
|
-
import WireMessage from '../wireMessage';
|
|
5
|
-
export declare namespace PutComponentOperation {
|
|
6
|
-
/**
|
|
7
|
-
* @param entity - Uint32 number of the entity
|
|
8
|
-
* @param componentId - Uint32 number of id
|
|
9
|
-
* @param timestamp - Uint64 Lamport timestamp
|
|
10
|
-
* @param data - Uint8[] data of component
|
|
11
|
-
*/
|
|
12
|
-
type Type = {
|
|
13
|
-
entity: Entity;
|
|
14
|
-
componentId: number;
|
|
15
|
-
timestamp: number;
|
|
16
|
-
data: Uint8Array;
|
|
17
|
-
};
|
|
18
|
-
const MESSAGE_HEADER_LENGTH = 20;
|
|
19
|
-
/**
|
|
20
|
-
* Call this function for an optimal writing data passing the ByteBuffer
|
|
21
|
-
* already allocated
|
|
22
|
-
*/
|
|
23
|
-
function write(entity: Entity, timestamp: number, componentDefinition: ComponentDefinition, buf: ByteBuffer): void;
|
|
24
|
-
function read(buf: ByteBuffer): (WireMessage.Header & Type) | null;
|
|
25
|
-
}
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import WireMessage from '../wireMessage';
|
|
2
|
-
export var PutComponentOperation;
|
|
3
|
-
(function (PutComponentOperation) {
|
|
4
|
-
PutComponentOperation.MESSAGE_HEADER_LENGTH = 20;
|
|
5
|
-
/**
|
|
6
|
-
* Call this function for an optimal writing data passing the ByteBuffer
|
|
7
|
-
* already allocated
|
|
8
|
-
*/
|
|
9
|
-
function write(entity, timestamp, componentDefinition, buf) {
|
|
10
|
-
// reserve the beginning
|
|
11
|
-
const startMessageOffset = buf.incrementWriteOffset(WireMessage.HEADER_LENGTH + PutComponentOperation.MESSAGE_HEADER_LENGTH);
|
|
12
|
-
// write body
|
|
13
|
-
componentDefinition.writeToByteBuffer(entity, buf);
|
|
14
|
-
const messageLength = buf.size() - startMessageOffset;
|
|
15
|
-
// Write WireMessage header
|
|
16
|
-
buf.setUint32(startMessageOffset, messageLength);
|
|
17
|
-
buf.setUint32(startMessageOffset + 4, WireMessage.Enum.PUT_COMPONENT);
|
|
18
|
-
// Write ComponentOperation header
|
|
19
|
-
buf.setUint32(startMessageOffset + 8, entity);
|
|
20
|
-
buf.setUint32(startMessageOffset + 12, componentDefinition._id);
|
|
21
|
-
buf.setUint64(startMessageOffset + 16, BigInt(timestamp));
|
|
22
|
-
buf.setUint32(startMessageOffset + 24, messageLength - PutComponentOperation.MESSAGE_HEADER_LENGTH - WireMessage.HEADER_LENGTH);
|
|
23
|
-
}
|
|
24
|
-
PutComponentOperation.write = write;
|
|
25
|
-
function read(buf) {
|
|
26
|
-
const header = WireMessage.readHeader(buf);
|
|
27
|
-
if (!header) {
|
|
28
|
-
return null;
|
|
29
|
-
}
|
|
30
|
-
return {
|
|
31
|
-
...header,
|
|
32
|
-
entity: buf.readUint32(),
|
|
33
|
-
componentId: buf.readInt32(),
|
|
34
|
-
timestamp: Number(buf.readUint64()),
|
|
35
|
-
data: buf.readBuffer()
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
PutComponentOperation.read = read;
|
|
39
|
-
})(PutComponentOperation || (PutComponentOperation = {}));
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The wire message is the top-level message that can be packed
|
|
3
|
-
* inside it can contain a data with another structure or protocol
|
|
4
|
-
*
|
|
5
|
-
* Each wire message has three primitive property that it'll never change
|
|
6
|
-
* ---> length uint32 (message size up to 4,294,967,295)
|
|
7
|
-
* ---> version uint32 (for now just a number which is zero)
|
|
8
|
-
* ---> message type uint32
|
|
9
|
-
* The length indicates how many bytes are above self, the version in
|
|
10
|
-
* combination with message type defines the set of handlers that will be
|
|
11
|
-
* available to process the message
|
|
12
|
-
*
|
|
13
|
-
*/
|
|
14
|
-
import { ByteBuffer } from './ByteBuffer';
|
|
15
|
-
declare type Uint32 = number;
|
|
16
|
-
export declare namespace WireMessage {
|
|
17
|
-
enum Enum {
|
|
18
|
-
RESERVED = 0,
|
|
19
|
-
PUT_COMPONENT = 1,
|
|
20
|
-
DELETE_COMPONENT = 2,
|
|
21
|
-
MAX_MESSAGE_TYPE = 3
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* @param length - Uint32 the length of all message (including the header)
|
|
25
|
-
* @param type - define the function which handles the data
|
|
26
|
-
*/
|
|
27
|
-
type Header = {
|
|
28
|
-
length: Uint32;
|
|
29
|
-
type: Uint32;
|
|
30
|
-
};
|
|
31
|
-
const HEADER_LENGTH = 8;
|
|
32
|
-
/**
|
|
33
|
-
* Validate if the message incoming is completed
|
|
34
|
-
* @param buf
|
|
35
|
-
*/
|
|
36
|
-
function validate(buf: ByteBuffer): boolean;
|
|
37
|
-
function readHeader(buf: ByteBuffer): Header | null;
|
|
38
|
-
}
|
|
39
|
-
export default WireMessage;
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The wire message is the top-level message that can be packed
|
|
3
|
-
* inside it can contain a data with another structure or protocol
|
|
4
|
-
*
|
|
5
|
-
* Each wire message has three primitive property that it'll never change
|
|
6
|
-
* ---> length uint32 (message size up to 4,294,967,295)
|
|
7
|
-
* ---> version uint32 (for now just a number which is zero)
|
|
8
|
-
* ---> message type uint32
|
|
9
|
-
* The length indicates how many bytes are above self, the version in
|
|
10
|
-
* combination with message type defines the set of handlers that will be
|
|
11
|
-
* available to process the message
|
|
12
|
-
*
|
|
13
|
-
*/
|
|
14
|
-
export var WireMessage;
|
|
15
|
-
(function (WireMessage) {
|
|
16
|
-
let Enum;
|
|
17
|
-
(function (Enum) {
|
|
18
|
-
Enum[Enum["RESERVED"] = 0] = "RESERVED";
|
|
19
|
-
// Component Operation
|
|
20
|
-
Enum[Enum["PUT_COMPONENT"] = 1] = "PUT_COMPONENT";
|
|
21
|
-
Enum[Enum["DELETE_COMPONENT"] = 2] = "DELETE_COMPONENT";
|
|
22
|
-
// TODO: ?
|
|
23
|
-
Enum[Enum["MAX_MESSAGE_TYPE"] = 3] = "MAX_MESSAGE_TYPE";
|
|
24
|
-
})(Enum = WireMessage.Enum || (WireMessage.Enum = {}));
|
|
25
|
-
WireMessage.HEADER_LENGTH = 8;
|
|
26
|
-
/**
|
|
27
|
-
* Validate if the message incoming is completed
|
|
28
|
-
* @param buf
|
|
29
|
-
*/
|
|
30
|
-
function validate(buf) {
|
|
31
|
-
const rem = buf.remainingBytes();
|
|
32
|
-
if (rem < WireMessage.HEADER_LENGTH) {
|
|
33
|
-
return false;
|
|
34
|
-
}
|
|
35
|
-
const messageLength = buf.getUint32(buf.currentReadOffset());
|
|
36
|
-
if (rem < messageLength) {
|
|
37
|
-
return false;
|
|
38
|
-
}
|
|
39
|
-
return true;
|
|
40
|
-
}
|
|
41
|
-
WireMessage.validate = validate;
|
|
42
|
-
function readHeader(buf) {
|
|
43
|
-
if (!validate(buf)) {
|
|
44
|
-
return null;
|
|
45
|
-
}
|
|
46
|
-
return {
|
|
47
|
-
length: buf.readUint32(),
|
|
48
|
-
type: buf.readUint32()
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
WireMessage.readHeader = readHeader;
|
|
52
|
-
})(WireMessage || (WireMessage = {}));
|
|
53
|
-
export default WireMessage;
|
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
import { crdtProtocol } from '@dcl/crdt';
|
|
2
|
-
import EntityUtils from '../../engine/entity-utils';
|
|
3
|
-
import { createByteBuffer } from '../../serialization/ByteBuffer';
|
|
4
|
-
import { PutComponentOperation as Message } from '../../serialization/crdt/componentOperation';
|
|
5
|
-
import WireMessage from '../../serialization/wireMessage';
|
|
6
|
-
import { getTransports } from './transport';
|
|
7
|
-
import CrdtUtils from './utils';
|
|
8
|
-
export function crdtSceneSystem(engine) {
|
|
9
|
-
// CRDT Client
|
|
10
|
-
const crdtClient = crdtProtocol();
|
|
11
|
-
// Messages that we received at transport.onMessage waiting to be processed
|
|
12
|
-
const receivedMessages = [];
|
|
13
|
-
// Messages already processed by the engine but that we need to broadcast to other transports.
|
|
14
|
-
const transportMessages = [];
|
|
15
|
-
// Map of entities already processed at least once
|
|
16
|
-
const crdtEntities = new Map();
|
|
17
|
-
const transports = getTransports();
|
|
18
|
-
transports.forEach((transport) => (transport.onmessage = parseChunkMessage(transport.type)));
|
|
19
|
-
/**
|
|
20
|
-
*
|
|
21
|
-
* @param transportType tranport id to identiy messages
|
|
22
|
-
* @returns a function to process received messages
|
|
23
|
-
*/
|
|
24
|
-
function parseChunkMessage(transportType) {
|
|
25
|
-
/**
|
|
26
|
-
* Receives a chunk of binary messages and stores all the valid
|
|
27
|
-
* Component Operation Messages at messages queue
|
|
28
|
-
* @param chunkMessage A chunk of binary messages
|
|
29
|
-
*/
|
|
30
|
-
return function parseChunkMessage(chunkMessage) {
|
|
31
|
-
if (!chunkMessage.data?.length)
|
|
32
|
-
return;
|
|
33
|
-
const buffer = createByteBuffer({
|
|
34
|
-
reading: { buffer: chunkMessage.data, currentOffset: 0 }
|
|
35
|
-
});
|
|
36
|
-
while (WireMessage.validate(buffer)) {
|
|
37
|
-
const offset = buffer.currentReadOffset();
|
|
38
|
-
const message = Message.read(buffer);
|
|
39
|
-
const { entity, componentId, data, timestamp } = message;
|
|
40
|
-
receivedMessages.push({
|
|
41
|
-
entity,
|
|
42
|
-
componentId,
|
|
43
|
-
data,
|
|
44
|
-
timestamp,
|
|
45
|
-
transportType,
|
|
46
|
-
messageBuffer: buffer
|
|
47
|
-
.buffer()
|
|
48
|
-
.subarray(offset, buffer.currentReadOffset())
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* Return and clear the messaes queue
|
|
55
|
-
* @returns messages recieved by the transport to process on the next tick
|
|
56
|
-
*/
|
|
57
|
-
function getMessages(value) {
|
|
58
|
-
const messagesToProcess = Array.from(value);
|
|
59
|
-
value.length = 0;
|
|
60
|
-
return messagesToProcess;
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* This fn will be called on every tick.
|
|
64
|
-
* Process all the messages queue received by the transport
|
|
65
|
-
*/
|
|
66
|
-
function receiveMessages() {
|
|
67
|
-
const messagesToProcess = getMessages(receivedMessages);
|
|
68
|
-
for (const transport of transports) {
|
|
69
|
-
const buffer = createByteBuffer();
|
|
70
|
-
for (const message of messagesToProcess) {
|
|
71
|
-
const { data, timestamp, componentId, entity } = message;
|
|
72
|
-
const crdtMessage = {
|
|
73
|
-
key: CrdtUtils.getKey(entity, componentId),
|
|
74
|
-
data: data,
|
|
75
|
-
timestamp: timestamp
|
|
76
|
-
};
|
|
77
|
-
const component = engine.getComponent(componentId);
|
|
78
|
-
const currentMessage = crdtClient.processMessage(crdtMessage);
|
|
79
|
-
// CRDT outdated message. Resend this message through the wire
|
|
80
|
-
// TODO: perf transactor
|
|
81
|
-
if (crdtMessage !== currentMessage) {
|
|
82
|
-
// CRDT outdated message. Resend this message through the wire
|
|
83
|
-
Message.write(entity, currentMessage.timestamp, component, buffer);
|
|
84
|
-
}
|
|
85
|
-
else {
|
|
86
|
-
const opts = { reading: { buffer: message.data, currentOffset: 0 } };
|
|
87
|
-
const bb = createByteBuffer(opts);
|
|
88
|
-
// Update engine component
|
|
89
|
-
component.upsertFromBinary(message.entity, bb);
|
|
90
|
-
component.clearDirty();
|
|
91
|
-
// Add message to transport queue to be processed by others transports
|
|
92
|
-
transportMessages.push(message);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
if (buffer.size()) {
|
|
96
|
-
transport.send(buffer.toBinary());
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* Iterates the dirty map and generates crdt messages to be send
|
|
102
|
-
* @param dirtyMap a map of { entities: [componentId] }
|
|
103
|
-
*/
|
|
104
|
-
function createMessages(dirtyMap) {
|
|
105
|
-
// CRDT Messages will be the merge between the recieved transport messages and the new crdt messages
|
|
106
|
-
const crdtMessages = getMessages(transportMessages);
|
|
107
|
-
const buffer = createByteBuffer();
|
|
108
|
-
for (const [entity, componentsId] of dirtyMap) {
|
|
109
|
-
for (const componentId of componentsId) {
|
|
110
|
-
const component = engine.getComponent(componentId);
|
|
111
|
-
const event = crdtClient.createEvent(CrdtUtils.getKey(entity, componentId), component.toBinary(entity).toBinary());
|
|
112
|
-
const offset = buffer.currentWriteOffset();
|
|
113
|
-
// There is no need to create messages for the static entities the first time they are created
|
|
114
|
-
// They are part of the scene loading. Send only updates.
|
|
115
|
-
if (!EntityUtils.isStaticEntity(entity) || crdtEntities.has(entity)) {
|
|
116
|
-
Message.write(entity, event.timestamp, component, buffer);
|
|
117
|
-
crdtMessages.push({
|
|
118
|
-
componentId,
|
|
119
|
-
entity,
|
|
120
|
-
timestamp: event.timestamp,
|
|
121
|
-
messageBuffer: buffer
|
|
122
|
-
.buffer()
|
|
123
|
-
.subarray(offset, buffer.currentWriteOffset())
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
crdtEntities.set(entity, true);
|
|
128
|
-
}
|
|
129
|
-
// Send messages to transports
|
|
130
|
-
const transportBuffer = createByteBuffer();
|
|
131
|
-
for (const transport of transports) {
|
|
132
|
-
transportBuffer.resetBuffer();
|
|
133
|
-
for (const message of crdtMessages) {
|
|
134
|
-
if (transport.filter(message)) {
|
|
135
|
-
transportBuffer.writeBuffer(message.messageBuffer, false);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
if (transportBuffer.size()) {
|
|
139
|
-
transport.send(transportBuffer.toBinary());
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
return {
|
|
144
|
-
createMessages,
|
|
145
|
-
receiveMessages
|
|
146
|
-
};
|
|
147
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { TransportMessage } from './types';
|
|
2
|
-
export declare type Transport = {
|
|
3
|
-
type: string;
|
|
4
|
-
send(message: Uint8Array): void;
|
|
5
|
-
onmessage?(message: MessageEvent<Uint8Array>): void;
|
|
6
|
-
filter(message: TransportMessage): boolean;
|
|
7
|
-
};
|
|
8
|
-
export declare function getTransports(): Transport[];
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
function networkTransport() {
|
|
2
|
-
// const rpc = new RpcTransport()
|
|
3
|
-
const rpc = {
|
|
4
|
-
send: () => { }
|
|
5
|
-
};
|
|
6
|
-
const type = 'network-transport';
|
|
7
|
-
return {
|
|
8
|
-
...rpc,
|
|
9
|
-
type,
|
|
10
|
-
filter(message) {
|
|
11
|
-
// Echo message, ignore them
|
|
12
|
-
if (message.transportType === type) {
|
|
13
|
-
return false;
|
|
14
|
-
}
|
|
15
|
-
return !!message; // validComponents.includes(componentId)
|
|
16
|
-
}
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
export function getTransports() {
|
|
20
|
-
return [networkTransport()];
|
|
21
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { Entity } from '../../engine/entity';
|
|
2
|
-
export declare type ReceiveMessage = {
|
|
3
|
-
entity: Entity;
|
|
4
|
-
componentId: number;
|
|
5
|
-
timestamp: number;
|
|
6
|
-
transportType?: string;
|
|
7
|
-
data: Uint8Array;
|
|
8
|
-
messageBuffer: Uint8Array;
|
|
9
|
-
};
|
|
10
|
-
export declare type TransportMessage = Omit<ReceiveMessage, 'data'>;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { ComponentDefinition } from '../../engine/component';
|
|
2
|
-
import { Entity } from '../../engine/entity';
|
|
3
|
-
export declare namespace CrdtUtils {
|
|
4
|
-
type ComponentID = ComponentDefinition['_id'];
|
|
5
|
-
function getKey(entity: Entity, componentId: ComponentID): string;
|
|
6
|
-
enum SynchronizedEntityType {
|
|
7
|
-
NETWORKED = 0,
|
|
8
|
-
RENDERER = 1
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
export default CrdtUtils;
|