-
Notifications
You must be signed in to change notification settings - Fork 461
feat: message versioning [MTT-3048] #2290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
717c086
f74863a
7f2ec21
b3eab39
75c1323
9895bd5
04da42a
a94ced8
86be430
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,12 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using Unity.Collections; | ||
|
|
||
| namespace Unity.Netcode | ||
| { | ||
| internal struct ConnectionApprovedMessage : INetworkMessage | ||
| { | ||
| public int Version => 0; | ||
|
|
||
| public ulong OwnerClientId; | ||
| public int NetworkTick; | ||
|
|
||
|
|
@@ -13,12 +15,24 @@ internal struct ConnectionApprovedMessage : INetworkMessage | |
|
|
||
| private FastBufferReader m_ReceivedSceneObjectData; | ||
|
|
||
| public void Serialize(FastBufferWriter writer) | ||
| public NativeArray<MessageVersionData> MessageVersions; | ||
|
|
||
| public void Serialize(FastBufferWriter writer, int targetVersion) | ||
| { | ||
| if (!writer.TryBeginWrite(sizeof(ulong) + sizeof(int) + sizeof(int))) | ||
| // ============================================================ | ||
|
NoelStephensUnity marked this conversation as resolved.
|
||
| // BEGIN FORBIDDEN SEGMENT | ||
| // DO NOT CHANGE THIS HEADER. Everything added to this message | ||
| // must go AFTER the message version header. | ||
| // ============================================================ | ||
| BytePacker.WriteValueBitPacked(writer, MessageVersions.Length); | ||
| foreach (var messageVersion in MessageVersions) | ||
|
Comment on lines
+27
to
+28
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is it necessary to include all versions in the message header rather than just the highest version?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Every message is versioned separately. We need to know what each message's version is... if message A is version 1 but message B is version 2, we don't want to tell the remote end that message A and B are both version 2, because then it'll serialize message A in a way we can't read. |
||
| { | ||
| throw new OverflowException($"Not enough space in the write buffer to serialize {nameof(ConnectionApprovedMessage)}"); | ||
| messageVersion.Serialize(writer); | ||
| } | ||
| // ============================================================ | ||
| // END FORBIDDEN SEGMENT | ||
| // ============================================================ | ||
|
|
||
| BytePacker.WriteValueBitPacked(writer, OwnerClientId); | ||
| BytePacker.WriteValueBitPacked(writer, NetworkTick); | ||
|
|
||
|
|
@@ -51,14 +65,42 @@ public void Serialize(FastBufferWriter writer) | |
| } | ||
| } | ||
|
|
||
| public bool Deserialize(FastBufferReader reader, ref NetworkContext context) | ||
| public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int receivedMessageVersion) | ||
| { | ||
| var networkManager = (NetworkManager)context.SystemOwner; | ||
| if (!networkManager.IsClient) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| // ============================================================ | ||
| // BEGIN FORBIDDEN SEGMENT | ||
| // DO NOT CHANGE THIS HEADER. Everything added to this message | ||
| // must go AFTER the message version header. | ||
| // ============================================================ | ||
| ByteUnpacker.ReadValueBitPacked(reader, out int length); | ||
| var messageHashesInOrder = new NativeArray<uint>(length, Allocator.Temp); | ||
| for (var i = 0; i < length; ++i) | ||
| { | ||
| var messageVersion = new MessageVersionData(); | ||
| messageVersion.Deserialize(reader); | ||
| networkManager.MessagingSystem.SetVersion(context.SenderId, messageVersion.Hash, messageVersion.Version); | ||
| messageHashesInOrder[i] = messageVersion.Hash; | ||
|
|
||
| // Update the received version since this message will always be passed version 0, due to the map not | ||
| // being initialized until just now. | ||
| var messageType = networkManager.MessagingSystem.GetMessageForHash(messageVersion.Hash); | ||
| if (messageType == typeof(ConnectionApprovedMessage)) | ||
| { | ||
| receivedMessageVersion = messageVersion.Version; | ||
| } | ||
| } | ||
| networkManager.MessagingSystem.SetServerMessageOrder(messageHashesInOrder); | ||
| messageHashesInOrder.Dispose(); | ||
| // ============================================================ | ||
| // END FORBIDDEN SEGMENT | ||
| // ============================================================ | ||
|
|
||
| ByteUnpacker.ReadValueBitPacked(reader, out OwnerClientId); | ||
| ByteUnpacker.ReadValueBitPacked(reader, out NetworkTick); | ||
| m_ReceivedSceneObjectData = reader; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.