PacketServer.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (c) Contributors, http://www.openmetaverse.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 System.Net;
  30. using System.Net.Sockets;
  31. using libsecondlife.Packets;
  32. using OpenSim.Assets;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Interfaces;
  35. using OpenSim.Region.Caches;
  36. namespace OpenSim.Region.ClientStack
  37. {
  38. public class PacketServer
  39. {
  40. private ClientStackNetworkHandler _networkHandler;
  41. private IWorld _localWorld;
  42. public Dictionary<uint, ClientView> ClientThreads = new Dictionary<uint, ClientView>();
  43. private ClientManager m_clientManager = new ClientManager();
  44. public ClientManager ClientManager
  45. {
  46. get { return m_clientManager; }
  47. }
  48. public PacketServer(ClientStackNetworkHandler networkHandler)
  49. {
  50. _networkHandler = networkHandler;
  51. _networkHandler.RegisterPacketServer(this);
  52. }
  53. public IWorld LocalWorld
  54. {
  55. set
  56. {
  57. this._localWorld = value;
  58. }
  59. }
  60. /// <summary>
  61. ///
  62. /// </summary>
  63. /// <param name="circuitCode"></param>
  64. /// <param name="packet"></param>
  65. public virtual void ClientInPacket(uint circuitCode, Packet packet)
  66. {
  67. if (this.ClientThreads.ContainsKey(circuitCode))
  68. {
  69. ClientThreads[circuitCode].InPacket(packet);
  70. }
  71. }
  72. /// <summary>
  73. ///
  74. /// </summary>
  75. /// <param name="circuitCode"></param>
  76. /// <returns></returns>
  77. public virtual bool AddNewCircuitCodeClient(uint circuitCode)
  78. {
  79. return false;
  80. }
  81. /// <summary>
  82. ///
  83. /// </summary>
  84. /// <param name="packet"></param>
  85. public virtual void SendPacketToAllClients(Packet packet)
  86. {
  87. }
  88. /// <summary>
  89. ///
  90. /// </summary>
  91. /// <param name="packet"></param>
  92. /// <param name="simClient"></param>
  93. public virtual void SendPacketToAllExcept(Packet packet, ClientView simClient)
  94. {
  95. }
  96. /// <summary>
  97. ///
  98. /// </summary>
  99. /// <param name="packetType"></param>
  100. /// <param name="handler"></param>
  101. public virtual void AddClientPacketHandler(PacketType packetType, PacketMethod handler)
  102. {
  103. }
  104. /// <summary>
  105. ///
  106. /// </summary>
  107. public virtual void RegisterClientPacketHandlers()
  108. {
  109. }
  110. /// <summary>
  111. ///
  112. /// </summary>
  113. /// <param name="remoteEP"></param>
  114. /// <param name="initialcirpack"></param>
  115. /// <param name="clientThreads"></param>
  116. /// <param name="world"></param>
  117. /// <param name="assetCache"></param>
  118. /// <param name="packServer"></param>
  119. /// <param name="inventoryCache"></param>
  120. /// <param name="authenSessions"></param>
  121. /// <returns></returns>
  122. protected virtual ClientView CreateNewClient(EndPoint remoteEP, UseCircuitCodePacket initialcirpack, Dictionary<uint, ClientView> clientThreads, IWorld world, AssetCache assetCache, PacketServer packServer, InventoryCache inventoryCache, AuthenticateSessionsBase authenSessions)
  123. {
  124. return new ClientView(remoteEP, initialcirpack, clientThreads, world, assetCache, packServer, inventoryCache, authenSessions );
  125. }
  126. /// <summary>
  127. ///
  128. /// </summary>
  129. /// <param name="epSender"></param>
  130. /// <param name="useCircuit"></param>
  131. /// <param name="assetCache"></param>
  132. /// <param name="inventoryCache"></param>
  133. /// <param name="authenticateSessionsClass"></param>
  134. /// <returns></returns>
  135. public virtual bool AddNewClient(EndPoint epSender, UseCircuitCodePacket useCircuit, AssetCache assetCache, InventoryCache inventoryCache, AuthenticateSessionsBase authenticateSessionsClass)
  136. {
  137. ClientView newuser =
  138. CreateNewClient(epSender, useCircuit, ClientThreads, _localWorld, assetCache, this, inventoryCache,
  139. authenticateSessionsClass);
  140. this.ClientThreads.Add(useCircuit.CircuitCode.Code, newuser);
  141. this.m_clientManager.Add(useCircuit.CircuitCode.Code, (IClientAPI)newuser);
  142. return true;
  143. }
  144. /// <summary>
  145. ///
  146. /// </summary>
  147. /// <param name="buffer"></param>
  148. /// <param name="size"></param>
  149. /// <param name="flags"></param>
  150. /// <param name="circuitcode"></param>
  151. public virtual void SendPacketTo(byte[] buffer, int size, SocketFlags flags, uint circuitcode)
  152. {
  153. this._networkHandler.SendPacketTo(buffer, size, flags, circuitcode);
  154. }
  155. /// <summary>
  156. ///
  157. /// </summary>
  158. /// <param name="circuitcode"></param>
  159. public virtual void RemoveClientCircuit(uint circuitcode)
  160. {
  161. this._networkHandler.RemoveClientCircuit(circuitcode);
  162. this.m_clientManager.Remove(circuitcode);
  163. }
  164. }
  165. }