Some classes for syncing entity state changes. Similar to the NetworkEvents in the old netcode, but the logic is split into separate classes which prevent the server from reading updates for entities that aren't IClientSerializable.
todo: add NetEntityEventManagers to server & client, some logic to prevent sending events that don't need to be sent (e.g. duplicate event state updates)
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
using Lidgren.Network;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Barotrauma.Networking
|
||||
{
|
||||
class ClientEntityEventManager : NetEntityEventManager
|
||||
{
|
||||
private List<ClientEntityEvent> events;
|
||||
|
||||
private UInt32 ID;
|
||||
|
||||
private GameClient thisClient;
|
||||
|
||||
public ClientEntityEventManager(GameClient client) { }
|
||||
|
||||
public void CreateEvent(IClientSerializable entity)
|
||||
{
|
||||
if (!(entity is Entity))
|
||||
{
|
||||
DebugConsole.ThrowError("Can't create an entity event for " + entity + "!");
|
||||
return;
|
||||
}
|
||||
|
||||
ID++;
|
||||
events.Add(new ClientEntityEvent(entity, ID));
|
||||
}
|
||||
|
||||
public void Write(NetOutgoingMessage msg)
|
||||
{
|
||||
var eventsToSync = events.SkipWhile(e => e.ID >= thisClient.LastSentEntityEventID).ToList();
|
||||
if (eventsToSync.Count == 0) return;
|
||||
|
||||
Write(msg, eventsToSync.Cast<NetEntityEvent>().ToList());
|
||||
}
|
||||
|
||||
protected override void WriteEvent(NetBuffer buffer, NetEntityEvent entityEvent, Client recipient = null)
|
||||
{
|
||||
var clientEvent = entityEvent as ClientEntityEvent;
|
||||
if (clientEvent == null) return;
|
||||
|
||||
clientEvent.Write(buffer);
|
||||
}
|
||||
|
||||
protected override void ReadEvent(NetIncomingMessage buffer, INetSerializable entity, float sendingTime, Client sender = null)
|
||||
{
|
||||
var clientEntity = entity as IClientSerializable;
|
||||
if (clientEntity == null) return;
|
||||
|
||||
clientEntity.ServerRead(buffer, sender);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using Lidgren.Network;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Barotrauma.Networking
|
||||
{
|
||||
abstract class NetEntityEvent
|
||||
{
|
||||
public readonly Entity Entity;
|
||||
public readonly UInt32 ID;
|
||||
|
||||
protected NetEntityEvent(INetSerializable entity, UInt32 id)
|
||||
{
|
||||
this.ID = id;
|
||||
this.Entity = entity as Entity;
|
||||
}
|
||||
}
|
||||
|
||||
class ServerEntityEvent : NetEntityEvent
|
||||
{
|
||||
private IServerSerializable serializable;
|
||||
|
||||
public ServerEntityEvent(IServerSerializable entity, UInt32 id)
|
||||
: base(entity, id)
|
||||
{
|
||||
serializable = entity;
|
||||
}
|
||||
|
||||
public void Write(NetBuffer msg, Client recipient)
|
||||
{
|
||||
serializable.ServerWrite(msg, recipient);
|
||||
}
|
||||
}
|
||||
|
||||
class ClientEntityEvent : NetEntityEvent
|
||||
{
|
||||
private IClientSerializable serializable;
|
||||
|
||||
public ClientEntityEvent(IClientSerializable entity, UInt32 id)
|
||||
: base(entity, id)
|
||||
{
|
||||
serializable = entity;
|
||||
}
|
||||
|
||||
public void Write(NetBuffer msg)
|
||||
{
|
||||
serializable.ClientWrite(msg);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
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
|
||||
{
|
||||
const int MaxEventsPerWrite = 255;
|
||||
|
||||
private UInt32 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)
|
||||
{
|
||||
//too many events for one packet
|
||||
if (eventsToSync.Count > MaxEventsPerWrite)
|
||||
{
|
||||
eventsToSync.RemoveRange(MaxEventsPerWrite, eventsToSync.Count - MaxEventsPerWrite);
|
||||
}
|
||||
|
||||
msg.Write((byte)ServerNetObject.ENTITY_STATE);
|
||||
|
||||
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();
|
||||
WriteEvent(tempBuffer, e, recipient);
|
||||
tempBuffer.WritePadBits();
|
||||
|
||||
Debug.Assert(
|
||||
tempBuffer.LengthBytes < 256,
|
||||
"Maximum EntityEvent size exceeded when serializing \""+e.Entity+"\"!");
|
||||
|
||||
msg.Write((UInt16)e.Entity.ID);
|
||||
msg.Write((byte)tempBuffer.LengthBytes);
|
||||
msg.Write(tempBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read the events from the message, ignoring ones we've already received
|
||||
/// </summary>
|
||||
public void Read(NetIncomingMessage msg, float sendingTime)
|
||||
{
|
||||
UInt32 firstEventID = msg.ReadUInt32();
|
||||
byte eventCount = msg.ReadByte();
|
||||
|
||||
for (int i = 0; i < eventCount; i++)
|
||||
{
|
||||
UInt32 thisEventID = firstEventID + (UInt32)i;
|
||||
UInt16 entityID = msg.ReadUInt16();
|
||||
byte msgLength = msg.ReadByte();
|
||||
|
||||
INetSerializable entity = Entity.FindEntityByID(entityID) as INetSerializable;
|
||||
|
||||
//skip the event if we've already received it or if the entity isn't found
|
||||
if (thisEventID <= lastReceivedEntityEventID || entity == null)
|
||||
{
|
||||
msg.Position += msgLength * 8;
|
||||
}
|
||||
else
|
||||
{
|
||||
ReadEvent(msg, entity, sendingTime);
|
||||
lastReceivedEntityEventID = thisEventID;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void WriteEvent(NetBuffer buffer, NetEntityEvent entityEvent, Client recipient = null)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
protected virtual void ReadEvent(NetIncomingMessage buffer, INetSerializable entity, float sendingTime, Client sender = null)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
using Lidgren.Network;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Barotrauma.Networking
|
||||
{
|
||||
class ServerEntityEventManager : NetEntityEventManager
|
||||
{
|
||||
private List<ServerEntityEvent> events;
|
||||
|
||||
private UInt32 ID;
|
||||
|
||||
public ServerEntityEventManager(GameServer server) { }
|
||||
|
||||
public void CreateEvent(IServerSerializable entity)
|
||||
{
|
||||
if (!(entity is Entity))
|
||||
{
|
||||
DebugConsole.ThrowError("Can't create an entity event for " + entity + "!");
|
||||
return;
|
||||
}
|
||||
|
||||
ID++;
|
||||
events.Add(new ServerEntityEvent(entity, ID));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes all the events that the client hasn't received yet into the outgoing message
|
||||
/// </summary>
|
||||
public void Write(Client client, NetOutgoingMessage msg)
|
||||
{
|
||||
var eventsToSync = events.SkipWhile(e => e.ID >= client.lastRecvEntityEventID).ToList();
|
||||
if (eventsToSync.Count == 0) return;
|
||||
|
||||
Write(msg, eventsToSync.Cast<NetEntityEvent>().ToList(), client);
|
||||
}
|
||||
|
||||
protected override void WriteEvent(NetBuffer buffer, NetEntityEvent entityEvent, Client recipient = null)
|
||||
{
|
||||
var serverEvent = entityEvent as ServerEntityEvent;
|
||||
if (serverEvent == null) return;
|
||||
|
||||
serverEvent.Write(buffer, recipient);
|
||||
}
|
||||
|
||||
protected override void ReadEvent(NetIncomingMessage buffer, INetSerializable entity, float sendingTime, Client sender = null)
|
||||
{
|
||||
var clientEntity = entity as IClientSerializable;
|
||||
if (clientEntity == null) return;
|
||||
|
||||
clientEntity.ServerRead(buffer, sender);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user