Skip to content

Commit 0281f5e

Browse files
fix: Adding INetworkSerializeByMemcpy where recommended for network serialization [MTT-5912] (#822)
* adding INetworkSerializeByMemcpy where recommended * changelog cleanup & addition * comments inside lobbyplayerstate, index added, changelog revision
1 parent 90c517f commit 0281f5e

File tree

5 files changed

+13
-18
lines changed

5 files changed

+13
-18
lines changed

Assets/Scripts/Gameplay/Action/ActionID.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,10 @@ namespace Unity.BossRoom.Gameplay.Actions
77
/// This struct is used by Action system (and GameDataSource) to refer to a specific action in runtime.
88
/// It wraps a simple integer.
99
/// </summary>
10-
public struct ActionID : INetworkSerializable, IEquatable<ActionID>
10+
public struct ActionID : INetworkSerializeByMemcpy, IEquatable<ActionID>
1111
{
1212
public int ID;
1313

14-
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
15-
{
16-
serializer.SerializeValue(ref ID);
17-
}
18-
1914
public bool Equals(ActionID other)
2015
{
2116
return ID == other.ID;

Assets/Scripts/Gameplay/GameState/NetworkCharSelection.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ public enum SeatState : byte
2020
/// <summary>
2121
/// Describes one of the players in the lobby, and their current character-select status.
2222
/// </summary>
23+
/// <remarks>
24+
/// Putting FixedString inside an INetworkSerializeByMemcpy struct is not recommended because it will lose the
25+
/// bandwidth optimization provided by INetworkSerializable -- an empty FixedString128Bytes serialized normally
26+
/// or through INetworkSerializable will use 4 bytes of bandwidth, but inside an INetworkSerializeByMemcpy, that
27+
/// same empty value would consume 132 bytes of bandwidth.
28+
/// </remarks>
2329
public struct LobbyPlayerState : INetworkSerializable, IEquatable<LobbyPlayerState>
2430
{
2531
public ulong ClientId;

Assets/Scripts/Infrastructure/NetworkGuid.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,10 @@
33

44
namespace Unity.BossRoom.Infrastructure
55
{
6-
public class NetworkGuid : INetworkSerializable
6+
public struct NetworkGuid : INetworkSerializeByMemcpy
77
{
88
public ulong FirstHalf;
99
public ulong SecondHalf;
10-
11-
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
12-
{
13-
serializer.SerializeValue(ref FirstHalf);
14-
serializer.SerializeValue(ref SecondHalf);
15-
}
1610
}
1711

1812
public static class NetworkGuidExtensions

CHANGELOG.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ Additional documentation and release notes are available at [Multiplayer Documen
1010

1111
### Changed
1212
* Replaced our polling for lobby updates with a subscription to the new Websocket based LobbyEvents (#805). This saves up a significant amount of bandwidth usage to and from the service, since updates are infrequent in this game. Now clients and hosts only use up bandwidth on the Lobby service when it is needed. With polling, we used to send a GET request per client once every 2s. The responses were between ~550 bytes and 900 bytes, so if we suppose an average of 725 bytes and 100 000 concurrent users (CCU), this amounted to around 725B * 30 calls per minute * 100 000 CCU = 2.175 GB per minute. Scaling this to a month would get us 93.96 TB per month. In our case, since the only changes to the lobbies happen when a user connects or disconnects, most of that data was not necessary and can be saved to reduce bandwidth usage. Since the cost of using the Lobby service depends on bandwidth usage, this would also save money on an actual game.
13-
*
13+
* Simplified reconnection flow by offloading responsibility to ConnectionMethod (#804). Now the ClientReconnectingState uses the ConnectionMethod it is configured with to handle setting up reconnection (i.e. reconnecting to the Lobby before trying to reconnect to the Relay server if it is using Relay and Lobby). It can now also fail early and stop retrying if the lobby doesn't exist anymore.
14+
1415
### Cleanup
1516
* Clarified a TODO comment inside ClientCharacter, detailing how anticipation should only be executed on owning client players (#786)
16-
* Removed now unnecessary cached NetworkBehaviour status on some components, since they now do not allocate memory (#799)
17-
18-
### Changed
19-
* Simplified reconnection flow by offloading responsibility to ConnectionMethod (#804). Now the ClientReconnectingState uses the ConnectionMethod it is configured with to handle setting up reconnection (i.e. reconnecting to the Lobby before trying to reconnect to the Relay server if it is using Relay and Lobby). It can now also fail early and stop retrying if the lobby doesn't exist anymore.
17+
* Removed now unnecessary cached NetworkBehaviour status on some components, since they now do not allocate memory (#799)
18+
* Certain structs converted to implement interface INetworkSerializeByMemcpy instead of INetworkSerializable (#822) INetworkSerializeByMemcpy optimizes for performance at the cost of bandwidth usage and flexibility, however it will only work with structs containing value types. For more details see the official [doc](https://docs-multiplayer.unity3d.com/netcode/current/advanced-topics/serialization/inetworkserializebymemcpy/index.html).
2019

2120
### Fixed
2221
* EnemyPortals' VFX get disabled and re-enabled once the breakable crystals are broken (#784)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ Running the game over internet currently requires setting up a relay.
169169
* Client driven movements - Boss Room is server driven with anticipation animation. See [Client Driven bitesize](https://github.com/Unity-Technologies/com.unity.multiplayer.samples.bitesize/tree/main/Basic/ClientDriven) for client driven gameplay
170170
* Player spawn - SpawnPlayer() in [Assets/Scripts/Gameplay/GameState/ServerBossRoomState.cs](Assets/Scripts/Gameplay/GameState/ServerBossRoomState.cs)
171171
* Player camera setup (with cinemachine) - OnNetworkSpawn() in [Assets/Scripts/Gameplay/GameplayObjects/Character/ClientCharacter.cs](Assets/Scripts/Gameplay/GameplayObjects/Character/ClientCharacter.cs)
172+
* INetworkSerializable (bandwidth optimization) vs INetworkSerializeByMemcpy (performance optimization) usage. See LobbyPlayerState vs ActionID structs [Assets/Scripts/Gameplay/GameState/NetworkCharSelection.cs](Assets/Scripts/Gameplay/GameState/NetworkCharSelection.cs) vs [Assets/Scripts/Gameplay/Action/ActionID.cs](Assets/Scripts/Gameplay/Action/ActionID.cs)
172173

173174
### Game Flow
174175
* Application Controller - [Assets/Scripts/ApplicationLifecycle/ApplicationController.cs ](Assets/Scripts/ApplicationLifecycle/ApplicationController.cs)

0 commit comments

Comments
 (0)