diff --git a/Assets/Mirror/Examples/Chat/Scripts/ChatNetworkManager.cs b/Assets/Mirror/Examples/Chat/Scripts/ChatNetworkManager.cs index 2136fbb38..6c1357e47 100644 --- a/Assets/Mirror/Examples/Chat/Scripts/ChatNetworkManager.cs +++ b/Assets/Mirror/Examples/Chat/Scripts/ChatNetworkManager.cs @@ -14,7 +14,7 @@ public void SetHostname(string hostname) public ChatWindow chatWindow; - public class CreatePlayerMessage : NetworkMessage + public struct CreatePlayerMessage : NetworkMessage { public string name; diff --git a/Assets/Mirror/Runtime/MessagePacker.cs b/Assets/Mirror/Runtime/MessagePacker.cs index a34711fe9..e749e48ed 100644 --- a/Assets/Mirror/Runtime/MessagePacker.cs +++ b/Assets/Mirror/Runtime/MessagePacker.cs @@ -16,7 +16,8 @@ namespace Mirror // (probably even shorter) public static class MessagePacker { - public static int GetId() where T : NetworkMessage + public static int GetId() + where T : struct, NetworkMessage { return GetId(typeof(T)); } @@ -32,13 +33,14 @@ public static int GetId(Type type) // pack message before sending // -> NetworkWriter passed as arg so that we can use .ToArraySegment // and do an allocation free send before recycling it. - public static void Pack(T message, NetworkWriter writer) where T : NetworkMessage + public static void Pack(T message, NetworkWriter writer) + where T : struct, NetworkMessage { // if it is a value type, just use typeof(T) to avoid boxing // this works because value types cannot be derived // if it is a reference type (for example IMessageBase), // ask the message for the real type - int msgType = GetId(default(T) != null ? typeof(T) : message.GetType()); + int msgType = GetId(typeof(T)); writer.WriteUInt16((ushort)msgType); // serialize message into writer @@ -46,7 +48,8 @@ public static void Pack(T message, NetworkWriter writer) where T : NetworkMes } // unpack a message we received - public static T Unpack(byte[] data) where T : NetworkMessage, new() + public static T Unpack(byte[] data) + where T : struct, NetworkMessage { using (PooledNetworkReader networkReader = NetworkReaderPool.GetReader(data)) { @@ -83,7 +86,7 @@ public static bool UnpackMessage(NetworkReader messageReader, out int msgType) } internal static NetworkMessageDelegate MessageHandler(Action handler, bool requireAuthenication) - where T : NetworkMessage, new() + where T : struct, NetworkMessage where C : NetworkConnection => (conn, reader, channelId) => { @@ -112,7 +115,7 @@ internal static NetworkMessageDelegate MessageHandler(Action handler // if it is a value type, just use defult(T) // otherwise allocate a new instance - message = default(T) != null ? default(T) : new T(); + message = default(T); message.Deserialize(reader); } catch (Exception exception) diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index 6c423f0e3..457d4065a 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -172,7 +172,8 @@ static void RemoveTransportHandlers() /// /// /// True if message was sent. - public static bool Send(T message, int channelId = Channels.DefaultReliable) where T : NetworkMessage + public static bool Send(T message, int channelId = Channels.DefaultReliable) + where T : struct, NetworkMessage { if (connection != null) { @@ -215,7 +216,8 @@ internal static void RegisterSystemHandlers() /// Message type /// Function handler which will be invoked when this message type is received. /// True if the message requires an authenticated connection - public static void RegisterHandler(Action handler, bool requireAuthentication = true) where T : NetworkMessage, new() + public static void RegisterHandler(Action handler, bool requireAuthentication = true) + where T : struct, NetworkMessage { int msgType = MessagePacker.GetId(); if (handlers.ContainsKey(msgType)) @@ -232,7 +234,8 @@ internal static void RegisterSystemHandlers() /// Message type /// Function handler which will be invoked when this message type is received. /// True if the message requires an authenticated connection - public static void RegisterHandler(Action handler, bool requireAuthentication = true) where T : NetworkMessage, new() + public static void RegisterHandler(Action handler, bool requireAuthentication = true) + where T : struct, NetworkMessage { RegisterHandler((NetworkConnection _, T value) => { handler(value); }, requireAuthentication); } @@ -244,7 +247,8 @@ internal static void RegisterSystemHandlers() /// Message type /// Function handler which will be invoked when this message type is received. /// True if the message requires an authenticated connection - public static void ReplaceHandler(Action handler, bool requireAuthentication = true) where T : NetworkMessage, new() + public static void ReplaceHandler(Action handler, bool requireAuthentication = true) + where T : struct, NetworkMessage { int msgType = MessagePacker.GetId(); handlers[msgType] = MessagePacker.MessageHandler(handler, requireAuthentication); @@ -257,7 +261,8 @@ internal static void RegisterSystemHandlers() /// Message type /// Function handler which will be invoked when this message type is received. /// True if the message requires an authenticated connection - public static void ReplaceHandler(Action handler, bool requireAuthentication = true) where T : NetworkMessage, new() + public static void ReplaceHandler(Action handler, bool requireAuthentication = true) + where T : struct, NetworkMessage { ReplaceHandler((NetworkConnection _, T value) => { handler(value); }, requireAuthentication); } @@ -266,7 +271,8 @@ internal static void RegisterSystemHandlers() /// Unregisters a network message handler. /// /// The message type to unregister. - public static bool UnregisterHandler() where T : NetworkMessage + public static bool UnregisterHandler() + where T : struct, NetworkMessage { // use int to minimize collisions int msgType = MessagePacker.GetId(); diff --git a/Assets/Mirror/Runtime/NetworkConnection.cs b/Assets/Mirror/Runtime/NetworkConnection.cs index e8c59fc08..c08b4cb0e 100644 --- a/Assets/Mirror/Runtime/NetworkConnection.cs +++ b/Assets/Mirror/Runtime/NetworkConnection.cs @@ -132,7 +132,8 @@ internal void SetHandlers(Dictionary handlers) /// The message to send. /// The transport layer channel to send on. /// - public bool Send(T msg, int channelId = Channels.DefaultReliable) where T : NetworkMessage + public bool Send(T msg, int channelId = Channels.DefaultReliable) + where T : struct, NetworkMessage { using (PooledNetworkWriter writer = NetworkWriterPool.GetWriter()) { @@ -221,7 +222,8 @@ internal bool InvokeHandler(int msgType, NetworkReader reader, int channelId) /// The message type to unregister. /// The message object to process. /// Returns true if the handler was successfully invoked - public bool InvokeHandler(T msg, int channelId) where T : NetworkMessage + public bool InvokeHandler(T msg, int channelId) + where T : struct, NetworkMessage { using (PooledNetworkWriter writer = NetworkWriterPool.GetWriter()) { @@ -229,7 +231,7 @@ public bool InvokeHandler(T msg, int channelId) where T : NetworkMessage // this works because value types cannot be derived // if it is a reference type (for example IMessageBase), // ask the message for the real type - int msgType = MessagePacker.GetId(default(T) != null ? typeof(T) : msg.GetType()); + int msgType = MessagePacker.GetId(typeof(T)); MessagePacker.Pack(msg, writer); ArraySegment segment = writer.ToArraySegment(); diff --git a/Assets/Mirror/Runtime/NetworkServer.cs b/Assets/Mirror/Runtime/NetworkServer.cs index f30f80ba2..1f413640e 100644 --- a/Assets/Mirror/Runtime/NetworkServer.cs +++ b/Assets/Mirror/Runtime/NetworkServer.cs @@ -199,7 +199,8 @@ public static bool RemoveConnection(int connectionId) /// /// /// - static void SendToObservers(NetworkIdentity identity, T msg, int channelId = Channels.DefaultReliable) where T : NetworkMessage + static void SendToObservers(NetworkIdentity identity, T msg, int channelId = Channels.DefaultReliable) + where T : struct, NetworkMessage { // Debug.Log("Server.SendToObservers id:" + typeof(T)); @@ -238,7 +239,8 @@ static void SendToObservers(NetworkIdentity identity, T msg, int channelId = /// Transport channel to use /// Indicates if only ready clients should receive the message /// - public static bool SendToAll(T msg, int channelId = Channels.DefaultReliable, bool sendToReadyOnly = false) where T : NetworkMessage + public static bool SendToAll(T msg, int channelId = Channels.DefaultReliable, bool sendToReadyOnly = false) + where T : struct, NetworkMessage { if (!active) { @@ -285,7 +287,8 @@ public static bool SendToAll(T msg, int channelId = Channels.DefaultReliable, /// Message /// Transport channel to use /// - public static bool SendToReady(T msg, int channelId = Channels.DefaultReliable) where T : NetworkMessage + public static bool SendToReady(T msg, int channelId = Channels.DefaultReliable) + where T : struct, NetworkMessage { if (!active) { @@ -306,7 +309,8 @@ public static bool SendToReady(T msg, int channelId = Channels.DefaultReliabl /// Should the owner of the object be included /// Transport channel to use /// - public static bool SendToReady(NetworkIdentity identity, T msg, bool includeOwner = true, int channelId = Channels.DefaultReliable) where T : NetworkMessage + public static bool SendToReady(NetworkIdentity identity, T msg, bool includeOwner = true, int channelId = Channels.DefaultReliable) + where T : struct, NetworkMessage { // Debug.Log("Server.SendToReady msgType:" + typeof(T)); @@ -352,7 +356,8 @@ public static bool SendToReady(NetworkIdentity identity, T msg, bool includeO /// Message /// Transport channel to use /// - public static bool SendToReady(NetworkIdentity identity, T msg, int channelId) where T : NetworkMessage + public static bool SendToReady(NetworkIdentity identity, T msg, int channelId) + where T : struct, NetworkMessage { return SendToReady(identity, msg, true, channelId); } @@ -525,7 +530,8 @@ static void OnError(int connectionId, Exception exception) /// Message type /// Function handler which will be invoked when this message type is received. /// True if the message requires an authenticated connection - public static void RegisterHandler(Action handler, bool requireAuthentication = true) where T : NetworkMessage, new() + public static void RegisterHandler(Action handler, bool requireAuthentication = true) + where T : struct, NetworkMessage { int msgType = MessagePacker.GetId(); if (handlers.ContainsKey(msgType)) @@ -542,7 +548,8 @@ static void OnError(int connectionId, Exception exception) /// Message type /// Function handler which will be invoked when this message type is received. /// True if the message requires an authenticated connection - public static void RegisterHandler(Action handler, bool requireAuthentication = true) where T : NetworkMessage, new() + public static void RegisterHandler(Action handler, bool requireAuthentication = true) + where T : struct, NetworkMessage { RegisterHandler((_, value) => { handler(value); }, requireAuthentication); } @@ -554,7 +561,8 @@ static void OnError(int connectionId, Exception exception) /// Message type /// Function handler which will be invoked when this message type is received. /// True if the message requires an authenticated connection - public static void ReplaceHandler(Action handler, bool requireAuthentication = true) where T : NetworkMessage, new() + public static void ReplaceHandler(Action handler, bool requireAuthentication = true) + where T : struct, NetworkMessage { int msgType = MessagePacker.GetId(); handlers[msgType] = MessagePacker.MessageHandler(handler, requireAuthentication); @@ -567,7 +575,8 @@ static void OnError(int connectionId, Exception exception) /// Message type /// Function handler which will be invoked when this message type is received. /// True if the message requires an authenticated connection - public static void ReplaceHandler(Action handler, bool requireAuthentication = true) where T : NetworkMessage, new() + public static void ReplaceHandler(Action handler, bool requireAuthentication = true) + where T : struct, NetworkMessage { ReplaceHandler((_, value) => { handler(value); }, requireAuthentication); } @@ -576,7 +585,8 @@ static void OnError(int connectionId, Exception exception) /// Unregisters a handler for a particular message type. /// /// Message type - public static void UnregisterHandler() where T : NetworkMessage + public static void UnregisterHandler() + where T : struct, NetworkMessage { int msgType = MessagePacker.GetId(); handlers.Remove(msgType); @@ -596,7 +606,8 @@ public static void ClearHandlers() /// Message type /// /// - public static void SendToClientOfPlayer(NetworkIdentity identity, T msg, int channelId = Channels.DefaultReliable) where T : NetworkMessage + public static void SendToClientOfPlayer(NetworkIdentity identity, T msg, int channelId = Channels.DefaultReliable) + where T : struct, NetworkMessage { if (identity != null) {