JoinSessionScreen.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. {
  37. // Create menu entries for each available session.
  38. MenuEntry menuEntry = new AvailableSessionMenuEntry(availableSession);
  39. menuEntry.Selected += AvailableSessionMenuEntrySelected;
  40. MenuEntries.Add(menuEntry);
  41. // Matchmaking can return up to 25 available sessions at a time, but
  42. // we don't have room to fit that many on the screen. In a perfect
  43. // world we should make the menu scroll if there are too many, but it
  44. // is easier to just not bother displaying more than we have room for.
  45. if (MenuEntries.Count >= MaxSearchResults)
  46. break;
  47. }
  48. // Add the Back menu entry.
  49. MenuEntry backMenuEntry = new MenuEntry(Resources.Back);
  50. backMenuEntry.Selected += BackMenuEntrySelected;
  51. MenuEntries.Add(backMenuEntry);
  52. }
  53. #endregion
  54. #region Event Handlers
  55. /// <summary>
  56. /// Event handler for when an available session menu entry is selected.
  57. /// </summary>
  58. void AvailableSessionMenuEntrySelected(object sender, PlayerIndexEventArgs e)
  59. {
  60. // Which menu entry was selected?
  61. AvailableSessionMenuEntry menuEntry = (AvailableSessionMenuEntry)sender;
  62. AvailableNetworkSession availableSession = menuEntry.AvailableSession;
  63. try
  64. {
  65. // Begin an asynchronous join network session operation.
  66. IAsyncResult asyncResult = NetworkSession.BeginJoin(availableSession,
  67. null, null);
  68. // Activate the network busy screen, which will display
  69. // an animation until this operation has completed.
  70. NetworkBusyScreen busyScreen = new NetworkBusyScreen(asyncResult);
  71. busyScreen.OperationCompleted += JoinSessionOperationCompleted;
  72. ScreenManager.AddScreen(busyScreen, ControllingPlayer);
  73. }
  74. catch (Exception exception)
  75. {
  76. NetworkErrorScreen errorScreen = new NetworkErrorScreen(exception);
  77. ScreenManager.AddScreen(errorScreen, ControllingPlayer);
  78. }
  79. }
  80. /// <summary>
  81. /// Event handler for when the asynchronous join network session
  82. /// operation has completed.
  83. /// </summary>
  84. void JoinSessionOperationCompleted(object sender, OperationCompletedEventArgs e)
  85. {
  86. try
  87. {
  88. // End the asynchronous join network session operation.
  89. NetworkSession networkSession = NetworkSession.EndJoin(e.AsyncResult);
  90. // Create a component that will manage the session we just joined.
  91. NetworkSessionComponent.Create(ScreenManager, networkSession);
  92. // Go to the lobby screen. We pass null as the controlling player,
  93. // because the lobby screen accepts input from all local players
  94. // who are in the session, not just a single controlling player.
  95. ScreenManager.AddScreen(new LobbyScreen(networkSession), null);
  96. availableSessions.Dispose();
  97. }
  98. catch (Exception exception)
  99. {
  100. NetworkErrorScreen errorScreen = new NetworkErrorScreen(exception);
  101. ScreenManager.AddScreen(errorScreen, ControllingPlayer);
  102. }
  103. }
  104. /// <summary>
  105. /// Event handler for when the Back menu entry is selected.
  106. /// </summary>
  107. void BackMenuEntrySelected(object sender, PlayerIndexEventArgs e)
  108. {
  109. availableSessions.Dispose();
  110. ExitScreen();
  111. }
  112. #endregion
  113. }
  114. }