ClientManager.cs 7.8 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 OpenSim 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 log4net;
  31. using OpenMetaverse;
  32. using OpenMetaverse.Packets;
  33. namespace OpenSim.Framework
  34. {
  35. public delegate void ForEachClientDelegate(IClientAPI client);
  36. public class ClientManager
  37. {
  38. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  39. private Dictionary<uint, IClientAPI> m_clients;
  40. public ClientManager()
  41. {
  42. m_clients = new Dictionary<uint, IClientAPI>();
  43. }
  44. public void ForEachClient(ForEachClientDelegate whatToDo)
  45. {
  46. IClientAPI[] LocalClients;
  47. lock (m_clients)
  48. {
  49. LocalClients = new IClientAPI[m_clients.Count];
  50. m_clients.Values.CopyTo(LocalClients, 0);
  51. }
  52. for (int i = 0; i < LocalClients.Length; i++)
  53. {
  54. try
  55. {
  56. whatToDo(LocalClients[i]);
  57. }
  58. catch (Exception e)
  59. {
  60. m_log.Warn("[CLIENT]: Unable to do ForEachClient for one of the clients" + "\n Reason: " + e.ToString());
  61. }
  62. }
  63. }
  64. public void Remove(uint id)
  65. {
  66. //m_log.InfoFormat("[CLIENT]: Removing client with code {0}, current count {1}", id, m_clients.Count);
  67. lock (m_clients)
  68. {
  69. m_clients.Remove(id);
  70. }
  71. m_log.InfoFormat("[CLIENT]: Removed client with code {0}, new client count {1}", id, m_clients.Count);
  72. }
  73. public void Add(uint id, IClientAPI client)
  74. {
  75. lock (m_clients)
  76. {
  77. m_clients.Add(id, client);
  78. }
  79. }
  80. /// <summary>
  81. /// Pass incoming packet to client.
  82. /// </summary>
  83. /// <param name="circuitCode">uint identifying the connection/client.</param>
  84. /// <param name="packet">object containing the packet.</param>
  85. public void InPacket(uint circuitCode, object packet)
  86. {
  87. IClientAPI client;
  88. bool tryGetRet = false;
  89. lock (m_clients)
  90. tryGetRet = m_clients.TryGetValue(circuitCode, out client);
  91. if (tryGetRet)
  92. {
  93. client.InPacket(packet);
  94. }
  95. }
  96. public void CloseAllAgents(uint circuitCode)
  97. {
  98. IClientAPI client;
  99. bool tryGetRet = false;
  100. lock (m_clients)
  101. tryGetRet = m_clients.TryGetValue(circuitCode, out client);
  102. if (tryGetRet)
  103. {
  104. CloseAllCircuits(client.AgentId);
  105. }
  106. }
  107. public void CloseAllCircuits(UUID agentId)
  108. {
  109. uint[] circuits = GetAllCircuits(agentId);
  110. // We're using a for loop here so changes to the circuits don't cause it to completely fail.
  111. for (int i = 0; i < circuits.Length; i++)
  112. {
  113. IClientAPI client;
  114. try
  115. {
  116. bool tryGetRet = false;
  117. lock (m_clients)
  118. tryGetRet = m_clients.TryGetValue(circuits[i], out client);
  119. if (tryGetRet)
  120. {
  121. Remove(client.CircuitCode);
  122. client.Close(false);
  123. }
  124. }
  125. catch (Exception e)
  126. {
  127. m_log.Error(string.Format("[CLIENT]: Unable to shutdown circuit for: {0}\n Reason: {1}", agentId, e));
  128. }
  129. }
  130. }
  131. // [Obsolete("Using Obsolete to drive development is invalid. Obsolete presumes that something new has already been created to replace this.")]
  132. public uint[] GetAllCircuits(UUID agentId)
  133. {
  134. List<uint> circuits = new List<uint>();
  135. // Wasteful, I know
  136. IClientAPI[] LocalClients = new IClientAPI[0];
  137. lock (m_clients)
  138. {
  139. LocalClients = new IClientAPI[m_clients.Count];
  140. m_clients.Values.CopyTo(LocalClients, 0);
  141. }
  142. for (int i = 0; i < LocalClients.Length; i++)
  143. {
  144. if (LocalClients[i].AgentId == agentId)
  145. {
  146. circuits.Add(LocalClients[i].CircuitCode);
  147. }
  148. }
  149. return circuits.ToArray();
  150. }
  151. public List<uint> GetAllCircuitCodes()
  152. {
  153. List<uint> circuits;
  154. lock (m_clients)
  155. {
  156. circuits = new List<uint>(m_clients.Keys);
  157. }
  158. return circuits;
  159. }
  160. public void ViewerEffectHandler(IClientAPI sender, List<ViewerEffectEventHandlerArg> args)
  161. {
  162. // TODO: don't create new blocks if recycling an old packet
  163. List<ViewerEffectPacket.EffectBlock> effectBlock = new List<ViewerEffectPacket.EffectBlock>();
  164. for (int i = 0; i < args.Count; i++)
  165. {
  166. ViewerEffectPacket.EffectBlock effect = new ViewerEffectPacket.EffectBlock();
  167. effect.AgentID = args[i].AgentID;
  168. effect.Color = args[i].Color;
  169. effect.Duration = args[i].Duration;
  170. effect.ID = args[i].ID;
  171. effect.Type = args[i].Type;
  172. effect.TypeData = args[i].TypeData;
  173. effectBlock.Add(effect);
  174. }
  175. ViewerEffectPacket.EffectBlock[] effectBlockArray = effectBlock.ToArray();
  176. IClientAPI[] LocalClients;
  177. lock (m_clients)
  178. {
  179. LocalClients = new IClientAPI[m_clients.Count];
  180. m_clients.Values.CopyTo(LocalClients, 0);
  181. }
  182. for (int i = 0; i < LocalClients.Length; i++)
  183. {
  184. if (LocalClients[i].AgentId != sender.AgentId)
  185. {
  186. LocalClients[i].SendViewerEffect(effectBlockArray);
  187. }
  188. }
  189. }
  190. public bool TryGetClient(uint circuitId, out IClientAPI user)
  191. {
  192. lock (m_clients)
  193. {
  194. return m_clients.TryGetValue(circuitId, out user);
  195. }
  196. }
  197. }
  198. }