AvailableSessionMenuEntry.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // AvailableSessionMenuEntry.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. #endregion
  14. namespace NetworkStateManagement
  15. {
  16. /// <summary>
  17. /// Helper class customizes the standard MenuEntry class
  18. /// for displaying AvailableNetworkSession objects.
  19. /// </summary>
  20. class AvailableSessionMenuEntry : MenuEntry
  21. {
  22. #region Fields
  23. AvailableNetworkSession availableSession;
  24. bool gotQualityOfService;
  25. #endregion
  26. #region Properties
  27. /// <summary>
  28. /// Gets the available network session corresponding to this menu entry.
  29. /// </summary>
  30. public AvailableNetworkSession AvailableSession
  31. {
  32. get { return availableSession; }
  33. }
  34. #endregion
  35. #region Initialization
  36. /// <summary>
  37. /// Constructs a menu entry describing an available network session.
  38. /// </summary>
  39. public AvailableSessionMenuEntry(AvailableNetworkSession availableSession)
  40. : base(GetMenuItemText(availableSession))
  41. {
  42. this.availableSession = availableSession;
  43. }
  44. /// <summary>
  45. /// Formats session information to create the menu text string.
  46. /// </summary>
  47. static string GetMenuItemText(AvailableNetworkSession session)
  48. {
  49. int totalSlots = session.CurrentGamerCount +
  50. session.OpenPublicGamerSlots;
  51. return string.Format("{0} ({1}/{2})", session.HostGamertag,
  52. session.CurrentGamerCount,
  53. totalSlots);
  54. }
  55. #endregion
  56. #region Update
  57. /// <summary>
  58. /// Updates the menu item text, adding information about the network
  59. /// quality of service as soon as that becomes available.
  60. /// </summary>
  61. public override void Update(MenuScreen screen, bool isSelected,
  62. GameTime gameTime)
  63. {
  64. base.Update(screen, isSelected, gameTime);
  65. // Quality of service data can take some time to query, so it will not
  66. // be filled in straight away when NetworkSession.Find returns. We want
  67. // to display the list of available sessions straight away, and then
  68. // fill in the quality of service data whenever that becomes available,
  69. // so we keep checking until this data shows up.
  70. if (screen.IsActive && !gotQualityOfService)
  71. {
  72. QualityOfService qualityOfService = availableSession.QualityOfService;
  73. if (qualityOfService.IsAvailable)
  74. {
  75. TimeSpan pingTime = qualityOfService.AverageRoundtripTime;
  76. Text += string.Format(" - {0:0} ms", pingTime.TotalMilliseconds);
  77. gotQualityOfService = true;
  78. }
  79. }
  80. }
  81. #endregion
  82. }
  83. }