X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=Project06%2FCS%203505%20Project%2006%2FCS%203505%20Project%2006%2FNetworkGame.cs;h=45c5d0a498fdd5a1ce9de1445b85cdbd309a6187;hb=b98320a31a73dc18d6ef83cb440a5304930af161;hp=55d8b2e0a0d3df33920b8ac9da526ea642e36659;hpb=904f5665bb8ea18a6a32d10632e2a0da2244b494;p=chaz%2Fcarfire 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 55d8b2e..45c5d0a 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++; @@ -368,7 +379,7 @@ namespace CS_3505_Project_06 /// - /// Get the chat messages that have been receive since the last time this + /// Get the chat messages that have been received since the last time this /// method was called. /// /// List container of the chat messages. @@ -398,6 +409,8 @@ namespace CS_3505_Project_06 /// The gamer to receive the message. public void SendChat(String message, NetworkGamer recipient) { + Debug.Assert(recipient != null && !recipient.IsDisposed); + WriteChatPacket(message); LocalGamer.SendData(mPacketWriter, SendDataOptions.ReliableInOrder, recipient); } @@ -437,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 @@ -573,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; @@ -602,7 +616,7 @@ namespace CS_3505_Project_06 void Reset() { mLatency = 1; - mHighestFrameNumber = -1; + mHighestFrameNumber = 0; mNextLatencyAdjustmentFrame = 1; mStallCount = 0; mLastStallCount = 0; @@ -627,6 +641,7 @@ namespace CS_3505_Project_06 NetworkGamer sender; localGamer.ReceiveData(mPacketReader, out sender); + if (sender == null || sender.IsDisposed) continue; GamerInfo senderInfo = mGamers[sender.Id]; PacketType packetId = (PacketType)mPacketReader.ReadByte(); @@ -648,12 +663,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); @@ -666,16 +700,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; @@ -754,12 +778,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) @@ -804,6 +828,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; @@ -816,8 +847,9 @@ namespace CS_3505_Project_06 gamerInfo.AverageOwd = gamerInfo.NextAverageOwd; } - // DEBUG +#if DEBUG int prevLatency = mLatency; +#endif if (maxStallCount > 0) { @@ -825,15 +857,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; @@ -876,9 +909,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. @@ -890,13 +924,13 @@ namespace CS_3505_Project_06 events.Add(new MouseButtonEventInfo(LocalGamer, frameOfApplication, MouseButton.Left, leftButtonPressed)); } - bool rightButtonPressed = mouseState.LeftButton == ButtonState.Pressed; + bool rightButtonPressed = mouseState.RightButton == ButtonState.Pressed; if (rightButtonPressed != mLastRightButtonPressed) { events.Add(new MouseButtonEventInfo(LocalGamer, frameOfApplication, MouseButton.Right, rightButtonPressed)); } - bool middleButtonPressed = mouseState.LeftButton == ButtonState.Pressed; + bool middleButtonPressed = mouseState.MiddleButton == ButtonState.Pressed; if (middleButtonPressed != mLastMiddleButtonPressed) { events.Add(new MouseButtonEventInfo(LocalGamer, frameOfApplication, MouseButton.Middle, middleButtonPressed)); @@ -936,20 +970,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); } } @@ -983,7 +1019,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; } @@ -991,16 +1030,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; } }