|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using MLAPI.Configuration; |
| 5 | +using MLAPI.Logging; |
| 6 | +using MLAPI.Security; |
| 7 | +using MLAPI.Serialization; |
| 8 | +using MLAPI.Serialization.Pooled; |
| 9 | + |
| 10 | +namespace MLAPI.Messaging |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// The manager class to manage custom messages, note that this is different from the NetworkingManager custom messages. |
| 14 | + /// These are named and are much easier to use. |
| 15 | + /// </summary> |
| 16 | + public static class CustomMessagingManager |
| 17 | + { |
| 18 | + #region Unnamed |
| 19 | + /// <summary> |
| 20 | + /// Delegate used for incoming unnamed messages |
| 21 | + /// </summary> |
| 22 | + /// <param name="clientId">The clientId that sent the message</param> |
| 23 | + /// <param name="stream">The stream containing the message data</param> |
| 24 | + public delegate void UnnamedMessageDelegate(ulong clientId, Stream stream); |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Event invoked when unnamed messages arrive |
| 28 | + /// </summary> |
| 29 | + public static event UnnamedMessageDelegate OnUnnamedMessage; |
| 30 | + |
| 31 | + internal static void InvokeUnnamedMessage(ulong clientId, Stream stream) |
| 32 | + { |
| 33 | + if (OnUnnamedMessage != null) |
| 34 | + { |
| 35 | + OnUnnamedMessage(clientId, stream); |
| 36 | + } |
| 37 | + |
| 38 | + NetworkingManager.Singleton.InvokeOnIncomingCustomMessage(clientId, stream); |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Sends unnamed message to a list of clients |
| 44 | + /// </summary> |
| 45 | + /// <param name="clientIds">The clients to send to, sends to everyone if null</param> |
| 46 | + /// <param name="stream">The message stream containing the data</param> |
| 47 | + /// <param name="channel">The channel to send the data on</param> |
| 48 | + /// <param name="security">The security settings to apply to the message</param> |
| 49 | + public static void SendUnnamedMessage(List<ulong> clientIds, BitStream stream, string channel = null, SecuritySendFlags security = SecuritySendFlags.None) |
| 50 | + { |
| 51 | + if (!NetworkingManager.Singleton.IsServer) |
| 52 | + { |
| 53 | + if (LogHelper.CurrentLogLevel <= LogLevel.Error) LogHelper.LogWarning("Can not send unnamed messages to multiple users as a client"); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + if (clientIds == null) |
| 58 | + { |
| 59 | + for (int i = 0; i < NetworkingManager.Singleton.ConnectedClientsList.Count; i++) |
| 60 | + { |
| 61 | + InternalMessageSender.Send(NetworkingManager.Singleton.ConnectedClientsList[i].ClientId, MLAPIConstants.MLAPI_UNNAMED_MESSAGE, string.IsNullOrEmpty(channel) ? "MLAPI_DEFAULT_MESSAGE" : channel, stream, security, null); |
| 62 | + } |
| 63 | + } |
| 64 | + else |
| 65 | + { |
| 66 | + for (int i = 0; i < clientIds.Count; i++) |
| 67 | + { |
| 68 | + InternalMessageSender.Send(clientIds[i], MLAPIConstants.MLAPI_UNNAMED_MESSAGE, string.IsNullOrEmpty(channel) ? "MLAPI_DEFAULT_MESSAGE" : channel, stream, security, null); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Sends a unnamed message to a specific client |
| 75 | + /// </summary> |
| 76 | + /// <param name="clientId">The client to send the message to</param> |
| 77 | + /// <param name="stream">The message stream containing the data</param> |
| 78 | + /// <param name="channel">The channel tos end the data on</param> |
| 79 | + /// <param name="security">The security settings to apply to the message</param> |
| 80 | + public static void SendUnnamedMessage(ulong clientId, BitStream stream, string channel = null, SecuritySendFlags security = SecuritySendFlags.None) |
| 81 | + { |
| 82 | + InternalMessageSender.Send(clientId, MLAPIConstants.MLAPI_UNNAMED_MESSAGE, string.IsNullOrEmpty(channel) ? "MLAPI_DEFAULT_MESSAGE" : channel, stream, security, null); |
| 83 | + } |
| 84 | + #endregion |
| 85 | + #region Named |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// Delegate used to handle named messages |
| 89 | + /// </summary> |
| 90 | + public delegate void HandleNamedMessageDelegate(ulong sender, Stream payload); |
| 91 | + |
| 92 | + private static readonly Dictionary<ulong, HandleNamedMessageDelegate> namedMessageHandlers = new Dictionary<ulong, HandleNamedMessageDelegate>(); |
| 93 | + |
| 94 | + internal static void InvokeNamedMessage(ulong hash, ulong sender, Stream stream) |
| 95 | + { |
| 96 | + if (namedMessageHandlers.ContainsKey(hash)) |
| 97 | + { |
| 98 | + namedMessageHandlers[hash](sender, stream); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// Registers a named message handler delegate. |
| 104 | + /// </summary> |
| 105 | + /// <param name="name">Name of the message.</param> |
| 106 | + /// <param name="callback">The callback to run when a named message is received.</param> |
| 107 | + public static void RegisterNamedMessageHandler(string name, HandleNamedMessageDelegate callback) |
| 108 | + { |
| 109 | + ulong hash = NetworkedBehaviour.HashMethodName(name); |
| 110 | + |
| 111 | + namedMessageHandlers[hash] = callback; |
| 112 | + } |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// Sends a named message |
| 116 | + /// </summary> |
| 117 | + /// <param name="name">The message name to send</param> |
| 118 | + /// <param name="clientId">The client to send the message to</param> |
| 119 | + /// <param name="stream">The message stream containing the data</param> |
| 120 | + /// <param name="channel">The channel tos end the data on</param> |
| 121 | + /// <param name="security">The security settings to apply to the message</param> |
| 122 | + public static void SendNamedMessage(string name, ulong clientId, Stream stream, string channel = null, SecuritySendFlags security = SecuritySendFlags.None) |
| 123 | + { |
| 124 | + ulong hash = NetworkedBehaviour.HashMethodName(name); |
| 125 | + |
| 126 | + using (PooledBitStream messageStream = PooledBitStream.Get()) |
| 127 | + { |
| 128 | + using (PooledBitWriter writer = PooledBitWriter.Get(messageStream)) |
| 129 | + { |
| 130 | + writer.WriteUInt64Packed(hash); |
| 131 | + } |
| 132 | + |
| 133 | + messageStream.CopyFrom(stream); |
| 134 | + |
| 135 | + InternalMessageSender.Send(clientId, MLAPIConstants.MLAPI_NAMED_MESSAGE, string.IsNullOrEmpty(channel) ? "MLAPI_DEFAULT_MESSAGE" : channel, messageStream, security, null); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + /// <summary> |
| 140 | + /// Sends the named message |
| 141 | + /// </summary> |
| 142 | + /// <param name="name">The message name to send</param> |
| 143 | + /// <param name="clientIds">The clients to send to, sends to everyone if null</param> |
| 144 | + /// <param name="stream">The message stream containing the data</param> |
| 145 | + /// <param name="channel">The channel to send the data on</param> |
| 146 | + /// <param name="security">The security settings to apply to the message</param> |
| 147 | + public static void SendNamedMessage(string name, List<ulong> clientIds, Stream stream, string channel = null, SecuritySendFlags security = SecuritySendFlags.None) |
| 148 | + { |
| 149 | + ulong hash = NetworkedBehaviour.HashMethodName(name); |
| 150 | + |
| 151 | + using (PooledBitStream messageStream = PooledBitStream.Get()) |
| 152 | + { |
| 153 | + using (PooledBitWriter writer = PooledBitWriter.Get(messageStream)) |
| 154 | + { |
| 155 | + writer.WriteUInt64Packed(hash); |
| 156 | + } |
| 157 | + |
| 158 | + messageStream.CopyFrom(stream); |
| 159 | + |
| 160 | + if (!NetworkingManager.Singleton.IsServer) |
| 161 | + { |
| 162 | + if (LogHelper.CurrentLogLevel <= LogLevel.Error) LogHelper.LogWarning("Can not send named messages to multiple users as a client"); |
| 163 | + return; |
| 164 | + } |
| 165 | + if (clientIds == null) |
| 166 | + { |
| 167 | + for (int i = 0; i < NetworkingManager.Singleton.ConnectedClientsList.Count; i++) |
| 168 | + { |
| 169 | + InternalMessageSender.Send(NetworkingManager.Singleton.ConnectedClientsList[i].ClientId, MLAPIConstants.MLAPI_NAMED_MESSAGE, string.IsNullOrEmpty(channel) ? "MLAPI_DEFAULT_MESSAGE" : channel, messageStream, security, null); |
| 170 | + } |
| 171 | + } |
| 172 | + else |
| 173 | + { |
| 174 | + for (int i = 0; i < clientIds.Count; i++) |
| 175 | + { |
| 176 | + InternalMessageSender.Send(clientIds[i], MLAPIConstants.MLAPI_NAMED_MESSAGE, string.IsNullOrEmpty(channel) ? "MLAPI_DEFAULT_MESSAGE" : channel, messageStream, security, null); |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + #endregion |
| 182 | + } |
| 183 | +} |
0 commit comments