AvailableSessionMenuEntry.cs 2.7 KB

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