AvailableSessionMenuEntry.cs 3.1 KB

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