ClientManager.cs 7.6 KB

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