ClientView.cs 11 KB

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