ClientManager.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Net;
  30. using OpenMetaverse;
  31. namespace OpenSim.Framework
  32. {
  33. /// <summary>
  34. /// Maps from client AgentID and RemoteEndPoint values to IClientAPI
  35. /// references for all of the connected clients
  36. /// </summary>
  37. public class ClientManager
  38. {
  39. /// <summary>A dictionary mapping from <seealso cref="UUID"/>
  40. /// to <seealso cref="IClientAPI"/> references</summary>
  41. private readonly Dictionary<UUID, IClientAPI> m_dictbyUUID = new();
  42. /// <summary>A dictionary mapping from <seealso cref="IPEndPoint"/>
  43. /// to <seealso cref="IClientAPI"/> references</summary>
  44. private readonly Dictionary<IPEndPoint, IClientAPI> m_dictbyIPe= new();
  45. /// <summary>snapshot collection of current <seealso cref="IClientAPI"/>
  46. /// references</summary>
  47. private IClientAPI[] m_array = null;
  48. /// <summary>Synchronization object for writing to the collections</summary>
  49. private readonly object m_syncRoot = new();
  50. /// <summary>Number of clients in the collection</summary>
  51. public int Count
  52. {
  53. get
  54. {
  55. lock (m_syncRoot)
  56. return m_dictbyUUID.Count;
  57. }
  58. }
  59. /// <summary>
  60. /// Default constructor
  61. /// </summary>
  62. public ClientManager()
  63. {
  64. }
  65. /// <summary>
  66. /// Add a client reference to the collection if it does not already
  67. /// exist
  68. /// </summary>
  69. /// <param name="value">Reference to the client object</param>
  70. /// <returns>True if the client reference was successfully added,
  71. /// otherwise false if the given key already existed in the collection</returns>
  72. public bool Add(IClientAPI value)
  73. {
  74. lock (m_syncRoot)
  75. {
  76. m_dictbyUUID[value.AgentId] = value;
  77. m_dictbyIPe[value.RemoteEndPoint] = value;
  78. m_array = null;
  79. }
  80. return true;
  81. }
  82. /// <summary>
  83. /// Remove a client from the collection
  84. /// </summary>
  85. /// <param name="key">UUID of the client to remove</param>
  86. /// <returns>True if a client was removed, or false if the given UUID
  87. /// was not present in the collection</returns>
  88. public bool Remove(UUID key)
  89. {
  90. lock (m_syncRoot)
  91. {
  92. if (m_dictbyUUID.Remove(key, out IClientAPI value))
  93. {
  94. m_dictbyIPe.Remove(value.RemoteEndPoint);
  95. m_array = null;
  96. return true;
  97. }
  98. }
  99. return false;
  100. }
  101. /// <summary>
  102. /// Resets the client collection
  103. /// </summary>
  104. public void Clear()
  105. {
  106. lock (m_syncRoot)
  107. {
  108. m_dictbyUUID.Clear();
  109. m_dictbyIPe.Clear();
  110. m_array = null;
  111. }
  112. }
  113. /// <summary>
  114. /// Checks if a UUID is in the collection
  115. /// </summary>
  116. /// <param name="key">UUID to check for</param>
  117. /// <returns>True if the UUID was found in the collection, otherwise false</returns>
  118. public bool ContainsKey(UUID key)
  119. {
  120. lock (m_syncRoot)
  121. return m_dictbyUUID.ContainsKey(key);
  122. }
  123. /// <summary>
  124. /// Checks if an endpoint is in the collection
  125. /// </summary>
  126. /// <param name="key">Endpoint to check for</param>
  127. /// <returns>True if the endpoint was found in the collection, otherwise false</returns>
  128. public bool ContainsKey(IPEndPoint key)
  129. {
  130. lock (m_syncRoot)
  131. return m_dictbyIPe.ContainsKey(key);
  132. }
  133. /// <summary>
  134. /// Attempts to fetch a value out of the collection
  135. /// </summary>
  136. /// <param name="key">UUID of the client to retrieve</param>
  137. /// <param name="value">Retrieved client, or null on lookup failure</param>
  138. /// <returns>True if the lookup succeeded, otherwise false</returns>
  139. public bool TryGetValue(UUID key, out IClientAPI value)
  140. {
  141. try
  142. {
  143. lock (m_syncRoot)
  144. return m_dictbyUUID.TryGetValue(key, out value);
  145. }
  146. catch
  147. {
  148. value = null;
  149. return false;
  150. }
  151. }
  152. /// <summary>
  153. /// Attempts to fetch a value out of the collection
  154. /// </summary>
  155. /// <param name="key">Endpoint of the client to retrieve</param>
  156. /// <param name="value">Retrieved client, or null on lookup failure</param>
  157. /// <returns>True if the lookup succeeded, otherwise false</returns>
  158. public bool TryGetValue(IPEndPoint key, out IClientAPI value)
  159. {
  160. try
  161. {
  162. lock (m_syncRoot)
  163. return m_dictbyIPe.TryGetValue(key, out value);
  164. }
  165. catch
  166. {
  167. value = null;
  168. return false;
  169. }
  170. }
  171. /// <summary>
  172. /// Performs a given task synchronously for each of the elements in
  173. /// the collection
  174. /// </summary>
  175. /// <param name="action">Action to perform on each element</param>
  176. public void ForEach(Action<IClientAPI> action)
  177. {
  178. IClientAPI[] localArray;
  179. lock (m_syncRoot)
  180. {
  181. if (m_array is null)
  182. {
  183. if (m_dictbyUUID.Count == 0)
  184. return;
  185. m_array = new IClientAPI[m_dictbyUUID.Count];
  186. m_dictbyUUID.Values.CopyTo(m_array, 0);
  187. }
  188. localArray = m_array;
  189. }
  190. for (int i = 0; i < localArray.Length; i++)
  191. action(localArray[i]);
  192. }
  193. }
  194. }