ClientView.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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;
  29. using System.Collections.Generic;
  30. using System.Net;
  31. using System.Text;
  32. using System.Threading;
  33. using System.Timers;
  34. using libsecondlife;
  35. using libsecondlife.Packets;
  36. using OpenSim.Assets;
  37. using OpenSim.Framework;
  38. using OpenSim.Framework.Console;
  39. using OpenSim.Framework.Interfaces;
  40. using OpenSim.Framework.Inventory;
  41. using OpenSim.Framework.Types;
  42. using OpenSim.Framework.Utilities;
  43. using OpenSim.Region.Caches;
  44. using Timer=System.Timers.Timer;
  45. namespace OpenSim.Region.ClientStack
  46. {
  47. public delegate bool PacketMethod(ClientView simClient, Packet packet);
  48. /// <summary>
  49. /// Handles new client connections
  50. /// Constructor takes a single Packet and authenticates everything
  51. /// </summary>
  52. public partial class ClientView : ClientViewBase, IClientAPI
  53. {
  54. public static TerrainManager TerrainManager;
  55. protected static Dictionary<PacketType, PacketMethod> PacketHandlers = new Dictionary<PacketType, PacketMethod>(); //Global/static handlers for all clients
  56. protected Dictionary<PacketType, PacketMethod> m_packetHandlers = new Dictionary<PacketType, PacketMethod>(); //local handlers for this instance
  57. public LLUUID AgentID;
  58. public LLUUID SessionID;
  59. public LLUUID SecureSessionID = LLUUID.Zero;
  60. public string firstName;
  61. public string lastName;
  62. public bool m_child = false;
  63. private UseCircuitCodePacket cirpack;
  64. public Thread ClientThread;
  65. public LLVector3 startpos;
  66. private AgentAssetUpload UploadAssets;
  67. private LLUUID newAssetFolder = LLUUID.Zero;
  68. private bool debug = false;
  69. protected IWorld m_world;
  70. private Dictionary<uint, ClientView> m_clientThreads;
  71. private AssetCache m_assetCache;
  72. private InventoryCache m_inventoryCache;
  73. private int cachedtextureserial = 0;
  74. protected AuthenticateSessionsBase m_authenticateSessionsHandler;
  75. private Encoding enc = Encoding.ASCII;
  76. // Dead client detection vars
  77. private Timer clientPingTimer;
  78. private int packetsReceived = 0;
  79. private int probesWithNoIngressPackets = 0;
  80. private int lastPacketsReceived = 0;
  81. public ClientView(EndPoint remoteEP, UseCircuitCodePacket initialcirpack, Dictionary<uint, ClientView> clientThreads, IWorld world, AssetCache assetCache, PacketServer packServer, InventoryCache inventoryCache, AuthenticateSessionsBase authenSessions )
  82. {
  83. m_world = world;
  84. m_clientThreads = clientThreads;
  85. m_assetCache = assetCache;
  86. m_networkServer = packServer;
  87. m_inventoryCache = inventoryCache;
  88. m_authenticateSessionsHandler = authenSessions;
  89. MainLog.Instance.Verbose( "OpenSimClient.cs - Started up new client thread to handle incoming request");
  90. cirpack = initialcirpack;
  91. userEP = remoteEP;
  92. this.startpos = m_authenticateSessionsHandler.GetPosition(initialcirpack.CircuitCode.Code);
  93. PacketQueue = new BlockingQueue<QueItem>();
  94. this.UploadAssets = new AgentAssetUpload(this, m_assetCache, m_inventoryCache);
  95. AckTimer = new Timer(500);
  96. AckTimer.Elapsed += new ElapsedEventHandler(AckTimer_Elapsed);
  97. AckTimer.Start();
  98. this.RegisterLocalPacketHandlers();
  99. ClientThread = new Thread(new ThreadStart(AuthUser));
  100. ClientThread.IsBackground = true;
  101. ClientThread.Start();
  102. }
  103. # region Client Methods
  104. public void KillClient()
  105. {
  106. clientPingTimer.Stop();
  107. this.m_inventoryCache.ClientLeaving(this.AgentID, null);
  108. m_world.RemoveClient(this.AgentId);
  109. m_clientThreads.Remove(this.CircuitCode);
  110. m_networkServer.RemoveClientCircuit(this.CircuitCode);
  111. this.ClientThread.Abort();
  112. }
  113. #endregion
  114. # region Packet Handling
  115. public static bool AddPacketHandler(PacketType packetType, PacketMethod handler)
  116. {
  117. bool result = false;
  118. lock (PacketHandlers)
  119. {
  120. if (!PacketHandlers.ContainsKey(packetType))
  121. {
  122. PacketHandlers.Add(packetType, handler);
  123. result = true;
  124. }
  125. }
  126. return result;
  127. }
  128. public bool AddLocalPacketHandler(PacketType packetType, PacketMethod handler)
  129. {
  130. bool result = false;
  131. lock (m_packetHandlers)
  132. {
  133. if (!m_packetHandlers.ContainsKey(packetType))
  134. {
  135. m_packetHandlers.Add(packetType, handler);
  136. result = true;
  137. }
  138. }
  139. return result;
  140. }
  141. protected virtual bool ProcessPacketMethod(Packet packet)
  142. {
  143. bool result = false;
  144. bool found = false;
  145. PacketMethod method;
  146. if (m_packetHandlers.TryGetValue(packet.Type, out method))
  147. {
  148. //there is a local handler for this packet type
  149. result = method(this, packet);
  150. }
  151. else
  152. {
  153. //there is not a local handler so see if there is a Global handler
  154. lock (PacketHandlers)
  155. {
  156. found = PacketHandlers.TryGetValue(packet.Type, out method);
  157. }
  158. if (found)
  159. {
  160. result = method(this, packet);
  161. }
  162. }
  163. return result;
  164. }
  165. protected virtual void ClientLoop()
  166. {
  167. MainLog.Instance.Verbose( "OpenSimClient.cs:ClientLoop() - Entered loop");
  168. while (true)
  169. {
  170. QueItem nextPacket = PacketQueue.Dequeue();
  171. if (nextPacket.Incoming)
  172. {
  173. //is a incoming packet
  174. if (nextPacket.Packet.Type != PacketType.AgentUpdate) {
  175. packetsReceived++;
  176. }
  177. ProcessInPacket(nextPacket.Packet);
  178. }
  179. else
  180. {
  181. //is a out going packet
  182. ProcessOutPacket(nextPacket.Packet);
  183. }
  184. }
  185. }
  186. # endregion
  187. protected void CheckClientConnectivity(object sender, ElapsedEventArgs e)
  188. {
  189. if (packetsReceived == lastPacketsReceived) {
  190. probesWithNoIngressPackets++;
  191. if (probesWithNoIngressPackets > 30) {
  192. this.KillClient();
  193. } else {
  194. // this will normally trigger at least one packet (ping response)
  195. SendStartPingCheck(0);
  196. }
  197. } else {
  198. // Something received in the meantime - we can reset the counters
  199. probesWithNoIngressPackets = 0;
  200. lastPacketsReceived = packetsReceived;
  201. }
  202. }
  203. # region Setup
  204. protected virtual void InitNewClient()
  205. {
  206. clientPingTimer = new Timer(1000);
  207. clientPingTimer.Elapsed += new ElapsedEventHandler(CheckClientConnectivity);
  208. clientPingTimer.Enabled = true;
  209. MainLog.Instance.Verbose( "OpenSimClient.cs:InitNewClient() - Adding viewer agent to world");
  210. this.m_world.AddNewClient(this, false);
  211. }
  212. protected virtual void AuthUser()
  213. {
  214. // AuthenticateResponse sessionInfo = m_gridServer.AuthenticateSession(cirpack.CircuitCode.SessionID, cirpack.CircuitCode.ID, cirpack.CircuitCode.Code);
  215. AuthenticateResponse sessionInfo = this.m_authenticateSessionsHandler.AuthenticateSession(cirpack.CircuitCode.SessionID, cirpack.CircuitCode.ID, cirpack.CircuitCode.Code);
  216. if (!sessionInfo.Authorised)
  217. {
  218. //session/circuit not authorised
  219. MainLog.Instance.Notice("OpenSimClient.cs:AuthUser() - New user request denied to " + userEP.ToString());
  220. ClientThread.Abort();
  221. }
  222. else
  223. {
  224. MainLog.Instance.Notice("OpenSimClient.cs:AuthUser() - Got authenticated connection from " + userEP.ToString());
  225. //session is authorised
  226. this.AgentID = cirpack.CircuitCode.ID;
  227. this.SessionID = cirpack.CircuitCode.SessionID;
  228. this.CircuitCode = cirpack.CircuitCode.Code;
  229. this.firstName = sessionInfo.LoginInfo.First;
  230. this.lastName = sessionInfo.LoginInfo.Last;
  231. if (sessionInfo.LoginInfo.SecureSession != LLUUID.Zero)
  232. {
  233. this.SecureSessionID = sessionInfo.LoginInfo.SecureSession;
  234. }
  235. InitNewClient();
  236. ClientLoop();
  237. }
  238. }
  239. # endregion
  240. protected override void KillThread()
  241. {
  242. this.ClientThread.Abort();
  243. }
  244. #region Inventory Creation
  245. private void SetupInventory(AuthenticateResponse sessionInfo)
  246. {
  247. }
  248. private AgentInventory CreateInventory(LLUUID baseFolder)
  249. {
  250. AgentInventory inventory = null;
  251. return inventory;
  252. }
  253. private void CreateInventoryItem(CreateInventoryItemPacket packet)
  254. {
  255. }
  256. #endregion
  257. }
  258. }