JoinSessionScreen.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // JoinSessionScreen.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.Net;
  13. using Microsoft.Xna.Framework.GamerServices;
  14. #endregion
  15. namespace NetworkStateManagement
  16. {
  17. /// <summary>
  18. /// This menu screen displays a list of available network sessions,
  19. /// and lets the user choose which one to join.
  20. /// </summary>
  21. class JoinSessionScreen : MenuScreen
  22. {
  23. #region Fields
  24. const int MaxSearchResults = 8;
  25. AvailableNetworkSessionCollection availableSessions;
  26. #endregion
  27. #region Initialization
  28. /// <summary>
  29. /// Constructs a menu screen listing the available network sessions.
  30. /// </summary>
  31. public JoinSessionScreen (AvailableNetworkSessionCollection availableSessions)
  32. : base(Resources.JoinSession)
  33. {
  34. this.availableSessions = availableSessions;
  35. foreach (AvailableNetworkSession availableSession in availableSessions) {
  36. // Create menu entries for each available session.
  37. MenuEntry menuEntry = new AvailableSessionMenuEntry (availableSession);
  38. menuEntry.Selected += AvailableSessionMenuEntrySelected;
  39. MenuEntries.Add (menuEntry);
  40. // Matchmaking can return up to 25 available sessions at a time, but
  41. // we don't have room to fit that many on the screen. In a perfect
  42. // world we should make the menu scroll if there are too many, but it
  43. // is easier to just not bother displaying more than we have room for.
  44. if (MenuEntries.Count >= MaxSearchResults)
  45. break;
  46. }
  47. // Add the Back menu entry.
  48. MenuEntry backMenuEntry = new MenuEntry (Resources.Back);
  49. backMenuEntry.Selected += BackMenuEntrySelected;
  50. MenuEntries.Add (backMenuEntry);
  51. }
  52. #endregion
  53. #region Event Handlers
  54. /// <summary>
  55. /// Event handler for when an available session menu entry is selected.
  56. /// </summary>
  57. void AvailableSessionMenuEntrySelected (object sender, PlayerIndexEventArgs e)
  58. {
  59. // Which menu entry was selected?
  60. AvailableSessionMenuEntry menuEntry = (AvailableSessionMenuEntry)sender;
  61. AvailableNetworkSession availableSession = menuEntry.AvailableSession;
  62. try {
  63. // Begin an asynchronous join network session operation.
  64. IAsyncResult asyncResult = NetworkSession.BeginJoin (availableSession,
  65. null, null);
  66. // Activate the network busy screen, which will display
  67. // an animation until this operation has completed.
  68. NetworkBusyScreen busyScreen = new NetworkBusyScreen (asyncResult);
  69. busyScreen.OperationCompleted += JoinSessionOperationCompleted;
  70. ScreenManager.AddScreen (busyScreen, ControllingPlayer);
  71. } catch (Exception exception) {
  72. NetworkErrorScreen errorScreen = new NetworkErrorScreen (exception);
  73. ScreenManager.AddScreen (errorScreen, ControllingPlayer);
  74. }
  75. }
  76. /// <summary>
  77. /// Event handler for when the asynchronous join network session
  78. /// operation has completed.
  79. /// </summary>
  80. void JoinSessionOperationCompleted (object sender, OperationCompletedEventArgs e)
  81. {
  82. try {
  83. // End the asynchronous join network session operation.
  84. NetworkSession networkSession = NetworkSession.EndJoin (e.AsyncResult);
  85. // Create a component that will manage the session we just joined.
  86. NetworkSessionComponent.Create (ScreenManager, networkSession);
  87. // Go to the lobby screen. We pass null as the controlling player,
  88. // because the lobby screen accepts input from all local players
  89. // who are in the session, not just a single controlling player.
  90. ScreenManager.AddScreen (new LobbyScreen (networkSession), null);
  91. availableSessions.Dispose ();
  92. } catch (Exception exception) {
  93. NetworkErrorScreen errorScreen = new NetworkErrorScreen (exception);
  94. ScreenManager.AddScreen (errorScreen, ControllingPlayer);
  95. }
  96. }
  97. /// <summary>
  98. /// Event handler for when the Back menu entry is selected.
  99. /// </summary>
  100. void BackMenuEntrySelected (object sender, PlayerIndexEventArgs e)
  101. {
  102. availableSessions.Dispose ();
  103. ExitScreen ();
  104. }
  105. #endregion
  106. }
  107. }