]> Dogcows Code - chaz/carfire/blob - Project06/CS 3505 Project 06/CS 3505 Project 06/NetworkGame.cs
null pointer dereference fix
[chaz/carfire] / Project06 / CS 3505 Project 06 / CS 3505 Project 06 / NetworkGame.cs
1 
2 //#define DEBUG
3
4 using System;
5 using System.Collections.Generic;
6 using System.Linq;
7 using System.Text;
8 using Microsoft.Xna.Framework.Net;
9 using System.Diagnostics;
10 using Microsoft.Xna.Framework.GamerServices;
11 using Microsoft.Xna.Framework.Graphics;
12 using Microsoft.Xna.Framework;
13 using Microsoft.Xna.Framework.Input;
14 using System.Collections;
15
16 namespace CS_3505_Project_06
17 {
18 /// <summary>
19 /// A manager class to handle network interactions between peers and
20 /// lobby/game switching.
21 /// </summary>
22 public class NetworkGame
23 {
24 // Public methods and properties
25 #region Public Methods
26
27 /// <summary>
28 /// Called when a session has been created or joined using CreateSession() or JoinSession().
29 /// </summary>
30 /// <param name="session">The new session that was created or joined.</param>
31 /// <param name="networkGame">The NetworkGame that joined the session.</param>
32 public delegate void JoinedSessionDelegate(NetworkSession session, NetworkGame networkGame);
33
34 /// <summary>
35 /// Called when sessions are found as a result of calling FindSessions().
36 /// </summary>
37 /// <param name="sessions">A container of the available sessions.</param>
38 /// <param name="networkGame">The NetworkGame that searched for the sessions.</param>
39 public delegate void FoundSessionsDelegate(AvailableNetworkSessionCollection sessions, NetworkGame networkGame);
40
41
42 /// <summary>
43 /// Called when an exception is thrown during an asynchronous operation.
44 /// </summary>
45 /// <param name="exception">The exception that was thrown.</param>
46 /// <param name="networkGame">The NetworkGame that errored.</param>
47 public delegate void CaughtErrorDelegate(Exception exception, NetworkGame networkGame);
48
49 /// <summary>
50 /// Get and set the error delegate, called when an exception is thrown during
51 /// and asynchronous operation. This will occur if you try to create or join a
52 /// session without being logged into a profile.
53 /// </summary>
54 public CaughtErrorDelegate ErrorDelegate;
55
56
57 /// <summary>
58 /// Construct a NetworkGame with a lobby and a game.
59 /// </summary>
60 /// <param name="lobby">Provides an associated lobby to update and draw.</param>
61 /// <param name="game">Provides a game object to be played over the network.</param>
62 public NetworkGame(ILobby lobby, IDeterministicGame game)
63 {
64 Debug.Assert(lobby != null && game != null);
65
66 mLobby = lobby;
67 mGame = game;
68 }
69
70
71 /// <summary>
72 /// Get the Gamer object for the local player.
73 /// </summary>
74 public LocalNetworkGamer LocalGamer
75 {
76 get
77 {
78 // TODO: Is this the correct way to get the single local gamer?
79 return mNetworkSession.LocalGamers[0];
80 }
81 }
82
83 /// <summary>
84 /// Get all the gamers associated with the active network session.
85 /// </summary>
86 public GamerCollection<NetworkGamer> NetworkGamers
87 {
88 get
89 {
90 return mNetworkSession.AllGamers;
91 }
92 }
93
94
95 /// <summary>
96 /// Begin a new network session with the local gamer as the host. You must not
97 /// call this method or use JoinSession without first using LeaveSession.
98 /// </summary>
99 /// <param name="callback">The delegate/method to call when the session is created.</param>
100 public void CreateSession(JoinedSessionDelegate callback)
101 {
102 CreateSession(mGame.MaximumSupportedPlayers, callback);
103 }
104
105 /// <summary>
106 /// Begin a new network session with the local gamer as the host. You must not
107 /// call this method or use JoinSession without first using LeaveSession.
108 /// </summary>
109 /// <param name="maxGamers">Provide the maximum number of players allowed to connect.</param>
110 /// <param name="callback">The delegate/method to call when the session is created.</param>
111 public void CreateSession(int maxGamers, JoinedSessionDelegate callback)
112 {
113 Debug.Assert(mNetworkSession == null);
114
115 mJoinedSessionDelegate = callback;
116 NetworkSession.BeginCreate(NetworkSessionType.SystemLink, 1, maxGamers, CreateSessionEnd, null);
117 }
118 void CreateSessionEnd(IAsyncResult result)
119 {
120 Debug.Assert(mNetworkSession == null);
121
122 try
123 {
124 mNetworkSession = NetworkSession.EndCreate(result);
125 mNetworkSession.AllowHostMigration = true;
126 mNetworkSession.AllowJoinInProgress = false;
127 mNetworkSession.GameStarted += new EventHandler<GameStartedEventArgs>(GameStartedEvent);
128 }
129 catch (Exception e)
130 {
131 if (ErrorDelegate != null) ErrorDelegate(e, this);
132 return;
133 }
134 mJoinedSessionDelegate(mNetworkSession, this);
135 mJoinedSessionDelegate = null;
136 }
137 void GameStartedEvent(object sender, GameStartedEventArgs e)
138 {
139 Reset();
140 }
141
142 /// <summary>
143 /// Determine whether or not the network game object is associated with any network session.
144 /// </summary>
145 /// <returns>True if there exists a NetworkSession; false otherwise.</returns>
146 public bool HasActiveSession
147 {
148 get
149 {
150 return mNetworkSession != null;
151 }
152 }
153
154
155 /// <summary>
156 /// Find available sessions to join. You should not already be in a session when
157 /// calling this method; call LeaveSession first.
158 /// </summary>
159 /// <param name="callback">The delegate/method to call when the search finishes.</param>
160 public void FindSessions(FoundSessionsDelegate callback)
161 {
162 Debug.Assert(mNetworkSession == null);
163
164 mFoundSessionsDelegate = callback;
165 NetworkSession.BeginFind(NetworkSessionType.SystemLink, 1, null, new AsyncCallback(FindSessionsEnd), null);
166 }
167 void FindSessionsEnd(IAsyncResult result)
168 {
169 AvailableNetworkSessionCollection sessions;
170 try
171 {
172 sessions = NetworkSession.EndFind(result);
173 }
174 catch (Exception e)
175 {
176 if (ErrorDelegate != null) ErrorDelegate(e, this);
177 return;
178 }
179 mFoundSessionsDelegate(sessions, this);
180 mFoundSessionsDelegate = null;
181 }
182
183 /// <summary>
184 /// Join a network session found using FindSessions(). This is for joining a game that
185 /// somebody else has already started hosting. You must not already be in a session.
186 /// </summary>
187 /// <param name="availableSession">Pass the session object to try to join.</param>
188 /// <param name="callback">The delegate/method to call when the search finishes.</param>
189 public void JoinSession(AvailableNetworkSession availableSession, JoinedSessionDelegate callback)
190 {
191 Debug.Assert(mNetworkSession == null);
192
193 mJoinedSessionDelegate = callback;
194 NetworkSession.BeginJoin(availableSession, JoinSessionEnd, null);
195 }
196 void JoinSessionEnd(IAsyncResult result)
197 {
198 Debug.Assert(mNetworkSession == null);
199
200 try
201 {
202 mNetworkSession = NetworkSession.EndJoin(result);
203 mNetworkSession.GameStarted += new EventHandler<GameStartedEventArgs>(GameStartedEvent);
204 }
205 catch (Exception e)
206 {
207 if (ErrorDelegate != null) ErrorDelegate(e, this);
208 return;
209 }
210 mJoinedSessionDelegate(mNetworkSession, this);
211 mJoinedSessionDelegate = null;
212 }
213
214
215 /// <summary>
216 /// Leave and dispose of any currently associated network session. You will find yourself
217 /// back in the lobby. You must already be in a session to leave it.
218 /// </summary>
219 public void LeaveSession()
220 {
221 Debug.Assert(mNetworkSession != null);
222
223 mNetworkSession.Dispose();
224 mNetworkSession = null;
225 }
226
227
228 /// <summary>
229 /// Set up the network session to simulate 200ms latency and 10% packet loss.
230 /// </summary>
231 public void SimulateBadNetwork()
232 {
233 Debug.Assert(mNetworkSession != null);
234
235 mNetworkSession.SimulatedLatency = new TimeSpan(0, 0, 0, 0, 200);
236 mNetworkSession.SimulatedPacketLoss = 0.1f;
237 }
238
239
240 /// <summary>
241 /// Indicate that the game should begin (moving players from the lobby to the game).
242 /// You must call CreateSession() before calling this.
243 /// </summary>
244 public void StartGame()
245 {
246 Debug.Assert(mNetworkSession != null && mNetworkSession.IsHost &&
247 mNetworkSession.AllGamers.Count >= mGame.MinimumSupportedPlayers &&
248 mNetworkSession.IsEveryoneReady);
249
250 ForceStartGame();
251 }
252
253 /// <summary>
254 /// Indicate that the game should begin. This is like StartGame() without the sanity
255 /// checks. Use this for debugging.
256 /// </summary>
257 public void ForceStartGame()
258 {
259 mNetworkSession.StartGame();
260 mNetworkSession.ResetReady();
261 }
262
263
264 /// <summary>
265 /// Manages the network session and allows either the lobby or game to update.
266 /// </summary>
267 /// <param name="gameTime">Pass the time away.</param>
268 public void Update(GameTime gameTime)
269 {
270 if (mNetworkSession == null)
271 {
272 mLobby.Update(gameTime, this);
273 }
274 else
275 {
276 mNetworkSession.Update();
277 HandleIncomingPackets();
278
279 if (mNetworkSession.SessionState == NetworkSessionState.Lobby)
280 {
281 mLobby.Update(gameTime, this);
282 }
283 else if (mNetworkSession.SessionState == NetworkSessionState.Playing)
284 {
285 if (mGame.IsGameOver(LocalGamerInfo) || mGame.IsTerminated(LocalGamerInfo))
286 {
287 // TODO: Should support moving back to the session lobby.
288 LeaveSession();
289 return;
290 }
291
292 if (HaveNeededEvents)
293 {
294 if (IsLatencyAdjustmentFrame)
295 {
296 AdjustLatency();
297 mLastStallCount = mStallCount;
298 mStallCount = 0;
299 }
300 mLocalEvents.AddRange(GetEventsFromInput());
301 SendLocalEvents();
302 ApplyEvents();
303
304 #if DEBUG
305 Console.WriteLine("HASH: " + mGame.CurrentFrameNumber + "\t" + mGame.CurrentChecksum);
306 #endif
307
308 mGame.Update(mTargetTimeSpan);
309 }
310 else // Stall!
311 {
312 if (mStallCount == 0)
313 {
314 #if DEBUG
315 Console.WriteLine("STAL: ====");
316 #endif
317 }
318 else if (mStallCount % 60 == 0)
319 {
320 // DEBUG
321 //Console.WriteLine("Stalled for " + mStallCount + " frames.");
322 }
323
324 mStallCount++;
325
326 // Send a reliable event packet to each stalled gamer.
327 if (mStallCount == 1)
328 {
329 foreach (GamerInfo gamerInfo in GamerArray)
330 {
331 if (gamerInfo.HighestFrameNumber < mGame.CurrentFrameNumber)
332 {
333 SendLocalEvents(gamerInfo.Gamer);
334 }
335 }
336 }
337
338 /*if (mStallCount > StallTimeout)
339 {
340 DropLostGamers();
341 mStallCount = 0;
342 }
343 else if (mStallCount == 1)
344 {
345 SendLocalEvents
346 }
347 else if (mStallCount % 60 == 0)
348 {
349 } TODO */
350 }
351 }
352 }
353 }
354
355 /// <summary>
356 /// Allows either the lobby or the game to draw, depending on the state
357 /// of the network connection and whether or not a game is in progress.
358 /// </summary>
359 /// <param name="gameTime">Pass the time away.</param>
360 /// <param name="spriteBatch">The sprite batch.</param>
361 public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
362 {
363 if (mNetworkSession == null)
364 {
365 mLobby.Draw(spriteBatch);
366 }
367 else
368 {
369 if (mNetworkSession.SessionState == NetworkSessionState.Lobby)
370 {
371 mLobby.Draw(spriteBatch);
372 }
373 else if (mNetworkSession.SessionState == NetworkSessionState.Playing)
374 {
375 mGame.Draw(spriteBatch);
376 }
377 }
378 }
379
380
381 /// <summary>
382 /// Get the chat messages that have been received since the last time this
383 /// method was called.
384 /// </summary>
385 /// <returns>List container of the chat messages.</returns>
386 public List<ChatInfo> ReceiveChats()
387 {
388 List<ChatInfo> chats = mChatPackets;
389 mChatPackets = new List<ChatInfo>();
390 return chats;
391 }
392
393 /// <summary>
394 /// Send a chat message to all gamers in the session. You should already be
395 /// in a session before calling this method.
396 /// </summary>
397 /// <param name="message">The text of the message.</param>
398 public void SendChat(String message)
399 {
400 WriteChatPacket(message);
401 LocalGamer.SendData(mPacketWriter, SendDataOptions.ReliableInOrder);
402 }
403
404 /// <summary>
405 /// Send a chat message to a specific gamer in the session. You should already
406 /// be in a session before calling this method.
407 /// </summary>
408 /// <param name="message">The text of the message.</param>
409 /// <param name="recipient">The gamer to receive the message.</param>
410 public void SendChat(String message, NetworkGamer recipient)
411 {
412 Debug.Assert(recipient != null && !recipient.IsDisposed);
413
414 WriteChatPacket(message);
415 LocalGamer.SendData(mPacketWriter, SendDataOptions.ReliableInOrder, recipient);
416 }
417
418 #endregion
419
420
421 // Private class variable members
422 #region Instance Variables
423
424 NetworkSession mNetworkSession;
425 PacketReader mPacketReader = new PacketReader();
426 PacketWriter mPacketWriter = new PacketWriter();
427
428 JoinedSessionDelegate mJoinedSessionDelegate;
429 FoundSessionsDelegate mFoundSessionsDelegate;
430
431 ILobby mLobby;
432 IDeterministicGame mGame;
433
434 List<ChatInfo> mChatPackets = new List<ChatInfo>();
435
436 List<EventInfo> mLocalEvents = new List<EventInfo>();
437 List<EventInfo> mLastLocalEvents = new List<EventInfo>();
438
439 List<Keys> mLastPressedKeys = new List<Keys>();
440 bool mLastLeftButtonPressed;
441 bool mLastRightButtonPressed;
442 bool mLastMiddleButtonPressed;
443 int mLastMousePositionX;
444 int mLastMousePositionY;
445
446 int mLatency;
447 long mHighestFrameNumber;
448 long mNextLatencyAdjustmentFrame;
449 int mStallCount;
450 int mLastStallCount;
451 int mAverageOwd;
452
453 #if DEBUG
454 bool mDontSendEvents;
455 #endif
456
457 TimeSpan mTargetTimeSpan = new TimeSpan(166666);
458 public TimeSpan TargetTimeSpan
459 {
460 get
461 {
462 return mTargetTimeSpan;
463 }
464 }
465
466 Dictionary<byte, GamerInfo> mGamers;
467 GamerInfo[] GamerArray
468 {
469 get
470 {
471 GamerInfo[] gamerList = mGamers.Values.ToArray();
472 Array.Sort(gamerList, delegate(GamerInfo a, GamerInfo b)
473 {
474 return a.Gamer.Id.CompareTo(b.Gamer.Id);
475 });
476 return gamerList;
477 }
478 }
479 GamerInfo LocalGamerInfo
480 {
481 get
482 {
483 return mGamers[LocalGamer.Id];
484 }
485 }
486
487 #endregion
488
489
490 // Private types for the implementation of the network protocol
491 #region Private Types
492
493 enum PacketType
494 {
495 Chat = 1,
496 Event = 2,
497 Stall = 3
498 }
499
500 enum EventType
501 {
502 KeyDown = 1,
503 KeyUp = 2,
504 MouseDown = 3,
505 MouseUp = 4,
506 MouseMove = 5
507 }
508
509 enum MouseButton
510 {
511 Left = 1,
512 Right = 2,
513 Middle = 3
514 }
515
516 abstract class EventInfo
517 {
518 public NetworkGamer Gamer;
519 public long FrameOfApplication;
520
521 public EventInfo(NetworkGamer gamer, long frameNumber)
522 {
523 Gamer = gamer;
524 FrameOfApplication = frameNumber;
525 }
526
527 public abstract EventType Id
528 {
529 get;
530 }
531 }
532
533 class KeyboardEventInfo : EventInfo
534 {
535 public Keys Key;
536 public bool IsKeyDown;
537
538 public KeyboardEventInfo(NetworkGamer gamer, long frameNumber, Keys key, bool isDown)
539 : base(gamer, frameNumber)
540 {
541 Key = key;
542 IsKeyDown = isDown;
543 }
544
545 public override EventType Id
546 {
547 get { return IsKeyDown ? EventType.KeyDown : EventType.KeyUp; }
548 }
549 }
550
551 class MouseButtonEventInfo : EventInfo
552 {
553 public MouseButton Button;
554 public bool IsButtonDown;
555
556 public MouseButtonEventInfo(NetworkGamer gamer, long frameNumber, MouseButton button, bool isDown)
557 : base(gamer, frameNumber)
558 {
559 Button = button;
560 IsButtonDown = isDown;
561 }
562
563 public override EventType Id
564 {
565 get { return IsButtonDown ? EventType.MouseDown : EventType.MouseUp; }
566 }
567 }
568
569 class MouseMotionEventInfo : EventInfo
570 {
571 public int X;
572 public int Y;
573
574 public MouseMotionEventInfo(NetworkGamer gamer, long frameNumber, int x, int y)
575 : base(gamer, frameNumber)
576 {
577 X = x;
578 Y = y;
579 }
580
581 public override EventType Id
582 {
583 get { return EventType.MouseMove; }
584 }
585 }
586
587 class GamerInfo
588 {
589 public NetworkGamer Gamer;
590 public long HighestFrameNumber = 0;
591 public int StallCount = 0;
592 public int AverageOwd = 0;
593 public int NextStallCount = 0;
594 public int NextAverageOwd = 0;
595 public bool IsWaitedOn = false;
596 public List<EventInfo>[] Events = new List<EventInfo>[MaximumLatency];
597
598 public GamerInfo(NetworkGamer gamer)
599 {
600 Gamer = gamer;
601 }
602 }
603
604 const int MaximumLatency = 120;
605 const int StallTimeout = 900;
606
607 #endregion
608
609
610 // Private implementation methods of the network protocol
611 #region Private Implementation Methods
612
613 /// <summary>
614 /// Reinitialize the private variables in preparation for a new game to start.
615 /// </summary>
616 void Reset()
617 {
618 mLatency = 1;
619 mHighestFrameNumber = 0;
620 mNextLatencyAdjustmentFrame = 1;
621 mStallCount = 0;
622 mLastStallCount = 0;
623 mAverageOwd = CurrentAverageOneWayDelay;
624
625 mGamers = new Dictionary<byte, GamerInfo>();
626 foreach (NetworkGamer gamer in NetworkGamers)
627 {
628 mGamers.Add(gamer.Id, new GamerInfo(gamer));
629 }
630
631 mGame.ResetGame(GamerArray, LocalGamerInfo);
632 }
633
634
635 void HandleIncomingPackets()
636 {
637 LocalNetworkGamer localGamer = LocalGamer;
638
639 while (localGamer.IsDataAvailable)
640 {
641 NetworkGamer sender;
642
643 localGamer.ReceiveData(mPacketReader, out sender);
644
645 PacketType packetId = (PacketType)mPacketReader.ReadByte();
646 switch (packetId)
647 {
648 case PacketType.Chat:
649
650 short messageLength = mPacketReader.ReadInt16();
651 char[] message = mPacketReader.ReadChars(messageLength);
652
653 ChatInfo chatPacket = new ChatInfo(sender, new String(message));
654 mChatPackets.Add(chatPacket);
655 break;
656
657 case PacketType.Event:
658
659 GamerInfo senderInfo = mGamers[sender.Id];
660
661 int stallCount = mPacketReader.ReadInt16();
662 int averageOwd = mPacketReader.ReadInt16();
663 int frameNumber = mPacketReader.ReadInt32();
664 int numEvents = mPacketReader.ReadByte();
665
666 if (frameNumber <= mNextLatencyAdjustmentFrame)
667 {
668 senderInfo.StallCount = stallCount;
669 senderInfo.AverageOwd = averageOwd;
670 }
671 else
672 {
673 senderInfo.NextStallCount = stallCount;
674 senderInfo.NextAverageOwd = averageOwd;
675 }
676
677 if (frameNumber <= senderInfo.HighestFrameNumber)
678 {
679 #if DEBUG
680 Console.WriteLine("SKP" + (char)sender.Id + ": " + mGame.CurrentFrameNumber + "\t" + frameNumber + "\t<=\t" + senderInfo.HighestFrameNumber + "\t#" + numEvents);
681 #endif
682
683 // we know about all these events, so don't bother reading them
684 break;
685 }
686
687 #if DEBUG
688 Console.WriteLine(" GOT" + (char)sender.Id + ": " + mGame.CurrentFrameNumber + "\t" + frameNumber + "\t>\t" + senderInfo.HighestFrameNumber + "\t#" + numEvents);
689 #endif
690
691 for (int i = 0; i < numEvents; i++)
692 {
693 EventInfo eventInfo = ReadEvent(mPacketReader, sender);
694
695 if (eventInfo != null && eventInfo.FrameOfApplication > senderInfo.HighestFrameNumber)
696 {
697 int index = GetEventArrayIndexForFrame(eventInfo.FrameOfApplication);
698 if (senderInfo.Events[index] == null) senderInfo.Events[index] = new List<EventInfo>();
699 senderInfo.Events[index].Add(eventInfo);
700 }
701 }
702
703 senderInfo.HighestFrameNumber = frameNumber;
704 break;
705
706 case PacketType.Stall:
707
708 GamerInfo senderInfo2 = mGamers[sender.Id];
709
710 byte numStalledPeers = mPacketReader.ReadByte();
711 byte[] stalledPeers = mPacketReader.ReadBytes(numStalledPeers);
712
713 // TODO
714 break;
715
716 default:
717
718 Console.WriteLine("Received unknown packet type: " + (int)packetId);
719 break;
720 }
721 }
722 }
723
724
725 int CurrentEventArrayIndex
726 {
727 get { return GetEventArrayIndexForFrame(mGame.CurrentFrameNumber); }
728 }
729
730 int GetEventArrayIndexForFrame(long frame)
731 {
732 return (int)(frame % MaximumLatency);
733 }
734
735 EventInfo ReadEvent(PacketReader packetReader, NetworkGamer sender)
736 {
737 EventType eventId = (EventType)packetReader.ReadByte();
738 long frameNumber = packetReader.ReadInt32();
739
740 switch (eventId)
741 {
742 case EventType.KeyDown:
743
744 Keys keyCode1 = (Keys)packetReader.ReadInt32();
745 return new KeyboardEventInfo(sender, frameNumber, keyCode1, true);
746
747 case EventType.KeyUp:
748
749 Keys keyCode2 = (Keys)packetReader.ReadInt32();
750 return new KeyboardEventInfo(sender, frameNumber, keyCode2, false);
751
752 case EventType.MouseDown:
753
754 MouseButton buttonId1 = (MouseButton)packetReader.ReadByte();
755 return new MouseButtonEventInfo(sender, frameNumber, buttonId1, true);
756
757 case EventType.MouseUp:
758
759 MouseButton buttonId2 = (MouseButton)packetReader.ReadByte();
760 return new MouseButtonEventInfo(sender, frameNumber, buttonId2, false);
761
762 case EventType.MouseMove:
763
764 short x = packetReader.ReadInt16();
765 short y = packetReader.ReadInt16();
766 return new MouseMotionEventInfo(sender, frameNumber, x, y);
767
768 default:
769
770 Console.WriteLine("Received unknown event type: " + (int)eventId);
771 return null;
772 }
773 }
774
775
776 void WriteChatPacket(String message)
777 {
778 mPacketWriter.Write((byte)PacketType.Chat);
779 mPacketWriter.Write((short)message.Length);
780 mPacketWriter.Write(message.ToCharArray());
781 }
782
783 void WriteEventPacket(List<EventInfo> events, long highestFrameNumber)
784 {
785 mPacketWriter.Write((byte)PacketType.Event);
786 mPacketWriter.Write((short)mLastStallCount);
787 mPacketWriter.Write((short)mAverageOwd);
788 mPacketWriter.Write((int)highestFrameNumber);
789 mPacketWriter.Write((byte)events.Count);
790
791 foreach (EventInfo eventInfo in events)
792 {
793 mPacketWriter.Write((byte)eventInfo.Id);
794 mPacketWriter.Write((int)eventInfo.FrameOfApplication);
795
796 KeyboardEventInfo keyboardEventInfo = eventInfo as KeyboardEventInfo;
797 if (keyboardEventInfo != null)
798 {
799 mPacketWriter.Write((int)keyboardEventInfo.Key);
800 continue;
801 }
802
803 MouseButtonEventInfo mouseButtonEventInfo = eventInfo as MouseButtonEventInfo;
804 if (mouseButtonEventInfo != null)
805 {
806 mPacketWriter.Write((byte)mouseButtonEventInfo.Button);
807 continue;
808 }
809
810 MouseMotionEventInfo mouseMotionEventInfo = eventInfo as MouseMotionEventInfo;
811 if (mouseMotionEventInfo != null)
812 {
813 mPacketWriter.Write((short)mouseMotionEventInfo.X);
814 mPacketWriter.Write((short)mouseMotionEventInfo.Y);
815 continue;
816 }
817 }
818 }
819
820
821 bool IsLatencyAdjustmentFrame
822 {
823 get
824 {
825 return mNextLatencyAdjustmentFrame == mGame.CurrentFrameNumber;
826 }
827 }
828
829 void AdjustLatency()
830 {
831 Debug.Assert(IsLatencyAdjustmentFrame);
832
833 #if DEBUG
834 if (mStallCount > 0)
835 {
836 Console.WriteLine("STL#: " + mGame.CurrentFrameNumber + "\t" + mStallCount);
837 }
838 #endif
839
840 int maxStallCount = 0;
841 int maxAverageOwd = 0;
842
843 foreach (GamerInfo gamerInfo in GamerArray)
844 {
845 if (gamerInfo.StallCount > maxStallCount) maxStallCount = gamerInfo.StallCount;
846 if (gamerInfo.AverageOwd > maxAverageOwd) maxAverageOwd = gamerInfo.AverageOwd;
847
848 gamerInfo.StallCount = gamerInfo.NextStallCount;
849 gamerInfo.AverageOwd = gamerInfo.NextAverageOwd;
850 }
851
852 #if DEBUG
853 int prevLatency = mLatency;
854 #endif
855
856 if (maxStallCount > 0)
857 {
858 mLatency += maxStallCount;
859 }
860 else
861 {
862 mLatency -= (int)(0.6 * (double)(mLatency - maxAverageOwd) + 1.0);
863 }
864
865 if (mLatency < 1) mLatency = 1;
866 if (mLatency > MaximumLatency) mLatency = MaximumLatency;
867
868 #if DEBUG
869 if (prevLatency != mLatency) Console.WriteLine("NLAG: " + mLatency);
870 #endif
871
872 mNextLatencyAdjustmentFrame = mGame.CurrentFrameNumber + mLatency;
873 mAverageOwd = CurrentAverageOneWayDelay;
874
875 mLastLocalEvents = mLocalEvents;
876 mLocalEvents = new List<EventInfo>();
877 }
878
879
880
881 List<EventInfo> GetEventsFromInput()
882 {
883 List<EventInfo> events = new List<EventInfo>();
884
885 long frameOfApplication = mGame.CurrentFrameNumber + mLatency;
886 if (frameOfApplication <= mHighestFrameNumber) return events;
887 else mHighestFrameNumber = frameOfApplication;
888
889 // 1. Find the keyboard differences; written by Peter.
890
891 KeyboardState keyState = Keyboard.GetState();
892
893 List<Keys> pressedKeys = new List<Keys>();
894 List<Keys> releasedKeys = new List<Keys>();
895
896 Keys[] pressedKeysArray = keyState.GetPressedKeys();
897 foreach (Keys k in pressedKeysArray)
898 {
899 if (!mLastPressedKeys.Contains(k)) pressedKeys.Add(k);
900 else mLastPressedKeys.Remove(k);
901 }
902
903 releasedKeys = mLastPressedKeys;
904
905 foreach (Keys key in pressedKeys)
906 {
907 events.Add(new KeyboardEventInfo(LocalGamer, frameOfApplication, key, true));
908 }
909 foreach (Keys key in releasedKeys)
910 {
911 events.Add(new KeyboardEventInfo(LocalGamer, frameOfApplication, key, false));
912 }
913
914 #if DEBUG
915 if (pressedKeys.Contains(Keys.Escape)) mDontSendEvents = true;
916 if (releasedKeys.Contains(Keys.Escape)) mDontSendEvents = false;
917 #endif
918
919 // 2. Find the mouse differences.
920
921 MouseState mouseState = Mouse.GetState();
922
923 bool leftButtonPressed = mouseState.LeftButton == ButtonState.Pressed;
924 if (leftButtonPressed != mLastLeftButtonPressed)
925 {
926 events.Add(new MouseButtonEventInfo(LocalGamer, frameOfApplication, MouseButton.Left, leftButtonPressed));
927 }
928
929 bool rightButtonPressed = mouseState.RightButton == ButtonState.Pressed;
930 if (rightButtonPressed != mLastRightButtonPressed)
931 {
932 events.Add(new MouseButtonEventInfo(LocalGamer, frameOfApplication, MouseButton.Right, rightButtonPressed));
933 }
934
935 bool middleButtonPressed = mouseState.MiddleButton == ButtonState.Pressed;
936 if (middleButtonPressed != mLastMiddleButtonPressed)
937 {
938 events.Add(new MouseButtonEventInfo(LocalGamer, frameOfApplication, MouseButton.Middle, middleButtonPressed));
939 }
940
941 int mousePositionX = mouseState.X;
942 int mousePositionY = mouseState.Y;
943 if (mousePositionX != mLastMousePositionX || mousePositionY != mLastMousePositionY)
944 {
945 events.Add(new MouseMotionEventInfo(LocalGamer, frameOfApplication, mousePositionX, mousePositionY));
946 }
947
948 // 3. Save the current peripheral state.
949
950 mLastPressedKeys = new List<Keys>(pressedKeysArray);
951 mLastLeftButtonPressed = leftButtonPressed;
952 mLastRightButtonPressed = rightButtonPressed;
953 mLastMiddleButtonPressed = middleButtonPressed;
954 mLastMousePositionX = mousePositionX;
955 mLastMousePositionY = mousePositionY;
956
957 return events;
958 }
959
960 void SendLocalEvents()
961 {
962 SendLocalEvents((NetworkGamer)null);
963 }
964
965 void SendLocalEvents(List<NetworkGamer> recipicents)
966 {
967 foreach (NetworkGamer gamer in recipicents)
968 {
969 SendLocalEvents(gamer);
970 }
971 }
972
973 void SendLocalEvents(NetworkGamer recipient)
974 {
975 #if DEBUG
976 if (mDontSendEvents) return;
977 #endif
978
979 List<EventInfo> events = new List<EventInfo>(mLocalEvents);
980 events.AddRange(mLastLocalEvents);
981
982 if (recipient != null && !recipient.IsDisposed)
983 {
984 // if there is a recipient, we are resending old events
985 WriteEventPacket(events, mGame.CurrentFrameNumber - 1);
986 LocalGamer.SendData(mPacketWriter, SendDataOptions.Reliable, recipient);
987 }
988 else
989 {
990 WriteEventPacket(events, mGame.CurrentFrameNumber + mLatency);
991 LocalGamer.SendData(mPacketWriter, SendDataOptions.None);
992 }
993 }
994
995
996 bool HaveNeededEvents
997 {
998 get
999 {
1000 long currentFrame = mGame.CurrentFrameNumber;
1001
1002 foreach (GamerInfo gamerInfo in mGamers.Values)
1003 {
1004 if (gamerInfo.HighestFrameNumber < currentFrame) return false;
1005 }
1006
1007 return true;
1008 }
1009 }
1010
1011 void ApplyEvents()
1012 {
1013 int index = CurrentEventArrayIndex;
1014
1015 foreach (GamerInfo gamerInfo in GamerArray)
1016 {
1017 if (gamerInfo.Events[index] == null) continue;
1018
1019 foreach (EventInfo eventInfo in gamerInfo.Events[index])
1020 {
1021 KeyboardEventInfo keyboardEventInfo = eventInfo as KeyboardEventInfo;
1022 if (keyboardEventInfo != null)
1023 {
1024 #if DEBUG
1025 Console.WriteLine(" KEY: " + keyboardEventInfo.FrameOfApplication + "\t" + keyboardEventInfo.Key + "," + keyboardEventInfo.IsKeyDown);
1026 #endif
1027
1028 mGame.ApplyKeyInput(gamerInfo, keyboardEventInfo.Key, keyboardEventInfo.IsKeyDown);
1029 continue;
1030 }
1031
1032 MouseButtonEventInfo mouseButtonEventInfo = eventInfo as MouseButtonEventInfo;
1033 if (mouseButtonEventInfo != null)
1034 {
1035 #if DEBUG
1036 Console.WriteLine(" BTN: " + mouseButtonEventInfo.FrameOfApplication + "\t" + mouseButtonEventInfo.IsButtonDown);
1037 #endif
1038
1039 mGame.ApplyMouseButtonInput(gamerInfo, mouseButtonEventInfo.IsButtonDown);
1040 continue;
1041 }
1042
1043 MouseMotionEventInfo mouseMotionEventInfo = eventInfo as MouseMotionEventInfo;
1044 if (mouseMotionEventInfo != null)
1045 {
1046 #if DEBUG
1047 Console.WriteLine(" MMV: " + mouseMotionEventInfo.FrameOfApplication + "\t" + mouseMotionEventInfo.X + "," + mouseMotionEventInfo.Y);
1048 #endif
1049
1050 mGame.ApplyMouseLocationInput(gamerInfo, mouseMotionEventInfo.X, mouseMotionEventInfo.Y);
1051 continue;
1052 }
1053 }
1054
1055 gamerInfo.Events[index] = null;
1056 }
1057 }
1058
1059
1060 int CurrentAverageOneWayDelay
1061 {
1062 get
1063 {
1064 Debug.Assert(mNetworkSession != null);
1065
1066 double numRemoteGamersTwice = 2 * mNetworkSession.RemoteGamers.Count;
1067 double averageOwd = 0;
1068
1069 foreach (NetworkGamer gamer in mNetworkSession.RemoteGamers)
1070 {
1071 TimeSpan timeSpan = gamer.RoundtripTime;
1072 averageOwd += timeSpan.TotalMilliseconds;
1073 }
1074
1075 return (int)((averageOwd / numRemoteGamersTwice) / 16.6666);
1076 }
1077 }
1078
1079 #endregion
1080 }
1081 }
This page took 0.090758 seconds and 4 git commands to generate.