ClientManager.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.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 Dictionary<uint, IClientAPI> m_clients;
  37. public void ForEachClient(ForEachClientDelegate whatToDo)
  38. {
  39. // Wasteful, I know
  40. IClientAPI[] LocalClients = new IClientAPI[0];
  41. lock (m_clients)
  42. {
  43. LocalClients = new IClientAPI[m_clients.Count];
  44. m_clients.Values.CopyTo(LocalClients, 0);
  45. }
  46. for (int i = 0; i < LocalClients.Length; i++)
  47. {
  48. try
  49. {
  50. whatToDo(LocalClients[i]);
  51. }
  52. catch (System.Exception e)
  53. {
  54. OpenSim.Framework.Console.MainLog.Instance.Warn("CLIENT", "Unable to do ForEachClient for one of the clients" + "\n Reason: " + e.ToString());
  55. }
  56. }
  57. }
  58. public ClientManager()
  59. {
  60. m_clients = new Dictionary<uint, IClientAPI>();
  61. }
  62. private void Remove(uint id)
  63. {
  64. m_clients.Remove(id);
  65. }
  66. public void Add(uint id, IClientAPI client)
  67. {
  68. m_clients.Add(id, client);
  69. }
  70. public void InPacket(uint circuitCode, Packet packet)
  71. {
  72. IClientAPI client;
  73. if (m_clients.TryGetValue(circuitCode, out client))
  74. {
  75. client.InPacket(packet);
  76. }
  77. }
  78. public void CloseAllAgents(uint circuitCode)
  79. {
  80. IClientAPI client;
  81. if (m_clients.TryGetValue(circuitCode, out client))
  82. {
  83. CloseAllCircuits(client.AgentId);
  84. }
  85. }
  86. public void CloseAllCircuits(LLUUID agentId)
  87. {
  88. uint[] circuits = GetAllCircuits(agentId);
  89. // We're using a for loop here so changes to the circuits don't cause it to completely fail.
  90. for (int i = 0; i < circuits.Length; i++)
  91. {
  92. IClientAPI client;
  93. try
  94. {
  95. if (m_clients.TryGetValue(circuits[i], out client))
  96. {
  97. Remove(client.CircuitCode);
  98. client.Close(false);
  99. }
  100. }
  101. catch (System.Exception e)
  102. {
  103. OpenSim.Framework.Console.MainLog.Instance.Error("CLIENT", string.Format("Unable to shutdown circuit for: {0}\n Reason: {1}", agentId, e));
  104. }
  105. }
  106. }
  107. private uint[] GetAllCircuits(LLUUID agentId)
  108. {
  109. List<uint> circuits = new List<uint>();
  110. // Wasteful, I know
  111. IClientAPI[] LocalClients = new IClientAPI[0];
  112. lock (m_clients)
  113. {
  114. LocalClients = new IClientAPI[m_clients.Count];
  115. m_clients.Values.CopyTo(LocalClients, 0);
  116. }
  117. for (int i = 0; i < LocalClients.Length; i++ )
  118. {
  119. if (LocalClients[i].AgentId == agentId)
  120. {
  121. circuits.Add(LocalClients[i].CircuitCode);
  122. }
  123. }
  124. return circuits.ToArray();
  125. }
  126. public void ViewerEffectHandler(IClientAPI sender, ViewerEffectPacket.EffectBlock[] effectBlock)
  127. {
  128. ViewerEffectPacket packet = (ViewerEffectPacket) PacketPool.Instance.GetPacket(PacketType.ViewerEffect);
  129. // TODO: don't create new blocks if recycling an old packet
  130. packet.Effect = effectBlock;
  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 != sender.AgentId)
  141. {
  142. packet.AgentData.AgentID = LocalClients[i].AgentId;
  143. packet.AgentData.SessionID = LocalClients[i].SessionId;
  144. LocalClients[i].OutPacket(packet, ThrottleOutPacketType.Task);
  145. }
  146. }
  147. }
  148. public bool TryGetClient(uint circuitId, out IClientAPI user)
  149. {
  150. return m_clients.TryGetValue(circuitId, out user);
  151. }
  152. }
  153. }