-using System;\r
+\r
+#undef DEBUG\r
+\r
+using System;\r
using System.Collections.Generic;\r
using System.Linq;\r
using System.Text;\r
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
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
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
void Reset()\r
{\r
mLatency = 1;\r
- mHighestFrameNumber = -1;\r
+ mHighestFrameNumber = 0;\r
mNextLatencyAdjustmentFrame = 1;\r
mStallCount = 0;\r
mLastStallCount = 0;\r
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
}\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
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
{\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
gamerInfo.AverageOwd = gamerInfo.NextAverageOwd;\r
}\r
\r
- // DEBUG\r
+#if DEBUG\r
int prevLatency = mLatency;\r
+#endif\r
\r
if (maxStallCount > 0)\r
{\r
}\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
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
\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
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
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