]> Dogcows Code - chaz/carfire/blobdiff - Project06/CS 3505 Project 06/CS 3505 Project 06/NetworkGame.cs
aasfdaf
[chaz/carfire] / Project06 / CS 3505 Project 06 / CS 3505 Project 06 / NetworkGame.cs
index 55d8b2e0a0d3df33920b8ac9da526ea642e36659..45c5d0a498fdd5a1ce9de1445b85cdbd309a6187 100644 (file)
@@ -1,4 +1,7 @@
-using System;\r
+\r
+#undef DEBUG\r
+\r
+using System;\r
 using System.Collections.Generic;\r
 using System.Linq;\r
 using System.Text;\r
@@ -297,17 +300,25 @@ namespace CS_3505_Project_06
                         mLocalEvents.AddRange(GetEventsFromInput());\r
                         SendLocalEvents();\r
                         ApplyEvents();\r
+\r
+#if DEBUG\r
+                        Console.WriteLine("HASH: " + mGame.CurrentFrameNumber + "\t" + mGame.CurrentChecksum);\r
+#endif\r
+\r
                         mGame.Update(mTargetTimeSpan);\r
                     }\r
                     else // Stall!\r
                     {\r
                         if (mStallCount == 0)\r
                         {\r
-                            Console.WriteLine("===== STALL =====");\r
+#if DEBUG\r
+                            Console.WriteLine("STAL: ====");\r
+#endif\r
                         }\r
                         else if (mStallCount % 60 == 0)\r
                         {\r
-                            Console.WriteLine("Stalled for " + mStallCount + " frames.");\r
+                            // DEBUG\r
+                            //Console.WriteLine("Stalled for " + mStallCount + " frames.");\r
                         }\r
 \r
                         mStallCount++;\r
@@ -368,7 +379,7 @@ namespace CS_3505_Project_06
 \r
 \r
         /// <summary>\r
-        /// Get the chat messages that have been receive since the last time this\r
+        /// Get the chat messages that have been received since the last time this\r
         /// method was called.\r
         /// </summary>\r
         /// <returns>List container of the chat messages.</returns>\r
@@ -398,6 +409,8 @@ namespace CS_3505_Project_06
         /// <param name="recipient">The gamer to receive the message.</param>\r
         public void SendChat(String message, NetworkGamer recipient)\r
         {\r
+            Debug.Assert(recipient != null && !recipient.IsDisposed);\r
+\r
             WriteChatPacket(message);\r
             LocalGamer.SendData(mPacketWriter, SendDataOptions.ReliableInOrder, recipient);\r
         }\r
@@ -437,8 +450,9 @@ namespace CS_3505_Project_06
         int mLastStallCount;\r
         int mAverageOwd;\r
 \r
-        // DEBUG\r
+#if DEBUG\r
         bool mDontSendEvents;\r
+#endif\r
 \r
         TimeSpan mTargetTimeSpan = new TimeSpan(166666);\r
         public TimeSpan TargetTimeSpan\r
@@ -573,7 +587,7 @@ namespace CS_3505_Project_06
         class GamerInfo\r
         {\r
             public NetworkGamer Gamer;\r
-            public long HighestFrameNumber = -1;\r
+            public long HighestFrameNumber = 0;\r
             public int StallCount = 0;\r
             public int AverageOwd = 0;\r
             public int NextStallCount = 0;\r
@@ -602,7 +616,7 @@ namespace CS_3505_Project_06
         void Reset()\r
         {\r
             mLatency = 1;\r
-            mHighestFrameNumber = -1;\r
+            mHighestFrameNumber = 0;\r
             mNextLatencyAdjustmentFrame = 1;\r
             mStallCount = 0;\r
             mLastStallCount = 0;\r
@@ -627,6 +641,7 @@ namespace CS_3505_Project_06
                 NetworkGamer sender;\r
 \r
                 localGamer.ReceiveData(mPacketReader, out sender);\r
+                if (sender == null || sender.IsDisposed) continue;\r
                 GamerInfo senderInfo = mGamers[sender.Id];\r
 \r
                 PacketType packetId = (PacketType)mPacketReader.ReadByte();\r
@@ -648,12 +663,31 @@ namespace CS_3505_Project_06
                         int frameNumber = mPacketReader.ReadInt32();\r
                         int numEvents = mPacketReader.ReadByte();\r
 \r
+                        if (frameNumber <= mNextLatencyAdjustmentFrame)\r
+                        {\r
+                            senderInfo.StallCount = stallCount;\r
+                            senderInfo.AverageOwd = averageOwd;\r
+                        }\r
+                        else\r
+                        {\r
+                            senderInfo.NextStallCount = stallCount;\r
+                            senderInfo.NextAverageOwd = averageOwd;\r
+                        }\r
+\r
                         if (frameNumber <= senderInfo.HighestFrameNumber)\r
                         {\r
+#if DEBUG\r
+                            Console.WriteLine("SKP" + (char)sender.Id + ": " + mGame.CurrentFrameNumber + "\t" + frameNumber + "\t<=\t" + senderInfo.HighestFrameNumber + "\t#" + numEvents);\r
+#endif\r
+\r
                             // we know about all these events, so don't bother reading them\r
                             break;\r
                         }\r
 \r
+#if DEBUG\r
+                        Console.WriteLine(" GOT" + (char)sender.Id + ": " + mGame.CurrentFrameNumber + "\t" + frameNumber + "\t>\t" + senderInfo.HighestFrameNumber + "\t#" + numEvents);\r
+#endif\r
+\r
                         for (int i = 0; i < numEvents; i++)\r
                         {\r
                             EventInfo eventInfo = ReadEvent(mPacketReader, sender);\r
@@ -666,16 +700,6 @@ namespace CS_3505_Project_06
                             }\r
                         }\r
 \r
-                        if (frameNumber <= mNextLatencyAdjustmentFrame)\r
-                        {\r
-                            senderInfo.StallCount = stallCount;\r
-                            senderInfo.AverageOwd = averageOwd;\r
-                        }\r
-                        else\r
-                        {\r
-                            senderInfo.NextStallCount = stallCount;\r
-                            senderInfo.NextAverageOwd = averageOwd;\r
-                        }\r
                         senderInfo.HighestFrameNumber = frameNumber;\r
                         break;\r
 \r
@@ -754,12 +778,12 @@ namespace CS_3505_Project_06
             mPacketWriter.Write(message.ToCharArray());\r
         }\r
 \r
-        void WriteEventPacket(List<EventInfo> events)\r
+        void WriteEventPacket(List<EventInfo> events, long highestFrameNumber)\r
         {\r
             mPacketWriter.Write((byte)PacketType.Event);\r
             mPacketWriter.Write((short)mLastStallCount);\r
             mPacketWriter.Write((short)mAverageOwd);\r
-            mPacketWriter.Write((int)(mGame.CurrentFrameNumber + mLatency));\r
+            mPacketWriter.Write((int)highestFrameNumber);\r
             mPacketWriter.Write((byte)events.Count);\r
 \r
             foreach (EventInfo eventInfo in events)\r
@@ -804,6 +828,13 @@ namespace CS_3505_Project_06
         {\r
             Debug.Assert(IsLatencyAdjustmentFrame);\r
 \r
+#if DEBUG\r
+            if (mStallCount > 0)\r
+            {\r
+                Console.WriteLine("STL#: " + mGame.CurrentFrameNumber + "\t" + mStallCount);\r
+            }\r
+#endif\r
+\r
             int maxStallCount = 0;\r
             int maxAverageOwd = 0;\r
 \r
@@ -816,8 +847,9 @@ namespace CS_3505_Project_06
                 gamerInfo.AverageOwd = gamerInfo.NextAverageOwd;\r
             }\r
 \r
-            // DEBUG\r
+#if DEBUG\r
             int prevLatency = mLatency;\r
+#endif\r
 \r
             if (maxStallCount > 0)\r
             {\r
@@ -825,15 +857,16 @@ namespace CS_3505_Project_06
             }\r
             else\r
             {\r
-                mLatency = (int)(0.6 * (double)(mLatency - maxAverageOwd) + 1.0);\r
+                mLatency -= (int)(0.6 * (double)(mLatency - maxAverageOwd) + 1.0);\r
             }\r
 \r
-            // DEBUG OUTPUT\r
-            if (prevLatency != mLatency) Console.WriteLine("Latency readjusted to " + mLatency);\r
-\r
             if (mLatency < 1) mLatency = 1;\r
             if (mLatency > MaximumLatency) mLatency = MaximumLatency;\r
 \r
+#if DEBUG\r
+            if (prevLatency != mLatency) Console.WriteLine("NLAG: " + mLatency);\r
+#endif\r
+\r
             mNextLatencyAdjustmentFrame = mGame.CurrentFrameNumber + mLatency;\r
             mAverageOwd = CurrentAverageOneWayDelay;\r
 \r
@@ -876,9 +909,10 @@ namespace CS_3505_Project_06
                 events.Add(new KeyboardEventInfo(LocalGamer, frameOfApplication, key, false));\r
             }\r
 \r
-            // DEBUG\r
+#if DEBUG\r
             if (pressedKeys.Contains(Keys.Escape)) mDontSendEvents = true;\r
             if (releasedKeys.Contains(Keys.Escape)) mDontSendEvents = false;\r
+#endif\r
 \r
             // 2. Find the mouse differences.\r
 \r
@@ -890,13 +924,13 @@ namespace CS_3505_Project_06
                 events.Add(new MouseButtonEventInfo(LocalGamer, frameOfApplication, MouseButton.Left, leftButtonPressed));\r
             }\r
 \r
-            bool rightButtonPressed = mouseState.LeftButton == ButtonState.Pressed;\r
+            bool rightButtonPressed = mouseState.RightButton == ButtonState.Pressed;\r
             if (rightButtonPressed != mLastRightButtonPressed)\r
             {\r
                 events.Add(new MouseButtonEventInfo(LocalGamer, frameOfApplication, MouseButton.Right, rightButtonPressed));\r
             }\r
 \r
-            bool middleButtonPressed = mouseState.LeftButton == ButtonState.Pressed;\r
+            bool middleButtonPressed = mouseState.MiddleButton == ButtonState.Pressed;\r
             if (middleButtonPressed != mLastMiddleButtonPressed)\r
             {\r
                 events.Add(new MouseButtonEventInfo(LocalGamer, frameOfApplication, MouseButton.Middle, middleButtonPressed));\r
@@ -936,20 +970,22 @@ namespace CS_3505_Project_06
 \r
         void SendLocalEvents(NetworkGamer recipient)\r
         {\r
-            // DEBUG\r
+#if DEBUG\r
             if (mDontSendEvents) return;\r
+#endif\r
 \r
             List<EventInfo> events = new List<EventInfo>(mLocalEvents);\r
             events.AddRange(mLastLocalEvents);\r
 \r
-            WriteEventPacket(events);\r
-\r
             if (recipient != null && !recipient.IsDisposed)\r
             {\r
+                // if there is a recipient, we are resending old events\r
+                WriteEventPacket(events, mGame.CurrentFrameNumber - 1);\r
                 LocalGamer.SendData(mPacketWriter, SendDataOptions.Reliable, recipient);\r
             }\r
             else\r
             {\r
+                WriteEventPacket(events, mGame.CurrentFrameNumber + mLatency);\r
                 LocalGamer.SendData(mPacketWriter, SendDataOptions.None);\r
             }\r
         }\r
@@ -983,7 +1019,10 @@ namespace CS_3505_Project_06
                     KeyboardEventInfo keyboardEventInfo = eventInfo as KeyboardEventInfo;\r
                     if (keyboardEventInfo != null)\r
                     {\r
-                        Console.WriteLine(keyboardEventInfo.FrameOfApplication + " KEY: " + keyboardEventInfo.Key + "," + keyboardEventInfo.IsKeyDown);\r
+#if DEBUG\r
+                        Console.WriteLine(" KEY: " + keyboardEventInfo.FrameOfApplication + "\t" + keyboardEventInfo.Key + "," + keyboardEventInfo.IsKeyDown);\r
+#endif\r
+\r
                         mGame.ApplyKeyInput(gamerInfo, keyboardEventInfo.Key, keyboardEventInfo.IsKeyDown);\r
                         continue;\r
                     }\r
@@ -991,16 +1030,22 @@ namespace CS_3505_Project_06
                     MouseButtonEventInfo mouseButtonEventInfo = eventInfo as MouseButtonEventInfo;\r
                     if (mouseButtonEventInfo != null)\r
                     {\r
+#if DEBUG\r
+                        Console.WriteLine(" BTN: " + mouseButtonEventInfo.FrameOfApplication + "\t" + mouseButtonEventInfo.IsButtonDown);\r
+#endif\r
+\r
                         mGame.ApplyMouseButtonInput(gamerInfo, mouseButtonEventInfo.IsButtonDown);\r
-                        Console.WriteLine(mouseButtonEventInfo.FrameOfApplication + " BTN: " + mouseButtonEventInfo.IsButtonDown);\r
                         continue;\r
                     }\r
 \r
                     MouseMotionEventInfo mouseMotionEventInfo = eventInfo as MouseMotionEventInfo;\r
                     if (mouseMotionEventInfo != null)\r
                     {\r
+#if DEBUG\r
+                        Console.WriteLine(" MMV: " + mouseMotionEventInfo.FrameOfApplication + "\t" + mouseMotionEventInfo.X + "," + mouseMotionEventInfo.Y);\r
+#endif\r
+\r
                         mGame.ApplyMouseLocationInput(gamerInfo, mouseMotionEventInfo.X, mouseMotionEventInfo.Y);\r
-                        Console.WriteLine(mouseMotionEventInfo.FrameOfApplication + " MMV: " + mouseMotionEventInfo.X + "," + mouseMotionEventInfo.Y);\r
                         continue;\r
                     }\r
                 }\r
This page took 0.029531 seconds and 4 git commands to generate.