Merge pull request #1 from TwoTenPvP/targeted-messages
Added targeted object targeted messages
This commit is contained in:
commit
b22642f26b
@ -68,14 +68,14 @@ namespace MLAPI
|
|||||||
|
|
||||||
protected int RegisterMessageHandler(string name, Action<int, byte[]> action)
|
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);
|
registeredMessageHandlers.Add(name, counter);
|
||||||
return counter;
|
return counter;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void DeregisterMessageHandler(string name, int counter)
|
protected void DeregisterMessageHandler(string name, int counter)
|
||||||
{
|
{
|
||||||
MessageManager.RemoveIncomingMessageHandler(name, counter);
|
MessageManager.RemoveIncomingMessageHandler(name, counter, networkId);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnDestroy()
|
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)
|
protected void SendToLocalClient(string messageType, string channelName, byte[] data)
|
||||||
{
|
{
|
||||||
if (!isServer)
|
if (!isServer)
|
||||||
@ -108,6 +120,16 @@ namespace MLAPI
|
|||||||
NetworkingManager.singleton.Send(ownerClientId, messageType, channelName, data);
|
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)
|
protected void SendToNonLocalClients(string messageType, string channelName, byte[] data)
|
||||||
{
|
{
|
||||||
if (!isServer)
|
if (!isServer)
|
||||||
@ -118,6 +140,16 @@ namespace MLAPI
|
|||||||
NetworkingManager.singleton.Send(messageType, channelName, data, ownerClientId);
|
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)
|
protected void SendToClient(int clientId, string messageType, string channelName, byte[] data)
|
||||||
{
|
{
|
||||||
if (!isServer)
|
if (!isServer)
|
||||||
@ -128,6 +160,16 @@ namespace MLAPI
|
|||||||
NetworkingManager.singleton.Send(clientId, messageType, channelName, data);
|
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)
|
protected void SendToClients(int[] clientIds, string messageType, string channelName, byte[] data)
|
||||||
{
|
{
|
||||||
if (!isServer)
|
if (!isServer)
|
||||||
@ -138,6 +180,16 @@ namespace MLAPI
|
|||||||
NetworkingManager.singleton.Send(clientIds, messageType, channelName, data);
|
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)
|
protected void SendToClients(List<int> clientIds, string messageType, string channelName, byte[] data)
|
||||||
{
|
{
|
||||||
if (!isServer)
|
if (!isServer)
|
||||||
@ -148,6 +200,16 @@ namespace MLAPI
|
|||||||
NetworkingManager.singleton.Send(clientIds, messageType, channelName, data);
|
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)
|
protected void SendToClients(string messageType, string channelName, byte[] data)
|
||||||
{
|
{
|
||||||
if (!isServer)
|
if (!isServer)
|
||||||
@ -158,6 +220,16 @@ namespace MLAPI
|
|||||||
NetworkingManager.singleton.Send(messageType, channelName, data);
|
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)
|
protected NetworkedObject GetNetworkedObject(uint networkId)
|
||||||
{
|
{
|
||||||
return SpawnManager.spawnedObjects[networkId];
|
return SpawnManager.spawnedObjects[networkId];
|
||||||
|
@ -66,6 +66,7 @@ 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>>>();
|
||||||
SpawnManager.spawnedObjects = new Dictionary<uint, NetworkedObject>();
|
SpawnManager.spawnedObjects = new Dictionary<uint, NetworkedObject>();
|
||||||
SpawnManager.releasedNetworkObjectIds = new Stack<uint>();
|
SpawnManager.releasedNetworkObjectIds = new Stack<uint>();
|
||||||
if (NetworkConfig.HandleObjectSpawning)
|
if (NetworkConfig.HandleObjectSpawning)
|
||||||
@ -319,6 +320,10 @@ namespace MLAPI
|
|||||||
using (BinaryReader reader = new BinaryReader(readStream))
|
using (BinaryReader reader = new BinaryReader(readStream))
|
||||||
{
|
{
|
||||||
ushort messageType = reader.ReadUInt16();
|
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.
|
//Client tried to send a network message that was not the connection request before he was accepted.
|
||||||
if (isServer && pendingClients.Contains(clientId) && messageType != 0)
|
if (isServer && pendingClients.Contains(clientId) && messageType != 0)
|
||||||
@ -332,10 +337,22 @@ namespace MLAPI
|
|||||||
if (messageType >= 32)
|
if (messageType >= 32)
|
||||||
{
|
{
|
||||||
//Custom message, invoke all message handlers
|
//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
|
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)
|
if(isHost && clientId == -1)
|
||||||
{
|
{
|
||||||
//Host trying to send data to it's own client
|
//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;
|
return;
|
||||||
}
|
}
|
||||||
else if(clientId == -1)
|
else if(clientId == -1)
|
||||||
@ -496,28 +516,30 @@ namespace MLAPI
|
|||||||
using (BinaryWriter writer = new BinaryWriter(stream))
|
using (BinaryWriter writer = new BinaryWriter(stream))
|
||||||
{
|
{
|
||||||
writer.Write(MessageManager.messageTypes[messageType]);
|
writer.Write(MessageManager.messageTypes[messageType]);
|
||||||
|
writer.Write(networkId != null);
|
||||||
|
if (networkId != null)
|
||||||
|
writer.Write(networkId.Value);
|
||||||
writer.Write((ushort)data.Length);
|
writer.Write((ushort)data.Length);
|
||||||
writer.Write(data);
|
writer.Write(data);
|
||||||
}
|
}
|
||||||
//2 bytes for message type and 2 bytes for byte length
|
|
||||||
int size = data.Length + 4;
|
|
||||||
byte[] dataToSend = stream.ToArray();
|
byte[] dataToSend = stream.ToArray();
|
||||||
NetworkTransport.Send(hostId, clientId, MessageManager.channels[channelName], dataToSend, dataToSend.Length, out error);
|
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 (MemoryStream stream = new MemoryStream())
|
||||||
{
|
{
|
||||||
using (BinaryWriter writer = new BinaryWriter(stream))
|
using (BinaryWriter writer = new BinaryWriter(stream))
|
||||||
{
|
{
|
||||||
writer.Write(MessageManager.messageTypes[messageType]);
|
writer.Write(MessageManager.messageTypes[messageType]);
|
||||||
|
writer.Write(networkId != null);
|
||||||
|
if (networkId != null)
|
||||||
|
writer.Write(networkId.Value);
|
||||||
writer.Write((ushort)data.Length);
|
writer.Write((ushort)data.Length);
|
||||||
writer.Write(data);
|
writer.Write(data);
|
||||||
}
|
}
|
||||||
//2 bytes for message type and 2 bytes for byte length
|
|
||||||
int size = data.Length + 4;
|
|
||||||
byte[] dataToSend = stream.ToArray();
|
byte[] dataToSend = stream.ToArray();
|
||||||
int channel = MessageManager.channels[channelName];
|
int channel = MessageManager.channels[channelName];
|
||||||
for (int i = 0; i < clientIds.Length; i++)
|
for (int i = 0; i < clientIds.Length; i++)
|
||||||
@ -525,7 +547,10 @@ namespace MLAPI
|
|||||||
int clientId = clientIds[i];
|
int clientId = clientIds[i];
|
||||||
if (isHost && clientId == -1)
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
else if (clientId == -1)
|
else if (clientId == -1)
|
||||||
@ -533,23 +558,24 @@ namespace MLAPI
|
|||||||
//Client trying to send data to host
|
//Client trying to send data to host
|
||||||
clientId = serverClientId;
|
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 (MemoryStream stream = new MemoryStream())
|
||||||
{
|
{
|
||||||
using (BinaryWriter writer = new BinaryWriter(stream))
|
using (BinaryWriter writer = new BinaryWriter(stream))
|
||||||
{
|
{
|
||||||
writer.Write(MessageManager.messageTypes[messageType]);
|
writer.Write(MessageManager.messageTypes[messageType]);
|
||||||
|
writer.Write(networkId != null);
|
||||||
|
if (networkId != null)
|
||||||
|
writer.Write(networkId.Value);
|
||||||
writer.Write((ushort)data.Length);
|
writer.Write((ushort)data.Length);
|
||||||
writer.Write(data);
|
writer.Write(data);
|
||||||
}
|
}
|
||||||
//2 bytes for message type and 2 bytes for byte length
|
|
||||||
int size = data.Length + 4;
|
|
||||||
byte[] dataToSend = stream.ToArray();
|
byte[] dataToSend = stream.ToArray();
|
||||||
int channel = MessageManager.channels[channelName];
|
int channel = MessageManager.channels[channelName];
|
||||||
for (int i = 0; i < clientIds.Count; i++)
|
for (int i = 0; i < clientIds.Count; i++)
|
||||||
@ -557,7 +583,10 @@ namespace MLAPI
|
|||||||
int clientId = clientIds[i];
|
int clientId = clientIds[i];
|
||||||
if (isHost && clientId == -1)
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
else if (clientId == -1)
|
else if (clientId == -1)
|
||||||
@ -565,23 +594,24 @@ namespace MLAPI
|
|||||||
//Client trying to send data to host
|
//Client trying to send data to host
|
||||||
clientId = serverClientId;
|
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 (MemoryStream stream = new MemoryStream())
|
||||||
{
|
{
|
||||||
using (BinaryWriter writer = new BinaryWriter(stream))
|
using (BinaryWriter writer = new BinaryWriter(stream))
|
||||||
{
|
{
|
||||||
writer.Write(MessageManager.messageTypes[messageType]);
|
writer.Write(MessageManager.messageTypes[messageType]);
|
||||||
|
writer.Write(networkId != null);
|
||||||
|
if (networkId != null)
|
||||||
|
writer.Write(networkId.Value);
|
||||||
writer.Write((ushort)data.Length);
|
writer.Write((ushort)data.Length);
|
||||||
writer.Write(data);
|
writer.Write(data);
|
||||||
}
|
}
|
||||||
//2 bytes for message type and 2 bytes for byte length
|
|
||||||
int size = data.Length + 4;
|
|
||||||
byte[] dataToSend = stream.ToArray();
|
byte[] dataToSend = stream.ToArray();
|
||||||
int channel = MessageManager.channels[channelName];
|
int channel = MessageManager.channels[channelName];
|
||||||
foreach (KeyValuePair<int, NetworkedClient> pair in connectedClients)
|
foreach (KeyValuePair<int, NetworkedClient> pair in connectedClients)
|
||||||
@ -589,7 +619,10 @@ namespace MLAPI
|
|||||||
int clientId = pair.Key;
|
int clientId = pair.Key;
|
||||||
if(isHost && pair.Key == -1)
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
else if (clientId == -1)
|
else if (clientId == -1)
|
||||||
@ -597,24 +630,25 @@ namespace MLAPI
|
|||||||
//Client trying to send data to host
|
//Client trying to send data to host
|
||||||
clientId = serverClientId;
|
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 (MemoryStream stream = new MemoryStream())
|
||||||
{
|
{
|
||||||
using (BinaryWriter writer = new BinaryWriter(stream))
|
using (BinaryWriter writer = new BinaryWriter(stream))
|
||||||
{
|
{
|
||||||
writer.Write(MessageManager.messageTypes[messageType]);
|
writer.Write(MessageManager.messageTypes[messageType]);
|
||||||
|
writer.Write(networkId != null);
|
||||||
|
if (networkId != null)
|
||||||
|
writer.Write(networkId.Value);
|
||||||
writer.Write((ushort)data.Length);
|
writer.Write((ushort)data.Length);
|
||||||
writer.Write(data);
|
writer.Write(data);
|
||||||
}
|
}
|
||||||
//2 bytes for message type and 2 bytes for byte length
|
|
||||||
int size = data.Length + 4;
|
|
||||||
byte[] dataToSend = stream.ToArray();
|
byte[] dataToSend = stream.ToArray();
|
||||||
int channel = MessageManager.channels[channelName];
|
int channel = MessageManager.channels[channelName];
|
||||||
foreach (KeyValuePair<int, NetworkedClient> pair in connectedClients)
|
foreach (KeyValuePair<int, NetworkedClient> pair in connectedClients)
|
||||||
@ -624,7 +658,10 @@ namespace MLAPI
|
|||||||
int clientId = pair.Key;
|
int clientId = pair.Key;
|
||||||
if (isHost && pair.Key == -1)
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
else if (clientId == -1)
|
else if (clientId == -1)
|
||||||
@ -632,7 +669,7 @@ namespace MLAPI
|
|||||||
//Client trying to send data to host
|
//Client trying to send data to host
|
||||||
clientId = serverClientId;
|
clientId = serverClientId;
|
||||||
}
|
}
|
||||||
NetworkTransport.Send(hostId, clientId, channel, dataToSend, size, out error);
|
NetworkTransport.Send(hostId, clientId, channel, dataToSend, dataToSend.Length, out error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,8 @@ namespace MLAPI.NetworkingManagerComponents
|
|||||||
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
|
||||||
|
internal static Dictionary<ushort, Dictionary<uint, List<int>>> targetedMessages;
|
||||||
|
|
||||||
private static NetworkingManager netManager
|
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 (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;
|
||||||
@ -58,6 +79,7 @@ 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
|
||||||
@ -65,6 +87,7 @@ 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -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)
|
if (counter == -1)
|
||||||
return;
|
return;
|
||||||
@ -86,6 +109,7 @@ 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