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 /// Construct a NetworkGame with a lobby and a game.
42 /// <param name="lobby">Provides an associated lobby to update and draw.</param>
43 /// <param name="game">Provides a game object to be played over the network.</param>
44 public NetworkGame(ILobby lobby, IDeterministicGame game)
46 Debug.Assert(lobby != null && game != null);
54 /// Get the Gamer object for the local player.
56 public LocalNetworkGamer LocalGamer
60 // TODO: Is this the correct way to get the single local gamer?
61 return mNetworkSession.LocalGamers[0];
66 /// Get all the gamers associated with the active network session.
68 public GamerCollection<NetworkGamer> NetworkGamers
72 return mNetworkSession.AllGamers;
78 /// Begin a new network session with the local gamer as the host. You must not
79 /// call this method or use JoinSession without first using LeaveSession.
81 /// <param name="callback">The delegate/method to call when the session is created.</param>
82 public void CreateSession(JoinedSessionDelegate callback)
84 CreateSession(mGame.MaximumSupportedPlayers, callback);
88 /// Begin a new network session with the local gamer as the host. You must not
89 /// call this method or use JoinSession without first using LeaveSession.
91 /// <param name="maxGamers">Provide the maximum number of players allowed to connect.</param>
92 /// <param name="callback">The delegate/method to call when the session is created.</param>
93 public void CreateSession(int maxGamers, JoinedSessionDelegate callback)
95 Debug.Assert(mNetworkSession == null);
97 mJoinedSessionDelegate = callback;
98 NetworkSession.BeginCreate(NetworkSessionType.SystemLink, 1, maxGamers, CreateSessionEnd, null);
100 private void CreateSessionEnd(IAsyncResult result)
102 Debug.Assert(mNetworkSession == null);
104 mNetworkSession = NetworkSession.EndCreate(result);
105 mNetworkSession.AllowHostMigration = true;
106 mNetworkSession.AllowJoinInProgress = false;
107 mNetworkSession.GameStarted += new EventHandler<GameStartedEventArgs>(mNetworkSession_GameStarted);
108 mJoinedSessionDelegate(mNetworkSession, this);
113 void mNetworkSession_GameStarted(object sender, GameStartedEventArgs e)
119 /// Determine whether or not the network game object is associated with any network session.
121 /// <returns>True if there exists a NetworkSession; false otherwise.</returns>
122 public bool HasActiveSession
126 return mNetworkSession != null;
132 /// Find available sessions to join. You should not already be in a session when
133 /// calling this method; call LeaveSession first.
135 /// <param name="callback">The delegate/method to call when the search finishes.</param>
136 public void FindSessions(FoundSessionsDelegate callback)
138 Debug.Assert(mNetworkSession == null);
140 mFoundSessionsDelegate = callback;
141 NetworkSession.BeginFind(NetworkSessionType.SystemLink, 1, null, new AsyncCallback(FindSessionsEnd), null);
143 private void FindSessionsEnd(IAsyncResult result)
145 AvailableNetworkSessionCollection sessions = NetworkSession.EndFind(result);
146 mFoundSessionsDelegate(sessions, this);
150 /// Join a network session found using FindSessions(). This is for joining a game that
151 /// somebody else has already started hosting. You must not already be in a session.
153 /// <param name="availableSession">Pass the session object to try to join.</param>
154 /// <param name="callback">The delegate/method to call when the search finishes.</param>
155 public void JoinSession(AvailableNetworkSession availableSession, JoinedSessionDelegate callback)
157 Debug.Assert(mNetworkSession == null);
159 mJoinedSessionDelegate = callback;
160 NetworkSession.BeginJoin(availableSession, JoinSessionEnd, null);
162 private void JoinSessionEnd(IAsyncResult result)
164 Debug.Assert(mNetworkSession == null);
166 mNetworkSession = NetworkSession.EndJoin(result);
168 mJoinedSessionDelegate(mNetworkSession, this);
169 mJoinedSessionDelegate = null;
171 mNetworkSession.GameStarted += new EventHandler<GameStartedEventArgs>(mNetworkSession_GameStarted);
176 /// Leave and dispose of any currently associated network session. You will find yourself
177 /// back in the lobby. You must already be in a session to leave it.
179 public void LeaveSession()
181 Debug.Assert(mNetworkSession != null);
183 mNetworkSession.Dispose();
184 mNetworkSession = null;
189 /// Set up the network session to simulate 200ms latency and 10% packet loss.
191 public void SimulateBadNetwork()
193 Debug.Assert(mNetworkSession != null);
195 mNetworkSession.SimulatedLatency = new TimeSpan(0, 0, 0, 0, 200);
196 mNetworkSession.SimulatedPacketLoss = 0.1f;
201 /// Indicate that the game should begin (moving players from the lobby to the game).
202 /// You must call CreateSession() before calling this.
204 public void StartGame()
206 Debug.Assert(mNetworkSession != null && mNetworkSession.IsHost &&
207 mNetworkSession.AllGamers.Count >= mGame.MinimumSupportedPlayers &&
208 mNetworkSession.IsEveryoneReady);
214 /// Indicate that the game should begin. This is like StartGame() without the sanity
215 /// checks. Use this for debugging.
217 public void ForceStartGame()
219 mNetworkSession.StartGame();
220 mNetworkSession.ResetReady();
227 /// Manages the network session and allows either the lobby or game to update.
229 /// <param name="gameTime">Pass the time away.</param>
230 public void Update(GameTime gameTime)
232 if (mNetworkSession == null)
234 mLobby.Update(gameTime, this);
238 mNetworkSession.Update();
239 HandleIncomingPackets();
241 if (mNetworkSession.SessionState == NetworkSessionState.Lobby)
243 mLobby.Update(gameTime, this);
245 else if (mNetworkSession.SessionState == NetworkSessionState.Playing)
247 //TODO reset needs to be called for all players, currently LocalGamerInfo throughs exception for all nonhosts
248 // because in start game reset is only called on hosts games thus other clients never get an updated list
252 //thoughs exeption see TODO above
253 //if (mGame.IsGameOver(LocalGamerInfo) || mGame.IsTerminated(LocalGamerInfo))
255 // // TODO: Should support moving back to the session lobby.
260 if (HaveNeededEvents)
262 if (IsLatencyAdjustmentFrame)
267 mLocalEvents.AddRange(GetEventsFromInput());
270 mGame.Update(mTargetTimeSpan);
276 if (mStallCount % 60 == 0)
278 Console.WriteLine("Stalled for " + mStallCount + " frames.");
281 /*if (mStallCount > StallTimeout)
286 else if (mStallCount == 1)
290 else if (mStallCount % 60 == 0)
299 /// Allows either the lobby or the game to draw, depending on the state
300 /// of the network connection and whether or not a game is in progress.
302 /// <param name="gameTime">Pass the time away.</param>
303 /// <param name="spriteBatch">The sprite batch.</param>
304 public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
306 if (mNetworkSession == null)
308 mLobby.Draw(spriteBatch);
312 if (mNetworkSession.SessionState == NetworkSessionState.Lobby)
314 mLobby.Draw(spriteBatch);
316 else if (mNetworkSession.SessionState == NetworkSessionState.Playing)
318 mGame.Draw(spriteBatch);
325 /// Get the chat messages that have been receive since the last time this
326 /// method was called.
328 /// <returns>List container of the chat messages.</returns>
329 public List<ChatInfo> ReceiveChats()
331 List<ChatInfo> chats = mChatPackets;
332 mChatPackets = new List<ChatInfo>();
337 /// Send a chat message to all gamers in the session. You should already be
338 /// in a session before calling this method.
340 /// <param name="message">The text of the message.</param>
341 public void SendChat(String message)
343 WriteChatPacket(message);
344 LocalGamer.SendData(mPacketWriter, SendDataOptions.ReliableInOrder);
348 /// Send a chat message to a specific gamer in the session. You should already
349 /// be in a session before calling this method.
351 /// <param name="message">The text of the message.</param>
352 /// <param name="recipient">The gamer to receive the message.</param>
353 public void SendChat(String message, NetworkGamer recipient)
355 WriteChatPacket(message);
356 LocalGamer.SendData(mPacketWriter, SendDataOptions.ReliableInOrder, recipient);
362 // Private class variable members
363 #region Instance Variables
365 NetworkSession mNetworkSession;
366 PacketReader mPacketReader = new PacketReader();
367 PacketWriter mPacketWriter = new PacketWriter();
369 JoinedSessionDelegate mJoinedSessionDelegate;
370 FoundSessionsDelegate mFoundSessionsDelegate;
373 IDeterministicGame mGame;
375 List<ChatInfo> mChatPackets = new List<ChatInfo>();
377 List<EventInfo> mLocalEvents = new List<EventInfo>();
378 List<EventInfo> mLastLocalEvents = new List<EventInfo>();
380 List<Keys> mLastPressedKeys = new List<Keys>();
381 bool mLastLeftButtonPressed;
382 bool mLastRightButtonPressed;
383 bool mLastMiddleButtonPressed;
384 int mLastMousePositionX;
385 int mLastMousePositionY;
388 long mNextLatencyAdjustmentFrame;
392 TimeSpan mTargetTimeSpan = new TimeSpan(166666);
393 public TimeSpan TargetTimeSpan
397 return mTargetTimeSpan;
401 Dictionary<byte, GamerInfo> mGamers;
402 GamerInfo[] GamerArray
406 GamerInfo[] gamerList = mGamers.Values.ToArray();
407 Array.Sort(gamerList, delegate(GamerInfo a, GamerInfo b)
409 return a.Gamer.Id.CompareTo(b.Gamer.Id);
414 GamerInfo LocalGamerInfo
418 return mGamers[LocalGamer.Id];
425 // Private types for the implementation of the network protocol
426 #region Private Types
451 abstract class EventInfo
453 public NetworkGamer Gamer;
454 public long FrameOfApplication;
456 public EventInfo(NetworkGamer gamer, long frameNumber)
459 FrameOfApplication = frameNumber;
462 public abstract EventType Id
468 class KeyboardEventInfo : EventInfo
471 public bool IsKeyDown;
473 public KeyboardEventInfo(NetworkGamer gamer, long frameNumber, Keys key, bool isDown)
474 : base(gamer, frameNumber)
480 public override EventType Id
482 get { return IsKeyDown ? EventType.KeyDown : EventType.KeyUp; }
486 class MouseButtonEventInfo : EventInfo
488 public MouseButton Button;
489 public bool IsButtonDown;
491 public MouseButtonEventInfo(NetworkGamer gamer, long frameNumber, MouseButton button, bool isDown)
492 : base(gamer, frameNumber)
495 IsButtonDown = isDown;
498 public override EventType Id
500 get { return IsButtonDown ? EventType.MouseDown : EventType.MouseUp; }
504 class MouseMotionEventInfo : EventInfo
509 public MouseMotionEventInfo(NetworkGamer gamer, long frameNumber, int x, int y)
510 : base(gamer, frameNumber)
516 public override EventType Id
518 get { return EventType.MouseMove; }
524 public NetworkGamer Gamer;
525 public long HighestFrameNumber = 0;
526 public int StallCount = 0;
527 public int AverageOwd = 0;
528 public bool IsWaitedOn = false;
529 public List<EventInfo>[] Events = new List<EventInfo>[MaximumLatency];
531 public GamerInfo(NetworkGamer gamer)
537 const int MaximumLatency = 120;
538 const int StallTimeout = 900;
543 // Private implementation methods of the network protocol
544 #region Private Implementation Methods
547 /// Reinitialize the private variables in preparation for a new game to start.
552 mNextLatencyAdjustmentFrame = 1;
554 mAverageOwd = CurrentAverageOneWayDelay;
556 mGamers = new Dictionary<byte, GamerInfo>();
557 foreach (NetworkGamer gamer in NetworkGamers)
559 mGamers.Add(gamer.Id, new GamerInfo(gamer));
562 mGame.ResetGame(GamerArray, LocalGamerInfo);
566 void HandleIncomingPackets()
568 LocalNetworkGamer localGamer = LocalGamer;
570 while (localGamer.IsDataAvailable)
574 localGamer.ReceiveData(mPacketReader, out sender);
575 GamerInfo senderInfo = mGamers[sender.Id];
577 PacketType packetId = (PacketType)mPacketReader.ReadByte();
580 case PacketType.Chat:
582 short messageLength = mPacketReader.ReadInt16();
583 char[] message = mPacketReader.ReadChars(messageLength);
585 ChatInfo chatPacket = new ChatInfo(sender, new String(message));
586 mChatPackets.Add(chatPacket);
589 case PacketType.Event:
591 short stallCount = mPacketReader.ReadInt16();
592 short averageOwd = mPacketReader.ReadInt16();
593 int frameNumber = mPacketReader.ReadInt32();
594 byte numEvents = mPacketReader.ReadByte();
596 if (frameNumber <= senderInfo.HighestFrameNumber)
598 // we know about all these events, so don't bother reading them
602 for (byte i = 0; i < numEvents; ++i)
604 EventInfo eventInfo = ReadEvent(mPacketReader, sender);
606 if (eventInfo != null && eventInfo.FrameOfApplication < senderInfo.HighestFrameNumber)
608 int index = EventArrayIndex;
609 if (senderInfo.Events[index] == null) senderInfo.Events[index] = new List<EventInfo>();
610 senderInfo.Events[index].Add(eventInfo);
614 senderInfo.StallCount = stallCount;
615 senderInfo.AverageOwd = averageOwd;
616 senderInfo.HighestFrameNumber = frameNumber;
619 case PacketType.Stall:
621 byte numStalledPeers = mPacketReader.ReadByte();
622 byte[] stalledPeers = mPacketReader.ReadBytes(numStalledPeers);
630 Console.WriteLine("Received unknown packet type: " + (int)packetId);
639 get { return (int)(mGame.CurrentFrameNumber % MaximumLatency); }
642 EventInfo ReadEvent(PacketReader packetReader, NetworkGamer sender)
644 EventType eventId = (EventType)packetReader.ReadByte();
645 long frameNumber = packetReader.ReadInt32();
649 case EventType.KeyDown:
651 int keyCode1 = packetReader.ReadInt32();
652 return new KeyboardEventInfo(sender, frameNumber, (Keys)keyCode1, true);
654 case EventType.KeyUp:
656 int keyCode2 = packetReader.ReadInt32();
657 return new KeyboardEventInfo(sender, frameNumber, (Keys)keyCode2, false);
659 case EventType.MouseDown:
661 byte buttonId1 = packetReader.ReadByte();
662 return new MouseButtonEventInfo(sender, frameNumber, (MouseButton)buttonId1, true);
664 case EventType.MouseUp:
666 byte buttonId2 = packetReader.ReadByte();
667 return new MouseButtonEventInfo(sender, frameNumber, (MouseButton)buttonId2, false);
669 case EventType.MouseMove:
671 short x = packetReader.ReadInt16();
672 short y = packetReader.ReadInt16();
673 return new MouseMotionEventInfo(sender, frameNumber, x, y);
677 Console.WriteLine("Received unknown event type: " + (int)eventId);
683 void WriteChatPacket(String message)
685 mPacketWriter.Write((byte)PacketType.Chat);
686 mPacketWriter.Write((short)message.Length);
687 mPacketWriter.Write(message.ToCharArray());
690 void WriteEventPacket(List<EventInfo> events)
692 mPacketWriter.Write((byte)PacketType.Event);
693 mPacketWriter.Write((short)mStallCount);
694 mPacketWriter.Write((short)mAverageOwd);
695 mPacketWriter.Write((int)(mGame.CurrentFrameNumber + mLatency));
696 mPacketWriter.Write((byte)events.Count);
698 foreach (EventInfo eventInfo in events)
700 mPacketWriter.Write((byte)eventInfo.Id);
701 mPacketWriter.Write((int)eventInfo.FrameOfApplication);
703 KeyboardEventInfo keyboardEventInfo = eventInfo as KeyboardEventInfo;
704 if (keyboardEventInfo != null)
706 mPacketWriter.Write((int)keyboardEventInfo.Key);
710 MouseButtonEventInfo mouseButtonEventInfo = eventInfo as MouseButtonEventInfo;
711 if (mouseButtonEventInfo != null)
713 mPacketWriter.Write((byte)mouseButtonEventInfo.Button);
717 MouseMotionEventInfo mouseMotionEventInfo = eventInfo as MouseMotionEventInfo;
718 if (mouseMotionEventInfo != null)
720 mPacketWriter.Write((short)mouseMotionEventInfo.X);
721 mPacketWriter.Write((short)mouseMotionEventInfo.Y);
728 bool IsLatencyAdjustmentFrame
732 return mNextLatencyAdjustmentFrame == mGame.CurrentFrameNumber;
738 Debug.Assert(IsLatencyAdjustmentFrame);
740 int maxStallCount = 0;
741 int maxAverageOwd = 0;
743 foreach (GamerInfo gamerInfo in GamerArray)
745 if (gamerInfo.StallCount > maxStallCount) maxStallCount = gamerInfo.StallCount;
746 if (gamerInfo.AverageOwd > maxAverageOwd) maxAverageOwd = gamerInfo.AverageOwd;
750 int prevLatency = mLatency;
752 if (maxStallCount > 0)
754 mLatency += maxStallCount;
758 mLatency = (int)(0.6 * (double)(mLatency - maxAverageOwd) + 1.0);
762 if (prevLatency != mLatency) Console.WriteLine("Latency readjusted to " + mLatency);
764 if (mLatency < 1) mLatency = 1;
765 if (mLatency > MaximumLatency) mLatency = MaximumLatency;
767 mNextLatencyAdjustmentFrame = mGame.CurrentFrameNumber + mLatency;
768 mAverageOwd = CurrentAverageOneWayDelay;
770 mLastLocalEvents = mLocalEvents;
771 mLocalEvents = new List<EventInfo>();
776 List<EventInfo> GetEventsFromInput()
778 List<EventInfo> events = new List<EventInfo>();
780 // 1. Find the keyboard differences; written by Peter.
782 KeyboardState keyState = Keyboard.GetState();
784 List<Keys> pressedKeys = new List<Keys>();
785 List<Keys> releasedKeys = new List<Keys>();
787 Keys[] pressedKeysArray = keyState.GetPressedKeys();
788 foreach (Keys k in pressedKeysArray)
790 if (!mLastPressedKeys.Contains(k)) pressedKeys.Add(k);
791 else mLastPressedKeys.Remove(k);
794 releasedKeys = mLastPressedKeys;
796 foreach (Keys key in pressedKeys)
798 events.Add(new KeyboardEventInfo(LocalGamer, mGame.CurrentFrameNumber, key, true));
800 foreach (Keys key in releasedKeys)
802 events.Add(new KeyboardEventInfo(LocalGamer, mGame.CurrentFrameNumber, key, false));
805 // 2. Find the mouse differences.
807 MouseState mouseState = Mouse.GetState();
809 bool leftButtonPressed = mouseState.LeftButton == ButtonState.Pressed;
810 if (leftButtonPressed != mLastLeftButtonPressed)
812 events.Add(new MouseButtonEventInfo(LocalGamer, mGame.CurrentFrameNumber, MouseButton.Left, leftButtonPressed));
815 bool rightButtonPressed = mouseState.LeftButton == ButtonState.Pressed;
816 if (rightButtonPressed != mLastRightButtonPressed)
818 events.Add(new MouseButtonEventInfo(LocalGamer, mGame.CurrentFrameNumber, MouseButton.Right, rightButtonPressed));
821 bool middleButtonPressed = mouseState.LeftButton == ButtonState.Pressed;
822 if (middleButtonPressed != mLastMiddleButtonPressed)
824 events.Add(new MouseButtonEventInfo(LocalGamer, mGame.CurrentFrameNumber, MouseButton.Middle, middleButtonPressed));
827 int mousePositionX = mouseState.X;
828 int mousePositionY = mouseState.Y;
829 if (mousePositionX != mLastMousePositionX || mousePositionY != mLastMousePositionY)
831 events.Add(new MouseMotionEventInfo(LocalGamer, mGame.CurrentFrameNumber, mousePositionX, mousePositionY));
834 // 3. Save the current peripheral state.
836 mLastPressedKeys = new List<Keys>(pressedKeysArray);
837 mLastLeftButtonPressed = leftButtonPressed;
838 mLastRightButtonPressed = rightButtonPressed;
839 mLastMiddleButtonPressed = middleButtonPressed;
840 mLastMousePositionX = mousePositionX;
841 mLastMousePositionY = mousePositionY;
846 void SendLocalEvents()
848 SendLocalEvents((NetworkGamer)null);
851 void SendLocalEvents(List<NetworkGamer> recipicents)
853 foreach (NetworkGamer gamer in recipicents)
855 SendLocalEvents(gamer);
859 void SendLocalEvents(NetworkGamer recipient)
861 List<EventInfo> events = new List<EventInfo>(mLocalEvents);
862 events.AddRange(mLastLocalEvents);
864 WriteEventPacket(events);
866 if (recipient != null)
868 LocalGamer.SendData(mPacketWriter, SendDataOptions.Reliable, recipient);
872 LocalGamer.SendData(mPacketWriter, SendDataOptions.None);
877 bool HaveNeededEvents
881 long currentFrame = mGame.CurrentFrameNumber;
883 foreach (GamerInfo gamerInfo in mGamers.Values)
885 if (gamerInfo.HighestFrameNumber < currentFrame) return false;
894 int index = EventArrayIndex;
896 foreach (GamerInfo gamerInfo in GamerArray)
898 if (gamerInfo.Events[index] == null) continue;
900 foreach (EventInfo eventInfo in gamerInfo.Events[index])
902 KeyboardEventInfo keyboardEventInfo = eventInfo as KeyboardEventInfo;
903 if (keyboardEventInfo != null)
905 mGame.ApplyKeyInput(gamerInfo, keyboardEventInfo.Key, keyboardEventInfo.IsKeyDown);
909 MouseButtonEventInfo mouseButtonEventInfo = eventInfo as MouseButtonEventInfo;
910 if (mouseButtonEventInfo != null)
912 mGame.ApplyMouseButtonInput(gamerInfo, mouseButtonEventInfo.IsButtonDown);
916 MouseMotionEventInfo mouseMotionEventInfo = eventInfo as MouseMotionEventInfo;
917 if (mouseMotionEventInfo != null)
919 mGame.ApplyMouseLocationInput(gamerInfo, mouseMotionEventInfo.X, mouseMotionEventInfo.Y);
924 gamerInfo.Events[index] = null;
929 int CurrentAverageOneWayDelay
933 Debug.Assert(mNetworkSession != null);
935 double numRemoteGamersTwice = 2 * mNetworkSession.RemoteGamers.Count;
936 double averageOwd = 0;
938 foreach (NetworkGamer gamer in mNetworkSession.RemoteGamers)
940 TimeSpan timeSpan = gamer.RoundtripTime;
941 averageOwd += timeSpan.TotalMilliseconds;
944 return (int)(averageOwd / numRemoteGamersTwice / 16.6666);