ClientManager.cs 6.9 KB

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