1
0

ClientManager.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 { get { return m_dict1.Count; } }
  52. /// <summary>
  53. /// Default constructor
  54. /// </summary>
  55. public ClientManager()
  56. {
  57. m_dict1 = new Dictionary<UUID, IClientAPI>();
  58. m_dict2 = new Dictionary<IPEndPoint, IClientAPI>();
  59. m_array = new IClientAPI[0];
  60. }
  61. /// <summary>
  62. /// Add a client reference to the collection if it does not already
  63. /// exist
  64. /// </summary>
  65. /// <param name="value">Reference to the client object</param>
  66. /// <returns>True if the client reference was successfully added,
  67. /// otherwise false if the given key already existed in the collection</returns>
  68. public bool Add(IClientAPI value)
  69. {
  70. lock (m_syncRoot)
  71. {
  72. // allow self healing
  73. // if (m_dict1.ContainsKey(value.AgentId) || m_dict2.ContainsKey(value.RemoteEndPoint))
  74. // return false;
  75. m_dict1[value.AgentId] = value;
  76. m_dict2[value.RemoteEndPoint] = value;
  77. // dict1 is the master
  78. IClientAPI[] newArray = new IClientAPI[m_dict1.Count];
  79. m_dict1.Values.CopyTo(newArray, 0);
  80. m_array = newArray;
  81. }
  82. return true;
  83. }
  84. /// <summary>
  85. /// Remove a client from the collection
  86. /// </summary>
  87. /// <param name="key">UUID of the client to remove</param>
  88. /// <returns>True if a client was removed, or false if the given UUID
  89. /// was not present in the collection</returns>
  90. public bool Remove(UUID key)
  91. {
  92. lock (m_syncRoot)
  93. {
  94. IClientAPI value;
  95. if (m_dict1.TryGetValue(key, out value))
  96. {
  97. m_dict1.Remove(key);
  98. m_dict2.Remove(value.RemoteEndPoint);
  99. IClientAPI[] newArray = new IClientAPI[m_dict1.Count];
  100. m_dict1.Values.CopyTo(newArray, 0);
  101. m_array = newArray;
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107. /// <summary>
  108. /// Resets the client collection
  109. /// </summary>
  110. public void Clear()
  111. {
  112. lock (m_syncRoot)
  113. {
  114. m_dict1.Clear();
  115. m_dict2.Clear();
  116. m_array = new IClientAPI[0];
  117. }
  118. }
  119. /// <summary>
  120. /// Checks if a UUID is in the collection
  121. /// </summary>
  122. /// <param name="key">UUID to check for</param>
  123. /// <returns>True if the UUID was found in the collection, otherwise false</returns>
  124. public bool ContainsKey(UUID key)
  125. {
  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. return m_dict2.ContainsKey(key);
  136. }
  137. /// <summary>
  138. /// Attempts to fetch a value out of the collection
  139. /// </summary>
  140. /// <param name="key">UUID of the client to retrieve</param>
  141. /// <param name="value">Retrieved client, or null on lookup failure</param>
  142. /// <returns>True if the lookup succeeded, otherwise false</returns>
  143. public bool TryGetValue(UUID key, out IClientAPI value)
  144. {
  145. try { return m_dict1.TryGetValue(key, out value); }
  146. catch (Exception)
  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 { return m_dict2.TryGetValue(key, out value); }
  161. catch (Exception)
  162. {
  163. value = null;
  164. return false;
  165. }
  166. }
  167. /// <summary>
  168. /// Performs a given task synchronously for each of the elements in
  169. /// the collection
  170. /// </summary>
  171. /// <param name="action">Action to perform on each element</param>
  172. public void ForEach(Action<IClientAPI> action)
  173. {
  174. IClientAPI[] localArray = m_array;
  175. for (int i = 0; i < localArray.Length; i++)
  176. action(localArray[i]);
  177. }
  178. }
  179. }