fix: Enable custom messages to be sent to self when running as host. (#2296)

This commit is contained in:
Kitty Draper 2022-11-09 11:56:03 -06:00 коммит произвёл GitHub
Родитель 9b3a07d05a
Коммит 8fb7c612f8
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 271 добавлений и 94 удалений

Просмотреть файл

@ -19,12 +19,12 @@ Additional documentation and release notes are available at [Multiplayer Documen
### Fixed
- Custom messages are now properly received by the local client when they're sent while running in host mode. (#2296)
- Fixed issue where the host would receive more than one event completed notification when loading or unloading a scene only when no clients were connected. (#2292)
- Fixed an issue in `UnityTransport` where an error would be logged if the 'Use Encryption' flag was enabled with a Relay configuration that used a secure protocol. (#2289)
- Fixed issue where in-scene placed `NetworkObjects` were not honoring the `AutoObjectParentSync` property. (#2281)
- Fixed the issue where `NetworkManager.OnClientConnectedCallback` was being invoked before in-scene placed `NetworkObject`s had been spawned when starting `NetworkManager` as a host. (#2277)
- Creating a `FastBufferReader` with `Allocator.None` will not result in extra memory being allocated for the buffer (since it's owned externally in that scenario). (#2265)
### Removed
- Removed the `NetworkObject` auto-add and Multiplayer Tools install reminder settings from the Menu interface. (#2285)

Просмотреть файл

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Unity.Collections;
namespace Unity.Netcode
{
@ -68,9 +69,23 @@ namespace Unity.Netcode
if (clientIds == null)
{
throw new ArgumentNullException("You must pass in a valid clientId List");
throw new ArgumentNullException(nameof(clientIds), "You must pass in a valid clientId List");
}
if (m_NetworkManager.IsHost)
{
for (var i = 0; i < clientIds.Count; ++i)
{
if (clientIds[i] == m_NetworkManager.LocalClientId)
{
InvokeUnnamedMessage(
m_NetworkManager.LocalClientId,
new FastBufferReader(messageBuffer, Allocator.None),
0
);
}
}
}
var message = new UnnamedMessage
{
SendData = messageBuffer
@ -92,6 +107,18 @@ namespace Unity.Netcode
/// <param name="networkDelivery">The delivery type (QoS) to send data with</param>
public void SendUnnamedMessage(ulong clientId, FastBufferWriter messageBuffer, NetworkDelivery networkDelivery = NetworkDelivery.ReliableSequenced)
{
if (m_NetworkManager.IsHost)
{
if (clientId == m_NetworkManager.LocalClientId)
{
InvokeUnnamedMessage(
m_NetworkManager.LocalClientId,
new FastBufferReader(messageBuffer, Allocator.None),
0
);
return;
}
}
var message = new UnnamedMessage
{
SendData = messageBuffer
@ -220,6 +247,20 @@ namespace Unity.Netcode
hash = XXHash.Hash64(messageName);
break;
}
if (m_NetworkManager.IsHost)
{
if (clientId == m_NetworkManager.LocalClientId)
{
InvokeNamedMessage(
hash,
m_NetworkManager.LocalClientId,
new FastBufferReader(messageStream, Allocator.None),
0
);
return;
}
}
var message = new NamedMessage
{
@ -251,7 +292,7 @@ namespace Unity.Netcode
if (clientIds == null)
{
throw new ArgumentNullException("You must pass in a valid clientId List");
throw new ArgumentNullException(nameof(clientIds), "You must pass in a valid clientId List");
}
ulong hash = 0;
@ -264,6 +305,21 @@ namespace Unity.Netcode
hash = XXHash.Hash64(messageName);
break;
}
if (m_NetworkManager.IsHost)
{
for (var i = 0; i < clientIds.Count; ++i)
{
if (clientIds[i] == m_NetworkManager.LocalClientId)
{
InvokeNamedMessage(
hash,
m_NetworkManager.LocalClientId,
new FastBufferReader(messageStream, Allocator.None),
0
);
}
}
}
var message = new NamedMessage
{
Hash = hash,

Просмотреть файл

@ -39,7 +39,10 @@ namespace Unity.Netcode.TestHelpers.Runtime
if (AllMessagesReceived)
{
return AllMessagesReceived;
foreach (var entry in m_MessageHookEntries)
{
entry.RemoveHook();
}
}
return AllMessagesReceived;
@ -110,6 +113,11 @@ namespace Unity.Netcode.TestHelpers.Runtime
Initialize();
}
internal void RemoveHook()
{
m_NetworkManager.MessagingSystem.Unhook(MessageHooks);
}
internal void AssignMessageType(Type type)
{
MessageType = type.Name;

Просмотреть файл

@ -3,7 +3,6 @@ using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using Unity.Collections;
using UnityEngine;
using UnityEngine.TestTools;
using Unity.Netcode.TestHelpers.Runtime;
@ -27,16 +26,6 @@ namespace Unity.Netcode.RuntimeTests
public IEnumerator NamedMessageIsReceivedOnClientWithContent()
{
var messageName = Guid.NewGuid().ToString();
var messageContent = new ForceNetworkSerializeByMemcpy<Guid>(Guid.NewGuid());
var writer = new FastBufferWriter(1300, Allocator.Temp);
using (writer)
{
writer.WriteValueSafe(messageContent);
m_ServerNetworkManager.CustomMessagingManager.SendNamedMessage(
messageName,
FirstClient.LocalClientId,
writer);
}
ulong receivedMessageSender = 0;
var receivedMessageContent = new ForceNetworkSerializeByMemcpy<Guid>(new Guid());
@ -49,7 +38,49 @@ namespace Unity.Netcode.RuntimeTests
reader.ReadValueSafe(out receivedMessageContent);
});
yield return new WaitForSeconds(0.2f);
var messageContent = new ForceNetworkSerializeByMemcpy<Guid>(Guid.NewGuid());
var writer = new FastBufferWriter(1300, Allocator.Temp);
using (writer)
{
writer.WriteValueSafe(messageContent);
m_ServerNetworkManager.CustomMessagingManager.SendNamedMessage(
messageName,
FirstClient.LocalClientId,
writer);
}
yield return WaitForMessageReceived<NamedMessage>(new List<NetworkManager> { FirstClient });
Assert.AreEqual(messageContent.Value, receivedMessageContent.Value);
Assert.AreEqual(m_ServerNetworkManager.LocalClientId, receivedMessageSender);
}
[Test]
public void NamedMessageIsReceivedOnHostWithContent()
{
var messageName = Guid.NewGuid().ToString();
ulong receivedMessageSender = 0;
var receivedMessageContent = new ForceNetworkSerializeByMemcpy<Guid>(new Guid());
m_ServerNetworkManager.CustomMessagingManager.RegisterNamedMessageHandler(
messageName,
(ulong sender, FastBufferReader reader) =>
{
receivedMessageSender = sender;
reader.ReadValueSafe(out receivedMessageContent);
});
var messageContent = new ForceNetworkSerializeByMemcpy<Guid>(Guid.NewGuid());
var writer = new FastBufferWriter(1300, Allocator.Temp);
using (writer)
{
writer.WriteValueSafe(messageContent);
m_ServerNetworkManager.CustomMessagingManager.SendNamedMessage(
messageName,
m_ServerNetworkManager.LocalClientId,
writer);
}
Assert.AreEqual(messageContent.Value, receivedMessageContent.Value);
Assert.AreEqual(m_ServerNetworkManager.LocalClientId, receivedMessageSender);
@ -59,16 +90,6 @@ namespace Unity.Netcode.RuntimeTests
public IEnumerator NamedMessageIsReceivedOnMultipleClientsWithContent()
{
var messageName = Guid.NewGuid().ToString();
var messageContent = new ForceNetworkSerializeByMemcpy<Guid>(Guid.NewGuid());
var writer = new FastBufferWriter(1300, Allocator.Temp);
using (writer)
{
writer.WriteValueSafe(messageContent);
m_ServerNetworkManager.CustomMessagingManager.SendNamedMessage(
messageName,
new List<ulong> { FirstClient.LocalClientId, SecondClient.LocalClientId },
writer);
}
ulong firstReceivedMessageSender = 0;
var firstReceivedMessageContent = new ForceNetworkSerializeByMemcpy<Guid>(new Guid());
@ -92,19 +113,78 @@ namespace Unity.Netcode.RuntimeTests
reader.ReadValueSafe(out secondReceivedMessageContent);
});
yield return new WaitForSeconds(0.2f);
ulong thirdReceivedMessageSender = 0;
var thirdReceivedMessageContent = new ForceNetworkSerializeByMemcpy<Guid>(new Guid());
m_ServerNetworkManager.CustomMessagingManager.RegisterNamedMessageHandler(
messageName,
(ulong sender, FastBufferReader reader) =>
{
thirdReceivedMessageSender = sender;
reader.ReadValueSafe(out thirdReceivedMessageContent);
});
var messageContent = new ForceNetworkSerializeByMemcpy<Guid>(Guid.NewGuid());
var writer = new FastBufferWriter(1300, Allocator.Temp);
using (writer)
{
writer.WriteValueSafe(messageContent);
m_ServerNetworkManager.CustomMessagingManager.SendNamedMessage(
messageName,
new List<ulong> { m_ServerNetworkManager.LocalClientId, FirstClient.LocalClientId, SecondClient.LocalClientId },
writer);
}
yield return WaitForMessageReceived<NamedMessage>(new List<NetworkManager> { FirstClient, SecondClient });
Assert.AreEqual(messageContent.Value, firstReceivedMessageContent.Value);
Assert.AreEqual(m_ServerNetworkManager.LocalClientId, firstReceivedMessageSender);
Assert.AreEqual(messageContent.Value, secondReceivedMessageContent.Value);
Assert.AreEqual(m_ServerNetworkManager.LocalClientId, secondReceivedMessageSender);
Assert.AreEqual(messageContent.Value, thirdReceivedMessageContent.Value);
Assert.AreEqual(m_ServerNetworkManager.LocalClientId, thirdReceivedMessageSender);
}
[UnityTest]
public IEnumerator WhenSendingNamedMessageToAll_AllClientsReceiveIt()
{
var messageName = Guid.NewGuid().ToString();
ulong firstReceivedMessageSender = 0;
var firstReceivedMessageContent = new ForceNetworkSerializeByMemcpy<Guid>(new Guid());
FirstClient.CustomMessagingManager.RegisterNamedMessageHandler(
messageName,
(ulong sender, FastBufferReader reader) =>
{
firstReceivedMessageSender = sender;
reader.ReadValueSafe(out firstReceivedMessageContent);
});
ulong secondReceivedMessageSender = 0;
var secondReceivedMessageContent = new ForceNetworkSerializeByMemcpy<Guid>(new Guid());
SecondClient.CustomMessagingManager.RegisterNamedMessageHandler(
messageName,
(ulong sender, FastBufferReader reader) =>
{
secondReceivedMessageSender = sender;
reader.ReadValueSafe(out secondReceivedMessageContent);
});
ulong thirdReceivedMessageSender = 0;
var thirdReceivedMessageContent = new ForceNetworkSerializeByMemcpy<Guid>(new Guid());
m_ServerNetworkManager.CustomMessagingManager.RegisterNamedMessageHandler(
messageName,
(ulong sender, FastBufferReader reader) =>
{
thirdReceivedMessageSender = sender;
reader.ReadValueSafe(out thirdReceivedMessageContent);
});
var messageContent = new ForceNetworkSerializeByMemcpy<Guid>(Guid.NewGuid());
var writer = new FastBufferWriter(1300, Allocator.Temp);
using (writer)
@ -113,35 +193,16 @@ namespace Unity.Netcode.RuntimeTests
m_ServerNetworkManager.CustomMessagingManager.SendNamedMessageToAll(messageName, writer);
}
ulong firstReceivedMessageSender = 0;
var firstReceivedMessageContent = new ForceNetworkSerializeByMemcpy<Guid>(new Guid());
FirstClient.CustomMessagingManager.RegisterNamedMessageHandler(
messageName,
(ulong sender, FastBufferReader reader) =>
{
firstReceivedMessageSender = sender;
reader.ReadValueSafe(out firstReceivedMessageContent);
});
ulong secondReceivedMessageSender = 0;
var secondReceivedMessageContent = new ForceNetworkSerializeByMemcpy<Guid>(new Guid());
SecondClient.CustomMessagingManager.RegisterNamedMessageHandler(
messageName,
(ulong sender, FastBufferReader reader) =>
{
secondReceivedMessageSender = sender;
reader.ReadValueSafe(out secondReceivedMessageContent);
});
yield return new WaitForSeconds(0.2f);
yield return WaitForMessageReceived<NamedMessage>(new List<NetworkManager> { FirstClient, SecondClient });
Assert.AreEqual(messageContent.Value, firstReceivedMessageContent.Value);
Assert.AreEqual(m_ServerNetworkManager.LocalClientId, firstReceivedMessageSender);
Assert.AreEqual(messageContent.Value, secondReceivedMessageContent.Value);
Assert.AreEqual(m_ServerNetworkManager.LocalClientId, secondReceivedMessageSender);
Assert.AreEqual(messageContent.Value, thirdReceivedMessageContent.Value);
Assert.AreEqual(m_ServerNetworkManager.LocalClientId, thirdReceivedMessageSender);
}
[Test]

Просмотреть файл

@ -3,7 +3,6 @@ using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using Unity.Collections;
using UnityEngine;
using UnityEngine.TestTools;
using Unity.Netcode.TestHelpers.Runtime;
@ -19,16 +18,6 @@ namespace Unity.Netcode.RuntimeTests
[UnityTest]
public IEnumerator UnnamedMessageIsReceivedOnClientWithContent()
{
var messageContent = new ForceNetworkSerializeByMemcpy<Guid>(Guid.NewGuid());
var writer = new FastBufferWriter(1300, Allocator.Temp);
using (writer)
{
writer.WriteValueSafe(messageContent);
m_ServerNetworkManager.CustomMessagingManager.SendUnnamedMessage(
FirstClient.LocalClientId,
writer);
}
ulong receivedMessageSender = 0;
var receivedMessageContent = new ForceNetworkSerializeByMemcpy<Guid>(new Guid());
FirstClient.CustomMessagingManager.OnUnnamedMessage +=
@ -39,7 +28,44 @@ namespace Unity.Netcode.RuntimeTests
reader.ReadValueSafe(out receivedMessageContent);
};
yield return new WaitForSeconds(0.2f);
var messageContent = new ForceNetworkSerializeByMemcpy<Guid>(Guid.NewGuid());
var writer = new FastBufferWriter(1300, Allocator.Temp);
using (writer)
{
writer.WriteValueSafe(messageContent);
m_ServerNetworkManager.CustomMessagingManager.SendUnnamedMessage(
FirstClient.LocalClientId,
writer);
}
yield return WaitForMessageReceived<UnnamedMessage>(new List<NetworkManager> { FirstClient });
Assert.AreEqual(messageContent.Value, receivedMessageContent.Value);
Assert.AreEqual(m_ServerNetworkManager.LocalClientId, receivedMessageSender);
}
[Test]
public void UnnamedMessageIsReceivedOnHostWithContent()
{
ulong receivedMessageSender = 0;
var receivedMessageContent = new ForceNetworkSerializeByMemcpy<Guid>(new Guid());
m_ServerNetworkManager.CustomMessagingManager.OnUnnamedMessage +=
(ulong sender, FastBufferReader reader) =>
{
receivedMessageSender = sender;
reader.ReadValueSafe(out receivedMessageContent);
};
var messageContent = new ForceNetworkSerializeByMemcpy<Guid>(Guid.NewGuid());
var writer = new FastBufferWriter(1300, Allocator.Temp);
using (writer)
{
writer.WriteValueSafe(messageContent);
m_ServerNetworkManager.CustomMessagingManager.SendUnnamedMessage(
m_ServerNetworkManager.LocalClientId,
writer);
}
Assert.AreEqual(messageContent.Value, receivedMessageContent.Value);
Assert.AreEqual(m_ServerNetworkManager.LocalClientId, receivedMessageSender);
@ -48,16 +74,6 @@ namespace Unity.Netcode.RuntimeTests
[UnityTest]
public IEnumerator UnnamedMessageIsReceivedOnMultipleClientsWithContent()
{
var messageContent = new ForceNetworkSerializeByMemcpy<Guid>(Guid.NewGuid());
var writer = new FastBufferWriter(1300, Allocator.Temp);
using (writer)
{
writer.WriteValueSafe(messageContent);
m_ServerNetworkManager.CustomMessagingManager.SendUnnamedMessage(
new List<ulong> { FirstClient.LocalClientId, SecondClient.LocalClientId },
writer);
}
ulong firstReceivedMessageSender = 0;
var firstReceivedMessageContent = new ForceNetworkSerializeByMemcpy<Guid>(new Guid());
FirstClient.CustomMessagingManager.OnUnnamedMessage +=
@ -78,18 +94,71 @@ namespace Unity.Netcode.RuntimeTests
reader.ReadValueSafe(out secondReceivedMessageContent);
};
yield return new WaitForSeconds(0.2f);
ulong thirdReceivedMessageSender = 0;
var thirdReceivedMessageContent = new ForceNetworkSerializeByMemcpy<Guid>(new Guid());
m_ServerNetworkManager.CustomMessagingManager.OnUnnamedMessage +=
(ulong sender, FastBufferReader reader) =>
{
thirdReceivedMessageSender = sender;
reader.ReadValueSafe(out thirdReceivedMessageContent);
};
var messageContent = new ForceNetworkSerializeByMemcpy<Guid>(Guid.NewGuid());
var writer = new FastBufferWriter(1300, Allocator.Temp);
using (writer)
{
writer.WriteValueSafe(messageContent);
m_ServerNetworkManager.CustomMessagingManager.SendUnnamedMessage(
new List<ulong> { m_ServerNetworkManager.LocalClientId, FirstClient.LocalClientId, SecondClient.LocalClientId },
writer);
}
yield return WaitForMessageReceived<UnnamedMessage>(new List<NetworkManager> { FirstClient, SecondClient });
Assert.AreEqual(messageContent.Value, firstReceivedMessageContent.Value);
Assert.AreEqual(m_ServerNetworkManager.LocalClientId, firstReceivedMessageSender);
Assert.AreEqual(messageContent.Value, secondReceivedMessageContent.Value);
Assert.AreEqual(m_ServerNetworkManager.LocalClientId, secondReceivedMessageSender);
Assert.AreEqual(messageContent.Value, thirdReceivedMessageContent.Value);
Assert.AreEqual(m_ServerNetworkManager.LocalClientId, thirdReceivedMessageSender);
}
[UnityTest]
public IEnumerator WhenSendingUnnamedMessageToAll_AllClientsReceiveIt()
{
ulong firstReceivedMessageSender = 0;
var firstReceivedMessageContent = new ForceNetworkSerializeByMemcpy<Guid>(new Guid());
FirstClient.CustomMessagingManager.OnUnnamedMessage +=
(ulong sender, FastBufferReader reader) =>
{
firstReceivedMessageSender = sender;
reader.ReadValueSafe(out firstReceivedMessageContent);
};
ulong secondReceivedMessageSender = 0;
var secondReceivedMessageContent = new ForceNetworkSerializeByMemcpy<Guid>(new Guid());
SecondClient.CustomMessagingManager.OnUnnamedMessage +=
(ulong sender, FastBufferReader reader) =>
{
secondReceivedMessageSender = sender;
reader.ReadValueSafe(out secondReceivedMessageContent);
};
ulong thirdReceivedMessageSender = 0;
var thirdReceivedMessageContent = new ForceNetworkSerializeByMemcpy<Guid>(new Guid());
m_ServerNetworkManager.CustomMessagingManager.OnUnnamedMessage +=
(ulong sender, FastBufferReader reader) =>
{
thirdReceivedMessageSender = sender;
reader.ReadValueSafe(out thirdReceivedMessageContent);
};
var messageContent = new ForceNetworkSerializeByMemcpy<Guid>(Guid.NewGuid());
var writer = new FastBufferWriter(1300, Allocator.Temp);
using (writer)
@ -98,33 +167,16 @@ namespace Unity.Netcode.RuntimeTests
m_ServerNetworkManager.CustomMessagingManager.SendUnnamedMessageToAll(writer);
}
ulong firstReceivedMessageSender = 0;
var firstReceivedMessageContent = new ForceNetworkSerializeByMemcpy<Guid>(new Guid());
FirstClient.CustomMessagingManager.OnUnnamedMessage +=
(ulong sender, FastBufferReader reader) =>
{
firstReceivedMessageSender = sender;
reader.ReadValueSafe(out firstReceivedMessageContent);
};
ulong secondReceivedMessageSender = 0;
var secondReceivedMessageContent = new ForceNetworkSerializeByMemcpy<Guid>(new Guid());
SecondClient.CustomMessagingManager.OnUnnamedMessage +=
(ulong sender, FastBufferReader reader) =>
{
secondReceivedMessageSender = sender;
reader.ReadValueSafe(out secondReceivedMessageContent);
};
yield return new WaitForSeconds(0.2f);
yield return WaitForMessageReceived<UnnamedMessage>(new List<NetworkManager> { FirstClient, SecondClient });
Assert.AreEqual(messageContent.Value, firstReceivedMessageContent.Value);
Assert.AreEqual(m_ServerNetworkManager.LocalClientId, firstReceivedMessageSender);
Assert.AreEqual(messageContent.Value, secondReceivedMessageContent.Value);
Assert.AreEqual(m_ServerNetworkManager.LocalClientId, secondReceivedMessageSender);
Assert.AreEqual(messageContent.Value, thirdReceivedMessageContent.Value);
Assert.AreEqual(m_ServerNetworkManager.LocalClientId, thirdReceivedMessageSender);
}
[Test]