Server can no longer send messages to itself

This commit is contained in:
Albin Corén 2018-03-06 21:42:48 +01:00
parent c560614616
commit 4e39d37722
4 changed files with 27 additions and 71 deletions

View File

@ -126,12 +126,10 @@ namespace MLAPI
}
if (isServer)
{
MessageManager.InvokeMessageHandlers(messageType, data, -1);
}
else
{
NetworkingManager.singleton.Send(NetworkingManager.singleton.serverClientId, messageType, channelName, data);
Debug.LogWarning("MLAPI: Server can not send messages to server.");
return;
}
NetworkingManager.singleton.Send(NetworkingManager.singleton.serverClientId, messageType, channelName, data);
}
protected void SendToServerTarget(string messageType, string channelName, byte[] data)
@ -143,12 +141,10 @@ namespace MLAPI
}
if (isServer)
{
MessageManager.InvokeTargetedMessageHandler(messageType, data, -1, networkId);
}
else
{
NetworkingManager.singleton.Send(NetworkingManager.singleton.serverClientId, messageType, channelName, data, networkId);
Debug.LogWarning("MLAPI: Server can not send messages to server.");
return;
}
NetworkingManager.singleton.Send(NetworkingManager.singleton.serverClientId, messageType, channelName, data, networkId);
}
protected void SendToLocalClient(string messageType, string channelName, byte[] data)
@ -181,7 +177,7 @@ namespace MLAPI
NetworkingManager.singleton.Send(ownerClientId, messageType, channelName, data, networkId);
}
protected void SendToNonLocalClients(string messageType, string channelName, byte[] data, bool ignoreHost = false)
protected void SendToNonLocalClients(string messageType, string channelName, byte[] data)
{
if (MessageManager.messageTypes[messageType] < 32)
{
@ -193,10 +189,10 @@ 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, ignoreHost);
NetworkingManager.singleton.Send(messageType, channelName, data, ownerClientId, null);
}
protected void SendToNonLocalClientsTarget(string messageType, string channelName, byte[] data, bool ignoreHost = false)
protected void SendToNonLocalClientsTarget(string messageType, string channelName, byte[] data)
{
if (MessageManager.messageTypes[messageType] < 32)
{
@ -208,7 +204,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, true);
NetworkingManager.singleton.Send(messageType, channelName, data, ownerClientId, networkId);
}
protected void SendToClient(int clientId, string messageType, string channelName, byte[] data)

View File

@ -670,10 +670,7 @@ namespace MLAPI
if (isHost && targetId == -1)
{
//Host trying to send data to it's own client
if (networkId == null)
MessageManager.InvokeMessageHandlers(MessageManager.reverseMessageTypes[messageType], data, sourceId);
else
MessageManager.InvokeTargetedMessageHandler(MessageManager.reverseMessageTypes[messageType], data, sourceId, networkId.Value);
Debug.LogWarning("MLAPI: Send method got message aimed at server from the server?");
return;
}
@ -701,13 +698,10 @@ namespace MLAPI
internal void Send(int clientId, string messageType, string channelName, byte[] data, uint? networkId = null)
{
if(isHost && clientId == -1)
if(clientId == -1 && isHost)
{
//Host trying to send data to it's own client
if (networkId == null)
MessageManager.InvokeMessageHandlers(messageType, data, clientId);
else
MessageManager.InvokeTargetedMessageHandler(messageType, data, clientId, networkId.Value);
//Don't invoke the message on our own machine. Instant stack overflow.
Debug.LogWarning("MLAPI: Cannot send message to own client");
return;
}
else if(clientId == -1)
@ -775,10 +769,7 @@ namespace MLAPI
int clientId = clientIds[i];
if (isHost && clientId == -1)
{
if (networkId == null)
MessageManager.InvokeMessageHandlers(messageType, data, clientId);
else
MessageManager.InvokeTargetedMessageHandler(messageType, data, clientId, networkId.Value);
//Don't invoke the message on our own machine. Instant stack overflow.
continue;
}
else if (clientId == -1)
@ -815,12 +806,9 @@ namespace MLAPI
for (int i = 0; i < clientIds.Count; i++)
{
int clientId = clientIds[i];
if (isHost && clientId == -1)
if (clientId == -1 && isHost)
{
if (networkId == null)
MessageManager.InvokeMessageHandlers(messageType, data, clientId);
else
MessageManager.InvokeTargetedMessageHandler(messageType, data, clientId, networkId.Value);
//Don't invoke the message on our own machine. Instant stack overflow.
continue;
}
else if (clientId == -1)
@ -859,10 +847,7 @@ namespace MLAPI
int clientId = pair.Key;
if(isHost && pair.Key == -1)
{
if (networkId == null)
MessageManager.InvokeMessageHandlers(messageType, data, clientId);
else
MessageManager.InvokeTargetedMessageHandler(messageType, data, clientId, networkId.Value);
//Don't invoke the message on our own machine. Instant stack overflow.
continue;
}
else if (clientId == -1)
@ -876,7 +861,7 @@ namespace MLAPI
}
}
internal void Send(string messageType, string channelName, byte[] data, int clientIdToIgnore, uint? networkId = null, bool ignoreHost = false)
internal void Send(string messageType, string channelName, byte[] data, int clientIdToIgnore, uint? networkId = null)
{
//2 bytes for messageType, 2 bytes for buffer length and one byte for target bool
int sizeOfStream = 5;
@ -902,12 +887,9 @@ namespace MLAPI
if (pair.Key == clientIdToIgnore)
continue;
int clientId = pair.Key;
if (isHost && pair.Key == -1 && !ignoreHost)
if (isHost && pair.Key == -1)
{
if (networkId == null)
MessageManager.InvokeMessageHandlers(messageType, data, clientId);
else
MessageManager.InvokeTargetedMessageHandler(messageType, data, clientId, networkId.Value);
//Don't invoke the message on our own machine. Instant stack overflow.
continue;
}
else if (clientId == -1)

View File

@ -96,7 +96,7 @@ namespace MLAPI
}
if(isServer)
{
SendToNonLocalClientsTarget("MLAPI_HandleAnimationMessage", "MLAPI_ANIMATION_UPDATE", stream.ToArray(), true);
SendToNonLocalClientsTarget("MLAPI_HandleAnimationMessage", "MLAPI_ANIMATION_UPDATE", stream.ToArray());
}
else
{
@ -154,7 +154,7 @@ namespace MLAPI
}
if (isServer)
{
SendToNonLocalClientsTarget("MLAPI_HandleAnimationParameterMessage", "MLAPI_ANIMATION_UPDATE", stream.ToArray(), true);
SendToNonLocalClientsTarget("MLAPI_HandleAnimationParameterMessage", "MLAPI_ANIMATION_UPDATE", stream.ToArray());
}
else
{
@ -194,7 +194,7 @@ namespace MLAPI
if(isServer)
{
SendToNonLocalClientsTarget("MLAPI_HandleAnimationMessage", "MLAPI_ANIMATION_UPDATE", data, true);
SendToNonLocalClientsTarget("MLAPI_HandleAnimationMessage", "MLAPI_ANIMATION_UPDATE", data);
}
using(MemoryStream stream = new MemoryStream(data))
{
@ -215,7 +215,7 @@ namespace MLAPI
{
if (isServer)
{
SendToNonLocalClientsTarget("MLAPI_HandleAnimationParameterMessage", "MLAPI_ANIMATION_UPDATE", data, true);
SendToNonLocalClientsTarget("MLAPI_HandleAnimationParameterMessage", "MLAPI_ANIMATION_UPDATE", data);
}
using (MemoryStream stream = new MemoryStream(data))
{
@ -230,7 +230,7 @@ namespace MLAPI
{
if (isServer)
{
SendToNonLocalClientsTarget("MLAPI_HandleAnimationTriggerMessage", "MLAPI_ANIMATION_UPDATE", data, true);
SendToNonLocalClientsTarget("MLAPI_HandleAnimationTriggerMessage", "MLAPI_ANIMATION_UPDATE", data);
}
using (MemoryStream stream = new MemoryStream(data))
{
@ -329,7 +329,7 @@ namespace MLAPI
}
if (isServer)
{
SendToNonLocalClientsTarget("MLAPI_HandleAnimationTriggerMessage", "MLAPI_ANIMATION_UPDATE", stream.ToArray(), true);
SendToNonLocalClientsTarget("MLAPI_HandleAnimationTriggerMessage", "MLAPI_ANIMATION_UPDATE", stream.ToArray());
}
else
{

View File

@ -24,28 +24,6 @@ namespace MLAPI.NetworkingManagerComponents
}
}
internal static void InvokeMessageHandlers(string messageType, byte[] data, int clientId)
{
if (!messageTypes.ContainsKey(messageType) || !messageCallbacks.ContainsKey(messageTypes[messageType]))
return;
foreach (KeyValuePair<int, Action<int, byte[]>> pair in messageCallbacks[messageTypes[messageType]])
{
pair.Value(clientId, data);
}
}
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))