Added per NetworkBehaviour message Targets

This commit is contained in:
Albin Corén 2018-03-29 05:55:15 +02:00
parent 368d719201
commit e41ced9e8f
6 changed files with 111 additions and 41 deletions

View File

@ -104,16 +104,40 @@ namespace MLAPI
}
protected int RegisterMessageHandler(string name, Action<int, byte[]> action)
protected void RegisterMessageHandler(string name, Action<int, byte[]> action)
{
if (!MessageManager.messageTypes.ContainsKey(name))
{
Debug.LogWarning("MLAPI: The messageType " + name + " is not registered");
return;
}
ushort messageType = MessageManager.messageTypes[name];
ushort behaviourOrder = networkedObject.GetOrderIndex(this);
if (!networkedObject.targetMessageActions.ContainsKey(behaviourOrder))
networkedObject.targetMessageActions.Add(behaviourOrder, new Dictionary<ushort, Action<int, byte[]>>());
if (networkedObject.targetMessageActions[behaviourOrder].ContainsKey(messageType))
{
Debug.LogWarning("MLAPI: Each NetworkedBehaviour can only register one callback per instance per message type");
return;
}
networkedObject.targetMessageActions[behaviourOrder].Add(messageType, 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, networkId);
ushort messageType = MessageManager.messageTypes[name];
ushort behaviourOrder = networkedObject.GetOrderIndex(this);
if (networkedObject.targetMessageActions.ContainsKey(behaviourOrder) &&
networkedObject.targetMessageActions[behaviourOrder].ContainsKey(messageType))
{
networkedObject.targetMessageActions[behaviourOrder].Remove(messageType);
}
}
private void OnDisable()
@ -619,7 +643,7 @@ namespace MLAPI
Debug.LogWarning("MLAPI: Server can not send messages to server.");
return;
}
NetworkingManager.singleton.Send(NetworkingManager.singleton.serverClientId, messageType, channelName, data, networkId);
NetworkingManager.singleton.Send(NetworkingManager.singleton.serverClientId, messageType, channelName, data, networkId, networkedObject.GetOrderIndex(this));
}
protected void SendToLocalClient(string messageType, string channelName, byte[] data)
@ -649,7 +673,7 @@ namespace MLAPI
Debug.LogWarning("MLAPI: Invalid Passthrough send. Ensure AllowPassthroughMessages are turned on and that the MessageType " + messageType + " is registered as a passthroughMessageType");
return;
}
NetworkingManager.singleton.Send(ownerClientId, messageType, channelName, data, networkId);
NetworkingManager.singleton.Send(ownerClientId, messageType, channelName, data, networkId, networkedObject.GetOrderIndex(this));
}
protected void SendToNonLocalClients(string messageType, string channelName, byte[] data)
@ -664,7 +688,7 @@ namespace MLAPI
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
return;
}
NetworkingManager.singleton.Send(messageType, channelName, data, ownerClientId, null);
NetworkingManager.singleton.Send(messageType, channelName, data, ownerClientId);
}
protected void SendToNonLocalClientsTarget(string messageType, string channelName, byte[] data)
@ -679,7 +703,7 @@ namespace MLAPI
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
return;
}
NetworkingManager.singleton.Send(messageType, channelName, data, ownerClientId, networkId);
NetworkingManager.singleton.Send(messageType, channelName, data, ownerClientId, networkId, networkedObject.GetOrderIndex(this));
}
protected void SendToClient(int clientId, string messageType, string channelName, byte[] data)
@ -709,7 +733,7 @@ namespace MLAPI
Debug.LogWarning("MLAPI: Invalid Passthrough send. Ensure AllowPassthroughMessages are turned on and that the MessageType " + messageType + " is registered as a passthroughMessageType");
return;
}
NetworkingManager.singleton.Send(clientId, messageType, channelName, data, networkId);
NetworkingManager.singleton.Send(clientId, messageType, channelName, data, networkId, networkedObject.GetOrderIndex(this));
}
protected void SendToClients(int[] clientIds, string messageType, string channelName, byte[] data)
@ -739,7 +763,7 @@ namespace MLAPI
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
return;
}
NetworkingManager.singleton.Send(clientIds, messageType, channelName, data, networkId);
NetworkingManager.singleton.Send(clientIds, messageType, channelName, data, networkId, networkedObject.GetOrderIndex(this));
}
protected void SendToClients(List<int> clientIds, string messageType, string channelName, byte[] data)
@ -769,7 +793,7 @@ namespace MLAPI
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
return;
}
NetworkingManager.singleton.Send(clientIds, messageType, channelName, data, networkId);
NetworkingManager.singleton.Send(clientIds, messageType, channelName, data, networkId, networkedObject.GetOrderIndex(this));
}
protected void SendToClients(string messageType, string channelName, byte[] data)
@ -799,7 +823,7 @@ namespace MLAPI
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
return;
}
NetworkingManager.singleton.Send(messageType, channelName, data, networkId);
NetworkingManager.singleton.Send(messageType, channelName, data, networkId, networkedObject.GetOrderIndex(this));
}
protected NetworkedObject GetNetworkedObject(uint networkId)

View File

@ -1,4 +1,5 @@
using MLAPI.NetworkingManagerComponents;
using System;
using System.Collections.Generic;
using UnityEngine;
@ -143,5 +144,8 @@ namespace MLAPI
//TODO index out of bounds
return childNetworkedBehaviours[index];
}
//Key: behaviourOrderId, value key: messageType, value value callback
internal Dictionary<ushort, Dictionary<ushort, Action<int, byte[]>>> targetMessageActions = new Dictionary<ushort, Dictionary<ushort, Action<int, byte[]>>>();
}
}

View File

@ -91,7 +91,6 @@ 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>>>();
MessageManager.reverseChannels = new Dictionary<int, string>();
MessageManager.reverseMessageTypes = new Dictionary<ushort, string>();
SpawnManager.spawnedObjects = new Dictionary<uint, NetworkedObject>();
@ -388,7 +387,7 @@ namespace MLAPI
writer.Write(NetworkConfig.ConnectionData);
}
}
Send(clientId, "MLAPI_CONNECTION_REQUEST", "MLAPI_INTERNAL", writeStream.GetBuffer(), null, true);
Send(clientId, "MLAPI_CONNECTION_REQUEST", "MLAPI_INTERNAL", writeStream.GetBuffer(), null, null, true);
}
}
break;
@ -443,8 +442,12 @@ namespace MLAPI
ushort messageType = reader.ReadUInt16();
bool targeted = reader.ReadBoolean();
uint targetNetworkId = 0;
ushort networkOrderId = 0;
if(targeted)
{
targetNetworkId = reader.ReadUInt32();
networkOrderId = reader.ReadUInt16();
}
bool isPassthrough = reader.ReadBoolean();
int passthroughOrigin = 0;
@ -485,9 +488,13 @@ namespace MLAPI
return;
}
uint? netIdTarget = null;
ushort? netOrderId = null;
if (targeted)
{
netIdTarget = targetNetworkId;
PassthroughSend(passthroughTarget, clientId, messageType, channelId, incommingData, netIdTarget);
netOrderId = networkOrderId;
}
PassthroughSend(passthroughTarget, clientId, messageType, channelId, incommingData, netIdTarget, netOrderId);
return;
}
@ -496,6 +503,7 @@ namespace MLAPI
//Custom message, invoke all message handlers
if(targeted)
{
/*
if(!MessageManager.targetedMessages.ContainsKey(messageType))
{
Debug.LogWarning("MLAPI: No handlers for the given messagetype");
@ -514,6 +522,23 @@ namespace MLAPI
else
MessageManager.messageCallbacks[messageType][handlerIds[i]](clientId, incommingData);
}
*/
if (!SpawnManager.spawnedObjects.ContainsKey(targetNetworkId))
{
Debug.LogWarning("MLAPI: No target for message found");
return;
}
else if (!SpawnManager.spawnedObjects[targetNetworkId].targetMessageActions.ContainsKey(networkOrderId))
{
Debug.LogWarning("MLAPI: No target messageType for message found");
return;
}
else if (!SpawnManager.spawnedObjects[targetNetworkId].targetMessageActions[networkOrderId].ContainsKey(messageType))
{
Debug.LogWarning("MLAPI: No target found with the given messageType");
return;
}
SpawnManager.spawnedObjects[targetNetworkId].targetMessageActions[networkOrderId][messageType].Invoke(clientId, incommingData);
}
else
{
@ -861,7 +886,7 @@ namespace MLAPI
}
}
internal void PassthroughSend(int targetId, int sourceId, ushort messageType, int channelId, byte[] data, uint? networkId = null)
internal void PassthroughSend(int targetId, int sourceId, ushort messageType, int channelId, byte[] data, uint? networkId = null, ushort? orderId = null)
{
if (isHost && targetId == -1)
{
@ -873,6 +898,8 @@ namespace MLAPI
int sizeOfStream = 10;
if (networkId != null)
sizeOfStream += 4;
if (orderId != null)
sizeOfStream += 2;
sizeOfStream += data.Length;
using (MemoryStream stream = new MemoryStream(sizeOfStream))
@ -883,6 +910,8 @@ namespace MLAPI
writer.Write(networkId != null);
if (networkId != null)
writer.Write(networkId.Value);
if (orderId != null)
writer.Write(orderId.Value);
writer.Write(true);
writer.Write(sourceId);
writer.Write((ushort)data.Length);
@ -892,7 +921,7 @@ namespace MLAPI
}
}
internal void Send(int clientId, string messageType, string channelName, byte[] data, uint? networkId = null, bool skipQueue = false)
internal void Send(int clientId, string messageType, string channelName, byte[] data, uint? networkId = null, ushort? orderId = null, bool skipQueue = false)
{
if(clientId == -1 && isHost)
{
@ -916,6 +945,8 @@ namespace MLAPI
int sizeOfStream = 6;
if (networkId != null)
sizeOfStream += 4;
if (orderId != null)
sizeOfStream += 2;
if (isPassthrough)
sizeOfStream += 4;
sizeOfStream += data.Length;
@ -928,6 +959,8 @@ namespace MLAPI
writer.Write(networkId != null);
if (networkId != null)
writer.Write(networkId.Value);
if (orderId != null)
writer.Write(orderId.Value);
writer.Write(isPassthrough);
if (isPassthrough)
writer.Write(clientId);
@ -943,11 +976,13 @@ namespace MLAPI
}
}
internal void Send(int[] clientIds, string messageType, string channelName, byte[] data, uint? networkId = null)
internal void Send(int[] clientIds, string messageType, string channelName, byte[] data, uint? networkId = null, ushort? orderId = null)
{
int sizeOfStream = 6;
if (networkId != null)
sizeOfStream += 4;
if (orderId != null)
sizeOfStream += 2;
sizeOfStream += data.Length;
using (MemoryStream stream = new MemoryStream(sizeOfStream))
@ -958,6 +993,8 @@ namespace MLAPI
writer.Write(networkId != null);
if (networkId != null)
writer.Write(networkId.Value);
if (orderId != null)
writer.Write(orderId.Value);
writer.Write(false);
writer.Write((ushort)data.Length);
writer.Write(data);
@ -981,12 +1018,14 @@ namespace MLAPI
}
}
internal void Send(List<int> clientIds, string messageType, string channelName, byte[] data, uint? networkId = null)
internal void Send(List<int> clientIds, string messageType, string channelName, byte[] data, uint? networkId = null, ushort? orderId = null)
{
//2 bytes for messageType, 2 bytes for buffer length and one byte for target bool
int sizeOfStream = 6;
if (networkId != null)
sizeOfStream += 4;
if (orderId != null)
sizeOfStream += 2;
sizeOfStream += data.Length;
using (MemoryStream stream = new MemoryStream(sizeOfStream))
@ -997,6 +1036,8 @@ namespace MLAPI
writer.Write(networkId != null);
if (networkId != null)
writer.Write(networkId.Value);
if (orderId != null)
writer.Write(orderId.Value);
writer.Write(false);
writer.Write((ushort)data.Length);
writer.Write(data);
@ -1020,12 +1061,14 @@ namespace MLAPI
}
}
internal void Send(string messageType, string channelName, byte[] data, uint? networkId = null)
internal void Send(string messageType, string channelName, byte[] data, uint? networkId = null, ushort? orderId = null)
{
//2 bytes for messageType, 2 bytes for buffer length and one byte for target bool
int sizeOfStream = 6;
if (networkId != null)
sizeOfStream += 4;
if (orderId != null)
sizeOfStream += 2;
sizeOfStream += data.Length;
using (MemoryStream stream = new MemoryStream(sizeOfStream))
@ -1036,6 +1079,8 @@ namespace MLAPI
writer.Write(networkId != null);
if (networkId != null)
writer.Write(networkId.Value);
if (orderId != null)
writer.Write(orderId.Value);
writer.Write(false);
writer.Write((ushort)data.Length);
writer.Write(data);
@ -1060,12 +1105,14 @@ namespace MLAPI
}
}
internal void Send(string messageType, string channelName, byte[] data, int clientIdToIgnore, uint? networkId = null)
internal void Send(string messageType, string channelName, byte[] data, int clientIdToIgnore, uint? networkId = null, ushort? orderId = null)
{
//2 bytes for messageType, 2 bytes for buffer length and one byte for target bool
int sizeOfStream = 5;
if (networkId != null)
sizeOfStream += 4;
if (orderId != null)
sizeOfStream += 2;
sizeOfStream += data.Length;
using (MemoryStream stream = new MemoryStream(sizeOfStream))
@ -1076,6 +1123,8 @@ namespace MLAPI
writer.Write(networkId != null);
if (networkId != null)
writer.Write(networkId.Value);
if (orderId != null)
writer.Write(orderId.Value);
writer.Write(false);
writer.Write((ushort)data.Length);
writer.Write(data);
@ -1214,7 +1263,7 @@ namespace MLAPI
}
}
}
Send(clientId, "MLAPI_CONNECTION_APPROVED", "MLAPI_INTERNAL", writeStream.GetBuffer(), null, true);
Send(clientId, "MLAPI_CONNECTION_APPROVED", "MLAPI_INTERNAL", writeStream.GetBuffer(), null, null, true);
if (OnClientConnectedCallback != null)
OnClientConnectedCallback.Invoke(clientId);

View File

@ -61,7 +61,7 @@ namespace MLAPI.MonoBehaviours.Prototyping
}
if (!EnableProximity)
{
SendToClientsTarget("MLAPI_OnNavMeshStateUpdate", "MLAPI_NAV_AGENT_STATE", stream.GetBuffer());
SendToClientsTarget("MLAPI_OnNavMeshStateUpdate", "MLAPI_NAV_AGENT_STATE", stateUpdateBuffer);
}
else
{
@ -71,7 +71,7 @@ namespace MLAPI.MonoBehaviours.Prototyping
if (Vector3.Distance(client.Value.PlayerObject.transform.position, transform.position) <= ProximityRange)
proximityClients.Add(client.Key);
}
SendToClientsTarget(proximityClients, "MLAPI_OnNavMeshStateUpdate", "MLAPI_NAV_AGENT_STATE", stream.GetBuffer());
SendToClientsTarget(proximityClients, "MLAPI_OnNavMeshStateUpdate", "MLAPI_NAV_AGENT_STATE", stateUpdateBuffer);
}
}
}
@ -93,7 +93,7 @@ namespace MLAPI.MonoBehaviours.Prototyping
if (!EnableProximity)
{
SendToClientsTarget("MLAPI_OnNavMeshCorrectionUpdate", "MLAPI_NAV_AGENT_CORRECTION", stream.GetBuffer());
SendToClientsTarget("MLAPI_OnNavMeshCorrectionUpdate", "MLAPI_NAV_AGENT_CORRECTION", correctionBuffer);
}
else
{
@ -103,7 +103,7 @@ namespace MLAPI.MonoBehaviours.Prototyping
if (Vector3.Distance(client.Value.PlayerObject.transform.position, transform.position) <= ProximityRange)
proximityClients.Add(client.Key);
}
SendToClientsTarget(proximityClients, "MLAPI_OnNavMeshCorrectionUpdate", "MLAPI_NAV_AGENT_CORRECTION", stream.GetBuffer());
SendToClientsTarget(proximityClients, "MLAPI_OnNavMeshCorrectionUpdate", "MLAPI_NAV_AGENT_CORRECTION", correctionBuffer);
}
}
lastCorrectionTime = Time.time;

View File

@ -83,9 +83,9 @@ namespace MLAPI.MonoBehaviours.Prototyping
writer.Write(transform.rotation.eulerAngles.z);
}
if (isServer)
SendToClientsTarget("MLAPI_OnRecieveTransformFromServer", "MLAPI_POSITION_UPDATE", writeStream.GetBuffer());
SendToClientsTarget("MLAPI_OnRecieveTransformFromServer", "MLAPI_POSITION_UPDATE", positionUpdateBuffer);
else
SendToServerTarget("MLAPI_OnRecieveTransformFromClient", "MLAPI_POSITION_UPDATE", writeStream.GetBuffer());
SendToServerTarget("MLAPI_OnRecieveTransformFromClient", "MLAPI_POSITION_UPDATE", positionUpdateBuffer);
}
}
@ -170,13 +170,13 @@ namespace MLAPI.MonoBehaviours.Prototyping
{
if (Vector3.Distance(NetworkingManager.singleton.connectedClients[i].PlayerObject.transform.position, transform.position) <= ProximityRange)
{
SendToClientTarget(NetworkingManager.singleton.connectedClients[i].ClientId, "MLAPI_OnRecieveTransformFromServer", "MLAPI_POSITION_UPDATE", writeStream.GetBuffer());
SendToClientTarget(NetworkingManager.singleton.connectedClients[i].ClientId, "MLAPI_OnRecieveTransformFromServer", "MLAPI_POSITION_UPDATE", positionUpdateBuffer);
}
}
}
else
{
SendToNonLocalClientsTarget("MLAPI_OnRecieveTransformFromServer", "MLAPI_POSITION_UPDATE", writeStream.GetBuffer());
SendToNonLocalClientsTarget("MLAPI_OnRecieveTransformFromServer", "MLAPI_POSITION_UPDATE", positionUpdateBuffer);
}
}
}

View File

@ -10,11 +10,13 @@ namespace MLAPI.NetworkingManagerComponents
internal static Dictionary<int, string> reverseChannels;
internal static Dictionary<string, ushort> messageTypes;
internal static Dictionary<ushort, string> reverseMessageTypes;
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;
//Key: messageType, Value key: networkId, value value: handlerIds
//internal static Dictionary<ushort, Dictionary<uint, List<int>>> targetedMessages;
private static NetworkingManager netManager
{
@ -24,18 +26,11 @@ namespace MLAPI.NetworkingManagerComponents
}
}
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;
@ -59,7 +54,6 @@ namespace MLAPI.NetworkingManagerComponents
messageHandlerCounter.Add(messageTypes[name], handlerId + 1);
}
messageCallbacks[messageTypes[name]].Add(handlerId, action);
targetedMessages[messageTypes[name]][networkId].Add(handlerId);
return handlerId;
}
else
@ -67,7 +61,6 @@ 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;
}
}
@ -89,8 +82,8 @@ 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);
}
}
}
}