From 9b9f8951ffe9788deb63de067225cc88968035dc Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 30 Mar 2010 23:17:45 +0000 Subject: [PATCH] Fixed last synchronization problem (I hope). git-svn-id: https://bd85.net/svn/cs3505_group@40 92bb83a3-7c8f-8a45-bc97-515c4e399668 --- .../CS 3505 Project 06/CS 3505/TestHarness.cs | 4 - .../CS 3505 Project 06/NetworkGame.cs | 102 ++++++++++++------ 2 files changed, 72 insertions(+), 34 deletions(-) diff --git a/Project06/CS 3505 Project 06/CS 3505 Project 06/CS 3505/TestHarness.cs b/Project06/CS 3505 Project 06/CS 3505 Project 06/CS 3505/TestHarness.cs index b4860a4..883e8a0 100644 --- a/Project06/CS 3505 Project 06/CS 3505 Project 06/CS 3505/TestHarness.cs +++ b/Project06/CS 3505 Project 06/CS 3505 Project 06/CS 3505/TestHarness.cs @@ -294,10 +294,6 @@ namespace CS_3505_Project_06.CS_3505 public long Update(TimeSpan elapsedTime) { - // Draw() can be called multiple times between updates, so moved this here to avoid - // duplicate lines. - Console.WriteLine("Frame: " + state.frameNumber + " Checksum: " + state.Checksum); - state.advanceFrame(inputs, elapsedTime.Milliseconds); // Apply the inputs, advance game state. inputs = new NextInputs(); // Start with inputs cleared on the next frame. diff --git a/Project06/CS 3505 Project 06/CS 3505 Project 06/NetworkGame.cs b/Project06/CS 3505 Project 06/CS 3505 Project 06/NetworkGame.cs index c69e2fb..bc0617c 100644 --- a/Project06/CS 3505 Project 06/CS 3505 Project 06/NetworkGame.cs +++ b/Project06/CS 3505 Project 06/CS 3505 Project 06/NetworkGame.cs @@ -1,4 +1,7 @@ -using System; + +#undef DEBUG + +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -297,17 +300,25 @@ namespace CS_3505_Project_06 mLocalEvents.AddRange(GetEventsFromInput()); SendLocalEvents(); ApplyEvents(); + +#if DEBUG + Console.WriteLine("HASH: " + mGame.CurrentFrameNumber + "\t" + mGame.CurrentChecksum); +#endif + mGame.Update(mTargetTimeSpan); } else // Stall! { if (mStallCount == 0) { - Console.WriteLine("===== STALL ====="); +#if DEBUG + Console.WriteLine("STAL: ===="); +#endif } else if (mStallCount % 60 == 0) { - Console.WriteLine("Stalled for " + mStallCount + " frames."); + // DEBUG + //Console.WriteLine("Stalled for " + mStallCount + " frames."); } mStallCount++; @@ -439,8 +450,9 @@ namespace CS_3505_Project_06 int mLastStallCount; int mAverageOwd; - // DEBUG +#if DEBUG bool mDontSendEvents; +#endif TimeSpan mTargetTimeSpan = new TimeSpan(166666); public TimeSpan TargetTimeSpan @@ -575,7 +587,7 @@ namespace CS_3505_Project_06 class GamerInfo { public NetworkGamer Gamer; - public long HighestFrameNumber = -1; + public long HighestFrameNumber = 0; public int StallCount = 0; public int AverageOwd = 0; public int NextStallCount = 0; @@ -604,7 +616,7 @@ namespace CS_3505_Project_06 void Reset() { mLatency = 1; - mHighestFrameNumber = -1; + mHighestFrameNumber = 0; mNextLatencyAdjustmentFrame = 1; mStallCount = 0; mLastStallCount = 0; @@ -650,12 +662,31 @@ namespace CS_3505_Project_06 int frameNumber = mPacketReader.ReadInt32(); int numEvents = mPacketReader.ReadByte(); + if (frameNumber <= mNextLatencyAdjustmentFrame) + { + senderInfo.StallCount = stallCount; + senderInfo.AverageOwd = averageOwd; + } + else + { + senderInfo.NextStallCount = stallCount; + senderInfo.NextAverageOwd = averageOwd; + } + if (frameNumber <= senderInfo.HighestFrameNumber) { +#if DEBUG + Console.WriteLine("SKP" + (char)sender.Id + ": " + mGame.CurrentFrameNumber + "\t" + frameNumber + "\t<=\t" + senderInfo.HighestFrameNumber + "\t#" + numEvents); +#endif + // we know about all these events, so don't bother reading them break; } +#if DEBUG + Console.WriteLine(" GOT" + (char)sender.Id + ": " + mGame.CurrentFrameNumber + "\t" + frameNumber + "\t>\t" + senderInfo.HighestFrameNumber + "\t#" + numEvents); +#endif + for (int i = 0; i < numEvents; i++) { EventInfo eventInfo = ReadEvent(mPacketReader, sender); @@ -668,16 +699,6 @@ namespace CS_3505_Project_06 } } - if (frameNumber <= mNextLatencyAdjustmentFrame) - { - senderInfo.StallCount = stallCount; - senderInfo.AverageOwd = averageOwd; - } - else - { - senderInfo.NextStallCount = stallCount; - senderInfo.NextAverageOwd = averageOwd; - } senderInfo.HighestFrameNumber = frameNumber; break; @@ -756,12 +777,12 @@ namespace CS_3505_Project_06 mPacketWriter.Write(message.ToCharArray()); } - void WriteEventPacket(List events) + void WriteEventPacket(List events, long highestFrameNumber) { mPacketWriter.Write((byte)PacketType.Event); mPacketWriter.Write((short)mLastStallCount); mPacketWriter.Write((short)mAverageOwd); - mPacketWriter.Write((int)(mGame.CurrentFrameNumber + mLatency)); + mPacketWriter.Write((int)highestFrameNumber); mPacketWriter.Write((byte)events.Count); foreach (EventInfo eventInfo in events) @@ -806,6 +827,13 @@ namespace CS_3505_Project_06 { Debug.Assert(IsLatencyAdjustmentFrame); +#if DEBUG + if (mStallCount > 0) + { + Console.WriteLine("STL#: " + mGame.CurrentFrameNumber + "\t" + mStallCount); + } +#endif + int maxStallCount = 0; int maxAverageOwd = 0; @@ -818,8 +846,9 @@ namespace CS_3505_Project_06 gamerInfo.AverageOwd = gamerInfo.NextAverageOwd; } - // DEBUG +#if DEBUG int prevLatency = mLatency; +#endif if (maxStallCount > 0) { @@ -827,15 +856,16 @@ namespace CS_3505_Project_06 } else { - mLatency = (int)(0.6 * (double)(mLatency - maxAverageOwd) + 1.0); + mLatency -= (int)(0.6 * (double)(mLatency - maxAverageOwd) + 1.0); } - // DEBUG OUTPUT - if (prevLatency != mLatency) Console.WriteLine("Latency readjusted to " + mLatency); - if (mLatency < 1) mLatency = 1; if (mLatency > MaximumLatency) mLatency = MaximumLatency; +#if DEBUG + if (prevLatency != mLatency) Console.WriteLine("NLAG: " + mLatency); +#endif + mNextLatencyAdjustmentFrame = mGame.CurrentFrameNumber + mLatency; mAverageOwd = CurrentAverageOneWayDelay; @@ -878,9 +908,10 @@ namespace CS_3505_Project_06 events.Add(new KeyboardEventInfo(LocalGamer, frameOfApplication, key, false)); } - // DEBUG +#if DEBUG if (pressedKeys.Contains(Keys.Escape)) mDontSendEvents = true; if (releasedKeys.Contains(Keys.Escape)) mDontSendEvents = false; +#endif // 2. Find the mouse differences. @@ -938,20 +969,22 @@ namespace CS_3505_Project_06 void SendLocalEvents(NetworkGamer recipient) { - // DEBUG +#if DEBUG if (mDontSendEvents) return; +#endif List events = new List(mLocalEvents); events.AddRange(mLastLocalEvents); - WriteEventPacket(events); - if (recipient != null && !recipient.IsDisposed) { + // if there is a recipient, we are resending old events + WriteEventPacket(events, mGame.CurrentFrameNumber - 1); LocalGamer.SendData(mPacketWriter, SendDataOptions.Reliable, recipient); } else { + WriteEventPacket(events, mGame.CurrentFrameNumber + mLatency); LocalGamer.SendData(mPacketWriter, SendDataOptions.None); } } @@ -985,7 +1018,10 @@ namespace CS_3505_Project_06 KeyboardEventInfo keyboardEventInfo = eventInfo as KeyboardEventInfo; if (keyboardEventInfo != null) { - Console.WriteLine(keyboardEventInfo.FrameOfApplication + " KEY: " + keyboardEventInfo.Key + "," + keyboardEventInfo.IsKeyDown); +#if DEBUG + Console.WriteLine(" KEY: " + keyboardEventInfo.FrameOfApplication + "\t" + keyboardEventInfo.Key + "," + keyboardEventInfo.IsKeyDown); +#endif + mGame.ApplyKeyInput(gamerInfo, keyboardEventInfo.Key, keyboardEventInfo.IsKeyDown); continue; } @@ -993,16 +1029,22 @@ namespace CS_3505_Project_06 MouseButtonEventInfo mouseButtonEventInfo = eventInfo as MouseButtonEventInfo; if (mouseButtonEventInfo != null) { +#if DEBUG + Console.WriteLine(" BTN: " + mouseButtonEventInfo.FrameOfApplication + "\t" + mouseButtonEventInfo.IsButtonDown); +#endif + mGame.ApplyMouseButtonInput(gamerInfo, mouseButtonEventInfo.IsButtonDown); - Console.WriteLine(mouseButtonEventInfo.FrameOfApplication + " BTN: " + mouseButtonEventInfo.IsButtonDown); continue; } MouseMotionEventInfo mouseMotionEventInfo = eventInfo as MouseMotionEventInfo; if (mouseMotionEventInfo != null) { +#if DEBUG + Console.WriteLine(" MMV: " + mouseMotionEventInfo.FrameOfApplication + "\t" + mouseMotionEventInfo.X + "," + mouseMotionEventInfo.Y); +#endif + mGame.ApplyMouseLocationInput(gamerInfo, mouseMotionEventInfo.X, mouseMotionEventInfo.Y); - Console.WriteLine(mouseMotionEventInfo.FrameOfApplication + " MMV: " + mouseMotionEventInfo.X + "," + mouseMotionEventInfo.Y); continue; } } -- 2.43.0