Not sending the events at all would be a better solution, but then we'd need to shift the IDs of all the consecutive events and make sure it doesn't mess anything up with any of the clients. Not necessarily worth the effort, considering how rare these "empty events" are.
70 lines
2.3 KiB
C#
70 lines
2.3 KiB
C#
using Lidgren.Network;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Barotrauma.Networking
|
|
{
|
|
abstract class NetEntityEventManager
|
|
{
|
|
public const int MaxEventBufferLength = 1024;
|
|
public const int MaxEventsPerWrite = 64;
|
|
|
|
//public UInt16 LastReceivedEntityEventID
|
|
//{
|
|
// get { return lastReceivedEntityEventID; }
|
|
//}
|
|
|
|
/// <summary>
|
|
/// Write the events to the outgoing message. The recipient parameter is only needed for ServerEntityEventManager
|
|
/// </summary>
|
|
protected void Write(NetOutgoingMessage msg, List<NetEntityEvent> eventsToSync, Client recipient = null)
|
|
{
|
|
msg.Write(eventsToSync[0].ID);
|
|
msg.Write((byte)eventsToSync.Count);
|
|
|
|
foreach (NetEntityEvent e in eventsToSync)
|
|
{
|
|
//write into a temporary buffer so we can write the length before the actual data
|
|
NetBuffer tempBuffer = new NetBuffer();
|
|
try
|
|
{
|
|
WriteEvent(tempBuffer, e, recipient);
|
|
}
|
|
|
|
catch (Exception exception)
|
|
{
|
|
DebugConsole.ThrowError("Failed to write an event for the entity \""+e.Entity+"\"", exception);
|
|
continue;
|
|
}
|
|
|
|
Debug.Assert(
|
|
tempBuffer.LengthBytes < 128,
|
|
"Maximum EntityEvent size exceeded when serializing \""+e.Entity+"\"!");
|
|
|
|
if (Entity.FindEntityByID(e.Entity.ID) != e.Entity)
|
|
{
|
|
//DebugConsole.ThrowError("Error in NetEntityEventManager.Write (FindEntityByID(e.Entity.ID) != e.Entity)");
|
|
msg.Write((UInt16)0);
|
|
msg.WritePadBits();
|
|
}
|
|
else
|
|
{
|
|
msg.Write((UInt16)e.Entity.ID);
|
|
msg.Write((byte)tempBuffer.LengthBytes);
|
|
msg.Write(tempBuffer);
|
|
msg.WritePadBits();
|
|
}
|
|
}
|
|
}
|
|
|
|
protected virtual void WriteEvent(NetBuffer buffer, NetEntityEvent entityEvent, Client recipient = null)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
}
|