LocalSessionRegistry.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace Microsoft.Xna.Framework.Net
  4. {
  5. internal static class LocalSessionRegistry
  6. {
  7. private static readonly List<NetworkSession> sessions = new List<NetworkSession>();
  8. public static void RegisterSession(NetworkSession session)
  9. {
  10. lock (sessions)
  11. {
  12. sessions.Add(session);
  13. }
  14. }
  15. public static IEnumerable<AvailableNetworkSession> FindSessions(int maxLocalGamers)
  16. {
  17. lock (sessions)
  18. {
  19. return sessions
  20. .Where(s => s.MaxGamers >= maxLocalGamers && s.SessionState == NetworkSessionState.Lobby)
  21. .Select(s => new AvailableNetworkSession(
  22. sessionName: "LocalSession",
  23. hostGamertag: s.Host?.Gamertag ?? "Host",
  24. currentGamerCount: s.AllGamers.Count,
  25. openPublicGamerSlots: s.MaxGamers - s.AllGamers.Count,
  26. openPrivateGamerSlots: s.PrivateGamerSlots,
  27. sessionType: s.SessionType,
  28. sessionProperties: s.SessionProperties,
  29. sessionId: s.sessionId))
  30. .ToList();
  31. }
  32. }
  33. public static NetworkSession GetSessionById(string sessionId)
  34. {
  35. lock (sessions)
  36. {
  37. return sessions.FirstOrDefault(s => s.sessionId == sessionId);
  38. }
  39. }
  40. }
  41. }