1
0

ClientManager.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 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. lock (m_clients)
  67. {
  68. m_clients.Remove(id);
  69. }
  70. }
  71. public void Add(uint id, IClientAPI client)
  72. {
  73. lock (m_clients)
  74. {
  75. m_clients.Add(id, client);
  76. }
  77. }
  78. /// <summary>
  79. /// Pass incoming packet to client.
  80. /// </summary>
  81. /// <param name="circuitCode">uint identifying the connection/client.</param>
  82. /// <param name="packet">object containing the packet.</param>
  83. public void InPacket(uint circuitCode, object packet)
  84. {
  85. IClientAPI client;
  86. bool tryGetRet = false;
  87. lock (m_clients)
  88. tryGetRet = m_clients.TryGetValue(circuitCode, out client);
  89. if (tryGetRet)
  90. {
  91. client.InPacket(packet);
  92. }
  93. }
  94. public void CloseAllAgents(uint circuitCode)
  95. {
  96. IClientAPI client;
  97. bool tryGetRet = false;
  98. lock (m_clients)
  99. tryGetRet = m_clients.TryGetValue(circuitCode, out client);
  100. if (tryGetRet)
  101. {
  102. CloseAllCircuits(client.AgentId);
  103. }
  104. }
  105. public void CloseAllCircuits(UUID agentId)
  106. {
  107. uint[] circuits = GetAllCircuits(agentId);
  108. // We're using a for loop here so changes to the circuits don't cause it to completely fail.
  109. for (int i = 0; i < circuits.Length; i++)
  110. {
  111. IClientAPI client;
  112. try
  113. {
  114. bool tryGetRet = false;
  115. lock (m_clients)
  116. tryGetRet = m_clients.TryGetValue(circuits[i], out client);
  117. if (tryGetRet)
  118. {
  119. Remove(client.CircuitCode);
  120. client.Close(false);
  121. }
  122. }
  123. catch (Exception e)
  124. {
  125. m_log.Error(string.Format("[CLIENT]: Unable to shutdown circuit for: {0}\n Reason: {1}", agentId, e));
  126. }
  127. }
  128. }
  129. // [Obsolete("Using Obsolete to drive development is invalid. Obsolete presumes that something new has already been created to replace this.")]
  130. public uint[] GetAllCircuits(UUID agentId)
  131. {
  132. List<uint> circuits = new List<uint>();
  133. // Wasteful, I know
  134. IClientAPI[] LocalClients = new IClientAPI[0];
  135. lock (m_clients)
  136. {
  137. LocalClients = new IClientAPI[m_clients.Count];
  138. m_clients.Values.CopyTo(LocalClients, 0);
  139. }
  140. for (int i = 0; i < LocalClients.Length; i++)
  141. {
  142. if (LocalClients[i].AgentId == agentId)
  143. {
  144. circuits.Add(LocalClients[i].CircuitCode);
  145. }
  146. }
  147. return circuits.ToArray();
  148. }
  149. public List<uint> GetAllCircuitCodes()
  150. {
  151. List<uint> circuits;
  152. lock (m_clients)
  153. {
  154. circuits = new List<uint>(m_clients.Keys);
  155. }
  156. return circuits;
  157. }
  158. public void ViewerEffectHandler(IClientAPI sender, List<ViewerEffectEventHandlerArg> args)
  159. {
  160. // TODO: don't create new blocks if recycling an old packet
  161. List<ViewerEffectPacket.EffectBlock> effectBlock = new List<ViewerEffectPacket.EffectBlock>();
  162. for (int i = 0; i < args.Count; i++)
  163. {
  164. ViewerEffectPacket.EffectBlock effect = new ViewerEffectPacket.EffectBlock();
  165. effect.AgentID = args[i].AgentID;
  166. effect.Color = args[i].Color;
  167. effect.Duration = args[i].Duration;
  168. effect.ID = args[i].ID;
  169. effect.Type = args[i].Type;
  170. effect.TypeData = args[i].TypeData;
  171. effectBlock.Add(effect);
  172. }
  173. ViewerEffectPacket.EffectBlock[] effectBlockArray = effectBlock.ToArray();
  174. IClientAPI[] LocalClients;
  175. lock (m_clients)
  176. {
  177. LocalClients = new IClientAPI[m_clients.Count];
  178. m_clients.Values.CopyTo(LocalClients, 0);
  179. }
  180. for (int i = 0; i < LocalClients.Length; i++)
  181. {
  182. if (LocalClients[i].AgentId != sender.AgentId)
  183. {
  184. LocalClients[i].SendViewerEffect(effectBlockArray);
  185. }
  186. }
  187. }
  188. public bool TryGetClient(uint circuitId, out IClientAPI user)
  189. {
  190. lock (m_clients)
  191. {
  192. return m_clients.TryGetValue(circuitId, out user);
  193. }
  194. }
  195. }
  196. }