2
0

AvailableSessionMenuEntry.cs 2.8 KB

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