NetworkSessionComponent.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //-----------------------------------------------------------------------------
  2. // NetworkSessionComponent.cs
  3. // Adapted from NetworkStateManagement sample for Blackjack
  4. //-----------------------------------------------------------------------------
  5. using System;
  6. using GameStateManagement;
  7. using Microsoft.Xna.Framework;
  8. using Microsoft.Xna.Framework.Net;
  9. namespace Blackjack
  10. {
  11. /// <summary>
  12. /// Component in charge of owning and updating the current NetworkSession object.
  13. /// Responsible for calling NetworkSession.Update and exposing the session as a service.
  14. /// </summary>
  15. class NetworkSessionComponent : GameComponent
  16. {
  17. ScreenManager screenManager;
  18. NetworkSession networkSession;
  19. bool notifyWhenPlayersJoinOrLeave;
  20. string sessionEndMessage;
  21. public NetworkSessionComponent(ScreenManager screenManager, NetworkSession networkSession)
  22. : base(screenManager.Game)
  23. {
  24. this.screenManager = screenManager;
  25. this.networkSession = networkSession;
  26. networkSession.GamerJoined += GamerJoined;
  27. networkSession.GamerLeft += GamerLeft;
  28. networkSession.SessionEnded += NetworkSessionEnded;
  29. }
  30. public static void Create(ScreenManager screenManager, NetworkSession networkSession)
  31. {
  32. Game game = screenManager.Game;
  33. // Remove any existing NetworkSession service and component before adding new ones
  34. if (game.Services.GetService(typeof(NetworkSession)) != null)
  35. {
  36. game.Services.RemoveService(typeof(NetworkSession));
  37. }
  38. // Remove any existing NetworkSessionComponent
  39. var existingComponent = FindSessionComponent(game);
  40. if (existingComponent != null)
  41. {
  42. game.Components.Remove(existingComponent);
  43. existingComponent.Dispose();
  44. }
  45. game.Services.AddService(typeof(NetworkSession), networkSession);
  46. game.Components.Add(new NetworkSessionComponent(screenManager, networkSession));
  47. }
  48. /// <summary>
  49. /// Searches through the Game.Components collection to find the NetworkSessionComponent (if any exists).
  50. /// </summary>
  51. static NetworkSessionComponent FindSessionComponent(Game game)
  52. {
  53. foreach (var component in game.Components)
  54. {
  55. if (component is NetworkSessionComponent sessionComponent)
  56. return sessionComponent;
  57. }
  58. return null;
  59. }
  60. public override void Initialize()
  61. {
  62. base.Initialize();
  63. // Optionally hook up message display here if needed
  64. }
  65. protected override void Dispose(bool disposing)
  66. {
  67. if (disposing)
  68. {
  69. Game.Components.Remove(this);
  70. Game.Services.RemoveService(typeof(NetworkSession));
  71. if (networkSession != null)
  72. {
  73. networkSession.Dispose();
  74. networkSession = null;
  75. }
  76. }
  77. base.Dispose(disposing);
  78. }
  79. public override void Update(GameTime gameTime)
  80. {
  81. if (networkSession == null)
  82. return;
  83. try
  84. {
  85. networkSession.Update();
  86. if (networkSession.SessionState == NetworkSessionState.Ended)
  87. {
  88. LeaveSession();
  89. }
  90. }
  91. catch (Exception)
  92. {
  93. sessionEndMessage = "Network error.";
  94. LeaveSession();
  95. }
  96. }
  97. void GamerJoined(object sender, GamerJoinedEventArgs e)
  98. {
  99. // Optionally display message or update UI
  100. }
  101. void GamerLeft(object sender, GamerLeftEventArgs e)
  102. {
  103. // Optionally display message or update UI
  104. }
  105. void NetworkSessionEnded(object sender, NetworkSessionEndedEventArgs e)
  106. {
  107. sessionEndMessage = "Session ended.";
  108. LeaveSession();
  109. }
  110. void LeaveSession()
  111. {
  112. Dispose(true);
  113. // Optionally transition to main menu or show message
  114. }
  115. }
  116. }