NetworkSession.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using Microsoft.Xna.Framework.GamerServices;
  9. namespace Microsoft.Xna.Framework.Net
  10. {
  11. /// <summary>
  12. /// Represents a network session for multiplayer gaming.
  13. /// </summary>
  14. public class NetworkSession : IDisposable
  15. {
  16. private readonly List<NetworkGamer> gamers;
  17. private readonly GamerCollection gamerCollection;
  18. private readonly NetworkSessionType sessionType;
  19. private readonly int maxGamers;
  20. private readonly int privateGamerSlots;
  21. private readonly UdpClient udpClient;
  22. private readonly Dictionary<string, IPEndPoint> gamerEndpoints;
  23. private readonly object lockObject = new object();
  24. private NetworkSessionState sessionState;
  25. private bool disposed;
  26. private bool isHost;
  27. private string sessionId;
  28. private Task receiveTask;
  29. private CancellationTokenSource cancellationTokenSource;
  30. // Events
  31. public event EventHandler<GameStartedEventArgs> GameStarted;
  32. public event EventHandler<GameEndedEventArgs> GameEnded;
  33. public event EventHandler<GamerJoinedEventArgs> GamerJoined;
  34. public event EventHandler<GamerLeftEventArgs> GamerLeft;
  35. public event EventHandler<NetworkSessionEndedEventArgs> SessionEnded;
  36. // Static event for invite acceptance
  37. public static event EventHandler<InviteAcceptedEventArgs> InviteAccepted;
  38. /// <summary>
  39. /// Initializes a new NetworkSession.
  40. /// </summary>
  41. private NetworkSession(NetworkSessionType sessionType, int maxGamers, int privateGamerSlots, bool isHost)
  42. {
  43. this.sessionType = sessionType;
  44. this.maxGamers = maxGamers;
  45. this.privateGamerSlots = privateGamerSlots;
  46. this.isHost = isHost;
  47. this.sessionId = Guid.NewGuid().ToString();
  48. this.sessionState = NetworkSessionState.Creating;
  49. gamers = new List<NetworkGamer>();
  50. gamerCollection = new GamerCollection(gamers);
  51. gamerEndpoints = new Dictionary<string, IPEndPoint>();
  52. // Initialize UDP client for networking
  53. udpClient = new UdpClient();
  54. cancellationTokenSource = new CancellationTokenSource();
  55. // Add local gamer
  56. var localGamer = new LocalNetworkGamer(this, Guid.NewGuid().ToString(), isHost, SignedInGamer.Current?.Gamertag ?? "Player");
  57. NetworkGamer.LocalGamer = localGamer;
  58. AddGamer(localGamer);
  59. }
  60. /// <summary>
  61. /// Gets all gamers in the session.
  62. /// </summary>
  63. public GamerCollection AllGamers => gamerCollection;
  64. /// <summary>
  65. /// Gets local gamers in the session.
  66. /// </summary>
  67. public LocalGamerCollection LocalGamers
  68. {
  69. get
  70. {
  71. var localGamers = gamers.Where(g => g.IsLocal).OfType<LocalNetworkGamer>().ToList();
  72. return new LocalGamerCollection(localGamers);
  73. }
  74. }
  75. /// <summary>
  76. /// Gets remote gamers in the session.
  77. /// </summary>
  78. public GamerCollection RemoteGamers
  79. {
  80. get
  81. {
  82. var remoteGamers = gamers.Where(g => !g.IsLocal).ToList();
  83. return new GamerCollection(remoteGamers);
  84. }
  85. }
  86. /// <summary>
  87. /// Gets the host gamer.
  88. /// </summary>
  89. public NetworkGamer Host => AllGamers.Host;
  90. /// <summary>
  91. /// Gets whether this machine is the host.
  92. /// </summary>
  93. public bool IsHost => isHost;
  94. /// <summary>
  95. /// Gets whether everyone is ready to start the game.
  96. /// </summary>
  97. public bool IsEveryoneReady => AllGamers.All(g => g.IsReady);
  98. /// <summary>
  99. /// Gets the maximum number of gamers.
  100. /// </summary>
  101. public int MaxGamers => maxGamers;
  102. /// <summary>
  103. /// Gets the number of private gamer slots.
  104. /// </summary>
  105. public int PrivateGamerSlots => privateGamerSlots;
  106. /// <summary>
  107. /// Gets the session type.
  108. /// </summary>
  109. public NetworkSessionType SessionType => sessionType;
  110. /// <summary>
  111. /// Gets the current session state.
  112. /// </summary>
  113. public NetworkSessionState SessionState => sessionState;
  114. /// <summary>
  115. /// Gets the bytes per second being sent.
  116. /// </summary>
  117. public int BytesPerSecondSent => 0; // Mock implementation
  118. /// <summary>
  119. /// Gets the bytes per second being received.
  120. /// </summary>
  121. public int BytesPerSecondReceived => 0; // Mock implementation
  122. /// <summary>
  123. /// Gets whether the session allows host migration.
  124. /// </summary>
  125. public bool AllowHostMigration { get; set; } = true;
  126. /// <summary>
  127. /// Gets whether the session allows gamers to join during gameplay.
  128. /// </summary>
  129. public bool AllowJoinInProgress { get; set; } = true;
  130. /// <summary>
  131. /// Gets or sets custom session properties.
  132. /// </summary>
  133. public IDictionary<string, object> SessionProperties { get; set; } = new Dictionary<string, object>();
  134. // Simulation properties for testing network conditions
  135. private TimeSpan simulatedLatency = TimeSpan.Zero;
  136. private float simulatedPacketLoss = 0.0f;
  137. /// <summary>
  138. /// Gets or sets the simulated network latency for testing purposes.
  139. /// </summary>
  140. public TimeSpan SimulatedLatency
  141. {
  142. get => simulatedLatency;
  143. set => simulatedLatency = value;
  144. }
  145. /// <summary>
  146. /// Gets or sets the simulated packet loss percentage for testing purposes.
  147. /// </summary>
  148. public float SimulatedPacketLoss
  149. {
  150. get => simulatedPacketLoss;
  151. set => simulatedPacketLoss = Math.Max(0.0f, Math.Min(1.0f, value));
  152. }
  153. /// <summary>
  154. /// Begins creating a new network session.
  155. /// </summary>
  156. public static IAsyncResult BeginCreate(
  157. NetworkSessionType sessionType,
  158. IEnumerable<SignedInGamer> localGamers,
  159. int maxGamers,
  160. int privateGamerSlots,
  161. NetworkSessionProperties sessionProperties,
  162. AsyncCallback callback,
  163. object asyncState
  164. )
  165. {
  166. if (maxGamers < 1 || maxGamers > 4)
  167. {
  168. throw new ArgumentOutOfRangeException(nameof(maxGamers));
  169. }
  170. if (privateGamerSlots < 0 || privateGamerSlots > maxGamers)
  171. {
  172. throw new ArgumentOutOfRangeException(nameof(privateGamerSlots));
  173. }
  174. throw new NotImplementedException("Async creation with callback is not implemented yet.");
  175. }
  176. public static IAsyncResult BeginCreate(
  177. NetworkSessionType sessionType,
  178. int maxLocalGamers,
  179. int maxGamers,
  180. AsyncCallback callback = null,
  181. object asyncState = null
  182. )
  183. {
  184. if (maxLocalGamers < 1 || maxLocalGamers > 4)
  185. {
  186. throw new ArgumentOutOfRangeException(nameof(maxLocalGamers));
  187. }
  188. throw new NotImplementedException("Async creation with callback is not implemented yet.");
  189. }
  190. public static IAsyncResult BeginCreate(
  191. NetworkSessionType sessionType,
  192. int maxLocalGamers,
  193. int maxGamers,
  194. int privateGamerSlots,
  195. NetworkSessionProperties sessionProperties,
  196. AsyncCallback callback = null,
  197. object asyncState = null
  198. )
  199. {
  200. if (maxLocalGamers < 1 || maxLocalGamers > 4)
  201. {
  202. throw new ArgumentOutOfRangeException(nameof(maxLocalGamers));
  203. }
  204. if (maxLocalGamers < 1 || maxLocalGamers > 4)
  205. {
  206. throw new ArgumentOutOfRangeException(nameof(maxLocalGamers));
  207. }
  208. if (privateGamerSlots < 0 || privateGamerSlots > maxGamers)
  209. {
  210. throw new ArgumentOutOfRangeException(nameof(privateGamerSlots));
  211. }
  212. var task = Task.Run(() =>
  213. {
  214. var session = new NetworkSession(sessionType, maxGamers, privateGamerSlots, true);
  215. session.sessionState = NetworkSessionState.Lobby;
  216. return session;
  217. });
  218. return new AsyncResultWrapper<NetworkSession>(task, null, null);
  219. }
  220. /// <summary>
  221. /// Ends the create session operation.
  222. /// </summary>
  223. public static NetworkSession EndCreate(IAsyncResult asyncResult)
  224. {
  225. if (asyncResult is AsyncResultWrapper<NetworkSession> wrapper)
  226. {
  227. return wrapper.GetResult();
  228. }
  229. throw new ArgumentException("Invalid async result", nameof(asyncResult));
  230. }
  231. /// <summary>
  232. /// Begins finding available network sessions.
  233. /// </summary>
  234. public static IAsyncResult BeginFind(
  235. NetworkSessionType sessionType,
  236. int maxLocalGamers,
  237. NetworkSessionProperties searchProperties,
  238. AsyncCallback callback,
  239. object asyncState)
  240. {
  241. var task = Task.Run(() =>
  242. {
  243. // Mock implementation - return empty collection for now
  244. // In a real implementation, this would search for available sessions
  245. return new AvailableNetworkSessionCollection();
  246. });
  247. return new AsyncResultWrapper<AvailableNetworkSessionCollection>(task, callback, asyncState);
  248. }
  249. public static IAsyncResult BeginFind(
  250. NetworkSessionType sessionType,
  251. IEnumerable<SignedInGamer> localGamers,
  252. NetworkSessionProperties searchProperties,
  253. AsyncCallback callback = null,
  254. object asyncState = null
  255. )
  256. {
  257. var task = Task.Run(() =>
  258. {
  259. // Mock implementation - return empty collection for now
  260. // In a real implementation, this would search for available sessions
  261. return new AvailableNetworkSessionCollection();
  262. });
  263. return new AsyncResultWrapper<AvailableNetworkSessionCollection>(task, callback, asyncState);
  264. }
  265. /// <summary>
  266. /// Ends the find sessions operation.
  267. /// </summary>
  268. public static AvailableNetworkSessionCollection EndFind(IAsyncResult asyncResult)
  269. {
  270. if (asyncResult is AsyncResultWrapper<AvailableNetworkSessionCollection> wrapper)
  271. {
  272. return wrapper.GetResult();
  273. }
  274. throw new ArgumentException("Invalid async result", nameof(asyncResult));
  275. }
  276. /// <summary>
  277. /// Begins joining a network session.
  278. /// </summary>
  279. public static IAsyncResult BeginJoin(
  280. AvailableNetworkSession availableSession,
  281. AsyncCallback callback,
  282. object asyncState)
  283. {
  284. var task = Task.Run(() =>
  285. {
  286. // Mock implementation - create a new session as a client
  287. var session = new NetworkSession(availableSession.SessionType, availableSession.CurrentGamerCount, 0, false);
  288. session.sessionState = NetworkSessionState.Lobby;
  289. return session;
  290. });
  291. return new AsyncResultWrapper<NetworkSession>(task, callback, asyncState);
  292. }
  293. /// <summary>
  294. /// Ends the join session operation.
  295. /// </summary>
  296. public static NetworkSession EndJoin(IAsyncResult asyncResult)
  297. {
  298. if (asyncResult is AsyncResultWrapper<NetworkSession> wrapper)
  299. {
  300. return wrapper.GetResult();
  301. }
  302. throw new ArgumentException("Invalid async result", nameof(asyncResult));
  303. }
  304. /// <summary>
  305. /// Begins joining an invited session.
  306. /// </summary>
  307. public static IAsyncResult BeginJoinInvited(int maxLocalGamers, AsyncCallback callback, object asyncState)
  308. {
  309. var task = Task.Run(() =>
  310. {
  311. // Mock implementation
  312. var session = new NetworkSession(NetworkSessionType.PlayerMatch, 8, 0, false);
  313. session.sessionState = NetworkSessionState.Lobby;
  314. // Fire invite accepted event - pass a mock gamer
  315. InviteAccepted?.Invoke(null, new InviteAcceptedEventArgs(GamerServices.SignedInGamer.Current, false));
  316. return session;
  317. });
  318. return new AsyncResultWrapper<NetworkSession>(task, callback, asyncState);
  319. }
  320. public static IAsyncResult BeginJoinInvited(
  321. IEnumerable<SignedInGamer> localGamers,
  322. AsyncCallback callback,
  323. object asyncState
  324. )
  325. {
  326. throw new NotImplementedException();
  327. }
  328. /// <summary>
  329. /// Ends the join invited session operation.
  330. /// </summary>
  331. public static NetworkSession EndJoinInvited(IAsyncResult asyncResult)
  332. {
  333. return EndJoin(asyncResult);
  334. }
  335. /// <summary>
  336. /// Creates a new network session synchronously.
  337. /// </summary>
  338. /// <param name="sessionType">The type of session to create.</param>
  339. /// <param name="maxLocalGamers">Maximum number of local gamers.</param>
  340. /// <param name="maxGamers">Maximum total number of gamers.</param>
  341. /// <param name="privateGamerSlots">Number of private gamer slots.</param>
  342. /// <param name="sessionProperties">Session properties.</param>
  343. /// <returns>The created network session.</returns>
  344. public static NetworkSession Create(NetworkSessionType sessionType, int maxLocalGamers, int maxGamers, int privateGamerSlots, IDictionary<string, object> sessionProperties)
  345. {
  346. var props = new NetworkSessionProperties();
  347. if (sessionProperties != null)
  348. {
  349. foreach (var kvp in sessionProperties)
  350. {
  351. props[kvp.Key] = kvp.Value;
  352. }
  353. }
  354. var asyncResult = BeginCreate(sessionType, maxLocalGamers, maxGamers, privateGamerSlots, props);
  355. return EndCreate(asyncResult);
  356. }
  357. /// <summary>
  358. /// Creates a new network session synchronously with default properties.
  359. /// </summary>
  360. /// <param name="sessionType">The type of session to create.</param>
  361. /// <param name="maxLocalGamers">Maximum number of local gamers.</param>
  362. /// <param name="maxGamers">Maximum total number of gamers.</param>
  363. /// <returns>The created network session.</returns>
  364. public static NetworkSession Create(NetworkSessionType sessionType, int maxLocalGamers, int maxGamers)
  365. {
  366. return Create(sessionType, maxLocalGamers, maxGamers, 0, new Dictionary<string, object>());
  367. }
  368. /// <summary>
  369. /// Finds available network sessions synchronously.
  370. /// </summary>
  371. /// <param name="sessionType">The type of sessions to find.</param>
  372. /// <param name="maxLocalGamers">Maximum number of local gamers.</param>
  373. /// <param name="sessionProperties">Session properties to search for.</param>
  374. /// <returns>Collection of available sessions.</returns>
  375. public static AvailableNetworkSessionCollection Find(NetworkSessionType sessionType, int maxLocalGamers, IDictionary<string, object> sessionProperties)
  376. {
  377. var props = new NetworkSessionProperties();
  378. if (sessionProperties != null)
  379. {
  380. foreach (var kvp in sessionProperties)
  381. {
  382. props[kvp.Key] = kvp.Value;
  383. }
  384. }
  385. var asyncResult = BeginFind(sessionType, maxLocalGamers, props, null, null);
  386. return EndFind(asyncResult);
  387. }
  388. /// <summary>
  389. /// Finds available network sessions synchronously with default properties.
  390. /// </summary>
  391. /// <param name="sessionType">The type of sessions to find.</param>
  392. /// <param name="maxLocalGamers">Maximum number of local gamers.</param>
  393. /// <returns>Collection of available sessions.</returns>
  394. public static AvailableNetworkSessionCollection Find(NetworkSessionType sessionType, int maxLocalGamers)
  395. {
  396. return Find(sessionType, maxLocalGamers, new Dictionary<string, object>());
  397. }
  398. /// <summary>
  399. /// Joins an available network session synchronously.
  400. /// </summary>
  401. /// <param name="availableSession">The session to join.</param>
  402. /// <returns>The joined network session.</returns>
  403. public static NetworkSession Join(AvailableNetworkSession availableSession)
  404. {
  405. var asyncResult = BeginJoin(availableSession, null, null);
  406. return EndJoin(asyncResult);
  407. }
  408. /// <summary>
  409. /// Updates the network session.
  410. /// </summary>
  411. public void Update()
  412. {
  413. if (disposed)
  414. return;
  415. // Process any pending network messages
  416. ProcessIncomingMessages();
  417. }
  418. /// <summary>
  419. /// Sends data to all gamers in the session.
  420. /// </summary>
  421. public void SendToAll(PacketWriter writer, SendDataOptions options)
  422. {
  423. SendToAll(writer, options, NetworkGamer.LocalGamer);
  424. }
  425. /// <summary>
  426. /// Sends data to all gamers except the specified sender.
  427. /// </summary>
  428. public void SendToAll(PacketWriter writer, SendDataOptions options, NetworkGamer sender)
  429. {
  430. if (writer == null)
  431. throw new ArgumentNullException(nameof(writer));
  432. byte[] data = writer.GetData();
  433. lock (lockObject)
  434. {
  435. foreach (var gamer in gamers)
  436. {
  437. if (gamer != sender && !gamer.IsLocal)
  438. {
  439. SendDataToGamer(gamer, data, options);
  440. }
  441. }
  442. }
  443. }
  444. /// <summary>
  445. /// Starts the game.
  446. /// </summary>
  447. public void StartGame()
  448. {
  449. if (sessionState == NetworkSessionState.Lobby)
  450. {
  451. sessionState = NetworkSessionState.Playing;
  452. OnGameStarted();
  453. }
  454. }
  455. /// <summary>
  456. /// Ends the game and returns to lobby.
  457. /// </summary>
  458. public void EndGame()
  459. {
  460. if (sessionState == NetworkSessionState.Playing)
  461. {
  462. sessionState = NetworkSessionState.Lobby;
  463. OnGameEnded();
  464. }
  465. }
  466. /// <summary>
  467. /// Notifies when a gamer's readiness changes.
  468. /// </summary>
  469. internal void NotifyReadinessChanged(NetworkGamer gamer)
  470. {
  471. // Send readiness update to other players
  472. if (IsHost)
  473. {
  474. var writer = new PacketWriter();
  475. writer.Write("ReadinessUpdate");
  476. writer.Write(gamer.Id);
  477. writer.Write(gamer.IsReady);
  478. SendToAll(writer, SendDataOptions.Reliable, gamer);
  479. }
  480. }
  481. /// <summary>
  482. /// Sends data to a specific gamer.
  483. /// </summary>
  484. internal void SendDataToGamer(NetworkGamer gamer, PacketWriter writer, SendDataOptions options)
  485. {
  486. SendDataToGamer(gamer, writer.GetData(), options);
  487. }
  488. private void SendDataToGamer(NetworkGamer gamer, byte[] data, SendDataOptions options)
  489. {
  490. if (gamerEndpoints.TryGetValue(gamer.Id, out IPEndPoint endpoint))
  491. {
  492. try
  493. {
  494. udpClient.Send(data, data.Length, endpoint);
  495. }
  496. catch (Exception ex)
  497. {
  498. // Log the error but don't crash the game
  499. System.Diagnostics.Debug.WriteLine($"Failed to send data to {gamer.Gamertag}: {ex.Message}");
  500. }
  501. }
  502. }
  503. private void AddGamer(NetworkGamer gamer)
  504. {
  505. lock (lockObject)
  506. {
  507. gamers.Add(gamer);
  508. }
  509. OnGamerJoined(gamer);
  510. }
  511. private void RemoveGamer(NetworkGamer gamer)
  512. {
  513. lock (lockObject)
  514. {
  515. gamers.Remove(gamer);
  516. gamerEndpoints.Remove(gamer.Id);
  517. }
  518. OnGamerLeft(gamer);
  519. }
  520. private void ProcessIncomingMessages()
  521. {
  522. // Mock implementation - in a real scenario this would process UDP messages
  523. }
  524. private void OnGameStarted()
  525. {
  526. GameStarted?.Invoke(this, new GameStartedEventArgs());
  527. }
  528. private void OnGameEnded()
  529. {
  530. GameEnded?.Invoke(this, new GameEndedEventArgs());
  531. }
  532. private void OnGamerJoined(NetworkGamer gamer)
  533. {
  534. GamerJoined?.Invoke(this, new GamerJoinedEventArgs(gamer));
  535. }
  536. private void OnGamerLeft(NetworkGamer gamer)
  537. {
  538. GamerLeft?.Invoke(this, new GamerLeftEventArgs(gamer));
  539. }
  540. private void OnSessionEnded(NetworkSessionEndReason reason)
  541. {
  542. sessionState = NetworkSessionState.Ended;
  543. SessionEnded?.Invoke(this, new NetworkSessionEndedEventArgs(reason));
  544. }
  545. /// <summary>
  546. /// Disposes the network session.
  547. /// </summary>
  548. public void Dispose()
  549. {
  550. if (!disposed)
  551. {
  552. cancellationTokenSource?.Cancel();
  553. receiveTask?.Wait(1000); // Wait up to 1 second
  554. udpClient?.Close();
  555. udpClient?.Dispose();
  556. cancellationTokenSource?.Dispose();
  557. OnSessionEnded(NetworkSessionEndReason.ClientSignedOut);
  558. disposed = true;
  559. }
  560. }
  561. }
  562. }