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) if (isServer)
{ {
MessageManager.InvokeMessageHandlers(messageType, data, -1); Debug.LogWarning("MLAPI: Server can not send messages to server.");
} return;
else
{
NetworkingManager.singleton.Send(NetworkingManager.singleton.serverClientId, messageType, channelName, data);
} }
NetworkingManager.singleton.Send(NetworkingManager.singleton.serverClientId, messageType, channelName, data);
} }
protected void SendToServerTarget(string messageType, string channelName, byte[] data) protected void SendToServerTarget(string messageType, string channelName, byte[] data)
@ -143,12 +141,10 @@ namespace MLAPI
} }
if (isServer) if (isServer)
{ {
MessageManager.InvokeTargetedMessageHandler(messageType, data, -1, networkId); Debug.LogWarning("MLAPI: Server can not send messages to server.");
} return;
else
{
NetworkingManager.singleton.Send(NetworkingManager.singleton.serverClientId, messageType, channelName, data, networkId);
} }
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)
@ -181,7 +177,7 @@ namespace MLAPI
NetworkingManager.singleton.Send(ownerClientId, messageType, channelName, data, networkId); 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) 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"); Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
return; 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) 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"); Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
return; 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) protected void SendToClient(int clientId, string messageType, string channelName, byte[] data)

View File

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

View File

@ -96,7 +96,7 @@ namespace MLAPI
} }
if(isServer) if(isServer)
{ {
SendToNonLocalClientsTarget("MLAPI_HandleAnimationMessage", "MLAPI_ANIMATION_UPDATE", stream.ToArray(), true); SendToNonLocalClientsTarget("MLAPI_HandleAnimationMessage", "MLAPI_ANIMATION_UPDATE", stream.ToArray());
} }
else else
{ {
@ -154,7 +154,7 @@ namespace MLAPI
} }
if (isServer) if (isServer)
{ {
SendToNonLocalClientsTarget("MLAPI_HandleAnimationParameterMessage", "MLAPI_ANIMATION_UPDATE", stream.ToArray(), true); SendToNonLocalClientsTarget("MLAPI_HandleAnimationParameterMessage", "MLAPI_ANIMATION_UPDATE", stream.ToArray());
} }
else else
{ {
@ -194,7 +194,7 @@ namespace MLAPI
if(isServer) if(isServer)
{ {
SendToNonLocalClientsTarget("MLAPI_HandleAnimationMessage", "MLAPI_ANIMATION_UPDATE", data, true); SendToNonLocalClientsTarget("MLAPI_HandleAnimationMessage", "MLAPI_ANIMATION_UPDATE", data);
} }
using(MemoryStream stream = new MemoryStream(data)) using(MemoryStream stream = new MemoryStream(data))
{ {
@ -215,7 +215,7 @@ namespace MLAPI
{ {
if (isServer) if (isServer)
{ {
SendToNonLocalClientsTarget("MLAPI_HandleAnimationParameterMessage", "MLAPI_ANIMATION_UPDATE", data, true); SendToNonLocalClientsTarget("MLAPI_HandleAnimationParameterMessage", "MLAPI_ANIMATION_UPDATE", data);
} }
using (MemoryStream stream = new MemoryStream(data)) using (MemoryStream stream = new MemoryStream(data))
{ {
@ -230,7 +230,7 @@ namespace MLAPI
{ {
if (isServer) if (isServer)
{ {
SendToNonLocalClientsTarget("MLAPI_HandleAnimationTriggerMessage", "MLAPI_ANIMATION_UPDATE", data, true); SendToNonLocalClientsTarget("MLAPI_HandleAnimationTriggerMessage", "MLAPI_ANIMATION_UPDATE", data);
} }
using (MemoryStream stream = new MemoryStream(data)) using (MemoryStream stream = new MemoryStream(data))
{ {
@ -329,7 +329,7 @@ namespace MLAPI
} }
if (isServer) if (isServer)
{ {
SendToNonLocalClientsTarget("MLAPI_HandleAnimationTriggerMessage", "MLAPI_ANIMATION_UPDATE", stream.ToArray(), true); SendToNonLocalClientsTarget("MLAPI_HandleAnimationTriggerMessage", "MLAPI_ANIMATION_UPDATE", stream.ToArray());
} }
else 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) internal static int AddIncomingMessageHandler(string name, Action<int, byte[]> action, uint networkId)
{ {
if (messageTypes.ContainsKey(name)) if (messageTypes.ContainsKey(name))