custom messages can be sent by anyone on server

main
Kyle Johnsen 2022-06-23 14:00:15 -04:00
parent 3154fc5094
commit 6c80452538
2 changed files with 28 additions and 18 deletions

View File

@ -469,7 +469,8 @@ namespace VelNet
}
else
{
Debug.LogError("Received message from player that doesn't exist ");
LocalPlayer?.HandleMessage(dm, true);
//Debug.LogError("Received message from player that doesn't exist ");
}
break;

View File

@ -68,7 +68,7 @@ namespace VelNet
///
/// The length of the byte[] for message is fixed according to the message type
/// </summary>
public void HandleMessage(VelNetManager.DataMessage m)
public void HandleMessage(VelNetManager.DataMessage m, bool unknown_sender = false)
{
using MemoryStream mem = new MemoryStream(m.data);
using BinaryReader reader = new BinaryReader(mem);
@ -76,6 +76,30 @@ namespace VelNet
//individual message parameters separated by comma
VelNetManager.MessageType messageType = (VelNetManager.MessageType)reader.ReadByte();
if(messageType == VelNetManager.MessageType.Custom)
{
// Custom packets. These are global data that can be sent from anywhere.
// Any script can subscribe to the callback to receive the message data.
// todo: strange hack that any player can handle the custom message, which then simply calls velnetmanager.
int len = reader.ReadInt32();
try
{
VelNetManager.CustomMessageReceived?.Invoke(m.senderId, reader.ReadBytes(len));
}
catch (Exception e)
{
Debug.LogError(e);
}
return;
}
if (unknown_sender)
{
Debug.LogError("Received non-custom message from player that doesn't exist ");
return;
}
switch (messageType)
{
// sync update for an object "I" may own
@ -147,22 +171,7 @@ namespace VelNet
break;
}
// Custom packets. These are global data that can be sent from anywhere.
// Any script can subscribe to the callback to receive the message data.
case VelNetManager.MessageType.Custom:
{
int len = reader.ReadInt32();
try
{
VelNetManager.CustomMessageReceived?.Invoke(m.senderId, reader.ReadBytes(len));
}
catch (Exception e)
{
Debug.LogError(e);
}
break;
}
default:
throw new ArgumentOutOfRangeException();
}