ClientManager.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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.Reflection;
  30. using System.Net;
  31. using OpenMetaverse;
  32. using OpenMetaverse.Packets;
  33. namespace OpenSim.Framework
  34. {
  35. /// <summary>
  36. /// Maps from client AgentID and RemoteEndPoint values to IClientAPI
  37. /// references for all of the connected clients
  38. /// </summary>
  39. public class ClientManager
  40. {
  41. /// <summary>A dictionary mapping from <seealso cref="UUID"/>
  42. /// to <seealso cref="IClientAPI"/> references</summary>
  43. private Dictionary<UUID, IClientAPI> m_dict1;
  44. /// <summary>A dictionary mapping from <seealso cref="IPEndPoint"/>
  45. /// to <seealso cref="IClientAPI"/> references</summary>
  46. private Dictionary<IPEndPoint, IClientAPI> m_dict2;
  47. /// <summary>An immutable collection of <seealso cref="IClientAPI"/>
  48. /// references</summary>
  49. private IClientAPI[] m_array;
  50. /// <summary>Synchronization object for writing to the collections</summary>
  51. private object m_syncRoot = new object();
  52. /// <summary>Number of clients in the collection</summary>
  53. public int Count { get { return m_dict1.Count; } }
  54. /// <summary>
  55. /// Default constructor
  56. /// </summary>
  57. public ClientManager()
  58. {
  59. m_dict1 = new Dictionary<UUID, IClientAPI>();
  60. m_dict2 = new Dictionary<IPEndPoint, IClientAPI>();
  61. m_array = new IClientAPI[0];
  62. }
  63. /// <summary>
  64. /// Add a client reference to the collection if it does not already
  65. /// exist
  66. /// </summary>
  67. /// <param name="value">Reference to the client object</param>
  68. /// <returns>True if the client reference was successfully added,
  69. /// otherwise false if the given key already existed in the collection</returns>
  70. public bool Add(IClientAPI value)
  71. {
  72. lock (m_syncRoot)
  73. {
  74. if (m_dict1.ContainsKey(value.AgentId) || m_dict2.ContainsKey(value.RemoteEndPoint))
  75. return false;
  76. m_dict1[value.AgentId] = value;
  77. m_dict2[value.RemoteEndPoint] = value;
  78. IClientAPI[] oldArray = m_array;
  79. int oldLength = oldArray.Length;
  80. IClientAPI[] newArray = new IClientAPI[oldLength + 1];
  81. for (int i = 0; i < oldLength; i++)
  82. newArray[i] = oldArray[i];
  83. newArray[oldLength] = value;
  84. m_array = newArray;
  85. }
  86. return true;
  87. }
  88. /// <summary>
  89. /// Remove a client from the collection
  90. /// </summary>
  91. /// <param name="key">UUID of the client to remove</param>
  92. /// <returns>True if a client was removed, or false if the given UUID
  93. /// was not present in the collection</returns>
  94. public bool Remove(UUID key)
  95. {
  96. lock (m_syncRoot)
  97. {
  98. IClientAPI value;
  99. if (m_dict1.TryGetValue(key, out value))
  100. {
  101. m_dict1.Remove(key);
  102. m_dict2.Remove(value.RemoteEndPoint);
  103. IClientAPI[] oldArray = m_array;
  104. int oldLength = oldArray.Length;
  105. IClientAPI[] newArray = new IClientAPI[oldLength - 1];
  106. int j = 0;
  107. for (int i = 0; i < oldLength; i++)
  108. {
  109. if (oldArray[i] != value)
  110. newArray[j++] = oldArray[i];
  111. }
  112. m_array = newArray;
  113. return true;
  114. }
  115. }
  116. return false;
  117. }
  118. /// <summary>
  119. /// Resets the client collection
  120. /// </summary>
  121. public void Clear()
  122. {
  123. lock (m_syncRoot)
  124. {
  125. m_dict1.Clear();
  126. m_dict2.Clear();
  127. m_array = new IClientAPI[0];
  128. }
  129. }
  130. /// <summary>
  131. /// Checks if a UUID is in the collection
  132. /// </summary>
  133. /// <param name="key">UUID to check for</param>
  134. /// <returns>True if the UUID was found in the collection, otherwise false</returns>
  135. public bool ContainsKey(UUID key)
  136. {
  137. return m_dict1.ContainsKey(key);
  138. }
  139. /// <summary>
  140. /// Checks if an endpoint is in the collection
  141. /// </summary>
  142. /// <param name="key">Endpoint to check for</param>
  143. /// <returns>True if the endpoint was found in the collection, otherwise false</returns>
  144. public bool ContainsKey(IPEndPoint key)
  145. {
  146. return m_dict2.ContainsKey(key);
  147. }
  148. /// <summary>
  149. /// Attempts to fetch a value out of the collection
  150. /// </summary>
  151. /// <param name="key">UUID of the client to retrieve</param>
  152. /// <param name="value">Retrieved client, or null on lookup failure</param>
  153. /// <returns>True if the lookup succeeded, otherwise false</returns>
  154. public bool TryGetValue(UUID key, out IClientAPI value)
  155. {
  156. try { return m_dict1.TryGetValue(key, out value); }
  157. catch (Exception)
  158. {
  159. value = null;
  160. return false;
  161. }
  162. }
  163. /// <summary>
  164. /// Attempts to fetch a value out of the collection
  165. /// </summary>
  166. /// <param name="key">Endpoint of the client to retrieve</param>
  167. /// <param name="value">Retrieved client, or null on lookup failure</param>
  168. /// <returns>True if the lookup succeeded, otherwise false</returns>
  169. public bool TryGetValue(IPEndPoint key, out IClientAPI value)
  170. {
  171. try { return m_dict2.TryGetValue(key, out value); }
  172. catch (Exception)
  173. {
  174. value = null;
  175. return false;
  176. }
  177. }
  178. /// <summary>
  179. /// Performs a given task in parallel for each of the elements in the
  180. /// collection
  181. /// </summary>
  182. /// <param name="action">Action to perform on each element</param>
  183. public void ForEach(Action<IClientAPI> action)
  184. {
  185. IClientAPI[] localArray = m_array;
  186. Parallel.ForEach<IClientAPI>(localArray, action);
  187. }
  188. /// <summary>
  189. /// Performs a given task synchronously for each of the elements in
  190. /// the collection
  191. /// </summary>
  192. /// <param name="action">Action to perform on each element</param>
  193. public void ForEachSync(Action<IClientAPI> action)
  194. {
  195. IClientAPI[] localArray = m_array;
  196. for (int i = 0; i < localArray.Length; i++)
  197. action(localArray[i]);
  198. }
  199. }
  200. }