Added targeted object targeted messages

This commit is contained in:
Albin Corén 2018-01-10 08:23:54 +01:00
parent 3dfc999c38
commit dc8adf0de8
3 changed files with 164 additions and 31 deletions

View File

@ -68,14 +68,14 @@ namespace MLAPI
protected int RegisterMessageHandler(string name, Action<int, byte[]> action)
{
int counter = MessageManager.AddIncomingMessageHandler(name, action);
int counter = MessageManager.AddIncomingMessageHandler(name, action, networkId);
registeredMessageHandlers.Add(name, counter);
return counter;
}
protected void DeregisterMessageHandler(string name, int counter)
{
MessageManager.RemoveIncomingMessageHandler(name, counter);
MessageManager.RemoveIncomingMessageHandler(name, counter, networkId);
}
private void OnDestroy()
@ -98,6 +98,18 @@ namespace MLAPI
}
}
protected void SendToServerTarget(string messageType, string channelName, byte[] data)
{
if (isServer)
{
MessageManager.InvokeTargetedMessageHandler(messageType, data, -1, networkId);
}
else
{
NetworkingManager.singleton.Send(NetworkingManager.singleton.serverClientId, messageType, channelName, data, networkId);
}
}
protected void SendToLocalClient(string messageType, string channelName, byte[] data)
{
if (!isServer)
@ -108,6 +120,16 @@ namespace MLAPI
NetworkingManager.singleton.Send(ownerClientId, messageType, channelName, data);
}
protected void SendToLocalClientTarget(string messageType, string channelName, byte[] data)
{
if (!isServer)
{
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
return;
}
NetworkingManager.singleton.Send(ownerClientId, messageType, channelName, data, networkId);
}
protected void SendToNonLocalClients(string messageType, string channelName, byte[] data)
{
if (!isServer)
@ -118,6 +140,16 @@ namespace MLAPI
NetworkingManager.singleton.Send(messageType, channelName, data, ownerClientId);
}
protected void SendToNonLocalClientsTarget(string messageType, string channelName, byte[] data)
{
if (!isServer)
{
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
return;
}
NetworkingManager.singleton.Send(messageType, channelName, data, ownerClientId, networkId);
}
protected void SendToClient(int clientId, string messageType, string channelName, byte[] data)
{
if (!isServer)
@ -128,6 +160,16 @@ namespace MLAPI
NetworkingManager.singleton.Send(clientId, messageType, channelName, data);
}
protected void SendToClientTarget(int clientId, string messageType, string channelName, byte[] data)
{
if (!isServer)
{
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
return;
}
NetworkingManager.singleton.Send(clientId, messageType, channelName, data, networkId);
}
protected void SendToClients(int[] clientIds, string messageType, string channelName, byte[] data)
{
if (!isServer)
@ -138,6 +180,16 @@ namespace MLAPI
NetworkingManager.singleton.Send(clientIds, messageType, channelName, data);
}
protected void SendToClientsTarget(int[] clientIds, string messageType, string channelName, byte[] data)
{
if (!isServer)
{
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
return;
}
NetworkingManager.singleton.Send(clientIds, messageType, channelName, data, networkId);
}
protected void SendToClients(List<int> clientIds, string messageType, string channelName, byte[] data)
{
if (!isServer)
@ -148,6 +200,16 @@ namespace MLAPI
NetworkingManager.singleton.Send(clientIds, messageType, channelName, data);
}
protected void SendToClientsTarget(List<int> clientIds, string messageType, string channelName, byte[] data)
{
if (!isServer)
{
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
return;
}
NetworkingManager.singleton.Send(clientIds, messageType, channelName, data, networkId);
}
protected void SendToClients(string messageType, string channelName, byte[] data)
{
if (!isServer)
@ -158,6 +220,16 @@ namespace MLAPI
NetworkingManager.singleton.Send(messageType, channelName, data);
}
protected void SendToClientsTarget(string messageType, string channelName, byte[] data)
{
if (!isServer)
{
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
return;
}
NetworkingManager.singleton.Send(messageType, channelName, data, networkId);
}
protected NetworkedObject GetNetworkedObject(uint networkId)
{
return SpawnManager.spawnedObjects[networkId];

View File

@ -66,6 +66,7 @@ namespace MLAPI
MessageManager.messageCallbacks = new Dictionary<ushort, Dictionary<int, Action<int, byte[]>>>();
MessageManager.messageHandlerCounter = new Dictionary<ushort, int>();
MessageManager.releasedMessageHandlerCounters = new Dictionary<ushort, Stack<int>>();
MessageManager.targetedMessages = new Dictionary<ushort, Dictionary<uint, List<int>>>();
SpawnManager.spawnedObjects = new Dictionary<uint, NetworkedObject>();
SpawnManager.releasedNetworkObjectIds = new Stack<uint>();
if (NetworkConfig.HandleObjectSpawning)
@ -319,6 +320,10 @@ namespace MLAPI
using (BinaryReader reader = new BinaryReader(readStream))
{
ushort messageType = reader.ReadUInt16();
bool targeted = reader.ReadBoolean();
uint targetNetworkId = 0;
if(targeted)
targetNetworkId = reader.ReadUInt32();
//Client tried to send a network message that was not the connection request before he was accepted.
if (isServer && pendingClients.Contains(clientId) && messageType != 0)
@ -332,10 +337,22 @@ namespace MLAPI
if (messageType >= 32)
{
//Custom message, invoke all message handlers
foreach (KeyValuePair<int, Action<int, byte[]>> pair in MessageManager.messageCallbacks[messageType])
if(targeted)
{
pair.Value(clientId, incommingData);
}
List<int> handlerIds = MessageManager.targetedMessages[messageType][targetNetworkId];
Debug.Log(handlerIds.Count);
for (int i = 0; i < handlerIds.Count; i++)
{
MessageManager.messageCallbacks[messageType][handlerIds[i]](clientId, incommingData);
}
}
else
{
foreach (KeyValuePair<int, Action<int, byte[]>> pair in MessageManager.messageCallbacks[messageType])
{
pair.Value(clientId, incommingData);
}
}
}
else
{
@ -478,12 +495,15 @@ namespace MLAPI
}
}
internal void Send(int clientId, string messageType, string channelName, byte[] data)
internal void Send(int clientId, string messageType, string channelName, byte[] data, uint? networkId = null)
{
if(isHost && clientId == -1)
{
//Host trying to send data to it's own client
MessageManager.InvokeMessageHandlers(messageType, data, clientId);
if (networkId == null)
MessageManager.InvokeMessageHandlers(messageType, data, clientId);
else
MessageManager.InvokeTargetedMessageHandler(messageType, data, clientId, networkId.Value);
return;
}
else if(clientId == -1)
@ -496,28 +516,30 @@ namespace MLAPI
using (BinaryWriter writer = new BinaryWriter(stream))
{
writer.Write(MessageManager.messageTypes[messageType]);
writer.Write(networkId != null);
if (networkId != null)
writer.Write(networkId.Value);
writer.Write((ushort)data.Length);
writer.Write(data);
}
//2 bytes for message type and 2 bytes for byte length
int size = data.Length + 4;
byte[] dataToSend = stream.ToArray();
NetworkTransport.Send(hostId, clientId, MessageManager.channels[channelName], dataToSend, dataToSend.Length, out error);
}
}
internal void Send(int[] clientIds, string messageType, string channelName, byte[] data)
internal void Send(int[] clientIds, string messageType, string channelName, byte[] data, uint? networkId = null)
{
using (MemoryStream stream = new MemoryStream())
{
using (BinaryWriter writer = new BinaryWriter(stream))
{
writer.Write(MessageManager.messageTypes[messageType]);
writer.Write(networkId != null);
if (networkId != null)
writer.Write(networkId.Value);
writer.Write((ushort)data.Length);
writer.Write(data);
}
//2 bytes for message type and 2 bytes for byte length
int size = data.Length + 4;
byte[] dataToSend = stream.ToArray();
int channel = MessageManager.channels[channelName];
for (int i = 0; i < clientIds.Length; i++)
@ -525,7 +547,10 @@ namespace MLAPI
int clientId = clientIds[i];
if (isHost && clientId == -1)
{
MessageManager.InvokeMessageHandlers(messageType, data, clientId);
if (networkId == null)
MessageManager.InvokeMessageHandlers(messageType, data, clientId);
else
MessageManager.InvokeTargetedMessageHandler(messageType, data, clientId, networkId.Value);
continue;
}
else if (clientId == -1)
@ -533,23 +558,24 @@ namespace MLAPI
//Client trying to send data to host
clientId = serverClientId;
}
NetworkTransport.Send(hostId, clientId, channel, dataToSend, size, out error);
NetworkTransport.Send(hostId, clientId, channel, dataToSend, dataToSend.Length, out error);
}
}
}
internal void Send(List<int> clientIds, string messageType, string channelName, byte[] data)
internal void Send(List<int> clientIds, string messageType, string channelName, byte[] data, uint? networkId = null)
{
using (MemoryStream stream = new MemoryStream())
{
using (BinaryWriter writer = new BinaryWriter(stream))
{
writer.Write(MessageManager.messageTypes[messageType]);
writer.Write(networkId != null);
if (networkId != null)
writer.Write(networkId.Value);
writer.Write((ushort)data.Length);
writer.Write(data);
}
//2 bytes for message type and 2 bytes for byte length
int size = data.Length + 4;
byte[] dataToSend = stream.ToArray();
int channel = MessageManager.channels[channelName];
for (int i = 0; i < clientIds.Count; i++)
@ -557,7 +583,10 @@ namespace MLAPI
int clientId = clientIds[i];
if (isHost && clientId == -1)
{
MessageManager.InvokeMessageHandlers(messageType, data, clientId);
if (networkId == null)
MessageManager.InvokeMessageHandlers(messageType, data, clientId);
else
MessageManager.InvokeTargetedMessageHandler(messageType, data, clientId, networkId.Value);
continue;
}
else if (clientId == -1)
@ -565,23 +594,24 @@ namespace MLAPI
//Client trying to send data to host
clientId = serverClientId;
}
NetworkTransport.Send(hostId, clientId, channel, dataToSend, size, out error);
NetworkTransport.Send(hostId, clientId, channel, dataToSend, dataToSend.Length, out error);
}
}
}
internal void Send(string messageType, string channelName, byte[] data)
internal void Send(string messageType, string channelName, byte[] data, uint? networkId = null)
{
using (MemoryStream stream = new MemoryStream())
{
using (BinaryWriter writer = new BinaryWriter(stream))
{
writer.Write(MessageManager.messageTypes[messageType]);
writer.Write(networkId != null);
if (networkId != null)
writer.Write(networkId.Value);
writer.Write((ushort)data.Length);
writer.Write(data);
}
//2 bytes for message type and 2 bytes for byte length
int size = data.Length + 4;
byte[] dataToSend = stream.ToArray();
int channel = MessageManager.channels[channelName];
foreach (KeyValuePair<int, NetworkedClient> pair in connectedClients)
@ -589,7 +619,10 @@ namespace MLAPI
int clientId = pair.Key;
if(isHost && pair.Key == -1)
{
MessageManager.InvokeMessageHandlers(messageType, data, pair.Key);
if (networkId == null)
MessageManager.InvokeMessageHandlers(messageType, data, clientId);
else
MessageManager.InvokeTargetedMessageHandler(messageType, data, clientId, networkId.Value);
continue;
}
else if (clientId == -1)
@ -597,24 +630,25 @@ namespace MLAPI
//Client trying to send data to host
clientId = serverClientId;
}
NetworkTransport.Send(hostId, clientId, channel, dataToSend, size, out error);
NetworkTransport.Send(hostId, clientId, channel, dataToSend, dataToSend.Length, out error);
}
}
}
internal void Send(string messageType, string channelName, byte[] data, int clientIdToIgnore)
internal void Send(string messageType, string channelName, byte[] data, int clientIdToIgnore, uint? networkId = null)
{
using (MemoryStream stream = new MemoryStream())
{
using (BinaryWriter writer = new BinaryWriter(stream))
{
writer.Write(MessageManager.messageTypes[messageType]);
writer.Write(networkId != null);
if (networkId != null)
writer.Write(networkId.Value);
writer.Write((ushort)data.Length);
writer.Write(data);
}
//2 bytes for message type and 2 bytes for byte length
int size = data.Length + 4;
byte[] dataToSend = stream.ToArray();
int channel = MessageManager.channels[channelName];
foreach (KeyValuePair<int, NetworkedClient> pair in connectedClients)
@ -624,7 +658,10 @@ namespace MLAPI
int clientId = pair.Key;
if (isHost && pair.Key == -1)
{
MessageManager.InvokeMessageHandlers(messageType, data, clientId);
if (networkId == null)
MessageManager.InvokeMessageHandlers(messageType, data, clientId);
else
MessageManager.InvokeTargetedMessageHandler(messageType, data, clientId, networkId.Value);
continue;
}
else if (clientId == -1)
@ -632,7 +669,7 @@ namespace MLAPI
//Client trying to send data to host
clientId = serverClientId;
}
NetworkTransport.Send(hostId, clientId, channel, dataToSend, size, out error);
NetworkTransport.Send(hostId, clientId, channel, dataToSend, dataToSend.Length, out error);
}
}
}

View File

@ -11,6 +11,8 @@ namespace MLAPI.NetworkingManagerComponents
internal static Dictionary<ushort, Dictionary<int, Action<int, byte[]>>> messageCallbacks;
internal static Dictionary<ushort, int> messageHandlerCounter;
internal static Dictionary<ushort, Stack<int>> releasedMessageHandlerCounters;
//Key: messageType, Value key: networkId, value value: handlerId
internal static Dictionary<ushort, Dictionary<uint, List<int>>> targetedMessages;
private static NetworkingManager netManager
{
@ -31,10 +33,29 @@ namespace MLAPI.NetworkingManagerComponents
}
}
internal static int AddIncomingMessageHandler(string name, Action<int, byte[]> action)
internal static void InvokeTargetedMessageHandler(string messageType, byte[] data, int clientId, uint networkId)
{
if (!messageTypes.ContainsKey(messageType) || !messageCallbacks.ContainsKey(messageTypes[messageType]))
return;
List<int> handlerIds = targetedMessages[messageTypes[messageType]][networkId];
for (int i = 0; i < handlerIds.Count; i++)
{
messageCallbacks[messageTypes[messageType]][handlerIds[i]](clientId, data);
}
}
internal static int AddIncomingMessageHandler(string name, Action<int, byte[]> action, uint networkId)
{
if (messageTypes.ContainsKey(name))
{
if(!targetedMessages.ContainsKey(messageTypes[name]))
{
targetedMessages.Add(messageTypes[name], new Dictionary<uint, List<int>>());
}
if(!targetedMessages[messageTypes[name]].ContainsKey(networkId))
{
targetedMessages[messageTypes[name]].Add(networkId, new List<int>());
}
if (messageCallbacks.ContainsKey(messageTypes[name]))
{
int handlerId = 0;
@ -58,6 +79,7 @@ namespace MLAPI.NetworkingManagerComponents
messageHandlerCounter.Add(messageTypes[name], handlerId + 1);
}
messageCallbacks[messageTypes[name]].Add(handlerId, action);
targetedMessages[messageTypes[name]][networkId].Add(handlerId);
return handlerId;
}
else
@ -65,6 +87,7 @@ namespace MLAPI.NetworkingManagerComponents
messageCallbacks.Add(messageTypes[name], new Dictionary<int, Action<int, byte[]>>());
messageHandlerCounter.Add(messageTypes[name], 1);
messageCallbacks[messageTypes[name]].Add(0, action);
targetedMessages[messageTypes[name]][networkId].Add(0);
return 0;
}
}
@ -75,7 +98,7 @@ namespace MLAPI.NetworkingManagerComponents
}
}
internal static void RemoveIncomingMessageHandler(string name, int counter)
internal static void RemoveIncomingMessageHandler(string name, int counter, uint networkId)
{
if (counter == -1)
return;
@ -86,6 +109,7 @@ namespace MLAPI.NetworkingManagerComponents
if (!releasedMessageHandlerCounters.ContainsKey(messageTypes[name]))
releasedMessageHandlerCounters.Add(messageTypes[name], new Stack<int>());
releasedMessageHandlerCounters[messageTypes[name]].Push(counter);
targetedMessages[messageTypes[name]][networkId].Remove(counter);
}
}
}