ClientView.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. Copyright (c) OpenSim project, http://osgrid.org/
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * * Neither the name of the <organization> nor the
  12. * names of its contributors may be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY <copyright holder> ``AS IS'' AND ANY
  16. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
  19. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. using System;
  27. using System.Collections;
  28. using System.Collections.Generic;
  29. using libsecondlife;
  30. using libsecondlife.Packets;
  31. using Nwc.XmlRpc;
  32. using System.Net;
  33. using System.Net.Sockets;
  34. using System.IO;
  35. using System.Threading;
  36. using System.Timers;
  37. using OpenSim.Framework;
  38. using OpenSim.Framework.Interfaces;
  39. using OpenSim.Framework.Types;
  40. using OpenSim.Framework.Inventory;
  41. using OpenSim.Framework.Utilities;
  42. using OpenSim.Assets;
  43. namespace OpenSim
  44. {
  45. public delegate bool PacketMethod(ClientView simClient, Packet packet);
  46. /// <summary>
  47. /// Handles new client connections
  48. /// Constructor takes a single Packet and authenticates everything
  49. /// </summary>
  50. public partial class ClientView : ClientViewBase, IClientAPI
  51. {
  52. public static TerrainManager TerrainManager;
  53. protected static Dictionary<PacketType, PacketMethod> PacketHandlers = new Dictionary<PacketType, PacketMethod>(); //Global/static handlers for all clients
  54. protected Dictionary<PacketType, PacketMethod> m_packetHandlers = new Dictionary<PacketType, PacketMethod>(); //local handlers for this instance
  55. public LLUUID AgentID;
  56. public LLUUID SessionID;
  57. public LLUUID SecureSessionID = LLUUID.Zero;
  58. public string firstName;
  59. public string lastName;
  60. public bool m_child = false;
  61. private UseCircuitCodePacket cirpack;
  62. public Thread ClientThread;
  63. public LLVector3 startpos;
  64. private AgentAssetUpload UploadAssets;
  65. private LLUUID newAssetFolder = LLUUID.Zero;
  66. private bool debug = false;
  67. private IWorld m_world;
  68. private Dictionary<uint, ClientView> m_clientThreads;
  69. private AssetCache m_assetCache;
  70. private IGridServer m_gridServer;
  71. private InventoryCache m_inventoryCache;
  72. private int cachedtextureserial = 0;
  73. private RegionInfo m_regionData;
  74. protected AuthenticateSessionsBase m_authenticateSessionsHandler;
  75. protected uint serverPort = 0;
  76. public ClientView(EndPoint remoteEP, UseCircuitCodePacket initialcirpack, Dictionary<uint, ClientView> clientThreads, IWorld world, AssetCache assetCache, PacketServer packServer, InventoryCache inventoryCache, AuthenticateSessionsBase authenSessions, uint port)
  77. {
  78. this.serverPort = port;
  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.m_child = m_authenticateSessionsHandler.GetAgentChildStatus(initialcirpack.CircuitCode.Code);
  89. this.startpos = m_authenticateSessionsHandler.GetPosition(initialcirpack.CircuitCode.Code);
  90. PacketQueue = new BlockingQueue<QueItem>();
  91. this.UploadAssets = new AgentAssetUpload(this, m_assetCache, m_inventoryCache);
  92. AckTimer = new System.Timers.Timer(500);
  93. AckTimer.Elapsed += new ElapsedEventHandler(AckTimer_Elapsed);
  94. AckTimer.Start();
  95. this.RegisterLocalPacketHandlers();
  96. ClientThread = new Thread(new ThreadStart(AuthUser));
  97. ClientThread.IsBackground = true;
  98. ClientThread.Start();
  99. }
  100. # region Client Methods
  101. public void UpgradeClient()
  102. {
  103. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW, "SimClient.cs:UpgradeClient() - upgrading child to full agent");
  104. this.m_child = false;
  105. this.startpos = m_authenticateSessionsHandler.GetPosition(CircuitCode);
  106. m_authenticateSessionsHandler.UpdateAgentChildStatus(CircuitCode, false);
  107. OnChildAgentStatus(this.m_child);
  108. }
  109. public void DowngradeClient()
  110. {
  111. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW, "SimClient.cs:UpgradeClient() - changing full agent to child");
  112. this.m_child = true;
  113. OnChildAgentStatus(this.m_child);
  114. }
  115. public void KillClient()
  116. {
  117. KillObjectPacket kill = new KillObjectPacket();
  118. kill.ObjectData = new KillObjectPacket.ObjectDataBlock[1];
  119. kill.ObjectData[0] = new KillObjectPacket.ObjectDataBlock();
  120. //kill.ObjectData[0].ID = this.ClientAvatar.localid;
  121. foreach (ClientView client in m_clientThreads.Values)
  122. {
  123. client.OutPacket(kill);
  124. }
  125. this.m_inventoryCache.ClientLeaving(this.AgentID, null);
  126. // m_world.RemoveViewerAgent(this);
  127. m_clientThreads.Remove(this.CircuitCode);
  128. m_networkServer.RemoveClientCircuit(this.CircuitCode);
  129. this.ClientThread.Abort();
  130. }
  131. #endregion
  132. # region Packet Handling
  133. public static bool AddPacketHandler(PacketType packetType, PacketMethod handler)
  134. {
  135. bool result = false;
  136. lock (PacketHandlers)
  137. {
  138. if (!PacketHandlers.ContainsKey(packetType))
  139. {
  140. PacketHandlers.Add(packetType, handler);
  141. result = true;
  142. }
  143. }
  144. return result;
  145. }
  146. public bool AddLocalPacketHandler(PacketType packetType, PacketMethod handler)
  147. {
  148. bool result = false;
  149. lock (m_packetHandlers)
  150. {
  151. if (!m_packetHandlers.ContainsKey(packetType))
  152. {
  153. m_packetHandlers.Add(packetType, handler);
  154. result = true;
  155. }
  156. }
  157. return result;
  158. }
  159. protected virtual bool ProcessPacketMethod(Packet packet)
  160. {
  161. bool result = false;
  162. bool found = false;
  163. PacketMethod method;
  164. if (m_packetHandlers.TryGetValue(packet.Type, out method))
  165. {
  166. //there is a local handler for this packet type
  167. result = method(this, packet);
  168. }
  169. else
  170. {
  171. //there is not a local handler so see if there is a Global handler
  172. lock (PacketHandlers)
  173. {
  174. found = PacketHandlers.TryGetValue(packet.Type, out method);
  175. }
  176. if (found)
  177. {
  178. result = method(this, packet);
  179. }
  180. }
  181. return result;
  182. }
  183. protected virtual void ClientLoop()
  184. {
  185. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW, "OpenSimClient.cs:ClientLoop() - Entered loop");
  186. while (true)
  187. {
  188. QueItem nextPacket = PacketQueue.Dequeue();
  189. if (nextPacket.Incoming)
  190. {
  191. //is a incoming packet
  192. ProcessInPacket(nextPacket.Packet);
  193. }
  194. else
  195. {
  196. //is a out going packet
  197. ProcessOutPacket(nextPacket.Packet);
  198. }
  199. }
  200. }
  201. # endregion
  202. # region Setup
  203. protected virtual void InitNewClient()
  204. {
  205. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW, "OpenSimClient.cs:InitNewClient() - Adding viewer agent to world");
  206. this.m_world.AddNewAvatar(this, this.AgentID, false);
  207. }
  208. protected virtual void AuthUser()
  209. {
  210. // AuthenticateResponse sessionInfo = m_gridServer.AuthenticateSession(cirpack.CircuitCode.SessionID, cirpack.CircuitCode.ID, cirpack.CircuitCode.Code);
  211. AuthenticateResponse sessionInfo = this.m_authenticateSessionsHandler.AuthenticateSession(cirpack.CircuitCode.SessionID, cirpack.CircuitCode.ID, cirpack.CircuitCode.Code);
  212. if (!sessionInfo.Authorised)
  213. {
  214. //session/circuit not authorised
  215. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.NORMAL, "OpenSimClient.cs:AuthUser() - New user request denied to " + userEP.ToString());
  216. ClientThread.Abort();
  217. }
  218. else
  219. {
  220. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.NORMAL, "OpenSimClient.cs:AuthUser() - Got authenticated connection from " + userEP.ToString());
  221. //session is authorised
  222. this.AgentID = cirpack.CircuitCode.ID;
  223. this.SessionID = cirpack.CircuitCode.SessionID;
  224. this.CircuitCode = cirpack.CircuitCode.Code;
  225. this.firstName = sessionInfo.LoginInfo.First;
  226. this.lastName = sessionInfo.LoginInfo.Last;
  227. if (sessionInfo.LoginInfo.SecureSession != LLUUID.Zero)
  228. {
  229. this.SecureSessionID = sessionInfo.LoginInfo.SecureSession;
  230. }
  231. InitNewClient();
  232. ClientLoop();
  233. }
  234. }
  235. # endregion
  236. protected override void KillThread()
  237. {
  238. this.ClientThread.Abort();
  239. }
  240. #region Inventory Creation
  241. private void SetupInventory(AuthenticateResponse sessionInfo)
  242. {
  243. }
  244. private AgentInventory CreateInventory(LLUUID baseFolder)
  245. {
  246. AgentInventory inventory = null;
  247. return inventory;
  248. }
  249. private void CreateInventoryItem(CreateInventoryItemPacket packet)
  250. {
  251. }
  252. #endregion
  253. }
  254. }