Added per NetworkBehaviour message Targets
This commit is contained in:
parent
368d719201
commit
e41ced9e8f
@ -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);
|
int counter = MessageManager.AddIncomingMessageHandler(name, action, networkId);
|
||||||
registeredMessageHandlers.Add(name, counter);
|
registeredMessageHandlers.Add(name, counter);
|
||||||
return counter;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void DeregisterMessageHandler(string name, int counter)
|
protected void DeregisterMessageHandler(string name, int counter)
|
||||||
{
|
{
|
||||||
MessageManager.RemoveIncomingMessageHandler(name, counter, networkId);
|
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()
|
private void OnDisable()
|
||||||
@ -619,7 +643,7 @@ namespace MLAPI
|
|||||||
Debug.LogWarning("MLAPI: Server can not send messages to server.");
|
Debug.LogWarning("MLAPI: Server can not send messages to server.");
|
||||||
return;
|
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)
|
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");
|
Debug.LogWarning("MLAPI: Invalid Passthrough send. Ensure AllowPassthroughMessages are turned on and that the MessageType " + messageType + " is registered as a passthroughMessageType");
|
||||||
return;
|
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)
|
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");
|
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
|
||||||
return;
|
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)
|
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");
|
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
|
||||||
return;
|
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)
|
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");
|
Debug.LogWarning("MLAPI: Invalid Passthrough send. Ensure AllowPassthroughMessages are turned on and that the MessageType " + messageType + " is registered as a passthroughMessageType");
|
||||||
return;
|
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)
|
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");
|
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
|
||||||
return;
|
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)
|
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");
|
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
|
||||||
return;
|
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)
|
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");
|
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
NetworkingManager.singleton.Send(messageType, channelName, data, networkId);
|
NetworkingManager.singleton.Send(messageType, channelName, data, networkId, networkedObject.GetOrderIndex(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected NetworkedObject GetNetworkedObject(uint networkId)
|
protected NetworkedObject GetNetworkedObject(uint networkId)
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using MLAPI.NetworkingManagerComponents;
|
using MLAPI.NetworkingManagerComponents;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
@ -143,5 +144,8 @@ namespace MLAPI
|
|||||||
//TODO index out of bounds
|
//TODO index out of bounds
|
||||||
return childNetworkedBehaviours[index];
|
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[]>>>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -91,7 +91,6 @@ namespace MLAPI
|
|||||||
MessageManager.messageCallbacks = new Dictionary<ushort, Dictionary<int, Action<int, byte[]>>>();
|
MessageManager.messageCallbacks = new Dictionary<ushort, Dictionary<int, Action<int, byte[]>>>();
|
||||||
MessageManager.messageHandlerCounter = new Dictionary<ushort, int>();
|
MessageManager.messageHandlerCounter = new Dictionary<ushort, int>();
|
||||||
MessageManager.releasedMessageHandlerCounters = new Dictionary<ushort, Stack<int>>();
|
MessageManager.releasedMessageHandlerCounters = new Dictionary<ushort, Stack<int>>();
|
||||||
MessageManager.targetedMessages = new Dictionary<ushort, Dictionary<uint, List<int>>>();
|
|
||||||
MessageManager.reverseChannels = new Dictionary<int, string>();
|
MessageManager.reverseChannels = new Dictionary<int, string>();
|
||||||
MessageManager.reverseMessageTypes = new Dictionary<ushort, string>();
|
MessageManager.reverseMessageTypes = new Dictionary<ushort, string>();
|
||||||
SpawnManager.spawnedObjects = new Dictionary<uint, NetworkedObject>();
|
SpawnManager.spawnedObjects = new Dictionary<uint, NetworkedObject>();
|
||||||
@ -388,7 +387,7 @@ namespace MLAPI
|
|||||||
writer.Write(NetworkConfig.ConnectionData);
|
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;
|
break;
|
||||||
@ -443,8 +442,12 @@ namespace MLAPI
|
|||||||
ushort messageType = reader.ReadUInt16();
|
ushort messageType = reader.ReadUInt16();
|
||||||
bool targeted = reader.ReadBoolean();
|
bool targeted = reader.ReadBoolean();
|
||||||
uint targetNetworkId = 0;
|
uint targetNetworkId = 0;
|
||||||
|
ushort networkOrderId = 0;
|
||||||
if(targeted)
|
if(targeted)
|
||||||
|
{
|
||||||
targetNetworkId = reader.ReadUInt32();
|
targetNetworkId = reader.ReadUInt32();
|
||||||
|
networkOrderId = reader.ReadUInt16();
|
||||||
|
}
|
||||||
bool isPassthrough = reader.ReadBoolean();
|
bool isPassthrough = reader.ReadBoolean();
|
||||||
|
|
||||||
int passthroughOrigin = 0;
|
int passthroughOrigin = 0;
|
||||||
@ -485,9 +488,13 @@ namespace MLAPI
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
uint? netIdTarget = null;
|
uint? netIdTarget = null;
|
||||||
|
ushort? netOrderId = null;
|
||||||
if (targeted)
|
if (targeted)
|
||||||
|
{
|
||||||
netIdTarget = targetNetworkId;
|
netIdTarget = targetNetworkId;
|
||||||
PassthroughSend(passthroughTarget, clientId, messageType, channelId, incommingData, netIdTarget);
|
netOrderId = networkOrderId;
|
||||||
|
}
|
||||||
|
PassthroughSend(passthroughTarget, clientId, messageType, channelId, incommingData, netIdTarget, netOrderId);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -496,6 +503,7 @@ namespace MLAPI
|
|||||||
//Custom message, invoke all message handlers
|
//Custom message, invoke all message handlers
|
||||||
if(targeted)
|
if(targeted)
|
||||||
{
|
{
|
||||||
|
/*
|
||||||
if(!MessageManager.targetedMessages.ContainsKey(messageType))
|
if(!MessageManager.targetedMessages.ContainsKey(messageType))
|
||||||
{
|
{
|
||||||
Debug.LogWarning("MLAPI: No handlers for the given messagetype");
|
Debug.LogWarning("MLAPI: No handlers for the given messagetype");
|
||||||
@ -514,6 +522,23 @@ namespace MLAPI
|
|||||||
else
|
else
|
||||||
MessageManager.messageCallbacks[messageType][handlerIds[i]](clientId, incommingData);
|
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
|
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)
|
if (isHost && targetId == -1)
|
||||||
{
|
{
|
||||||
@ -873,6 +898,8 @@ namespace MLAPI
|
|||||||
int sizeOfStream = 10;
|
int sizeOfStream = 10;
|
||||||
if (networkId != null)
|
if (networkId != null)
|
||||||
sizeOfStream += 4;
|
sizeOfStream += 4;
|
||||||
|
if (orderId != null)
|
||||||
|
sizeOfStream += 2;
|
||||||
sizeOfStream += data.Length;
|
sizeOfStream += data.Length;
|
||||||
|
|
||||||
using (MemoryStream stream = new MemoryStream(sizeOfStream))
|
using (MemoryStream stream = new MemoryStream(sizeOfStream))
|
||||||
@ -883,6 +910,8 @@ namespace MLAPI
|
|||||||
writer.Write(networkId != null);
|
writer.Write(networkId != null);
|
||||||
if (networkId != null)
|
if (networkId != null)
|
||||||
writer.Write(networkId.Value);
|
writer.Write(networkId.Value);
|
||||||
|
if (orderId != null)
|
||||||
|
writer.Write(orderId.Value);
|
||||||
writer.Write(true);
|
writer.Write(true);
|
||||||
writer.Write(sourceId);
|
writer.Write(sourceId);
|
||||||
writer.Write((ushort)data.Length);
|
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)
|
if(clientId == -1 && isHost)
|
||||||
{
|
{
|
||||||
@ -916,6 +945,8 @@ namespace MLAPI
|
|||||||
int sizeOfStream = 6;
|
int sizeOfStream = 6;
|
||||||
if (networkId != null)
|
if (networkId != null)
|
||||||
sizeOfStream += 4;
|
sizeOfStream += 4;
|
||||||
|
if (orderId != null)
|
||||||
|
sizeOfStream += 2;
|
||||||
if (isPassthrough)
|
if (isPassthrough)
|
||||||
sizeOfStream += 4;
|
sizeOfStream += 4;
|
||||||
sizeOfStream += data.Length;
|
sizeOfStream += data.Length;
|
||||||
@ -928,6 +959,8 @@ namespace MLAPI
|
|||||||
writer.Write(networkId != null);
|
writer.Write(networkId != null);
|
||||||
if (networkId != null)
|
if (networkId != null)
|
||||||
writer.Write(networkId.Value);
|
writer.Write(networkId.Value);
|
||||||
|
if (orderId != null)
|
||||||
|
writer.Write(orderId.Value);
|
||||||
writer.Write(isPassthrough);
|
writer.Write(isPassthrough);
|
||||||
if (isPassthrough)
|
if (isPassthrough)
|
||||||
writer.Write(clientId);
|
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;
|
int sizeOfStream = 6;
|
||||||
if (networkId != null)
|
if (networkId != null)
|
||||||
sizeOfStream += 4;
|
sizeOfStream += 4;
|
||||||
|
if (orderId != null)
|
||||||
|
sizeOfStream += 2;
|
||||||
sizeOfStream += data.Length;
|
sizeOfStream += data.Length;
|
||||||
|
|
||||||
using (MemoryStream stream = new MemoryStream(sizeOfStream))
|
using (MemoryStream stream = new MemoryStream(sizeOfStream))
|
||||||
@ -958,6 +993,8 @@ namespace MLAPI
|
|||||||
writer.Write(networkId != null);
|
writer.Write(networkId != null);
|
||||||
if (networkId != null)
|
if (networkId != null)
|
||||||
writer.Write(networkId.Value);
|
writer.Write(networkId.Value);
|
||||||
|
if (orderId != null)
|
||||||
|
writer.Write(orderId.Value);
|
||||||
writer.Write(false);
|
writer.Write(false);
|
||||||
writer.Write((ushort)data.Length);
|
writer.Write((ushort)data.Length);
|
||||||
writer.Write(data);
|
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
|
//2 bytes for messageType, 2 bytes for buffer length and one byte for target bool
|
||||||
int sizeOfStream = 6;
|
int sizeOfStream = 6;
|
||||||
if (networkId != null)
|
if (networkId != null)
|
||||||
sizeOfStream += 4;
|
sizeOfStream += 4;
|
||||||
|
if (orderId != null)
|
||||||
|
sizeOfStream += 2;
|
||||||
sizeOfStream += data.Length;
|
sizeOfStream += data.Length;
|
||||||
|
|
||||||
using (MemoryStream stream = new MemoryStream(sizeOfStream))
|
using (MemoryStream stream = new MemoryStream(sizeOfStream))
|
||||||
@ -997,6 +1036,8 @@ namespace MLAPI
|
|||||||
writer.Write(networkId != null);
|
writer.Write(networkId != null);
|
||||||
if (networkId != null)
|
if (networkId != null)
|
||||||
writer.Write(networkId.Value);
|
writer.Write(networkId.Value);
|
||||||
|
if (orderId != null)
|
||||||
|
writer.Write(orderId.Value);
|
||||||
writer.Write(false);
|
writer.Write(false);
|
||||||
writer.Write((ushort)data.Length);
|
writer.Write((ushort)data.Length);
|
||||||
writer.Write(data);
|
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
|
//2 bytes for messageType, 2 bytes for buffer length and one byte for target bool
|
||||||
int sizeOfStream = 6;
|
int sizeOfStream = 6;
|
||||||
if (networkId != null)
|
if (networkId != null)
|
||||||
sizeOfStream += 4;
|
sizeOfStream += 4;
|
||||||
|
if (orderId != null)
|
||||||
|
sizeOfStream += 2;
|
||||||
sizeOfStream += data.Length;
|
sizeOfStream += data.Length;
|
||||||
|
|
||||||
using (MemoryStream stream = new MemoryStream(sizeOfStream))
|
using (MemoryStream stream = new MemoryStream(sizeOfStream))
|
||||||
@ -1036,6 +1079,8 @@ namespace MLAPI
|
|||||||
writer.Write(networkId != null);
|
writer.Write(networkId != null);
|
||||||
if (networkId != null)
|
if (networkId != null)
|
||||||
writer.Write(networkId.Value);
|
writer.Write(networkId.Value);
|
||||||
|
if (orderId != null)
|
||||||
|
writer.Write(orderId.Value);
|
||||||
writer.Write(false);
|
writer.Write(false);
|
||||||
writer.Write((ushort)data.Length);
|
writer.Write((ushort)data.Length);
|
||||||
writer.Write(data);
|
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
|
//2 bytes for messageType, 2 bytes for buffer length and one byte for target bool
|
||||||
int sizeOfStream = 5;
|
int sizeOfStream = 5;
|
||||||
if (networkId != null)
|
if (networkId != null)
|
||||||
sizeOfStream += 4;
|
sizeOfStream += 4;
|
||||||
|
if (orderId != null)
|
||||||
|
sizeOfStream += 2;
|
||||||
sizeOfStream += data.Length;
|
sizeOfStream += data.Length;
|
||||||
|
|
||||||
using (MemoryStream stream = new MemoryStream(sizeOfStream))
|
using (MemoryStream stream = new MemoryStream(sizeOfStream))
|
||||||
@ -1076,6 +1123,8 @@ namespace MLAPI
|
|||||||
writer.Write(networkId != null);
|
writer.Write(networkId != null);
|
||||||
if (networkId != null)
|
if (networkId != null)
|
||||||
writer.Write(networkId.Value);
|
writer.Write(networkId.Value);
|
||||||
|
if (orderId != null)
|
||||||
|
writer.Write(orderId.Value);
|
||||||
writer.Write(false);
|
writer.Write(false);
|
||||||
writer.Write((ushort)data.Length);
|
writer.Write((ushort)data.Length);
|
||||||
writer.Write(data);
|
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)
|
if (OnClientConnectedCallback != null)
|
||||||
OnClientConnectedCallback.Invoke(clientId);
|
OnClientConnectedCallback.Invoke(clientId);
|
||||||
|
@ -61,7 +61,7 @@ namespace MLAPI.MonoBehaviours.Prototyping
|
|||||||
}
|
}
|
||||||
if (!EnableProximity)
|
if (!EnableProximity)
|
||||||
{
|
{
|
||||||
SendToClientsTarget("MLAPI_OnNavMeshStateUpdate", "MLAPI_NAV_AGENT_STATE", stream.GetBuffer());
|
SendToClientsTarget("MLAPI_OnNavMeshStateUpdate", "MLAPI_NAV_AGENT_STATE", stateUpdateBuffer);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -71,7 +71,7 @@ namespace MLAPI.MonoBehaviours.Prototyping
|
|||||||
if (Vector3.Distance(client.Value.PlayerObject.transform.position, transform.position) <= ProximityRange)
|
if (Vector3.Distance(client.Value.PlayerObject.transform.position, transform.position) <= ProximityRange)
|
||||||
proximityClients.Add(client.Key);
|
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)
|
if (!EnableProximity)
|
||||||
{
|
{
|
||||||
SendToClientsTarget("MLAPI_OnNavMeshCorrectionUpdate", "MLAPI_NAV_AGENT_CORRECTION", stream.GetBuffer());
|
SendToClientsTarget("MLAPI_OnNavMeshCorrectionUpdate", "MLAPI_NAV_AGENT_CORRECTION", correctionBuffer);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -103,7 +103,7 @@ namespace MLAPI.MonoBehaviours.Prototyping
|
|||||||
if (Vector3.Distance(client.Value.PlayerObject.transform.position, transform.position) <= ProximityRange)
|
if (Vector3.Distance(client.Value.PlayerObject.transform.position, transform.position) <= ProximityRange)
|
||||||
proximityClients.Add(client.Key);
|
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;
|
lastCorrectionTime = Time.time;
|
||||||
|
@ -83,9 +83,9 @@ namespace MLAPI.MonoBehaviours.Prototyping
|
|||||||
writer.Write(transform.rotation.eulerAngles.z);
|
writer.Write(transform.rotation.eulerAngles.z);
|
||||||
}
|
}
|
||||||
if (isServer)
|
if (isServer)
|
||||||
SendToClientsTarget("MLAPI_OnRecieveTransformFromServer", "MLAPI_POSITION_UPDATE", writeStream.GetBuffer());
|
SendToClientsTarget("MLAPI_OnRecieveTransformFromServer", "MLAPI_POSITION_UPDATE", positionUpdateBuffer);
|
||||||
else
|
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)
|
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
|
else
|
||||||
{
|
{
|
||||||
SendToNonLocalClientsTarget("MLAPI_OnRecieveTransformFromServer", "MLAPI_POSITION_UPDATE", writeStream.GetBuffer());
|
SendToNonLocalClientsTarget("MLAPI_OnRecieveTransformFromServer", "MLAPI_POSITION_UPDATE", positionUpdateBuffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,11 +10,13 @@ namespace MLAPI.NetworkingManagerComponents
|
|||||||
internal static Dictionary<int, string> reverseChannels;
|
internal static Dictionary<int, string> reverseChannels;
|
||||||
internal static Dictionary<string, ushort> messageTypes;
|
internal static Dictionary<string, ushort> messageTypes;
|
||||||
internal static Dictionary<ushort, string> reverseMessageTypes;
|
internal static Dictionary<ushort, string> reverseMessageTypes;
|
||||||
|
|
||||||
internal static Dictionary<ushort, Dictionary<int, Action<int, byte[]>>> messageCallbacks;
|
internal static Dictionary<ushort, Dictionary<int, Action<int, byte[]>>> messageCallbacks;
|
||||||
internal static Dictionary<ushort, int> messageHandlerCounter;
|
internal static Dictionary<ushort, int> messageHandlerCounter;
|
||||||
internal static Dictionary<ushort, Stack<int>> releasedMessageHandlerCounters;
|
internal static Dictionary<ushort, Stack<int>> releasedMessageHandlerCounters;
|
||||||
//Key: messageType, Value key: networkId, value value: handlerId
|
//Key: messageType, Value key: networkId, value value: handlerIds
|
||||||
internal static Dictionary<ushort, Dictionary<uint, List<int>>> targetedMessages;
|
//internal static Dictionary<ushort, Dictionary<uint, List<int>>> targetedMessages;
|
||||||
|
|
||||||
|
|
||||||
private static NetworkingManager netManager
|
private static NetworkingManager netManager
|
||||||
{
|
{
|
||||||
@ -24,18 +26,11 @@ namespace MLAPI.NetworkingManagerComponents
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
internal static int AddIncomingMessageHandler(string name, Action<int, byte[]> action, uint networkId)
|
internal static int AddIncomingMessageHandler(string name, Action<int, byte[]> action, uint networkId)
|
||||||
{
|
{
|
||||||
if (messageTypes.ContainsKey(name))
|
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]))
|
if (messageCallbacks.ContainsKey(messageTypes[name]))
|
||||||
{
|
{
|
||||||
int handlerId = 0;
|
int handlerId = 0;
|
||||||
@ -59,7 +54,6 @@ namespace MLAPI.NetworkingManagerComponents
|
|||||||
messageHandlerCounter.Add(messageTypes[name], handlerId + 1);
|
messageHandlerCounter.Add(messageTypes[name], handlerId + 1);
|
||||||
}
|
}
|
||||||
messageCallbacks[messageTypes[name]].Add(handlerId, action);
|
messageCallbacks[messageTypes[name]].Add(handlerId, action);
|
||||||
targetedMessages[messageTypes[name]][networkId].Add(handlerId);
|
|
||||||
return handlerId;
|
return handlerId;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -67,7 +61,6 @@ namespace MLAPI.NetworkingManagerComponents
|
|||||||
messageCallbacks.Add(messageTypes[name], new Dictionary<int, Action<int, byte[]>>());
|
messageCallbacks.Add(messageTypes[name], new Dictionary<int, Action<int, byte[]>>());
|
||||||
messageHandlerCounter.Add(messageTypes[name], 1);
|
messageHandlerCounter.Add(messageTypes[name], 1);
|
||||||
messageCallbacks[messageTypes[name]].Add(0, action);
|
messageCallbacks[messageTypes[name]].Add(0, action);
|
||||||
targetedMessages[messageTypes[name]][networkId].Add(0);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -89,8 +82,8 @@ namespace MLAPI.NetworkingManagerComponents
|
|||||||
if (!releasedMessageHandlerCounters.ContainsKey(messageTypes[name]))
|
if (!releasedMessageHandlerCounters.ContainsKey(messageTypes[name]))
|
||||||
releasedMessageHandlerCounters.Add(messageTypes[name], new Stack<int>());
|
releasedMessageHandlerCounters.Add(messageTypes[name], new Stack<int>());
|
||||||
releasedMessageHandlerCounters[messageTypes[name]].Push(counter);
|
releasedMessageHandlerCounters[messageTypes[name]].Push(counter);
|
||||||
targetedMessages[messageTypes[name]][networkId].Remove(counter);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user