2 using System.Collections.Generic;
5 using Microsoft.Xna.Framework.Net;
6 using System.Diagnostics;
7 using Microsoft.Xna.Framework.GamerServices;
8 using Microsoft.Xna.Framework.Graphics;
9 using Microsoft.Xna.Framework;
10 using Microsoft.Xna.Framework.Input;
11 using System.Collections;
13 namespace CS_3505_Project_06
16 /// A manager class to handle network interactions between peers and
17 /// lobby/game switching.
19 public class NetworkGame
21 // Public methods and properties
22 #region Public Methods
25 /// Called when a session has been created or joined using CreateSession() or JoinSession().
27 /// <param name="session">The new session that was created or joined.</param>
28 /// <param name="networkGame">The NetworkGame that joined the session.</param>
29 public delegate void JoinedSessionDelegate(NetworkSession session, NetworkGame networkGame);
32 /// Called when sessions are found as a result of calling FindSessions().
34 /// <param name="sessions">A container of the available sessions.</param>
35 /// <param name="networkGame">The NetworkGame that searched for the sessions.</param>
36 public delegate void FoundSessionsDelegate(AvailableNetworkSessionCollection sessions, NetworkGame networkGame);
40 /// Called when an exception is thrown during an asynchronous operation.
42 /// <param name="exception">The exception that was thrown.</param>
43 /// <param name="networkGame">The NetworkGame that errored.</param>
44 public delegate void CaughtErrorDelegate(Exception exception, NetworkGame networkGame);
47 /// Get and set the error delegate, called when an exception is thrown during
48 /// and asynchronous operation. This will occur if you try to create or join a
49 /// session without being logged into a profile.
51 public CaughtErrorDelegate ErrorDelegate;
55 /// Construct a NetworkGame with a lobby and a game.
57 /// <param name="lobby">Provides an associated lobby to update and draw.</param>
58 /// <param name="game">Provides a game object to be played over the network.</param>
59 public NetworkGame(ILobby lobby, IDeterministicGame game)
61 Debug.Assert(lobby != null && game != null);
69 /// Get the Gamer object for the local player.
71 public LocalNetworkGamer LocalGamer
75 // TODO: Is this the correct way to get the single local gamer?
76 return mNetworkSession.LocalGamers[0];
81 /// Get all the gamers associated with the active network session.
83 public GamerCollection<NetworkGamer> NetworkGamers
87 return mNetworkSession.AllGamers;
93 /// Begin a new network session with the local gamer as the host. You must not
94 /// call this method or use JoinSession without first using LeaveSession.
96 /// <param name="callback">The delegate/method to call when the session is created.</param>
97 public void CreateSession(JoinedSessionDelegate callback)
99 CreateSession(mGame.MaximumSupportedPlayers, callback);
103 /// Begin a new network session with the local gamer as the host. You must not
104 /// call this method or use JoinSession without first using LeaveSession.
106 /// <param name="maxGamers">Provide the maximum number of players allowed to connect.</param>
107 /// <param name="callback">The delegate/method to call when the session is created.</param>
108 public void CreateSession(int maxGamers, JoinedSessionDelegate callback)
110 Debug.Assert(mNetworkSession == null);
112 mJoinedSessionDelegate = callback;
113 NetworkSession.BeginCreate(NetworkSessionType.SystemLink, 1, maxGamers, CreateSessionEnd, null);
115 void CreateSessionEnd(IAsyncResult result)
117 Debug.Assert(mNetworkSession == null);
121 mNetworkSession = NetworkSession.EndCreate(result);
122 mNetworkSession.AllowHostMigration = true;
123 mNetworkSession.AllowJoinInProgress = false;
124 mNetworkSession.GameStarted += new EventHandler<GameStartedEventArgs>(GameStartedEvent);
128 if (ErrorDelegate != null) ErrorDelegate(e, this);
131 mJoinedSessionDelegate(mNetworkSession, this);
132 mJoinedSessionDelegate = null;
134 void GameStartedEvent(object sender, GameStartedEventArgs e)
140 /// Determine whether or not the network game object is associated with any network session.
142 /// <returns>True if there exists a NetworkSession; false otherwise.</returns>
143 public bool HasActiveSession
147 return mNetworkSession != null;
153 /// Find available sessions to join. You should not already be in a session when
154 /// calling this method; call LeaveSession first.
156 /// <param name="callback">The delegate/method to call when the search finishes.</param>
157 public void FindSessions(FoundSessionsDelegate callback)
159 Debug.Assert(mNetworkSession == null);
161 mFoundSessionsDelegate = callback;
162 NetworkSession.BeginFind(NetworkSessionType.SystemLink, 1, null, new AsyncCallback(FindSessionsEnd), null);
164 void FindSessionsEnd(IAsyncResult result)
166 AvailableNetworkSessionCollection sessions;
169 sessions = NetworkSession.EndFind(result);
173 if (ErrorDelegate != null) ErrorDelegate(e, this);
176 mFoundSessionsDelegate(sessions, this);
177 mFoundSessionsDelegate = null;
181 /// Join a network session found using FindSessions(). This is for joining a game that
182 /// somebody else has already started hosting. You must not already be in a session.
184 /// <param name="availableSession">Pass the session object to try to join.</param>
185 /// <param name="callback">The delegate/method to call when the search finishes.</param>
186 public void JoinSession(AvailableNetworkSession availableSession, JoinedSessionDelegate callback)
188 Debug.Assert(mNetworkSession == null);
190 mJoinedSessionDelegate = callback;
191 NetworkSession.BeginJoin(availableSession, JoinSessionEnd, null);
193 void JoinSessionEnd(IAsyncResult result)
195 Debug.Assert(mNetworkSession == null);
199 mNetworkSession = NetworkSession.EndJoin(result);
200 mNetworkSession.GameStarted += new EventHandler<GameStartedEventArgs>(GameStartedEvent);
204 if (ErrorDelegate != null) ErrorDelegate(e, this);
207 mJoinedSessionDelegate(mNetworkSession, this);
208 mJoinedSessionDelegate = null;
213 /// Leave and dispose of any currently associated network session. You will find yourself
214 /// back in the lobby. You must already be in a session to leave it.
216 public void LeaveSession()
218 Debug.Assert(mNetworkSession != null);
220 mNetworkSession.Dispose();
221 mNetworkSession = null;
226 /// Set up the network session to simulate 200ms latency and 10% packet loss.
228 public void SimulateBadNetwork()
230 Debug.Assert(mNetworkSession != null);
232 mNetworkSession.SimulatedLatency = new TimeSpan(0, 0, 0, 0, 200);
233 mNetworkSession.SimulatedPacketLoss = 0.1f;
238 /// Indicate that the game should begin (moving players from the lobby to the game).
239 /// You must call CreateSession() before calling this.
241 public void StartGame()
243 Debug.Assert(mNetworkSession != null && mNetworkSession.IsHost &&
244 mNetworkSession.AllGamers.Count >= mGame.MinimumSupportedPlayers &&
245 mNetworkSession.IsEveryoneReady);
251 /// Indicate that the game should begin. This is like StartGame() without the sanity
252 /// checks. Use this for debugging.
254 public void ForceStartGame()
256 mNetworkSession.StartGame();
257 mNetworkSession.ResetReady();
262 /// Manages the network session and allows either the lobby or game to update.
264 /// <param name="gameTime">Pass the time away.</param>
265 public void Update(GameTime gameTime)
267 if (mNetworkSession == null)
269 mLobby.Update(gameTime, this);
273 mNetworkSession.Update();
274 HandleIncomingPackets();
276 if (mNetworkSession.SessionState == NetworkSessionState.Lobby)
278 mLobby.Update(gameTime, this);
280 else if (mNetworkSession.SessionState == NetworkSessionState.Playing)
282 if (mGame.IsGameOver(LocalGamerInfo) || mGame.IsTerminated(LocalGamerInfo))
284 // TODO: Should support moving back to the session lobby.
289 if (HaveNeededEvents)
291 if (IsLatencyAdjustmentFrame)
294 mLastStallCount = mStallCount;
297 mLocalEvents.AddRange(GetEventsFromInput());
300 mGame.Update(mTargetTimeSpan);
304 if (mStallCount == 0)
306 Console.WriteLine("===== STALL =====");
308 else if (mStallCount % 60 == 0)
310 Console.WriteLine("Stalled for " + mStallCount + " frames.");
315 // Send a reliable event packet to each stalled gamer.
316 if (mStallCount == 1)
318 foreach (GamerInfo gamerInfo in GamerArray)
320 if (gamerInfo.HighestFrameNumber < mGame.CurrentFrameNumber)
322 SendLocalEvents(gamerInfo.Gamer);
327 /*if (mStallCount > StallTimeout)
332 else if (mStallCount == 1)
336 else if (mStallCount % 60 == 0)
345 /// Allows either the lobby or the game to draw, depending on the state
346 /// of the network connection and whether or not a game is in progress.
348 /// <param name="gameTime">Pass the time away.</param>
349 /// <param name="spriteBatch">The sprite batch.</param>
350 public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
352 if (mNetworkSession == null)
354 mLobby.Draw(spriteBatch);
358 if (mNetworkSession.SessionState == NetworkSessionState.Lobby)
360 mLobby.Draw(spriteBatch);
362 else if (mNetworkSession.SessionState == NetworkSessionState.Playing)
364 mGame.Draw(spriteBatch);
371 /// Get the chat messages that have been received since the last time this
372 /// method was called.
374 /// <returns>List container of the chat messages.</returns>
375 public List<ChatInfo> ReceiveChats()
377 List<ChatInfo> chats = mChatPackets;
378 mChatPackets = new List<ChatInfo>();
383 /// Send a chat message to all gamers in the session. You should already be
384 /// in a session before calling this method.
386 /// <param name="message">The text of the message.</param>
387 public void SendChat(String message)
389 WriteChatPacket(message);
390 LocalGamer.SendData(mPacketWriter, SendDataOptions.ReliableInOrder);
394 /// Send a chat message to a specific gamer in the session. You should already
395 /// be in a session before calling this method.
397 /// <param name="message">The text of the message.</param>
398 /// <param name="recipient">The gamer to receive the message.</param>
399 public void SendChat(String message, NetworkGamer recipient)
401 Debug.Assert(recipient != null && !recipient.IsDisposed);
403 WriteChatPacket(message);
404 LocalGamer.SendData(mPacketWriter, SendDataOptions.ReliableInOrder, recipient);
410 // Private class variable members
411 #region Instance Variables
413 NetworkSession mNetworkSession;
414 PacketReader mPacketReader = new PacketReader();
415 PacketWriter mPacketWriter = new PacketWriter();
417 JoinedSessionDelegate mJoinedSessionDelegate;
418 FoundSessionsDelegate mFoundSessionsDelegate;
421 IDeterministicGame mGame;
423 List<ChatInfo> mChatPackets = new List<ChatInfo>();
425 List<EventInfo> mLocalEvents = new List<EventInfo>();
426 List<EventInfo> mLastLocalEvents = new List<EventInfo>();
428 List<Keys> mLastPressedKeys = new List<Keys>();
429 bool mLastLeftButtonPressed;
430 bool mLastRightButtonPressed;
431 bool mLastMiddleButtonPressed;
432 int mLastMousePositionX;
433 int mLastMousePositionY;
436 long mHighestFrameNumber;
437 long mNextLatencyAdjustmentFrame;
443 bool mDontSendEvents;
445 TimeSpan mTargetTimeSpan = new TimeSpan(166666);
446 public TimeSpan TargetTimeSpan
450 return mTargetTimeSpan;
454 Dictionary<byte, GamerInfo> mGamers;
455 GamerInfo[] GamerArray
459 GamerInfo[] gamerList = mGamers.Values.ToArray();
460 Array.Sort(gamerList, delegate(GamerInfo a, GamerInfo b)
462 return a.Gamer.Id.CompareTo(b.Gamer.Id);
467 GamerInfo LocalGamerInfo
471 return mGamers[LocalGamer.Id];
478 // Private types for the implementation of the network protocol
479 #region Private Types
504 abstract class EventInfo
506 public NetworkGamer Gamer;
507 public long FrameOfApplication;
509 public EventInfo(NetworkGamer gamer, long frameNumber)
512 FrameOfApplication = frameNumber;
515 public abstract EventType Id
521 class KeyboardEventInfo : EventInfo
524 public bool IsKeyDown;
526 public KeyboardEventInfo(NetworkGamer gamer, long frameNumber, Keys key, bool isDown)
527 : base(gamer, frameNumber)
533 public override EventType Id
535 get { return IsKeyDown ? EventType.KeyDown : EventType.KeyUp; }
539 class MouseButtonEventInfo : EventInfo
541 public MouseButton Button;
542 public bool IsButtonDown;
544 public MouseButtonEventInfo(NetworkGamer gamer, long frameNumber, MouseButton button, bool isDown)
545 : base(gamer, frameNumber)
548 IsButtonDown = isDown;
551 public override EventType Id
553 get { return IsButtonDown ? EventType.MouseDown : EventType.MouseUp; }
557 class MouseMotionEventInfo : EventInfo
562 public MouseMotionEventInfo(NetworkGamer gamer, long frameNumber, int x, int y)
563 : base(gamer, frameNumber)
569 public override EventType Id
571 get { return EventType.MouseMove; }
577 public NetworkGamer Gamer;
578 public long HighestFrameNumber = -1;
579 public int StallCount = 0;
580 public int AverageOwd = 0;
581 public int NextStallCount = 0;
582 public int NextAverageOwd = 0;
583 public bool IsWaitedOn = false;
584 public List<EventInfo>[] Events = new List<EventInfo>[MaximumLatency];
586 public GamerInfo(NetworkGamer gamer)
592 const int MaximumLatency = 120;
593 const int StallTimeout = 900;
598 // Private implementation methods of the network protocol
599 #region Private Implementation Methods
602 /// Reinitialize the private variables in preparation for a new game to start.
607 mHighestFrameNumber = -1;
608 mNextLatencyAdjustmentFrame = 1;
611 mAverageOwd = CurrentAverageOneWayDelay;
613 mGamers = new Dictionary<byte, GamerInfo>();
614 foreach (NetworkGamer gamer in NetworkGamers)
616 mGamers.Add(gamer.Id, new GamerInfo(gamer));
619 mGame.ResetGame(GamerArray, LocalGamerInfo);
623 void HandleIncomingPackets()
625 LocalNetworkGamer localGamer = LocalGamer;
627 while (localGamer.IsDataAvailable)
631 localGamer.ReceiveData(mPacketReader, out sender);
632 GamerInfo senderInfo = mGamers[sender.Id];
634 PacketType packetId = (PacketType)mPacketReader.ReadByte();
637 case PacketType.Chat:
639 short messageLength = mPacketReader.ReadInt16();
640 char[] message = mPacketReader.ReadChars(messageLength);
642 ChatInfo chatPacket = new ChatInfo(sender, new String(message));
643 mChatPackets.Add(chatPacket);
646 case PacketType.Event:
648 int stallCount = mPacketReader.ReadInt16();
649 int averageOwd = mPacketReader.ReadInt16();
650 int frameNumber = mPacketReader.ReadInt32();
651 int numEvents = mPacketReader.ReadByte();
653 if (frameNumber <= senderInfo.HighestFrameNumber)
655 // we know about all these events, so don't bother reading them
659 for (int i = 0; i < numEvents; i++)
661 EventInfo eventInfo = ReadEvent(mPacketReader, sender);
663 if (eventInfo != null && eventInfo.FrameOfApplication > senderInfo.HighestFrameNumber)
665 int index = GetEventArrayIndexForFrame(eventInfo.FrameOfApplication);
666 if (senderInfo.Events[index] == null) senderInfo.Events[index] = new List<EventInfo>();
667 senderInfo.Events[index].Add(eventInfo);
671 if (frameNumber <= mNextLatencyAdjustmentFrame)
673 senderInfo.StallCount = stallCount;
674 senderInfo.AverageOwd = averageOwd;
678 senderInfo.NextStallCount = stallCount;
679 senderInfo.NextAverageOwd = averageOwd;
681 senderInfo.HighestFrameNumber = frameNumber;
684 case PacketType.Stall:
686 byte numStalledPeers = mPacketReader.ReadByte();
687 byte[] stalledPeers = mPacketReader.ReadBytes(numStalledPeers);
694 Console.WriteLine("Received unknown packet type: " + (int)packetId);
701 int CurrentEventArrayIndex
703 get { return GetEventArrayIndexForFrame(mGame.CurrentFrameNumber); }
706 int GetEventArrayIndexForFrame(long frame)
708 return (int)(frame % MaximumLatency);
711 EventInfo ReadEvent(PacketReader packetReader, NetworkGamer sender)
713 EventType eventId = (EventType)packetReader.ReadByte();
714 long frameNumber = packetReader.ReadInt32();
718 case EventType.KeyDown:
720 Keys keyCode1 = (Keys)packetReader.ReadInt32();
721 return new KeyboardEventInfo(sender, frameNumber, keyCode1, true);
723 case EventType.KeyUp:
725 Keys keyCode2 = (Keys)packetReader.ReadInt32();
726 return new KeyboardEventInfo(sender, frameNumber, keyCode2, false);
728 case EventType.MouseDown:
730 MouseButton buttonId1 = (MouseButton)packetReader.ReadByte();
731 return new MouseButtonEventInfo(sender, frameNumber, buttonId1, true);
733 case EventType.MouseUp:
735 MouseButton buttonId2 = (MouseButton)packetReader.ReadByte();
736 return new MouseButtonEventInfo(sender, frameNumber, buttonId2, false);
738 case EventType.MouseMove:
740 short x = packetReader.ReadInt16();
741 short y = packetReader.ReadInt16();
742 return new MouseMotionEventInfo(sender, frameNumber, x, y);
746 Console.WriteLine("Received unknown event type: " + (int)eventId);
752 void WriteChatPacket(String message)
754 mPacketWriter.Write((byte)PacketType.Chat);
755 mPacketWriter.Write((short)message.Length);
756 mPacketWriter.Write(message.ToCharArray());
759 void WriteEventPacket(List<EventInfo> events)
761 mPacketWriter.Write((byte)PacketType.Event);
762 mPacketWriter.Write((short)mLastStallCount);
763 mPacketWriter.Write((short)mAverageOwd);
764 mPacketWriter.Write((int)(mGame.CurrentFrameNumber + mLatency));
765 mPacketWriter.Write((byte)events.Count);
767 foreach (EventInfo eventInfo in events)
769 mPacketWriter.Write((byte)eventInfo.Id);
770 mPacketWriter.Write((int)eventInfo.FrameOfApplication);
772 KeyboardEventInfo keyboardEventInfo = eventInfo as KeyboardEventInfo;
773 if (keyboardEventInfo != null)
775 mPacketWriter.Write((int)keyboardEventInfo.Key);
779 MouseButtonEventInfo mouseButtonEventInfo = eventInfo as MouseButtonEventInfo;
780 if (mouseButtonEventInfo != null)
782 mPacketWriter.Write((byte)mouseButtonEventInfo.Button);
786 MouseMotionEventInfo mouseMotionEventInfo = eventInfo as MouseMotionEventInfo;
787 if (mouseMotionEventInfo != null)
789 mPacketWriter.Write((short)mouseMotionEventInfo.X);
790 mPacketWriter.Write((short)mouseMotionEventInfo.Y);
797 bool IsLatencyAdjustmentFrame
801 return mNextLatencyAdjustmentFrame == mGame.CurrentFrameNumber;
807 Debug.Assert(IsLatencyAdjustmentFrame);
809 int maxStallCount = 0;
810 int maxAverageOwd = 0;
812 foreach (GamerInfo gamerInfo in GamerArray)
814 if (gamerInfo.StallCount > maxStallCount) maxStallCount = gamerInfo.StallCount;
815 if (gamerInfo.AverageOwd > maxAverageOwd) maxAverageOwd = gamerInfo.AverageOwd;
817 gamerInfo.StallCount = gamerInfo.NextStallCount;
818 gamerInfo.AverageOwd = gamerInfo.NextAverageOwd;
822 int prevLatency = mLatency;
824 if (maxStallCount > 0)
826 mLatency += maxStallCount;
830 mLatency = (int)(0.6 * (double)(mLatency - maxAverageOwd) + 1.0);
834 if (prevLatency != mLatency) Console.WriteLine("Latency readjusted to " + mLatency);
836 if (mLatency < 1) mLatency = 1;
837 if (mLatency > MaximumLatency) mLatency = MaximumLatency;
839 mNextLatencyAdjustmentFrame = mGame.CurrentFrameNumber + mLatency;
840 mAverageOwd = CurrentAverageOneWayDelay;
842 mLastLocalEvents = mLocalEvents;
843 mLocalEvents = new List<EventInfo>();
848 List<EventInfo> GetEventsFromInput()
850 List<EventInfo> events = new List<EventInfo>();
852 long frameOfApplication = mGame.CurrentFrameNumber + mLatency;
853 if (frameOfApplication <= mHighestFrameNumber) return events;
854 else mHighestFrameNumber = frameOfApplication;
856 // 1. Find the keyboard differences; written by Peter.
858 KeyboardState keyState = Keyboard.GetState();
860 List<Keys> pressedKeys = new List<Keys>();
861 List<Keys> releasedKeys = new List<Keys>();
863 Keys[] pressedKeysArray = keyState.GetPressedKeys();
864 foreach (Keys k in pressedKeysArray)
866 if (!mLastPressedKeys.Contains(k)) pressedKeys.Add(k);
867 else mLastPressedKeys.Remove(k);
870 releasedKeys = mLastPressedKeys;
872 foreach (Keys key in pressedKeys)
874 events.Add(new KeyboardEventInfo(LocalGamer, frameOfApplication, key, true));
876 foreach (Keys key in releasedKeys)
878 events.Add(new KeyboardEventInfo(LocalGamer, frameOfApplication, key, false));
882 if (pressedKeys.Contains(Keys.Escape)) mDontSendEvents = true;
883 if (releasedKeys.Contains(Keys.Escape)) mDontSendEvents = false;
885 // 2. Find the mouse differences.
887 MouseState mouseState = Mouse.GetState();
889 bool leftButtonPressed = mouseState.LeftButton == ButtonState.Pressed;
890 if (leftButtonPressed != mLastLeftButtonPressed)
892 events.Add(new MouseButtonEventInfo(LocalGamer, frameOfApplication, MouseButton.Left, leftButtonPressed));
895 bool rightButtonPressed = mouseState.RightButton == ButtonState.Pressed;
896 if (rightButtonPressed != mLastRightButtonPressed)
898 events.Add(new MouseButtonEventInfo(LocalGamer, frameOfApplication, MouseButton.Right, rightButtonPressed));
901 bool middleButtonPressed = mouseState.MiddleButton == ButtonState.Pressed;
902 if (middleButtonPressed != mLastMiddleButtonPressed)
904 events.Add(new MouseButtonEventInfo(LocalGamer, frameOfApplication, MouseButton.Middle, middleButtonPressed));
907 int mousePositionX = mouseState.X;
908 int mousePositionY = mouseState.Y;
909 if (mousePositionX != mLastMousePositionX || mousePositionY != mLastMousePositionY)
911 events.Add(new MouseMotionEventInfo(LocalGamer, frameOfApplication, mousePositionX, mousePositionY));
914 // 3. Save the current peripheral state.
916 mLastPressedKeys = new List<Keys>(pressedKeysArray);
917 mLastLeftButtonPressed = leftButtonPressed;
918 mLastRightButtonPressed = rightButtonPressed;
919 mLastMiddleButtonPressed = middleButtonPressed;
920 mLastMousePositionX = mousePositionX;
921 mLastMousePositionY = mousePositionY;
926 void SendLocalEvents()
928 SendLocalEvents((NetworkGamer)null);
931 void SendLocalEvents(List<NetworkGamer> recipicents)
933 foreach (NetworkGamer gamer in recipicents)
935 SendLocalEvents(gamer);
939 void SendLocalEvents(NetworkGamer recipient)
942 if (mDontSendEvents) return;
944 List<EventInfo> events = new List<EventInfo>(mLocalEvents);
945 events.AddRange(mLastLocalEvents);
947 WriteEventPacket(events);
949 if (recipient != null && !recipient.IsDisposed)
951 LocalGamer.SendData(mPacketWriter, SendDataOptions.Reliable, recipient);
955 LocalGamer.SendData(mPacketWriter, SendDataOptions.None);
960 bool HaveNeededEvents
964 long currentFrame = mGame.CurrentFrameNumber;
966 foreach (GamerInfo gamerInfo in mGamers.Values)
968 if (gamerInfo.HighestFrameNumber < currentFrame) return false;
977 int index = CurrentEventArrayIndex;
979 foreach (GamerInfo gamerInfo in GamerArray)
981 if (gamerInfo.Events[index] == null) continue;
983 foreach (EventInfo eventInfo in gamerInfo.Events[index])
985 KeyboardEventInfo keyboardEventInfo = eventInfo as KeyboardEventInfo;
986 if (keyboardEventInfo != null)
988 Console.WriteLine(keyboardEventInfo.FrameOfApplication + " KEY: " + keyboardEventInfo.Key + "," + keyboardEventInfo.IsKeyDown);
989 mGame.ApplyKeyInput(gamerInfo, keyboardEventInfo.Key, keyboardEventInfo.IsKeyDown);
993 MouseButtonEventInfo mouseButtonEventInfo = eventInfo as MouseButtonEventInfo;
994 if (mouseButtonEventInfo != null)
996 mGame.ApplyMouseButtonInput(gamerInfo, mouseButtonEventInfo.IsButtonDown);
997 Console.WriteLine(mouseButtonEventInfo.FrameOfApplication + " BTN: " + mouseButtonEventInfo.IsButtonDown);
1001 MouseMotionEventInfo mouseMotionEventInfo = eventInfo as MouseMotionEventInfo;
1002 if (mouseMotionEventInfo != null)
1004 mGame.ApplyMouseLocationInput(gamerInfo, mouseMotionEventInfo.X, mouseMotionEventInfo.Y);
1005 Console.WriteLine(mouseMotionEventInfo.FrameOfApplication + " MMV: " + mouseMotionEventInfo.X + "," + mouseMotionEventInfo.Y);
1010 gamerInfo.Events[index] = null;
1015 int CurrentAverageOneWayDelay
1019 Debug.Assert(mNetworkSession != null);
1021 double numRemoteGamersTwice = 2 * mNetworkSession.RemoteGamers.Count;
1022 double averageOwd = 0;
1024 foreach (NetworkGamer gamer in mNetworkSession.RemoteGamers)
1026 TimeSpan timeSpan = gamer.RoundtripTime;
1027 averageOwd += timeSpan.TotalMilliseconds;
1030 return (int)((averageOwd / numRemoteGamersTwice) / 16.6666);