INetworkSession.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Xna.Framework;
  4. namespace Microsoft.Xna.Framework.Net
  5. {
  6. /// <summary>
  7. /// Abstraction for a multiplayer network session.
  8. /// Supports different networking backends (UDP, Steam, etc.) through dependency injection.
  9. /// </summary>
  10. public interface INetworkSession : IDisposable
  11. {
  12. /// <summary>
  13. /// Gets all gamers in the session (local and remote).
  14. /// </summary>
  15. IReadOnlyList<INetworkGamer> AllGamers { get; }
  16. /// <summary>
  17. /// Gets the local player.
  18. /// </summary>
  19. ILocalNetworkGamer LocalGamer { get; }
  20. /// <summary>
  21. /// Gets the current session state.
  22. /// </summary>
  23. NetworkSessionState State { get; }
  24. /// <summary>
  25. /// Gets the unique session identifier.
  26. /// </summary>
  27. string SessionId { get; }
  28. /// <summary>
  29. /// Occurs when a network message is received from any remote gamer.
  30. /// </summary>
  31. event EventHandler<MessageReceivedEventArgs> MessageReceived;
  32. /// <summary>
  33. /// Occurs when a remote gamer joins the session.
  34. /// </summary>
  35. event EventHandler<GamerJoinedEventArgs> GamerJoined;
  36. /// <summary>
  37. /// Occurs when a remote gamer leaves the session.
  38. /// </summary>
  39. event EventHandler<GamerLeftEventArgs> GamerLeft;
  40. /// <summary>
  41. /// Occurs when the game is ready to start (all players ready).
  42. /// </summary>
  43. event EventHandler<GameStartedEventArgs> GameStarted;
  44. /// <summary>
  45. /// Occurs when the game has ended.
  46. /// </summary>
  47. event EventHandler<GameEndedEventArgs> GameEnded;
  48. /// <summary>
  49. /// Occurs when the session ends.
  50. /// </summary>
  51. event EventHandler<NetworkSessionEndedEventArgs> SessionEnded;
  52. /// <summary>
  53. /// Creates a new session as the host.
  54. /// </summary>
  55. /// <param name="sessionType">The type of session (SystemLink, etc.)</param>
  56. /// <param name="maxGamers">Maximum number of gamers in the session.</param>
  57. /// <param name="privateGamerSlots">Number of private (invitation-only) slots.</param>
  58. Task CreateAsync(NetworkSessionType sessionType, int maxGamers, int privateGamerSlots);
  59. /// <summary>
  60. /// Joins an existing session.
  61. /// </summary>
  62. /// <param name="hostAddress">Address or identifier of the session to join.</param>
  63. Task JoinAsync(string hostAddress);
  64. /// <summary>
  65. /// Sends a network message to a specific gamer.
  66. /// </summary>
  67. /// <param name="message">The message to send.</param>
  68. /// <param name="recipient">The gamer to send to.</param>
  69. void SendMessage(INetworkMessage message, INetworkGamer recipient);
  70. /// <summary>
  71. /// Broadcasts a message to all remote gamers.
  72. /// </summary>
  73. /// <param name="message">The message to broadcast.</param>
  74. void BroadcastMessage(INetworkMessage message);
  75. /// <summary>
  76. /// Updates the session (processes incoming messages, etc.).
  77. /// Should be called once per game frame.
  78. /// </summary>
  79. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  80. void Update(GameTime gameTime);
  81. /// <summary>
  82. /// Closes the session and disconnects all players.
  83. /// </summary>
  84. Task CloseAsync();
  85. }
  86. }