AvailableNetworkSession.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Threading.Tasks;
  5. namespace Microsoft.Xna.Framework.Net
  6. {
  7. /// <summary>
  8. /// Represents an available network session that can be joined.
  9. /// </summary>
  10. public class AvailableNetworkSession
  11. {
  12. internal AvailableNetworkSession(
  13. string sessionName,
  14. string hostGamertag,
  15. int currentGamerCount,
  16. int openPublicGamerSlots,
  17. int openPrivateGamerSlots,
  18. NetworkSessionType sessionType,
  19. IDictionary<string, object> sessionProperties)
  20. {
  21. SessionName = sessionName;
  22. HostGamertag = hostGamertag;
  23. CurrentGamerCount = currentGamerCount;
  24. OpenPublicGamerSlots = openPublicGamerSlots;
  25. OpenPrivateGamerSlots = openPrivateGamerSlots;
  26. SessionType = sessionType;
  27. SessionProperties = new Dictionary<string, object>(sessionProperties);
  28. }
  29. /// <summary>
  30. /// Gets the name of the session.
  31. /// </summary>
  32. public string SessionName { get; }
  33. /// <summary>
  34. /// Gets the gamertag of the host.
  35. /// </summary>
  36. public string HostGamertag { get; }
  37. /// <summary>
  38. /// Gets the current number of gamers in the session.
  39. /// </summary>
  40. public int CurrentGamerCount { get; }
  41. /// <summary>
  42. /// Gets the number of open public gamer slots.
  43. /// </summary>
  44. public int OpenPublicGamerSlots { get; }
  45. /// <summary>
  46. /// Gets the number of open private gamer slots.
  47. /// </summary>
  48. public int OpenPrivateGamerSlots { get; }
  49. /// <summary>
  50. /// Gets the type of the session.
  51. /// </summary>
  52. public NetworkSessionType SessionType { get; }
  53. /// <summary>
  54. /// Gets the session properties.
  55. /// </summary>
  56. public IDictionary<string, object> SessionProperties { get; }
  57. /// <summary>
  58. /// Gets the quality of service information.
  59. /// </summary>
  60. public QualityOfService QualityOfService { get; internal set; } = new QualityOfService();
  61. }
  62. /// <summary>
  63. /// Collection of available network sessions.
  64. /// </summary>
  65. public class AvailableNetworkSessionCollection : ReadOnlyCollection<AvailableNetworkSession>, IDisposable
  66. {
  67. private bool disposed = false;
  68. internal AvailableNetworkSessionCollection() : base(new List<AvailableNetworkSession>()) { }
  69. internal AvailableNetworkSessionCollection(IList<AvailableNetworkSession> sessions) : base(sessions) { }
  70. /// <summary>
  71. /// Disposes of the collection resources.
  72. /// </summary>
  73. public void Dispose()
  74. {
  75. Dispose(true);
  76. GC.SuppressFinalize(this);
  77. }
  78. /// <summary>
  79. /// Disposes of the collection resources.
  80. /// </summary>
  81. /// <param name="disposing">True if disposing managed resources.</param>
  82. protected virtual void Dispose(bool disposing)
  83. {
  84. if (!disposed)
  85. {
  86. if (disposing)
  87. {
  88. // Clean up managed resources if needed
  89. // The ReadOnlyCollection doesn't need special cleanup
  90. }
  91. disposed = true;
  92. }
  93. }
  94. }
  95. /// <summary>
  96. /// Properties used when creating or searching for network sessions.
  97. /// </summary>
  98. public class NetworkSessionProperties : Dictionary<string, object>
  99. {
  100. /// <summary>
  101. /// Initializes a new NetworkSessionProperties.
  102. /// </summary>
  103. public NetworkSessionProperties() : base() { }
  104. /// <summary>
  105. /// Initializes a new NetworkSessionProperties with a specified capacity.
  106. /// </summary>
  107. public NetworkSessionProperties(int capacity) : base(capacity) { }
  108. }
  109. /// <summary>
  110. /// Quality of service information for a network session.
  111. /// </summary>
  112. public class QualityOfService
  113. {
  114. /// <summary>
  115. /// Gets the average round trip time in milliseconds.
  116. /// </summary>
  117. public TimeSpan AverageRoundTripTime { get; internal set; } = TimeSpan.FromMilliseconds(50);
  118. /// <summary>
  119. /// Gets the minimum round trip time in milliseconds.
  120. /// </summary>
  121. public TimeSpan MinimumRoundTripTime { get; internal set; } = TimeSpan.FromMilliseconds(20);
  122. /// <summary>
  123. /// Gets the maximum round trip time in milliseconds.
  124. /// </summary>
  125. public TimeSpan MaximumRoundTripTime { get; internal set; } = TimeSpan.FromMilliseconds(100);
  126. /// <summary>
  127. /// Gets the bytes per second being sent.
  128. /// </summary>
  129. public int BytesPerSecondSent { get; internal set; } = 0;
  130. /// <summary>
  131. /// Gets the bytes per second being received.
  132. /// </summary>
  133. public int BytesPerSecondReceived { get; internal set; } = 0;
  134. /// <summary>
  135. /// Gets whether the connection is available.
  136. /// </summary>
  137. public bool IsAvailable { get; internal set; } = true;
  138. }
  139. /// <summary>
  140. /// Wrapper for async operations to provide XNA-compatible IAsyncResult interface.
  141. /// </summary>
  142. internal class AsyncResultWrapper<T> : IAsyncResult
  143. {
  144. private readonly Task<T> task;
  145. private readonly object asyncState;
  146. public AsyncResultWrapper(Task<T> task, AsyncCallback callback, object asyncState)
  147. {
  148. this.task = task;
  149. this.asyncState = asyncState;
  150. if (callback != null)
  151. {
  152. task.ContinueWith(t => callback(this));
  153. }
  154. }
  155. public object AsyncState => asyncState;
  156. public System.Threading.WaitHandle AsyncWaitHandle => ((IAsyncResult)task).AsyncWaitHandle;
  157. public bool CompletedSynchronously => task.IsCompletedSuccessfully;
  158. public bool IsCompleted => task.IsCompleted;
  159. public T GetResult()
  160. {
  161. return task.Result;
  162. }
  163. }
  164. }