LLPacketServer.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.Net;
  28. using System.Net.Sockets;
  29. using libsecondlife;
  30. using libsecondlife.Packets;
  31. using OpenSim.Framework;
  32. using OpenSim.Framework.Communications.Cache;
  33. using OpenSim.Region.ClientStack.LindenUDP;
  34. namespace OpenSim.Region.ClientStack.LindenUDP
  35. {
  36. public class LLPacketServer
  37. {
  38. //private static readonly log4net.ILog m_log
  39. // = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  40. private LLClientStackNetworkHandler m_networkHandler;
  41. private IScene m_scene;
  42. //private readonly ClientManager m_clientManager = new ClientManager();
  43. //public ClientManager ClientManager
  44. //{
  45. // get { return m_clientManager; }
  46. //}
  47. public LLPacketServer(LLClientStackNetworkHandler networkHandler)
  48. {
  49. m_networkHandler = networkHandler;
  50. m_networkHandler.RegisterPacketServer(this);
  51. }
  52. public IScene LocalScene
  53. {
  54. set { m_scene = value; }
  55. }
  56. /// <summary>
  57. ///
  58. /// </summary>
  59. /// <param name="circuitCode"></param>
  60. /// <param name="packet"></param>
  61. public virtual void InPacket(uint circuitCode, Packet packet)
  62. {
  63. m_scene.ClientManager.InPacket(circuitCode, packet);
  64. }
  65. protected virtual IClientAPI CreateNewClient(EndPoint remoteEP, UseCircuitCodePacket initialcirpack,
  66. ClientManager clientManager, IScene scene, AssetCache assetCache,
  67. LLPacketServer packServer, AgentCircuitManager authenSessions,
  68. LLUUID agentId, LLUUID sessionId, uint circuitCode, EndPoint proxyEP)
  69. {
  70. return
  71. new LLClientView(remoteEP, scene, assetCache, packServer, authenSessions, agentId, sessionId, circuitCode, proxyEP);
  72. }
  73. public virtual bool AddNewClient(EndPoint epSender, UseCircuitCodePacket useCircuit, AssetCache assetCache,
  74. AgentCircuitManager authenticateSessionsClass, EndPoint proxyEP)
  75. {
  76. IClientAPI newuser;
  77. if (m_scene.ClientManager.TryGetClient(useCircuit.CircuitCode.Code, out newuser))
  78. {
  79. return false;
  80. }
  81. else
  82. {
  83. newuser = CreateNewClient(epSender, useCircuit, m_scene.ClientManager, m_scene, assetCache, this,
  84. authenticateSessionsClass, useCircuit.CircuitCode.ID,
  85. useCircuit.CircuitCode.SessionID, useCircuit.CircuitCode.Code, proxyEP);
  86. m_scene.ClientManager.Add(useCircuit.CircuitCode.Code, newuser);
  87. newuser.OnViewerEffect += m_scene.ClientManager.ViewerEffectHandler;
  88. newuser.OnLogout += LogoutHandler;
  89. newuser.OnConnectionClosed += CloseClient;
  90. return true;
  91. }
  92. }
  93. public void LogoutHandler(IClientAPI client)
  94. {
  95. client.SendLogoutPacket();
  96. CloseClient(client);
  97. }
  98. /// <summary>
  99. ///
  100. /// </summary>
  101. /// <param name="buffer"></param>
  102. /// <param name="size"></param>
  103. /// <param name="flags"></param>
  104. /// <param name="circuitcode"></param>
  105. public virtual void SendPacketTo(byte[] buffer, int size, SocketFlags flags, uint circuitcode)
  106. {
  107. m_networkHandler.SendPacketTo(buffer, size, flags, circuitcode);
  108. }
  109. /// <summary>
  110. ///
  111. /// </summary>
  112. /// <param name="circuitcode"></param>
  113. public virtual void CloseCircuit(uint circuitcode)
  114. {
  115. m_networkHandler.RemoveClientCircuit(circuitcode);
  116. //m_scene.ClientManager.CloseAllAgents(circuitcode);
  117. }
  118. /// <summary>
  119. /// Completely close down the given client.
  120. /// </summary>
  121. /// <param name="client"></param>
  122. public virtual void CloseClient(IClientAPI client)
  123. {
  124. //m_log.Info("PacketServer:CloseClient()");
  125. CloseCircuit(client.CircuitCode);
  126. m_scene.ClientManager.Remove(client.CircuitCode);
  127. client.Close(false);
  128. }
  129. }
  130. }