From ee2d0373eb5d795a9f844da270101edbe6c918f8 Mon Sep 17 00:00:00 2001 From: vis2k Date: Thu, 19 Jul 2018 21:03:00 +0200 Subject: [PATCH] UNetwork.MsgType converted to Enum. Easier + faster string conversion and more flexible code. --- .../Editor/NetworkManagerPreview.cs | 4 +- .../Runtime/ClientScene.cs | 54 +++---- .../Runtime/LocalClient.cs | 4 +- .../Runtime/NetworkAnimator.cs | 14 +- .../Runtime/NetworkBehaviour.cs | 8 +- .../Runtime/NetworkClient.cs | 12 +- .../Runtime/NetworkConnection.cs | 12 +- .../Runtime/NetworkIdentity.cs | 16 +- .../Runtime/NetworkManager.cs | 26 +-- .../Runtime/NetworkServer.cs | 56 +++---- .../Runtime/NetworkTranformChild.cs | 6 +- .../Runtime/NetworkTransform.cs | 6 +- .../Runtime/SyncList.cs | 4 +- .../Runtime/UNetwork.cs | 151 +++++------------- 14 files changed, 155 insertions(+), 218 deletions(-) diff --git a/Unity-Technologies-networking/Editor/NetworkManagerPreview.cs b/Unity-Technologies-networking/Editor/NetworkManagerPreview.cs index 56f46f028..9bb825df9 100644 --- a/Unity-Technologies-networking/Editor/NetworkManagerPreview.cs +++ b/Unity-Technologies-networking/Editor/NetworkManagerPreview.cs @@ -74,7 +74,7 @@ int ShowServerMessageHandlers(Rect r, int posY) foreach (var handler in NetworkServer.handlers) { - GUI.Label(new Rect(r.xMin + k_Padding * 4, posY, 400, k_RowHeight), MsgType.MsgTypeToString(handler.Key)); + GUI.Label(new Rect(r.xMin + k_Padding * 4, posY, 400, k_RowHeight), ((MsgType)handler.Key).ToString()); GUI.Label(new Rect(r.xMin + k_Padding * 4 + k_ColumnWidth, posY, 400, k_RowHeight), FormatHandler(handler)); posY += k_RowHeight; } @@ -95,7 +95,7 @@ int ShowClientMessageHandlers(Rect r, int posY) foreach (var handler in client.handlers) { - GUI.Label(new Rect(r.xMin + k_Padding * 4, posY, 400, k_RowHeight), MsgType.MsgTypeToString(handler.Key)); + GUI.Label(new Rect(r.xMin + k_Padding * 4, posY, 400, k_RowHeight), ((MsgType)handler.Key).ToString()); GUI.Label(new Rect(r.xMin + k_Padding * 4 + k_ColumnWidth, posY, 400, k_RowHeight), FormatHandler(handler)); posY += k_RowHeight; } diff --git a/Unity-Technologies-networking/Runtime/ClientScene.cs b/Unity-Technologies-networking/Runtime/ClientScene.cs index 5e7f24fe3..aa26b89bc 100644 --- a/Unity-Technologies-networking/Runtime/ClientScene.cs +++ b/Unity-Technologies-networking/Runtime/ClientScene.cs @@ -147,7 +147,7 @@ public static bool AddPlayer(NetworkConnection readyConn, short playerController extraMessage.Serialize(writer); msg.msgData = writer.ToArray(); } - s_ReadyConnection.Send(MsgType.AddPlayer, msg); + s_ReadyConnection.Send((short)MsgType.AddPlayer, msg); return true; } @@ -160,7 +160,7 @@ public static bool RemovePlayer(short playerControllerId) { var msg = new RemovePlayerMessage(); msg.playerControllerId = playerControllerId; - s_ReadyConnection.Send(MsgType.RemovePlayer, msg); + s_ReadyConnection.Send((short)MsgType.RemovePlayer, msg); s_ReadyConnection.RemovePlayerController(playerControllerId); s_LocalPlayers[playerControllerId] = new PlayerController(); @@ -185,7 +185,7 @@ public static bool Ready(NetworkConnection conn) if (conn != null) { var msg = new ReadyMessage(); - conn.Send(MsgType.Ready, msg); + conn.Send((short)MsgType.Ready, msg); s_IsReady = true; s_ReadyConnection = conn; s_ReadyConnection.isReady = true; @@ -246,31 +246,31 @@ static internal void RegisterSystemHandlers(NetworkClient client, bool localClie { if (localClient) { - client.RegisterHandler(MsgType.ObjectDestroy, OnLocalClientObjectDestroy); - client.RegisterHandler(MsgType.ObjectHide, OnLocalClientObjectHide); - client.RegisterHandler(MsgType.ObjectSpawn, OnLocalClientObjectSpawn); - client.RegisterHandler(MsgType.ObjectSpawnScene, OnLocalClientObjectSpawnScene); - client.RegisterHandler(MsgType.LocalClientAuthority, OnClientAuthority); + client.RegisterHandler((short)MsgType.ObjectDestroy, OnLocalClientObjectDestroy); + client.RegisterHandler((short)MsgType.ObjectHide, OnLocalClientObjectHide); + client.RegisterHandler((short)MsgType.ObjectSpawn, OnLocalClientObjectSpawn); + client.RegisterHandler((short)MsgType.ObjectSpawnScene, OnLocalClientObjectSpawnScene); + client.RegisterHandler((short)MsgType.LocalClientAuthority, OnClientAuthority); } else { // LocalClient shares the sim/scene with the server, no need for these events - client.RegisterHandler(MsgType.ObjectSpawn, OnObjectSpawn); - client.RegisterHandler(MsgType.ObjectSpawnScene, OnObjectSpawnScene); - client.RegisterHandler(MsgType.SpawnFinished, OnObjectSpawnFinished); - client.RegisterHandler(MsgType.ObjectDestroy, OnObjectDestroy); - client.RegisterHandler(MsgType.ObjectHide, OnObjectDestroy); - client.RegisterHandler(MsgType.UpdateVars, OnUpdateVarsMessage); - client.RegisterHandler(MsgType.Owner, OnOwnerMessage); - client.RegisterHandler(MsgType.SyncList, OnSyncListMessage); - client.RegisterHandler(MsgType.Animation, NetworkAnimator.OnAnimationClientMessage); - client.RegisterHandler(MsgType.AnimationParameters, NetworkAnimator.OnAnimationParametersClientMessage); - client.RegisterHandler(MsgType.LocalClientAuthority, OnClientAuthority); + client.RegisterHandler((short)MsgType.ObjectSpawn, OnObjectSpawn); + client.RegisterHandler((short)MsgType.ObjectSpawnScene, OnObjectSpawnScene); + client.RegisterHandler((short)MsgType.SpawnFinished, OnObjectSpawnFinished); + client.RegisterHandler((short)MsgType.ObjectDestroy, OnObjectDestroy); + client.RegisterHandler((short)MsgType.ObjectHide, OnObjectDestroy); + client.RegisterHandler((short)MsgType.UpdateVars, OnUpdateVarsMessage); + client.RegisterHandler((short)MsgType.Owner, OnOwnerMessage); + client.RegisterHandler((short)MsgType.SyncList, OnSyncListMessage); + client.RegisterHandler((short)MsgType.Animation, NetworkAnimator.OnAnimationClientMessage); + client.RegisterHandler((short)MsgType.AnimationParameters, NetworkAnimator.OnAnimationParametersClientMessage); + client.RegisterHandler((short)MsgType.LocalClientAuthority, OnClientAuthority); } - client.RegisterHandler(MsgType.Rpc, OnRPCMessage); - client.RegisterHandler(MsgType.SyncEvent, OnSyncEventMessage); - client.RegisterHandler(MsgType.AnimationTrigger, NetworkAnimator.OnAnimationTriggerClientMessage); + client.RegisterHandler((short)MsgType.Rpc, OnRPCMessage); + client.RegisterHandler((short)MsgType.SyncEvent, OnSyncEventMessage); + client.RegisterHandler((short)MsgType.AnimationTrigger, NetworkAnimator.OnAnimationTriggerClientMessage); } // ------------------------ NetworkScene pass-throughs --------------------- @@ -388,7 +388,7 @@ static void OnObjectSpawn(NetworkMessage netMsg) #if UNITY_EDITOR UnityEditor.NetworkDetailStats.IncrementStat( UnityEditor.NetworkDetailStats.NetworkDirection.Incoming, - MsgType.ObjectSpawn, GetStringForAssetId(msg.assetId), 1); + (short)MsgType.ObjectSpawn, GetStringForAssetId(msg.assetId), 1); #endif NetworkIdentity localNetworkIdentity; @@ -455,7 +455,7 @@ static void OnObjectSpawnScene(NetworkMessage netMsg) #if UNITY_EDITOR UnityEditor.NetworkDetailStats.IncrementStat( UnityEditor.NetworkDetailStats.NetworkDirection.Incoming, - MsgType.ObjectSpawnScene, "sceneId", 1); + (short)MsgType.ObjectSpawnScene, "sceneId", 1); #endif NetworkIdentity localNetworkIdentity; @@ -521,7 +521,7 @@ static void OnObjectDestroy(NetworkMessage netMsg) #if UNITY_EDITOR UnityEditor.NetworkDetailStats.IncrementStat( UnityEditor.NetworkDetailStats.NetworkDirection.Incoming, - MsgType.ObjectDestroy, GetStringForAssetId(localObject.assetId), 1); + (short)MsgType.ObjectDestroy, GetStringForAssetId(localObject.assetId), 1); #endif localObject.OnNetworkDestroy(); @@ -652,7 +652,7 @@ static void OnSyncEventMessage(NetworkMessage netMsg) #if UNITY_EDITOR UnityEditor.NetworkDetailStats.IncrementStat( UnityEditor.NetworkDetailStats.NetworkDirection.Outgoing, - MsgType.SyncEvent, NetworkBehaviour.GetCmdHashHandlerName(cmdHash), 1); + (short)MsgType.SyncEvent, NetworkBehaviour.GetCmdHashHandlerName(cmdHash), 1); #endif } @@ -676,7 +676,7 @@ static void OnSyncListMessage(NetworkMessage netMsg) #if UNITY_EDITOR UnityEditor.NetworkDetailStats.IncrementStat( UnityEditor.NetworkDetailStats.NetworkDirection.Outgoing, - MsgType.SyncList, NetworkBehaviour.GetCmdHashHandlerName(cmdHash), 1); + (short)MsgType.SyncList, NetworkBehaviour.GetCmdHashHandlerName(cmdHash), 1); #endif } diff --git a/Unity-Technologies-networking/Runtime/LocalClient.cs b/Unity-Technologies-networking/Runtime/LocalClient.cs index f04b48da8..746c210c5 100644 --- a/Unity-Technologies-networking/Runtime/LocalClient.cs +++ b/Unity-Technologies-networking/Runtime/LocalClient.cs @@ -21,7 +21,7 @@ public override void Disconnect() ClientScene.HandleClientDisconnect(m_Connection); if (m_Connected) { - PostInternalMessage(MsgType.Disconnect); + PostInternalMessage((short)MsgType.Disconnect); m_Connected = false; } m_AsyncConnect = ConnectState.Disconnected; @@ -40,7 +40,7 @@ internal void InternalConnectLocalServer(bool generateConnectMsg) if (generateConnectMsg) { - PostInternalMessage(MsgType.Connect); + PostInternalMessage((short)MsgType.Connect); } m_Connected = true; } diff --git a/Unity-Technologies-networking/Runtime/NetworkAnimator.cs b/Unity-Technologies-networking/Runtime/NetworkAnimator.cs index 16202415f..1e4cb4f65 100644 --- a/Unity-Technologies-networking/Runtime/NetworkAnimator.cs +++ b/Unity-Technologies-networking/Runtime/NetworkAnimator.cs @@ -114,7 +114,7 @@ void FixedUpdate() WriteParameters(m_ParameterWriter, false); animMsg.parameters = m_ParameterWriter.ToArray(); - SendMessage(MsgType.Animation, animMsg); + SendMessage((short)MsgType.Animation, animMsg); } bool CheckAnimStateChanged(out int stateHash, out float normalizedTime) @@ -165,7 +165,7 @@ void CheckSendRate() WriteParameters(m_ParameterWriter, true); animMsg.parameters = m_ParameterWriter.ToArray(); - SendMessage(MsgType.AnimationParameters, animMsg); + SendMessage((short)MsgType.AnimationParameters, animMsg); } } @@ -351,7 +351,7 @@ public void SetTrigger(int hash) var client = ClientScene.readyConnection; if (client != null) { - client.Send(MsgType.AnimationTrigger, animMsg); + client.Send((short)MsgType.AnimationTrigger, animMsg); } } return; @@ -359,7 +359,7 @@ public void SetTrigger(int hash) if (isServer && !localPlayerAuthority) { - NetworkServer.SendToReady(gameObject, MsgType.AnimationTrigger, animMsg); + NetworkServer.SendToReady(gameObject, (short)MsgType.AnimationTrigger, animMsg); } } @@ -382,7 +382,7 @@ static internal void OnAnimationServerMessage(NetworkMessage netMsg) NetworkReader reader = new NetworkReader(msg.parameters); animSync.HandleAnimMsg(msg, reader); - NetworkServer.SendToReady(go, MsgType.Animation, msg); + NetworkServer.SendToReady(go, (short)MsgType.Animation, msg); } } @@ -403,7 +403,7 @@ static internal void OnAnimationParametersServerMessage(NetworkMessage netMsg) { NetworkReader reader = new NetworkReader(msg.parameters); animSync.HandleAnimParamsMsg(msg, reader); - NetworkServer.SendToReady(go, MsgType.AnimationParameters, msg); + NetworkServer.SendToReady(go, (short)MsgType.AnimationParameters, msg); } } @@ -423,7 +423,7 @@ static internal void OnAnimationTriggerServerMessage(NetworkMessage netMsg) { animSync.HandleAnimTriggerMsg(msg.hash); - NetworkServer.SendToReady(go, MsgType.AnimationTrigger, msg); + NetworkServer.SendToReady(go, (short)MsgType.AnimationTrigger, msg); } } diff --git a/Unity-Technologies-networking/Runtime/NetworkBehaviour.cs b/Unity-Technologies-networking/Runtime/NetworkBehaviour.cs index 1bda9165c..8b9f738e1 100644 --- a/Unity-Technologies-networking/Runtime/NetworkBehaviour.cs +++ b/Unity-Technologies-networking/Runtime/NetworkBehaviour.cs @@ -73,7 +73,7 @@ protected void SendCommandInternal(NetworkWriter writer, int channelId, string c #if UNITY_EDITOR UnityEditor.NetworkDetailStats.IncrementStat( UnityEditor.NetworkDetailStats.NetworkDirection.Outgoing, - MsgType.Command, cmdName, 1); + (short)MsgType.Command, cmdName, 1); #endif } @@ -101,7 +101,7 @@ protected void SendRPCInternal(NetworkWriter writer, int channelId, string rpcNa #if UNITY_EDITOR UnityEditor.NetworkDetailStats.IncrementStat( UnityEditor.NetworkDetailStats.NetworkDirection.Outgoing, - MsgType.Rpc, rpcName, 1); + (short)MsgType.Rpc, rpcName, 1); #endif } @@ -122,7 +122,7 @@ protected void SendTargetRPCInternal(NetworkConnection conn, NetworkWriter write #if UNITY_EDITOR UnityEditor.NetworkDetailStats.IncrementStat( UnityEditor.NetworkDetailStats.NetworkDirection.Outgoing, - MsgType.Rpc, rpcName, 1); + (short)MsgType.Rpc, rpcName, 1); #endif } @@ -149,7 +149,7 @@ protected void SendEventInternal(NetworkWriter writer, int channelId, string eve #if UNITY_EDITOR UnityEditor.NetworkDetailStats.IncrementStat( UnityEditor.NetworkDetailStats.NetworkDirection.Outgoing, - MsgType.SyncEvent, eventName, 1); + (short)MsgType.SyncEvent, eventName, 1); #endif } diff --git a/Unity-Technologies-networking/Runtime/NetworkClient.cs b/Unity-Technologies-networking/Runtime/NetworkClient.cs index af7ed6ee5..a22af5965 100644 --- a/Unity-Technologies-networking/Runtime/NetworkClient.cs +++ b/Unity-Technologies-networking/Runtime/NetworkClient.cs @@ -337,7 +337,7 @@ public bool SendByChannel(short msgType, MessageBase msg, int channelId) #if UNITY_EDITOR UnityEditor.NetworkDetailStats.IncrementStat( UnityEditor.NetworkDetailStats.NetworkDirection.Outgoing, - MsgType.UserMessage, msgType.ToString() + ":" + msg.GetType().Name, 1); + (short)MsgType.UserMessage, msgType.ToString() + ":" + msg.GetType().Name, 1); #endif if (m_Connection != null) { @@ -430,7 +430,7 @@ internal virtual void Update() } m_AsyncConnect = ConnectState.Connected; - m_Connection.InvokeHandlerNoData(MsgType.Connect); + m_Connection.InvokeHandlerNoData((short)MsgType.Connect); break; case NetworkEventType.DataEvent: @@ -443,7 +443,7 @@ internal virtual void Update() #if UNITY_EDITOR UnityEditor.NetworkDetailStats.IncrementStat( UnityEditor.NetworkDetailStats.NetworkDirection.Incoming, - MsgType.LLAPIMsg, "msg", 1); + (short)MsgType.LLAPIMsg, "msg", 1); #endif m_MsgReader.SeekZero(); @@ -465,7 +465,7 @@ internal virtual void Update() ClientScene.HandleClientDisconnect(m_Connection); if (m_Connection != null) { - m_Connection.InvokeHandlerNoData(MsgType.Disconnect); + m_Connection.InvokeHandlerNoData((short)MsgType.Disconnect); } break; @@ -513,7 +513,7 @@ void GenerateDisconnectError(byte error) void GenerateError(byte error) { NetworkMessageDelegate msgDelegate; - if (m_MessageHandlers.TryGetValue(MsgType.Error, out msgDelegate)) + if (m_MessageHandlers.TryGetValue((short)MsgType.Error, out msgDelegate)) { ErrorMessage msg = new ErrorMessage(); msg.errorCode = error; @@ -523,7 +523,7 @@ void GenerateError(byte error) msg.Serialize(writer); NetworkMessage netMsg = new NetworkMessage(); - netMsg.msgType = MsgType.Error; + netMsg.msgType = (short)MsgType.Error; netMsg.reader = new NetworkReader(writer.ToArray()); netMsg.conn = m_Connection; netMsg.channelId = 0; diff --git a/Unity-Technologies-networking/Runtime/NetworkConnection.cs b/Unity-Technologies-networking/Runtime/NetworkConnection.cs index 8e20559b2..59dee7c6e 100644 --- a/Unity-Technologies-networking/Runtime/NetworkConnection.cs +++ b/Unity-Technologies-networking/Runtime/NetworkConnection.cs @@ -198,7 +198,7 @@ public virtual bool SendBytes(byte[] bytes, int bytesToSend, int channelId) #if UNITY_EDITOR UnityEditor.NetworkDetailStats.IncrementStat( UnityEditor.NetworkDetailStats.NetworkDirection.Outgoing, - MsgType.HLAPIMsg, "msg", 1); + (short)MsgType.HLAPIMsg, "msg", 1); #endif if (bytesToSend > UInt16.MaxValue) { @@ -287,7 +287,7 @@ protected void HandleReader(NetworkReader reader, int receivedSize, int channelI if (pauseQueue != null) { pauseQueue.Enqueue(msg); - if (LogFilter.logWarn) { Debug.LogWarning("HandleReader: added message to pause queue: " + msgType + " str=" + MsgType.MsgTypeToString(msgType) + " queue size=" + pauseQueue.Count); } + if (LogFilter.logWarn) { Debug.LogWarning("HandleReader: added message to pause queue: " + msgType + " str=" + ((MsgType)msgType) + " queue size=" + pauseQueue.Count); } } else { @@ -298,13 +298,13 @@ protected void HandleReader(NetworkReader reader, int receivedSize, int channelI #if UNITY_EDITOR UnityEditor.NetworkDetailStats.IncrementStat( UnityEditor.NetworkDetailStats.NetworkDirection.Incoming, - MsgType.HLAPIMsg, "msg", 1); + (short)MsgType.HLAPIMsg, "msg", 1); - if (msgType > MsgType.Highest) + if (msgType > (short)MsgType.Highest) { UnityEditor.NetworkDetailStats.IncrementStat( UnityEditor.NetworkDetailStats.NetworkDirection.Incoming, - MsgType.UserMessage, msgType.ToString() + ":" + msgType.GetType().Name, 1); + (short)MsgType.UserMessage, msgType.ToString() + ":" + msgType.GetType().Name, 1); } #endif } @@ -408,7 +408,7 @@ internal void ResumeHandling() { foreach (NetworkMessage msg in pauseQueue) { - if (LogFilter.logWarn) { Debug.LogWarning("processing queued message: " + msg.msgType + " str=" + MsgType.MsgTypeToString(msg.msgType)); } + if (LogFilter.logWarn) { Debug.LogWarning("processing queued message: " + msg.msgType + " str=" + msg.msgType); } var msgDelegate = m_MessageHandlers[msg.msgType]; msgDelegate(msg); } diff --git a/Unity-Technologies-networking/Runtime/NetworkIdentity.cs b/Unity-Technologies-networking/Runtime/NetworkIdentity.cs index eac97953d..3de30d900 100644 --- a/Unity-Technologies-networking/Runtime/NetworkIdentity.cs +++ b/Unity-Technologies-networking/Runtime/NetworkIdentity.cs @@ -536,7 +536,7 @@ internal void HandleSyncEvent(int cmdHash, NetworkReader reader) #if UNITY_EDITOR UnityEditor.NetworkDetailStats.IncrementStat( UnityEditor.NetworkDetailStats.NetworkDirection.Incoming, - MsgType.SyncEvent, NetworkBehaviour.GetCmdHashEventName(cmdHash), 1); + (short)MsgType.SyncEvent, NetworkBehaviour.GetCmdHashEventName(cmdHash), 1); #endif } @@ -579,7 +579,7 @@ internal void HandleSyncList(int cmdHash, NetworkReader reader) #if UNITY_EDITOR UnityEditor.NetworkDetailStats.IncrementStat( UnityEditor.NetworkDetailStats.NetworkDirection.Incoming, - MsgType.SyncList, NetworkBehaviour.GetCmdHashListName(cmdHash), 1); + (short)MsgType.SyncList, NetworkBehaviour.GetCmdHashListName(cmdHash), 1); #endif } @@ -622,7 +622,7 @@ internal void HandleCommand(int cmdHash, NetworkReader reader) #if UNITY_EDITOR UnityEditor.NetworkDetailStats.IncrementStat( UnityEditor.NetworkDetailStats.NetworkDirection.Incoming, - MsgType.Command, NetworkBehaviour.GetCmdHashCmdName(cmdHash), 1); + (short)MsgType.Command, NetworkBehaviour.GetCmdHashCmdName(cmdHash), 1); #endif } @@ -665,7 +665,7 @@ internal void HandleRPC(int cmdHash, NetworkReader reader) #if UNITY_EDITOR UnityEditor.NetworkDetailStats.IncrementStat( UnityEditor.NetworkDetailStats.NetworkDirection.Incoming, - MsgType.Rpc, NetworkBehaviour.GetCmdHashRpcName(cmdHash), 1); + (short)MsgType.Rpc, NetworkBehaviour.GetCmdHashRpcName(cmdHash), 1); #endif } @@ -691,7 +691,7 @@ internal void UNetUpdate() if ((dirtyChannelBits & (uint)(1 << channelId)) != 0) { NetworkWriter writer = new NetworkWriter(); - writer.StartMessage(MsgType.UpdateVars); + writer.StartMessage((short)MsgType.UpdateVars); writer.Write(netId); bool wroteData = false; @@ -714,7 +714,7 @@ internal void UNetUpdate() #if UNITY_EDITOR UnityEditor.NetworkDetailStats.IncrementStat( UnityEditor.NetworkDetailStats.NetworkDirection.Outgoing, - MsgType.UpdateVars, comp.GetType().Name, 1); + (short)MsgType.UpdateVars, comp.GetType().Name, 1); #endif wroteData = true; @@ -968,7 +968,7 @@ public bool RemoveClientAuthority(NetworkConnection conn) var msg = new ClientAuthorityMessage(); msg.netId = netId; msg.authority = false; - conn.Send(MsgType.LocalClientAuthority, msg); + conn.Send((short)MsgType.LocalClientAuthority, msg); if (clientAuthorityCallback != null) { @@ -1012,7 +1012,7 @@ public bool AssignClientAuthority(NetworkConnection conn) var msg = new ClientAuthorityMessage(); msg.netId = netId; msg.authority = true; - conn.Send(MsgType.LocalClientAuthority, msg); + conn.Send((short)MsgType.LocalClientAuthority, msg); if (clientAuthorityCallback != null) { diff --git a/Unity-Technologies-networking/Runtime/NetworkManager.cs b/Unity-Technologies-networking/Runtime/NetworkManager.cs index 5dc2ef153..5389f41c3 100644 --- a/Unity-Technologies-networking/Runtime/NetworkManager.cs +++ b/Unity-Technologies-networking/Runtime/NetworkManager.cs @@ -226,12 +226,12 @@ void OnValidate() internal void RegisterServerMessages() { - NetworkServer.RegisterHandler(MsgType.Connect, OnServerConnectInternal); - NetworkServer.RegisterHandler(MsgType.Disconnect, OnServerDisconnectInternal); - NetworkServer.RegisterHandler(MsgType.Ready, OnServerReadyMessageInternal); - NetworkServer.RegisterHandler(MsgType.AddPlayer, OnServerAddPlayerMessageInternal); - NetworkServer.RegisterHandler(MsgType.RemovePlayer, OnServerRemovePlayerMessageInternal); - NetworkServer.RegisterHandler(MsgType.Error, OnServerErrorInternal); + NetworkServer.RegisterHandler((short)MsgType.Connect, OnServerConnectInternal); + NetworkServer.RegisterHandler((short)MsgType.Disconnect, OnServerDisconnectInternal); + NetworkServer.RegisterHandler((short)MsgType.Ready, OnServerReadyMessageInternal); + NetworkServer.RegisterHandler((short)MsgType.AddPlayer, OnServerAddPlayerMessageInternal); + NetworkServer.RegisterHandler((short)MsgType.RemovePlayer, OnServerRemovePlayerMessageInternal); + NetworkServer.RegisterHandler((short)MsgType.Error, OnServerErrorInternal); } public bool StartServer() @@ -309,11 +309,11 @@ bool StartServer(ConnectionConfig config, int maxConnections) internal void RegisterClientMessages(NetworkClient client) { - client.RegisterHandler(MsgType.Connect, OnClientConnectInternal); - client.RegisterHandler(MsgType.Disconnect, OnClientDisconnectInternal); - client.RegisterHandler(MsgType.NotReady, OnClientNotReadyMessageInternal); - client.RegisterHandler(MsgType.Error, OnClientErrorInternal); - client.RegisterHandler(MsgType.Scene, OnClientSceneInternal); + client.RegisterHandler((short)MsgType.Connect, OnClientConnectInternal); + client.RegisterHandler((short)MsgType.Disconnect, OnClientDisconnectInternal); + client.RegisterHandler((short)MsgType.NotReady, OnClientNotReadyMessageInternal); + client.RegisterHandler((short)MsgType.Error, OnClientErrorInternal); + client.RegisterHandler((short)MsgType.Scene, OnClientSceneInternal); if (m_PlayerPrefab != null) { @@ -525,7 +525,7 @@ public virtual void ServerChangeScene(string newSceneName) s_LoadingSceneAsync = SceneManager.LoadSceneAsync(newSceneName); StringMessage msg = new StringMessage(networkSceneName); - NetworkServer.SendToAll(MsgType.Scene, msg); + NetworkServer.SendToAll((short)MsgType.Scene, msg); s_StartPositionIndex = 0; s_StartPositions.Clear(); @@ -693,7 +693,7 @@ internal void OnServerConnectInternal(NetworkMessage netMsg) if (networkSceneName != "" && networkSceneName != m_OfflineScene) { StringMessage msg = new StringMessage(networkSceneName); - netMsg.conn.Send(MsgType.Scene, msg); + netMsg.conn.Send((short)MsgType.Scene, msg); } OnServerConnect(netMsg.conn); diff --git a/Unity-Technologies-networking/Runtime/NetworkServer.cs b/Unity-Technologies-networking/Runtime/NetworkServer.cs index 47f3cec6b..7a34eca48 100644 --- a/Unity-Technologies-networking/Runtime/NetworkServer.cs +++ b/Unity-Technologies-networking/Runtime/NetworkServer.cs @@ -125,14 +125,14 @@ public static void Initialize() static internal void RegisterMessageHandlers() { - RegisterHandler(MsgType.Ready, OnClientReadyMessage); - RegisterHandler(MsgType.Command, OnCommandMessage); - RegisterHandler(MsgType.LocalPlayerTransform, NetworkTransform.HandleTransform); - RegisterHandler(MsgType.LocalChildTransform, NetworkTransformChild.HandleChildTransform); - RegisterHandler(MsgType.RemovePlayer, OnRemovePlayerMessage); - RegisterHandler(MsgType.Animation, NetworkAnimator.OnAnimationServerMessage); - RegisterHandler(MsgType.AnimationParameters, NetworkAnimator.OnAnimationParametersServerMessage); - RegisterHandler(MsgType.AnimationTrigger, NetworkAnimator.OnAnimationTriggerServerMessage); + RegisterHandler((short)MsgType.Ready, OnClientReadyMessage); + RegisterHandler((short)MsgType.Command, OnCommandMessage); + RegisterHandler((short)MsgType.LocalPlayerTransform, NetworkTransform.HandleTransform); + RegisterHandler((short)MsgType.LocalChildTransform, NetworkTransformChild.HandleChildTransform); + RegisterHandler((short)MsgType.RemovePlayer, OnRemovePlayerMessage); + RegisterHandler((short)MsgType.Animation, NetworkAnimator.OnAnimationServerMessage); + RegisterHandler((short)MsgType.AnimationParameters, NetworkAnimator.OnAnimationParametersServerMessage); + RegisterHandler((short)MsgType.AnimationTrigger, NetworkAnimator.OnAnimationTriggerServerMessage); // also setup max packet size. maxPacketSize = hostTopology.DefaultConfig.PacketSize; @@ -220,7 +220,7 @@ static internal int AddLocalClient(LocalClient localClient) s_LocalConnection.connectionId = 0; SetConnectionAtIndex(s_LocalConnection); - s_LocalConnection.InvokeHandlerNoData(MsgType.Connect); + s_LocalConnection.InvokeHandlerNoData((short)MsgType.Connect); return 0; } @@ -553,7 +553,7 @@ static void HandleConnect(int connectionId, byte error) static void OnConnected(NetworkConnection conn) { if (LogFilter.logDebug) { Debug.Log("Server accepted client:" + conn.connectionId); } - conn.InvokeHandlerNoData(MsgType.Connect); + conn.InvokeHandlerNoData((short)MsgType.Connect); } static void HandleDisconnect(int connectionId, byte error) @@ -586,7 +586,7 @@ static void HandleDisconnect(int connectionId, byte error) static void OnDisconnected(NetworkConnection conn) { - conn.InvokeHandlerNoData(MsgType.Disconnect); + conn.InvokeHandlerNoData((short)MsgType.Disconnect); if (conn.playerControllers.Any(pc => pc.gameObject != null)) { @@ -631,7 +631,7 @@ static void OnData(NetworkConnection conn, int receivedSize, int channelId) #if UNITY_EDITOR UnityEditor.NetworkDetailStats.IncrementStat( UnityEditor.NetworkDetailStats.NetworkDirection.Incoming, - MsgType.LLAPIMsg, "msg", 1); + (short)MsgType.LLAPIMsg, "msg", 1); #endif conn.TransportReceive(s_MsgBuffer, receivedSize, channelId); } @@ -658,7 +658,7 @@ static void GenerateDisconnectError(NetworkConnection conn, byte error) static void GenerateError(NetworkConnection conn, byte error) { - if (handlers.ContainsKey(MsgType.Error)) + if (handlers.ContainsKey((short)MsgType.Error)) { ErrorMessage msg = new ErrorMessage(); msg.errorCode = error; @@ -669,7 +669,7 @@ static void GenerateError(NetworkConnection conn, byte error) // pass a reader (attached to local buffer) to handler NetworkReader reader = new NetworkReader(writer.ToArray()); - conn.InvokeHandler(MsgType.Error, reader, 0); + conn.InvokeHandler((short)MsgType.Error, reader, 0); } } @@ -876,7 +876,7 @@ static void FinishPlayerForConnection(NetworkConnection conn, NetworkIdentity uv OwnerMessage owner = new OwnerMessage(); owner.netId = uv.netId; owner.playerControllerId = uv.playerControllerId; - conn.Send(MsgType.Owner, owner); + conn.Send((short)MsgType.Owner, owner); } static internal bool InternalReplacePlayerForConnection(NetworkConnection conn, GameObject playerGameObject, short playerControllerId) @@ -995,7 +995,7 @@ static internal void SetClientReadyInternal(NetworkConnection conn) ObjectSpawnFinishedMessage msg = new ObjectSpawnFinishedMessage(); msg.state = 0; - conn.Send(MsgType.SpawnFinished, msg); + conn.Send((short)MsgType.SpawnFinished, msg); foreach (NetworkIdentity uv in objects.Values) { @@ -1019,7 +1019,7 @@ static internal void SetClientReadyInternal(NetworkConnection conn) } msg.state = 1; - conn.Send(MsgType.SpawnFinished, msg); + conn.Send((short)MsgType.SpawnFinished, msg); } static internal void ShowForConnection(NetworkIdentity uv, NetworkConnection conn) @@ -1032,7 +1032,7 @@ static internal void HideForConnection(NetworkIdentity uv, NetworkConnection con { ObjectDestroyMessage msg = new ObjectDestroyMessage(); msg.netId = uv.netId; - conn.Send(MsgType.ObjectHide, msg); + conn.Send((short)MsgType.ObjectHide, msg); } // call this to make all the clients not ready, such as when changing levels. @@ -1062,7 +1062,7 @@ static internal void InternalSetClientNotReady(NetworkConnection conn) conn.RemoveObservers(); NotReadyMessage msg = new NotReadyMessage(); - conn.Send(MsgType.NotReady, msg); + conn.Send((short)MsgType.NotReady, msg); } } @@ -1177,17 +1177,17 @@ static internal void SendSpawnMessage(NetworkIdentity uv, NetworkConnection conn if (conn != null) { - conn.Send(MsgType.ObjectSpawn, msg); + conn.Send((short)MsgType.ObjectSpawn, msg); } else { - SendToReady(uv.gameObject, MsgType.ObjectSpawn, msg); + SendToReady(uv.gameObject, (short)MsgType.ObjectSpawn, msg); } #if UNITY_EDITOR UnityEditor.NetworkDetailStats.IncrementStat( UnityEditor.NetworkDetailStats.NetworkDirection.Outgoing, - MsgType.ObjectSpawn, uv.assetId.ToString(), 1); + (short)MsgType.ObjectSpawn, uv.assetId.ToString(), 1); #endif } else @@ -1207,17 +1207,17 @@ static internal void SendSpawnMessage(NetworkIdentity uv, NetworkConnection conn if (conn != null) { - conn.Send(MsgType.ObjectSpawnScene, msg); + conn.Send((short)MsgType.ObjectSpawnScene, msg); } else { - SendToReady(uv.gameObject, MsgType.ObjectSpawn, msg); + SendToReady(uv.gameObject, (short)MsgType.ObjectSpawn, msg); } #if UNITY_EDITOR UnityEditor.NetworkDetailStats.IncrementStat( UnityEditor.NetworkDetailStats.NetworkDirection.Outgoing, - MsgType.ObjectSpawnScene, "sceneId", 1); + (short)MsgType.ObjectSpawnScene, "sceneId", 1); #endif } } @@ -1312,12 +1312,12 @@ static void DestroyObject(NetworkIdentity uv, bool destroyServerObject) #if UNITY_EDITOR UnityEditor.NetworkDetailStats.IncrementStat( UnityEditor.NetworkDetailStats.NetworkDirection.Outgoing, - MsgType.ObjectDestroy, uv.assetId.ToString(), 1); + (short)MsgType.ObjectDestroy, uv.assetId.ToString(), 1); #endif ObjectDestroyMessage msg = new ObjectDestroyMessage(); msg.netId = uv.netId; - SendToObservers(uv.gameObject, MsgType.ObjectDestroy, msg); + SendToObservers(uv.gameObject, (short)MsgType.ObjectDestroy, msg); uv.ClearObservers(); if (NetworkClient.active && s_LocalClientActive) @@ -1502,7 +1502,7 @@ static bool AddExternalConnectionInternal(NetworkConnection conn) if (LogFilter.logDebug) { Debug.Log("AddExternalConnection external connection " + conn.connectionId); } SetConnectionAtIndex(conn); s_ExternalConnections.Add(conn.connectionId); - conn.InvokeHandlerNoData(MsgType.Connect); + conn.InvokeHandlerNoData((short)MsgType.Connect); return true; } diff --git a/Unity-Technologies-networking/Runtime/NetworkTranformChild.cs b/Unity-Technologies-networking/Runtime/NetworkTranformChild.cs index ce9c01498..ddbf8c820 100644 --- a/Unity-Technologies-networking/Runtime/NetworkTranformChild.cs +++ b/Unity-Technologies-networking/Runtime/NetworkTranformChild.cs @@ -391,7 +391,7 @@ void SendTransform() return; } - m_LocalTransformWriter.StartMessage(MsgType.LocalChildTransform); + m_LocalTransformWriter.StartMessage((short)MsgType.LocalChildTransform); m_LocalTransformWriter.Write(netId); m_LocalTransformWriter.WritePackedUInt32(m_ChildIndex); SerializeModeTransform(m_LocalTransformWriter); @@ -405,7 +405,7 @@ void SendTransform() #if UNITY_EDITOR UnityEditor.NetworkDetailStats.IncrementStat( UnityEditor.NetworkDetailStats.NetworkDirection.Outgoing, - MsgType.LocalChildTransform, "16:LocalChildTransform", 1); + (short)MsgType.LocalChildTransform, "16:LocalChildTransform", 1); #endif ClientScene.readyConnection.SendWriter(m_LocalTransformWriter, GetNetworkChannel()); } @@ -418,7 +418,7 @@ static internal void HandleChildTransform(NetworkMessage netMsg) #if UNITY_EDITOR UnityEditor.NetworkDetailStats.IncrementStat( UnityEditor.NetworkDetailStats.NetworkDirection.Incoming, - MsgType.LocalChildTransform, "16:LocalChildTransform", 1); + (short)MsgType.LocalChildTransform, "16:LocalChildTransform", 1); #endif GameObject foundObj = NetworkServer.FindLocalObject(netId); diff --git a/Unity-Technologies-networking/Runtime/NetworkTransform.cs b/Unity-Technologies-networking/Runtime/NetworkTransform.cs index fe10c1388..91b19cf22 100644 --- a/Unity-Technologies-networking/Runtime/NetworkTransform.cs +++ b/Unity-Technologies-networking/Runtime/NetworkTransform.cs @@ -1189,7 +1189,7 @@ void SendTransform() return; } - m_LocalTransformWriter.StartMessage(MsgType.LocalPlayerTransform); + m_LocalTransformWriter.StartMessage((short)MsgType.LocalPlayerTransform); m_LocalTransformWriter.Write(netId); switch (transformSyncMode) @@ -1243,7 +1243,7 @@ void SendTransform() #if UNITY_EDITOR UnityEditor.NetworkDetailStats.IncrementStat( UnityEditor.NetworkDetailStats.NetworkDirection.Outgoing, - MsgType.LocalPlayerTransform, "6:LocalPlayerTransform", 1); + (short)MsgType.LocalPlayerTransform, "6:LocalPlayerTransform", 1); #endif ClientScene.readyConnection.SendWriter(m_LocalTransformWriter, GetNetworkChannel()); } @@ -1255,7 +1255,7 @@ static public void HandleTransform(NetworkMessage netMsg) #if UNITY_EDITOR UnityEditor.NetworkDetailStats.IncrementStat( UnityEditor.NetworkDetailStats.NetworkDirection.Incoming, - MsgType.LocalPlayerTransform, "6:LocalPlayerTransform", 1); + (short)MsgType.LocalPlayerTransform, "6:LocalPlayerTransform", 1); #endif GameObject foundObj = NetworkServer.FindLocalObject(netId); diff --git a/Unity-Technologies-networking/Runtime/SyncList.cs b/Unity-Technologies-networking/Runtime/SyncList.cs index 547d4bcd9..520f513c4 100644 --- a/Unity-Technologies-networking/Runtime/SyncList.cs +++ b/Unity-Technologies-networking/Runtime/SyncList.cs @@ -250,7 +250,7 @@ void SendMsg(Operation op, int itemIndex, T item) } NetworkWriter writer = new NetworkWriter(); - writer.StartMessage(MsgType.SyncList); + writer.StartMessage((short)MsgType.SyncList); writer.Write(uv.netId); writer.WritePackedUInt32((uint)m_CmdHash); writer.Write((byte)op); @@ -263,7 +263,7 @@ void SendMsg(Operation op, int itemIndex, T item) #if UNITY_EDITOR UnityEditor.NetworkDetailStats.IncrementStat( UnityEditor.NetworkDetailStats.NetworkDirection.Outgoing, - MsgType.SyncList, op.ToString(), 1); + (short)MsgType.SyncList, op.ToString(), 1); #endif // ensure it is invoked on host diff --git a/Unity-Technologies-networking/Runtime/UNetwork.cs b/Unity-Technologies-networking/Runtime/UNetwork.cs index 8e243c74a..85ef92c7d 100644 --- a/Unity-Technologies-networking/Runtime/UNetwork.cs +++ b/Unity-Technologies-networking/Runtime/UNetwork.cs @@ -14,121 +14,58 @@ namespace UnityEngine.Networking public delegate void UnSpawnDelegate(GameObject spawned); // built-in system network messages - public class MsgType + // original HLAPI uses short, so let's keep short to not break packet header etc. + // => use .ToString() to get the field name from the field value + // => we specify the short values so it's easier to look up opcodes when debugging packets + public enum MsgType : short { // internal system messages - cannot be replaced by user code - public const short ObjectDestroy = 1; - public const short Rpc = 2; - public const short ObjectSpawn = 3; - public const short Owner = 4; - public const short Command = 5; - public const short LocalPlayerTransform = 6; - public const short SyncEvent = 7; - public const short UpdateVars = 8; - public const short SyncList = 9; - public const short ObjectSpawnScene = 10; - public const short NetworkInfo = 11; - public const short SpawnFinished = 12; - public const short ObjectHide = 13; - public const short CRC = 14; - public const short LocalClientAuthority = 15; - public const short LocalChildTransform = 16; - public const short Fragment = 17; - public const short PeerClientAuthority = 18; + ObjectDestroy = 1, + Rpc = 2, + ObjectSpawn = 3, + Owner = 4, + Command = 5, + LocalPlayerTransform = 6, + SyncEvent = 7, + UpdateVars = 8, + SyncList = 9, + ObjectSpawnScene = 10, + NetworkInfo = 11, + SpawnFinished = 12, + ObjectHide = 13, + CRC = 14, + LocalClientAuthority = 15, + LocalChildTransform = 16, + Fragment = 17, + PeerClientAuthority = 18, // used for profiling - internal const short UserMessage = 0; - internal const short HLAPIMsg = 28; - internal const short LLAPIMsg = 29; - internal const short HLAPIResend = 30; - internal const short HLAPIPending = 31; + UserMessage = 0, + HLAPIMsg = 28, + LLAPIMsg = 29, + HLAPIResend = 30, + HLAPIPending = 31, - public const short InternalHighest = 31; + InternalHighest = 31, // public system messages - can be replaced by user code - public const short Connect = 32; - public const short Disconnect = 33; - public const short Error = 34; - public const short Ready = 35; - public const short NotReady = 36; - public const short AddPlayer = 37; - public const short RemovePlayer = 38; - public const short Scene = 39; - public const short Animation = 40; - public const short AnimationParameters = 41; - public const short AnimationTrigger = 42; - public const short LobbyReadyToBegin = 43; - public const short LobbySceneLoaded = 44; - public const short LobbyAddPlayerFailed = 45; - public const short LobbyReturnToLobby = 46; + Connect = 32, + Disconnect = 33, + Error = 34, + Ready = 35, + NotReady = 36, + AddPlayer = 37, + RemovePlayer = 38, + Scene = 39, + Animation = 40, + AnimationParameters = 41, + AnimationTrigger = 42, + LobbyReadyToBegin = 43, + LobbySceneLoaded = 44, + LobbyAddPlayerFailed = 45, + LobbyReturnToLobby = 46, - //NOTE: update msgLabels below if this is changed. - public const short Highest = 47; - - static internal string[] msgLabels = - { - "none", - "ObjectDestroy", - "Rpc", - "ObjectSpawn", - "Owner", - "Command", - "LocalPlayerTransform", - "SyncEvent", - "UpdateVars", - "SyncList", - "ObjectSpawnScene", // 10 - "NetworkInfo", - "SpawnFinished", - "ObjectHide", - "CRC", - "LocalClientAuthority", - "LocalChildTransform", - "Fragment", - "PeerClientAuthority", - "", - "", // 20 - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", // 30 - "", // - SystemInternalHighest - "Connect", // 32, - "Disconnect", - "Error", - "Ready", - "NotReady", - "AddPlayer", - "RemovePlayer", - "Scene", - "Animation", // 40 - "AnimationParams", - "AnimationTrigger", - "LobbyReadyToBegin", - "LobbySceneLoaded", - "LobbyAddPlayerFailed", // 45 - "LobbyReturnToLobby", // 46 - }; - - static public string MsgTypeToString(short value) - { - if (value < 0 || value > Highest) - { - return String.Empty; - } - string result = msgLabels[value]; - if (string.IsNullOrEmpty(result)) - { - result = "[" + value + "]"; - } - return result; - } + Highest = 47 } public class NetworkMessage