JoinSessionScreen.cs 4.9 KB

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