| | 1 | | using System.Collections.Generic; |
| | 2 | | using DCL.CRDT; |
| | 3 | |
|
| | 4 | | namespace KernelCommunication |
| | 5 | | { |
| | 6 | | public static class KernelBinaryMessageSerializer |
| | 7 | | { |
| | 8 | | public static void Serialize(BinaryWriter binaryWriter, CRDTMessage message) |
| | 9 | | { |
| | 10 | | //sizeof(messageHeader) + sizeof(componentHeader) + dataLength |
| 9 | 11 | | int dataLength = (message.data as byte[])?.Length ?? 0; |
| 9 | 12 | | int messageLength = BinaryMessageConstants.CRDT_MESSAGE_BASE_HEADER_LENGTH + dataLength; |
| 9 | 13 | | int type = GetCRDTMessageType(message); |
| | 14 | |
|
| 9 | 15 | | binaryWriter.WriteInt32(messageLength); |
| 9 | 16 | | binaryWriter.WriteInt32(type); |
| | 17 | |
|
| 9 | 18 | | CRDTSerializer.Serialize(binaryWriter, message); |
| 9 | 19 | | } |
| | 20 | |
|
| | 21 | | public static void Serialize(BinaryWriter binaryWriter, CRDTProtocol crdt) |
| | 22 | | { |
| 2 | 23 | | IReadOnlyList<CRDTMessage> state = crdt.GetState(); |
| 8 | 24 | | for (int i = 0; i < state.Count; i++) |
| | 25 | | { |
| 2 | 26 | | Serialize(binaryWriter, state[i]); |
| | 27 | | } |
| 2 | 28 | | } |
| | 29 | |
|
| | 30 | | private static int GetCRDTMessageType(CRDTMessage message) |
| | 31 | | { |
| 0 | 32 | | if (message.data == null) |
| 0 | 33 | | return (int)CrdtMessageType.DELETE_COMPONENT; |
| 0 | 34 | | return (int)CrdtMessageType.PUT_COMPONENT; |
| | 35 | | } |
| | 36 | | } |
| | 37 | | } |