main
Anton Franzluebbers 2022-07-19 16:37:04 -04:00
commit 5506cd71fa
2 changed files with 31 additions and 22 deletions

View File

@ -508,7 +508,7 @@ namespace VelNet
} }
else else
{ {
VelNetLogger.Error("Received message from player that doesn't exist "); LocalPlayer?.HandleMessage(dm, true);
} }
break; break;
@ -861,7 +861,7 @@ namespace VelNet
} }
} }
} }
catch (ThreadAbortException ex) catch (ThreadAbortException)
{ {
// pass // pass
} }
@ -936,7 +936,7 @@ namespace VelNet
} }
} }
} }
catch (ThreadAbortException ex) catch (ThreadAbortException)
{ {
// pass // pass
} }

View File

@ -67,7 +67,7 @@ namespace VelNet
/// ///
/// The length of the byte[] for message is fixed according to the message type /// The length of the byte[] for message is fixed according to the message type
/// </summary> /// </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 MemoryStream mem = new MemoryStream(m.data);
using BinaryReader reader = new BinaryReader(mem); using BinaryReader reader = new BinaryReader(mem);
@ -75,6 +75,30 @@ namespace VelNet
//individual message parameters separated by comma //individual message parameters separated by comma
VelNetManager.MessageType messageType = (VelNetManager.MessageType)reader.ReadByte(); 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)
{
VelNetLogger.Error(e.ToString());
}
return;
}
if (unknown_sender)
{
VelNetLogger.Error("Received non-custom message from player that doesn't exist ");
return;
}
switch (messageType) switch (messageType)
{ {
// sync update for an object "I" may own // sync update for an object "I" may own
@ -112,7 +136,7 @@ namespace VelNet
} }
catch (Exception e) catch (Exception e)
{ {
Debug.LogError("Error in event handling.\n" + e); VelNetLogger.Error("Error in event handling.\n" + e);
} }
} }
@ -146,22 +170,7 @@ namespace VelNet
break; 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: default:
throw new ArgumentOutOfRangeException(); throw new ArgumentOutOfRangeException();
} }
@ -245,7 +254,7 @@ namespace VelNet
} }
catch (Exception e) catch (Exception e)
{ {
Debug.LogError("Error in event handling.\n" + e); VelNetLogger.Error("Error in event handling.\n" + e);
} }
// must be ordered, so that ownership transfers are not confused. // must be ordered, so that ownership transfers are not confused.