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