CreateOrFindSessionScreen.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // CreateOrFindSessionScreen.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 System.Collections.Generic;
  12. using Microsoft.Xna.Framework;
  13. using Microsoft.Xna.Framework.Net;
  14. using Microsoft.Xna.Framework.GamerServices;
  15. #endregion
  16. namespace NetworkStateManagement
  17. {
  18. /// <summary>
  19. /// This menu screen lets the user choose whether to create a new
  20. /// network session, or search for an existing session to join.
  21. /// </summary>
  22. class CreateOrFindSessionScreen : MenuScreen
  23. {
  24. #region Fields
  25. NetworkSessionType sessionType;
  26. #endregion
  27. #region Initialization
  28. /// <summary>
  29. /// Constructor fills in the menu contents.
  30. /// </summary>
  31. public CreateOrFindSessionScreen(NetworkSessionType sessionType)
  32. : base(GetMenuTitle(sessionType))
  33. {
  34. this.sessionType = sessionType;
  35. // Create our menu entries.
  36. MenuEntry createSessionMenuEntry = new MenuEntry(Resources.CreateSession);
  37. MenuEntry findSessionsMenuEntry = new MenuEntry(Resources.FindSessions);
  38. MenuEntry backMenuEntry = new MenuEntry(Resources.Back);
  39. // Hook up menu event handlers.
  40. createSessionMenuEntry.Selected += CreateSessionMenuEntrySelected;
  41. findSessionsMenuEntry.Selected += FindSessionsMenuEntrySelected;
  42. backMenuEntry.Selected += OnCancel;
  43. // Add entries to the menu.
  44. MenuEntries.Add(createSessionMenuEntry);
  45. MenuEntries.Add(findSessionsMenuEntry);
  46. MenuEntries.Add(backMenuEntry);
  47. }
  48. /// <summary>
  49. /// Helper chooses an appropriate menu title for the specified session type.
  50. /// </summary>
  51. static string GetMenuTitle(NetworkSessionType sessionType)
  52. {
  53. switch (sessionType)
  54. {
  55. case NetworkSessionType.PlayerMatch:
  56. return Resources.PlayerMatch;
  57. case NetworkSessionType.SystemLink:
  58. return Resources.SystemLink;
  59. default:
  60. throw new NotSupportedException();
  61. }
  62. }
  63. #endregion
  64. #region Event Handlers
  65. /// <summary>
  66. /// Event handler for when the Create Session menu entry is selected.
  67. /// </summary>
  68. void CreateSessionMenuEntrySelected(object sender, PlayerIndexEventArgs e)
  69. {
  70. try
  71. {
  72. // Which local profiles should we include in this session?
  73. IEnumerable<SignedInGamer> localGamers =
  74. NetworkSessionComponent.ChooseGamers(sessionType,
  75. ControllingPlayer.Value);
  76. // Begin an asynchronous create network session operation.
  77. IAsyncResult asyncResult = NetworkSession.BeginCreate(
  78. sessionType, localGamers,
  79. NetworkSessionComponent.MaxGamers,
  80. 0, null, null, null);
  81. // Activate the network busy screen, which will display
  82. // an animation until this operation has completed.
  83. NetworkBusyScreen busyScreen = new NetworkBusyScreen(asyncResult);
  84. busyScreen.OperationCompleted += CreateSessionOperationCompleted;
  85. ScreenManager.AddScreen(busyScreen, ControllingPlayer);
  86. }
  87. catch (Exception exception)
  88. {
  89. NetworkErrorScreen errorScreen = new NetworkErrorScreen(exception);
  90. ScreenManager.AddScreen(errorScreen, ControllingPlayer);
  91. }
  92. }
  93. /// <summary>
  94. /// Event handler for when the asynchronous create network session
  95. /// operation has completed.
  96. /// </summary>
  97. void CreateSessionOperationCompleted(object sender,
  98. OperationCompletedEventArgs e)
  99. {
  100. try
  101. {
  102. // End the asynchronous create network session operation.
  103. NetworkSession networkSession = NetworkSession.EndCreate(e.AsyncResult);
  104. // Create a component that will manage the session we just created.
  105. NetworkSessionComponent.Create(ScreenManager, networkSession);
  106. // Go to the lobby screen. We pass null as the controlling player,
  107. // because the lobby screen accepts input from all local players
  108. // who are in the session, not just a single controlling player.
  109. ScreenManager.AddScreen(new LobbyScreen(networkSession), null);
  110. }
  111. catch (Exception exception)
  112. {
  113. NetworkErrorScreen errorScreen = new NetworkErrorScreen(exception);
  114. ScreenManager.AddScreen(errorScreen, ControllingPlayer);
  115. }
  116. }
  117. /// <summary>
  118. /// Event handler for when the Find Sessions menu entry is selected.
  119. /// </summary>
  120. void FindSessionsMenuEntrySelected(object sender, PlayerIndexEventArgs e)
  121. {
  122. try
  123. {
  124. // Which local profiles should we include in this session?
  125. IEnumerable<SignedInGamer> localGamers =
  126. NetworkSessionComponent.ChooseGamers(sessionType,
  127. ControllingPlayer.Value);
  128. // Begin an asynchronous find network sessions operation.
  129. IAsyncResult asyncResult = NetworkSession.BeginFind(sessionType,
  130. localGamers, null, null, null);
  131. // Activate the network busy screen, which will display
  132. // an animation until this operation has completed.
  133. NetworkBusyScreen busyScreen = new NetworkBusyScreen(asyncResult);
  134. busyScreen.OperationCompleted += FindSessionsOperationCompleted;
  135. ScreenManager.AddScreen(busyScreen, ControllingPlayer);
  136. }
  137. catch (Exception exception)
  138. {
  139. NetworkErrorScreen errorScreen = new NetworkErrorScreen(exception);
  140. ScreenManager.AddScreen(errorScreen, ControllingPlayer);
  141. }
  142. }
  143. /// <summary>
  144. /// Event handler for when the asynchronous find network sessions
  145. /// operation has completed.
  146. /// </summary>
  147. void FindSessionsOperationCompleted(object sender,
  148. OperationCompletedEventArgs e)
  149. {
  150. GameScreen nextScreen;
  151. try
  152. {
  153. // End the asynchronous find network sessions operation.
  154. AvailableNetworkSessionCollection availableSessions =
  155. NetworkSession.EndFind(e.AsyncResult);
  156. if (availableSessions.Count == 0)
  157. {
  158. // If we didn't find any sessions, display an error.
  159. availableSessions.Dispose();
  160. nextScreen = new MessageBoxScreen(Resources.NoSessionsFound, false);
  161. }
  162. else
  163. {
  164. // If we did find some sessions, proceed to the JoinSessionScreen.
  165. nextScreen = new JoinSessionScreen(availableSessions);
  166. }
  167. }
  168. catch (Exception exception)
  169. {
  170. nextScreen = new NetworkErrorScreen(exception);
  171. }
  172. ScreenManager.AddScreen(nextScreen, ControllingPlayer);
  173. }
  174. #endregion
  175. }
  176. }