1
0

ClientManager.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 Dictionary<UUID, IClientAPI>();
  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 Dictionary<IPEndPoint, IClientAPI>();
  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 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_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. IClientAPI value;
  93. if (m_dictbyUUID.TryGetValue(key, out value))
  94. {
  95. m_dictbyUUID.Remove(key);
  96. m_dictbyIPe.Remove(value.RemoteEndPoint);
  97. m_array = null;
  98. return true;
  99. }
  100. }
  101. return false;
  102. }
  103. /// <summary>
  104. /// Resets the client collection
  105. /// </summary>
  106. public void Clear()
  107. {
  108. lock (m_syncRoot)
  109. {
  110. m_dictbyUUID.Clear();
  111. m_dictbyIPe.Clear();
  112. m_array = null;
  113. }
  114. }
  115. /// <summary>
  116. /// Checks if a UUID is in the collection
  117. /// </summary>
  118. /// <param name="key">UUID to check for</param>
  119. /// <returns>True if the UUID was found in the collection, otherwise false</returns>
  120. public bool ContainsKey(UUID key)
  121. {
  122. lock (m_syncRoot)
  123. return m_dictbyUUID.ContainsKey(key);
  124. }
  125. /// <summary>
  126. /// Checks if an endpoint is in the collection
  127. /// </summary>
  128. /// <param name="key">Endpoint to check for</param>
  129. /// <returns>True if the endpoint was found in the collection, otherwise false</returns>
  130. public bool ContainsKey(IPEndPoint key)
  131. {
  132. lock (m_syncRoot)
  133. return m_dictbyIPe.ContainsKey(key);
  134. }
  135. /// <summary>
  136. /// Attempts to fetch a value out of the collection
  137. /// </summary>
  138. /// <param name="key">UUID of the client to retrieve</param>
  139. /// <param name="value">Retrieved client, or null on lookup failure</param>
  140. /// <returns>True if the lookup succeeded, otherwise false</returns>
  141. public bool TryGetValue(UUID key, out IClientAPI value)
  142. {
  143. try
  144. {
  145. lock (m_syncRoot)
  146. return m_dictbyUUID.TryGetValue(key, out value);
  147. }
  148. catch
  149. {
  150. value = null;
  151. return false;
  152. }
  153. }
  154. /// <summary>
  155. /// Attempts to fetch a value out of the collection
  156. /// </summary>
  157. /// <param name="key">Endpoint of the client to retrieve</param>
  158. /// <param name="value">Retrieved client, or null on lookup failure</param>
  159. /// <returns>True if the lookup succeeded, otherwise false</returns>
  160. public bool TryGetValue(IPEndPoint key, out IClientAPI value)
  161. {
  162. try
  163. {
  164. lock (m_syncRoot)
  165. return m_dictbyIPe.TryGetValue(key, out value);
  166. }
  167. catch
  168. {
  169. value = null;
  170. return false;
  171. }
  172. }
  173. /// <summary>
  174. /// Performs a given task synchronously for each of the elements in
  175. /// the collection
  176. /// </summary>
  177. /// <param name="action">Action to perform on each element</param>
  178. public void ForEach(Action<IClientAPI> action)
  179. {
  180. IClientAPI[] localArray;
  181. lock (m_syncRoot)
  182. {
  183. if (m_array == null)
  184. {
  185. if (m_dictbyUUID.Count == 0)
  186. return;
  187. m_array = new IClientAPI[m_dictbyUUID.Count];
  188. m_dictbyUUID.Values.CopyTo(m_array, 0);
  189. }
  190. localArray = m_array;
  191. }
  192. for (int i = 0; i < localArray.Length; i++)
  193. action(localArray[i]);
  194. }
  195. }
  196. }