PacketServer.cs 5.8 KB

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