-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathCustomMessageManager.cs
More file actions
211 lines (187 loc) · 9.31 KB
/
CustomMessageManager.cs
File metadata and controls
211 lines (187 loc) · 9.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using System;
using System.Collections.Generic;
using System.IO;
using MLAPI.Configuration;
using MLAPI.Logging;
using MLAPI.Security;
using MLAPI.Serialization;
using MLAPI.Serialization.Pooled;
using MLAPI.Hashing;
namespace MLAPI.Messaging
{
/// <summary>
/// The manager class to manage custom messages, note that this is different from the NetworkingManager custom messages.
/// These are named and are much easier to use.
/// </summary>
public static class CustomMessagingManager
{
#region Unnamed
/// <summary>
/// Delegate used for incoming unnamed messages
/// </summary>
/// <param name="clientId">The clientId that sent the message</param>
/// <param name="stream">The stream containing the message data</param>
public delegate void UnnamedMessageDelegate(ulong clientId, Stream stream);
/// <summary>
/// Event invoked when unnamed messages arrive
/// </summary>
public static event UnnamedMessageDelegate OnUnnamedMessage;
internal static void InvokeUnnamedMessage(ulong clientId, Stream stream)
{
if (OnUnnamedMessage != null)
{
OnUnnamedMessage(clientId, stream);
}
NetworkingManager.Singleton.InvokeOnIncomingCustomMessage(clientId, stream);
}
/// <summary>
/// Sends unnamed message to a list of clients
/// </summary>
/// <param name="clientIds">The clients to send to, sends to everyone if null</param>
/// <param name="stream">The message stream containing the data</param>
/// <param name="channel">The channel to send the data on</param>
/// <param name="security">The security settings to apply to the message</param>
public static void SendUnnamedMessage(List<ulong> clientIds, BitStream stream, string channel = null, SecuritySendFlags security = SecuritySendFlags.None)
{
if (!NetworkingManager.Singleton.IsServer)
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Error) NetworkLog.LogWarning("Can not send unnamed messages to multiple users as a client");
return;
}
InternalMessageSender.Send(MLAPIConstants.MLAPI_UNNAMED_MESSAGE, string.IsNullOrEmpty(channel) ? "MLAPI_DEFAULT_MESSAGE" : channel, clientIds, stream, security);
}
/// <summary>
/// Sends a unnamed message to a specific client
/// </summary>
/// <param name="clientId">The client to send the message to</param>
/// <param name="stream">The message stream containing the data</param>
/// <param name="channel">The channel tos end the data on</param>
/// <param name="security">The security settings to apply to the message</param>
public static void SendUnnamedMessage(ulong clientId, BitStream stream, string channel = null, SecuritySendFlags security = SecuritySendFlags.None)
{
InternalMessageSender.Send(clientId, MLAPIConstants.MLAPI_UNNAMED_MESSAGE, string.IsNullOrEmpty(channel) ? "MLAPI_DEFAULT_MESSAGE" : channel, stream, security);
}
#endregion
#region Named
/// <summary>
/// Delegate used to handle named messages
/// </summary>
public delegate void HandleNamedMessageDelegate(ulong sender, Stream payload);
private static readonly Dictionary<ulong, HandleNamedMessageDelegate> namedMessageHandlers16 = new Dictionary<ulong, HandleNamedMessageDelegate>();
private static readonly Dictionary<ulong, HandleNamedMessageDelegate> namedMessageHandlers32 = new Dictionary<ulong, HandleNamedMessageDelegate>();
private static readonly Dictionary<ulong, HandleNamedMessageDelegate> namedMessageHandlers64 = new Dictionary<ulong, HandleNamedMessageDelegate>();
internal static void InvokeNamedMessage(ulong hash, ulong sender, Stream stream)
{
if (NetworkingManager.Singleton == null)
{
// We dont know what size to use. Try every (more collision prone)
if (namedMessageHandlers16.ContainsKey(hash))
{
namedMessageHandlers16[hash](sender, stream);
}
if (namedMessageHandlers32.ContainsKey(hash))
{
namedMessageHandlers32[hash](sender, stream);
}
if (namedMessageHandlers64.ContainsKey(hash))
{
namedMessageHandlers64[hash](sender, stream);
}
}
else
{
// Only check the right size.
if (NetworkingManager.Singleton.NetworkConfig.RpcHashSize == HashSize.VarIntTwoBytes)
{
if (namedMessageHandlers16.ContainsKey(hash))
{
namedMessageHandlers16[hash](sender, stream);
}
}
else if (NetworkingManager.Singleton.NetworkConfig.RpcHashSize == HashSize.VarIntFourBytes)
{
if (namedMessageHandlers32.ContainsKey(hash))
{
namedMessageHandlers32[hash](sender, stream);
}
}
else if (NetworkingManager.Singleton.NetworkConfig.RpcHashSize == HashSize.VarIntEightBytes)
{
if (namedMessageHandlers64.ContainsKey(hash))
{
namedMessageHandlers64[hash](sender, stream);
}
}
}
}
/// <summary>
/// Registers a named message handler delegate.
/// </summary>
/// <param name="name">Name of the message.</param>
/// <param name="callback">The callback to run when a named message is received.</param>
public static void RegisterNamedMessageHandler(string name, HandleNamedMessageDelegate callback)
{
namedMessageHandlers16[name.GetStableHash16()] = callback;
namedMessageHandlers32[name.GetStableHash32()] = callback;
namedMessageHandlers64[name.GetStableHash64()] = callback;
}
/// <summary>
/// Unregisters a named message handler.
/// </summary>
/// <param name="name">The name of the message.</param>
public static void UnregisterNamedMessageHandler(string name)
{
namedMessageHandlers16.Remove(name.GetStableHash16());
namedMessageHandlers32.Remove(name.GetStableHash32());
namedMessageHandlers64.Remove(name.GetStableHash64());
}
/// <summary>
/// Sends a named message
/// </summary>
/// <param name="name">The message name to send</param>
/// <param name="clientId">The client to send the message to</param>
/// <param name="stream">The message stream containing the data</param>
/// <param name="channel">The channel to send the data on</param>
/// <param name="security">The security settings to apply to the message</param>
public static void SendNamedMessage(string name, ulong clientId, Stream stream, string channel = null, SecuritySendFlags security = SecuritySendFlags.None)
{
ulong hash = NetworkedBehaviour.HashMethodName(name);
using (PooledBitStream messageStream = PooledBitStream.Get())
{
using (PooledBitWriter writer = PooledBitWriter.Get(messageStream))
{
writer.WriteUInt64Packed(hash);
}
messageStream.CopyFrom(stream);
InternalMessageSender.Send(clientId, MLAPIConstants.MLAPI_NAMED_MESSAGE, string.IsNullOrEmpty(channel) ? "MLAPI_DEFAULT_MESSAGE" : channel, messageStream, security);
}
}
/// <summary>
/// Sends the named message
/// </summary>
/// <param name="name">The message name to send</param>
/// <param name="clientIds">The clients to send to, sends to everyone if null</param>
/// <param name="stream">The message stream containing the data</param>
/// <param name="channel">The channel to send the data on</param>
/// <param name="security">The security settings to apply to the message</param>
public static void SendNamedMessage(string name, List<ulong> clientIds, Stream stream, string channel = null, SecuritySendFlags security = SecuritySendFlags.None)
{
ulong hash = NetworkedBehaviour.HashMethodName(name);
using (PooledBitStream messageStream = PooledBitStream.Get())
{
using (PooledBitWriter writer = PooledBitWriter.Get(messageStream))
{
writer.WriteUInt64Packed(hash);
}
messageStream.CopyFrom(stream);
if (!NetworkingManager.Singleton.IsServer)
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Error) NetworkLog.LogWarning("Can not send named messages to multiple users as a client");
return;
}
InternalMessageSender.Send(MLAPIConstants.MLAPI_NAMED_MESSAGE, string.IsNullOrEmpty(channel) ? "MLAPI_DEFAULT_MESSAGE" : channel, clientIds, messageStream, security);
}
}
#endregion
}
}