AvailableSessionMenuEntry.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 CatapultGame
  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 TimeSpan pingTime = qualityOfService.AverageRoundtripTime;
  65. Text += string.Format(" - {0:0} ms", pingTime.TotalMilliseconds); */
  66. gotQualityOfService = true;
  67. }
  68. }
  69. }
  70. }
  71. }