| | 1 | | namespace DCL.CRDT |
| | 2 | | { |
| | 3 | | public enum CrdtMessageType |
| | 4 | | { |
| | 5 | | NONE = 0, |
| | 6 | | PUT_COMPONENT = 1, |
| | 7 | | DELETE_COMPONENT = 2, |
| | 8 | | DELETE_ENTITY = 3, |
| | 9 | | APPEND_COMPONENT = 4 |
| | 10 | | } |
| | 11 | |
|
| | 12 | | public static class CrdtConstants |
| | 13 | | { |
| | 14 | | public const int MESSAGE_HEADER_LENGTH = 8; |
| | 15 | |
|
| | 16 | | public const int CRDT_PUT_COMPONENT_HEADER_LENGTH = 16; |
| | 17 | | public const int CRDT_DELETE_COMPONENT_HEADER_LENGTH = 12; |
| | 18 | | public const int CRDT_DELETE_ENTITY_HEADER_LENGTH = 4; |
| | 19 | | public const int CRDT_APPEND_COMPONENT_HEADER_LENGTH = 16; |
| | 20 | |
|
| | 21 | | public const int CRDT_PUT_COMPONENT_BASE_LENGTH = MESSAGE_HEADER_LENGTH + CRDT_PUT_COMPONENT_HEADER_LENGTH; |
| | 22 | | public const int CRDT_DELETE_COMPONENT_BASE_LENGTH = MESSAGE_HEADER_LENGTH + CRDT_DELETE_COMPONENT_HEADER_LENGTH |
| | 23 | | public const int CRDT_DELETE_ENTITY_BASE_LENGTH = MESSAGE_HEADER_LENGTH + CRDT_DELETE_ENTITY_HEADER_LENGTH; |
| | 24 | | public const int CRDT_APPEND_COMPONENT_BASE_LENGTH = MESSAGE_HEADER_LENGTH + CRDT_APPEND_COMPONENT_HEADER_LENGTH |
| | 25 | | } |
| | 26 | |
|
| | 27 | | public readonly struct CrdtMessage |
| | 28 | | { |
| | 29 | | public readonly CrdtMessageType Type; |
| | 30 | |
|
| | 31 | | // the entityId is stored as 64bit integer, but in the protocol is serialized as 32bit (ADR-117) |
| | 32 | | public readonly long EntityId; |
| | 33 | | public readonly int ComponentId; |
| | 34 | | public readonly int Timestamp; |
| | 35 | | public readonly object Data; |
| | 36 | |
|
| | 37 | | public CrdtMessage(CrdtMessageType type, long entityId, int componentId, int timestamp, object data) |
| | 38 | | { |
| 152 | 39 | | EntityId = entityId; |
| 152 | 40 | | ComponentId = componentId; |
| 152 | 41 | | Timestamp = timestamp; |
| 152 | 42 | | Data = data; |
| 152 | 43 | | Type = type; |
| 152 | 44 | | } |
| | 45 | |
|
| | 46 | | public static int GetMessageDataLength(CrdtMessage message) |
| | 47 | | { |
| 32 | 48 | | if (message.Type == CrdtMessageType.PUT_COMPONENT) { return CrdtConstants.CRDT_PUT_COMPONENT_BASE_LENGTH + ( |
| | 49 | |
|
| 4 | 50 | | if (message.Type == CrdtMessageType.DELETE_COMPONENT) { return CrdtConstants.CRDT_DELETE_COMPONENT_BASE_LENG |
| | 51 | |
|
| 0 | 52 | | if (message.Type == CrdtMessageType.DELETE_ENTITY) { return CrdtConstants.CRDT_DELETE_ENTITY_BASE_LENGTH; } |
| | 53 | |
|
| 0 | 54 | | if (message.Type == CrdtMessageType.APPEND_COMPONENT) { return CrdtConstants.CRDT_APPEND_COMPONENT_BASE_LENG |
| | 55 | |
|
| 0 | 56 | | return 0; |
| | 57 | | } |
| | 58 | | } |
| | 59 | | } |