/// <summary> /// Checks for and processes any new entity snapshot updates coming in from the server. /// </summary> private void receiveServerUpdates() { IncomingMessage incomingMessage; while ((incomingMessage = this.serverNetworkConnection.GetNextIncomingMessage()) != null) { // Get the oldest entity snapshot in the history that should be overwritten with the new incoming data, but only overwrite if the incoming data is actually newer EntitySnapshot newEntitySnapshot = this.getOldestHistoryEntitySnapshot(); if (!ServerUpdateSerializer.DeserializeIfNewer(incomingMessage, this.entitySnapshotHistory, newEntitySnapshot, out int newLatestClientTickAcknowledgedByServer, out int newCommandingEntityID)) { continue; } if (this.LatestServerTickReceived < 0) { // This must be our first update from the server, so sync our ticks with the server's ticks once we start getting data flow from the server this.FrameTick = newEntitySnapshot.ServerFrameTick; } if (this.LatestServerTickReceived < newEntitySnapshot.ServerFrameTick) { // This snapshot is the most recent, up-to-date server update we've gotten so track it accordingly this.LatestServerTickReceived = newEntitySnapshot.ServerFrameTick; this.LatestFrameTickAcknowledgedByServer = newLatestClientTickAcknowledgedByServer; this.CommandingEntityID = newCommandingEntityID; } } }
/// <summary> /// Serializes the given server update (entity snapshot and client-specific data) and immediately sends a packet, only writing data that has changed from a previous snapshot.. /// </summary> public static void Send(INetworkConnection clientNetworkConnection, EntitySnapshot previousEntitySnapshot, EntitySnapshot latestEntitySnapshot, int latestClientTickReceived, int clientCommandingEntityID) { OutgoingMessage outgoingMessage = clientNetworkConnection.GetOutgoingMessageToSend(); ServerUpdateSerializer.Serialize(outgoingMessage, previousEntitySnapshot, latestEntitySnapshot, latestClientTickReceived, clientCommandingEntityID); clientNetworkConnection.SendMessage(outgoingMessage); }
/// <summary> /// Sends the client an update of the given entity state and other information. /// </summary> public void SendServerUpdate(EntitySnapshot latestEntitySnapshot) { EntitySnapshot previousEntitySnapshot = this.parentServer.getEntitySnapshotForServerFrameTick(this.LatestFrameTickAcknowledgedByClient); ServerUpdateSerializer.Send(this.clientNetworkConnection, previousEntitySnapshot, latestEntitySnapshot, this.LatestClientTickReceived, this.CommandingEntityID); }