ClientManager.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. public uint[] GetAllCircuits(LLUUID agentId)
  128. {
  129. List<uint> circuits = new List<uint>();
  130. // Wasteful, I know
  131. IClientAPI[] LocalClients = new IClientAPI[0];
  132. lock (m_clients)
  133. {
  134. LocalClients = new IClientAPI[m_clients.Count];
  135. m_clients.Values.CopyTo(LocalClients, 0);
  136. }
  137. for (int i = 0; i < LocalClients.Length; i++)
  138. {
  139. if (LocalClients[i].AgentId == agentId)
  140. {
  141. circuits.Add(LocalClients[i].CircuitCode);
  142. }
  143. }
  144. return circuits.ToArray();
  145. }
  146. public List<uint> GetAllCircuitCodes()
  147. {
  148. List<uint> circuits;
  149. lock (m_clients)
  150. {
  151. circuits = new List<uint>(m_clients.Keys);
  152. }
  153. return circuits;
  154. }
  155. public void ViewerEffectHandler(IClientAPI sender, ViewerEffectPacket.EffectBlock[] effectBlock)
  156. {
  157. ViewerEffectPacket packet = (ViewerEffectPacket) PacketPool.Instance.GetPacket(PacketType.ViewerEffect);
  158. // TODO: don't create new blocks if recycling an old packet
  159. packet.Effect = effectBlock;
  160. // Wasteful, I know
  161. IClientAPI[] LocalClients = new IClientAPI[0];
  162. lock (m_clients)
  163. {
  164. LocalClients = new IClientAPI[m_clients.Count];
  165. m_clients.Values.CopyTo(LocalClients, 0);
  166. }
  167. for (int i = 0; i < LocalClients.Length; i++)
  168. {
  169. if (LocalClients[i].AgentId != sender.AgentId)
  170. {
  171. packet.AgentData.AgentID = LocalClients[i].AgentId;
  172. packet.AgentData.SessionID = LocalClients[i].SessionId;
  173. packet.Header.Reliable = false;
  174. LocalClients[i].OutPacket(packet, ThrottleOutPacketType.Task);
  175. }
  176. }
  177. }
  178. public bool TryGetClient(uint circuitId, out IClientAPI user)
  179. {
  180. lock (m_clients)
  181. {
  182. return m_clients.TryGetValue(circuitId, out user);
  183. }
  184. }
  185. }
  186. }