Sending NetEntityEvents from client to server, ItemComponent class doesn't implement the INetSerializable interfaces (only the derived classes do), reactor syncing

This commit is contained in:
Regalis
2016-11-13 19:34:23 +02:00
parent 724172fe7c
commit 3d234aef73
16 changed files with 146 additions and 59 deletions

View File

@@ -15,12 +15,26 @@ namespace Barotrauma.Networking
private GameClient thisClient;
//when was a specific entity event last sent to the client
// key = event id, value = NetTime.Now when sending
public Dictionary<UInt32, float> eventLastSent;
public UInt32 LastReceivedID
{
get { return lastReceivedID; }
}
private UInt32 lastReceivedID;
public ClientEntityEventManager(GameClient client)
{
events = new List<ClientEntityEvent>();
eventLastSent = new Dictionary<uint, float>();
thisClient = client;
}
public void CreateEvent(IClientSerializable entity)
public void CreateEvent(IClientSerializable entity, object[] extraData = null)
{
if (!(entity is Entity))
{
@@ -29,23 +43,45 @@ namespace Barotrauma.Networking
}
ID++;
events.Add(new ClientEntityEvent(entity, ID));
var newEvent = new ClientEntityEvent(entity, ID);
if (extraData != null) newEvent.SetData(extraData);
events.Add(newEvent);
}
public void Write(NetOutgoingMessage msg)
public void Write(NetOutgoingMessage msg, NetConnection serverConnection)
{
if (events.Count == 0) return;
List<NetEntityEvent> eventsToSync = new List<NetEntityEvent>();
for (int i = events.Count - 1; i >= 0 && events[i].ID > thisClient.LastSentEntityEventID; i--)
{
float lastSent = 0;
eventLastSent.TryGetValue(events[i].ID, out lastSent);
if (lastSent > NetTime.Now - serverConnection.AverageRoundtripTime)
{
break;
}
eventsToSync.Add(events[i]);
}
if (eventsToSync.Count == 0) return;
foreach (NetEntityEvent entityEvent in eventsToSync)
{
eventLastSent[entityEvent.ID] = (float)NetTime.Now;
}
msg.Write((byte)ClientNetObject.ENTITY_STATE);
Write(msg, eventsToSync);
}
public void Read(NetIncomingMessage msg, float sendingTime)
{
base.Read(msg, sendingTime, ref lastReceivedID);
}
protected override void WriteEvent(NetBuffer buffer, NetEntityEvent entityEvent, Client recipient = null)
{
var clientEvent = entityEvent as ClientEntityEvent;
@@ -65,6 +101,7 @@ namespace Barotrauma.Networking
public void Clear()
{
events.Clear();
eventLastSent.Clear();
}
}
}

View File

@@ -10,15 +10,14 @@ namespace Barotrauma.Networking
{
abstract class NetEntityEventManager
{
const int MaxEventBufferLength = 1024;
const int MaxEventsPerWrite = 64;
public UInt32 LastReceivedEntityEventID
{
get { return lastReceivedEntityEventID; }
}
//public UInt32 LastReceivedEntityEventID
//{
// get { return lastReceivedEntityEventID; }
//}
private UInt32 lastReceivedEntityEventID;
/// <summary>
/// Write the events to the outgoing message. The recipient parameter is only needed for ServerEntityEventManager
/// </summary>
@@ -30,8 +29,6 @@ namespace Barotrauma.Networking
eventsToSync.RemoveRange(MaxEventsPerWrite, eventsToSync.Count - MaxEventsPerWrite);
}
msg.Write((byte)ServerNetObject.ENTITY_STATE);
msg.Write(eventsToSync[0].ID);
msg.Write((byte)eventsToSync.Count);
@@ -55,7 +52,7 @@ namespace Barotrauma.Networking
/// <summary>
/// Read the events from the message, ignoring ones we've already received
/// </summary>
public void Read(NetIncomingMessage msg, float sendingTime)
protected void Read(NetIncomingMessage msg, float sendingTime, ref UInt32 lastReceivedID)
{
UInt32 firstEventID = msg.ReadUInt32();
int eventCount = msg.ReadByte();
@@ -69,7 +66,7 @@ namespace Barotrauma.Networking
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+1 || entity == null)
if (thisEventID != lastReceivedID + 1 || entity == null)
{
DebugConsole.NewMessage("received msg "+thisEventID, Microsoft.Xna.Framework.Color.Red);
msg.Position += msgLength * 8;
@@ -77,8 +74,8 @@ namespace Barotrauma.Networking
else
{
DebugConsole.NewMessage("received msg "+thisEventID, Microsoft.Xna.Framework.Color.Green);
lastReceivedID++;
ReadEvent(msg, entity, sendingTime);
lastReceivedEntityEventID = thisEventID;
}
msg.ReadPadBits();
}

View File

@@ -61,6 +61,7 @@ namespace Barotrauma.Networking
client.entityEventLastSent[entityEvent.ID] = (float)NetTime.Now;
}
msg.Write((byte)ServerNetObject.ENTITY_STATE);
Write(msg, eventsToSync, client);
}
@@ -80,6 +81,11 @@ namespace Barotrauma.Networking
clientEntity.ServerRead(buffer, sender);
}
public void Read(NetIncomingMessage msg, Client client)
{
base.Read(msg, 0.0f, ref client.lastSentEntityEventID);
}
public void Clear()
{
events.Clear();