ClientManager.cs 7.2 KB

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