PacketServer.cs 5.8 KB

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