Scene.PacketHandlers.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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 OpenSimulator 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. using System;
  28. using System.Collections.Generic;
  29. using System.Collections.Concurrent;
  30. using System.Threading;
  31. using OpenMetaverse;
  32. using OpenMetaverse.Packets;
  33. using OpenSim.Framework;
  34. using OpenSim.Services.Interfaces;
  35. namespace OpenSim.Region.Framework.Scenes
  36. {
  37. public partial class Scene
  38. {
  39. /// <summary>
  40. /// Send chat to listeners.
  41. /// </summary>
  42. /// <param name='message'></param>
  43. /// <param name='type'>/param>
  44. /// <param name='channel'></param>
  45. /// <param name='fromPos'></param>
  46. /// <param name='fromName'></param>
  47. /// <param name='fromID'></param>
  48. /// <param name='targetID'></param>
  49. /// <param name='fromAgent'></param>
  50. /// <param name='broadcast'></param>
  51. public void SimChat(byte[] message, ChatTypeEnum type, int channel, Vector3 fromPos, string fromName,
  52. UUID fromID, UUID targetID, bool fromAgent, bool broadcast)
  53. {
  54. OSChatMessage args = new OSChatMessage();
  55. args.Message = Utils.BytesToString(message);
  56. args.Channel = channel;
  57. args.Type = type;
  58. args.Position = fromPos;
  59. args.SenderUUID = fromID;
  60. args.Scene = this;
  61. args.Destination = targetID;
  62. if (fromAgent)
  63. {
  64. ScenePresence user = GetScenePresence(fromID);
  65. if (user != null)
  66. args.Sender = user.ControllingClient;
  67. }
  68. else
  69. {
  70. SceneObjectPart obj = GetSceneObjectPart(fromID);
  71. args.SenderObject = obj;
  72. }
  73. args.From = fromName;
  74. //args.
  75. // m_log.DebugFormat(
  76. // "[SCENE]: Sending message {0} on channel {1}, type {2} from {3}, broadcast {4}",
  77. // args.Message.Replace("\n", "\\n"), args.Channel, args.Type, fromName, broadcast);
  78. if (broadcast)
  79. EventManager.TriggerOnChatBroadcast(this, args);
  80. else
  81. EventManager.TriggerOnChatFromWorld(this, args);
  82. }
  83. protected void SimChat(byte[] message, ChatTypeEnum type, int channel, Vector3 fromPos, string fromName,
  84. UUID fromID, bool fromAgent, bool broadcast)
  85. {
  86. SimChat(message, type, channel, fromPos, fromName, fromID, UUID.Zero, fromAgent, broadcast);
  87. }
  88. /// <summary>
  89. ///
  90. /// </summary>
  91. /// <param name="message"></param>
  92. /// <param name="type"></param>
  93. /// <param name="fromPos"></param>
  94. /// <param name="fromName"></param>
  95. /// <param name="fromAgentID"></param>
  96. public void SimChat(byte[] message, ChatTypeEnum type, int channel, Vector3 fromPos, string fromName,
  97. UUID fromID, bool fromAgent)
  98. {
  99. SimChat(message, type, channel, fromPos, fromName, fromID, fromAgent, false);
  100. }
  101. public void SimChat(string message, ChatTypeEnum type, Vector3 fromPos, string fromName, UUID fromID, bool fromAgent)
  102. {
  103. SimChat(Utils.StringToBytes(message), type, 0, fromPos, fromName, fromID, fromAgent);
  104. }
  105. public void SimChat(string message, string fromName)
  106. {
  107. SimChat(message, ChatTypeEnum.Broadcast, Vector3.Zero, fromName, UUID.Zero, false);
  108. }
  109. /// <summary>
  110. ///
  111. /// </summary>
  112. /// <param name="message"></param>
  113. /// <param name="type"></param>
  114. /// <param name="fromPos"></param>
  115. /// <param name="fromName"></param>
  116. /// <param name="fromAgentID"></param>
  117. public void SimChatBroadcast(byte[] message, ChatTypeEnum type, int channel, Vector3 fromPos, string fromName,
  118. UUID fromID, bool fromAgent)
  119. {
  120. SimChat(message, type, channel, fromPos, fromName, fromID, fromAgent, true);
  121. }
  122. /// <summary>
  123. ///
  124. /// </summary>
  125. /// <param name="message"></param>
  126. /// <param name="type"></param>
  127. /// <param name="channel"></param>
  128. /// <param name="fromPos"></param>
  129. /// <param name="fromName"></param>
  130. /// <param name="fromAgentID"></param>
  131. /// <param name="targetID"></param>
  132. public void SimChatToAgent(UUID targetID, byte[] message, int channel, Vector3 fromPos, string fromName, UUID fromID, bool fromAgent)
  133. {
  134. SimChat(message, ChatTypeEnum.Region, channel, fromPos, fromName, fromID, targetID, fromAgent, false);
  135. }
  136. /// <summary>
  137. /// Invoked when the client requests a prim.
  138. /// </summary>
  139. /// <param name="primLocalID"></param>
  140. /// <param name="remoteClient"></param>
  141. public void RequestPrim(uint primLocalID, IClientAPI remoteClient)
  142. {
  143. SceneObjectPart part = GetSceneObjectPart(primLocalID);
  144. if (part != null)
  145. {
  146. SceneObjectGroup sog = part.ParentGroup;
  147. if(!sog.IsDeleted)
  148. {
  149. PrimUpdateFlags update = PrimUpdateFlags.FullUpdate;
  150. if (sog.RootPart.Shape.MeshFlagEntry)
  151. update = PrimUpdateFlags.FullUpdatewithAnim;
  152. part.SendUpdate(remoteClient, update);
  153. }
  154. }
  155. //SceneObjectGroup sog = GetGroupByPrim(primLocalID);
  156. //if (sog != null)
  157. //sog.SendFullAnimUpdateToClient(remoteClient);
  158. }
  159. /// <summary>
  160. /// Invoked when the client selects a prim.
  161. /// </summary>
  162. /// <param name="primLocalID"></param>
  163. /// <param name="remoteClient"></param>
  164. public void SelectPrim(List<uint> primIDs, IClientAPI remoteClient)
  165. {
  166. foreach(uint primLocalID in primIDs)
  167. {
  168. SceneObjectPart part = GetSceneObjectPart(primLocalID);
  169. if (part == null)
  170. continue;
  171. SceneObjectGroup sog = part.ParentGroup;
  172. if (sog == null)
  173. continue;
  174. // waste of time because properties do not send prim flags as they should
  175. // if a friend got or lost edit rights after login, a full update is needed
  176. if(sog.OwnerID != remoteClient.AgentId)
  177. part.SendFullUpdate(remoteClient);
  178. // A prim is only tainted if it's allowed to be edited by the person clicking it.
  179. if (Permissions.CanChangeSelectedState(part, (ScenePresence)remoteClient.SceneAgent))
  180. {
  181. bool oldsel = part.IsSelected;
  182. part.IsSelected = true;
  183. if(!oldsel)
  184. EventManager.TriggerParcelPrimCountTainted();
  185. }
  186. part.SendPropertiesToClient(remoteClient);
  187. }
  188. }
  189. /// <summary>
  190. /// Handle the update of an object's user group.
  191. /// </summary>
  192. /// <param name="remoteClient"></param>
  193. /// <param name="groupID"></param>
  194. /// <param name="objectLocalID"></param>
  195. /// <param name="Garbage"></param>
  196. private void HandleObjectGroupUpdate(
  197. IClientAPI remoteClient, UUID groupID, uint objectLocalID, UUID Garbage)
  198. {
  199. if (m_groupsModule == null)
  200. return;
  201. // XXX: Might be better to get rid of this special casing and have GetMembershipData return something
  202. // reasonable for a UUID.Zero group.
  203. if (groupID != UUID.Zero)
  204. {
  205. GroupMembershipData gmd = m_groupsModule.GetMembershipData(groupID, remoteClient.AgentId);
  206. if (gmd == null)
  207. {
  208. // m_log.WarnFormat(
  209. // "[GROUPS]: User {0} is not a member of group {1} so they can't update {2} to this group",
  210. // remoteClient.Name, GroupID, objectLocalID);
  211. return;
  212. }
  213. }
  214. SceneObjectGroup so = ((Scene)remoteClient.Scene).GetGroupByPrim(objectLocalID);
  215. if (so != null)
  216. {
  217. if (so.OwnerID == remoteClient.AgentId)
  218. {
  219. so.SetGroup(groupID, remoteClient);
  220. EventManager.TriggerParcelPrimCountTainted();
  221. }
  222. }
  223. }
  224. /// <summary>
  225. /// Handle the deselection of a prim from the client.
  226. /// </summary>
  227. /// <param name="primLocalID"></param>
  228. /// <param name="remoteClient"></param>
  229. public void DeselectPrim(uint primLocalID, IClientAPI remoteClient)
  230. {
  231. SceneObjectPart part = GetSceneObjectPart(primLocalID);
  232. if (part == null)
  233. return;
  234. bool oldgprSelect = part.ParentGroup.IsSelected;
  235. part.IsSelected = false;
  236. if (oldgprSelect != part.ParentGroup.IsSelected)
  237. {
  238. if (!part.ParentGroup.IsAttachment )
  239. EventManager.TriggerParcelPrimCountTainted();
  240. }
  241. // restore targetOmega
  242. if (part.AngularVelocity != Vector3.Zero)
  243. part.ScheduleTerseUpdate();
  244. }
  245. public virtual void ProcessMoneyTransferRequest(UUID source, UUID destination, int amount,
  246. int transactiontype, string description)
  247. {
  248. EventManager.MoneyTransferArgs args = new EventManager.MoneyTransferArgs(source, destination, amount,
  249. transactiontype, description);
  250. EventManager.TriggerMoneyTransfer(this, args);
  251. }
  252. public virtual void ProcessParcelBuy(UUID agentId, UUID groupId, bool final, bool groupOwned,
  253. bool removeContribution, int parcelLocalID, int parcelArea, int parcelPrice, bool authenticated)
  254. {
  255. EventManager.LandBuyArgs args = new EventManager.LandBuyArgs(agentId, groupId, final, groupOwned,
  256. removeContribution, parcelLocalID, parcelArea,
  257. parcelPrice, authenticated);
  258. // First, allow all validators a stab at it
  259. m_eventManager.TriggerValidateLandBuy(this, args);
  260. // Then, check validation and transfer
  261. m_eventManager.TriggerLandBuy(this, args);
  262. }
  263. public virtual void ProcessObjectGrab(uint localID, Vector3 offsetPos, IClientAPI remoteClient, List<SurfaceTouchEventArgs> surfaceArgs)
  264. {
  265. SceneObjectPart part = GetSceneObjectPart(localID);
  266. if (part == null)
  267. return;
  268. SceneObjectGroup obj = part.ParentGroup;
  269. SurfaceTouchEventArgs surfaceArg = null;
  270. if (surfaceArgs != null && surfaceArgs.Count > 0)
  271. surfaceArg = surfaceArgs[0];
  272. // Currently only grab/touch for the single prim
  273. // the client handles rez correctly
  274. obj.ObjectGrabHandler(localID, offsetPos, remoteClient);
  275. uint partLocalId = part.LocalId;
  276. // If the touched prim handles touches, deliver it
  277. if ((part.ScriptEvents & scriptEvents.touch_start) != 0)
  278. {
  279. EventManager.TriggerObjectGrab(partLocalId, 0, offsetPos, remoteClient, surfaceArg);
  280. if(!part.PassTouches)
  281. return;
  282. }
  283. // Deliver to the root prim if the touched prim doesn't handle touches
  284. // or if we're meant to pass on touches anyway.
  285. uint rootLocalId = obj.RootPart.LocalId;
  286. if (partLocalId != rootLocalId && (obj.RootPart.ScriptEvents & scriptEvents.touch_start) != 0)
  287. {
  288. EventManager.TriggerObjectGrab(rootLocalId, partLocalId, offsetPos, remoteClient, surfaceArg);
  289. }
  290. }
  291. public virtual void ProcessObjectGrabUpdate(
  292. UUID objectID, Vector3 offset, Vector3 pos, IClientAPI remoteClient, List<SurfaceTouchEventArgs> surfaceArgs)
  293. {
  294. SceneObjectPart part = GetSceneObjectPart(objectID);
  295. if (part == null)
  296. return;
  297. SceneObjectGroup group = part.ParentGroup;
  298. if(group == null || group.IsDeleted)
  299. return;
  300. if (Permissions.CanMoveObject(group, remoteClient))
  301. {
  302. group.GrabMovement(objectID, offset, pos, remoteClient);
  303. }
  304. // This is outside the above permissions condition
  305. // so that if the object is locked the client moving the object
  306. // get's it's position on the simulator even if it was the same as before
  307. // This keeps the moving user's client in sync with the rest of the world.
  308. group.SendGroupTerseUpdate();
  309. SurfaceTouchEventArgs surfaceArg = null;
  310. if (surfaceArgs != null && surfaceArgs.Count > 0)
  311. surfaceArg = surfaceArgs[0];
  312. Vector3 grabOffset = pos - part.AbsolutePosition;
  313. // If the touched prim handles touches, deliver it
  314. uint partLocalId = part.LocalId;
  315. if ((part.ScriptEvents & scriptEvents.touch) != 0)
  316. {
  317. EventManager.TriggerObjectGrabbing(partLocalId, 0, grabOffset, remoteClient, surfaceArg);
  318. if(!part.PassTouches)
  319. return;
  320. }
  321. uint rootLocalId = group.RootPart.LocalId;
  322. // Deliver to the root prim if the touched prim doesn't handle touches
  323. // or if we're meant to pass on touches anyway.
  324. if (partLocalId != rootLocalId && (group.RootPart.ScriptEvents & scriptEvents.touch) != 0)
  325. {
  326. EventManager.TriggerObjectGrabbing(rootLocalId, partLocalId, grabOffset, remoteClient, surfaceArg);
  327. }
  328. }
  329. public virtual void ProcessObjectDeGrab(uint localID, IClientAPI remoteClient, List<SurfaceTouchEventArgs> surfaceArgs)
  330. {
  331. SceneObjectPart part = GetSceneObjectPart(localID);
  332. if (part == null)
  333. return;
  334. SceneObjectGroup grp = part.ParentGroup;
  335. SurfaceTouchEventArgs surfaceArg = null;
  336. if (surfaceArgs != null && surfaceArgs.Count > 0)
  337. surfaceArg = surfaceArgs[0];
  338. uint partLocalId = part.LocalId;
  339. // If the touched prim handles touches, deliver it
  340. if ((part.ScriptEvents & scriptEvents.touch_end) != 0)
  341. {
  342. EventManager.TriggerObjectDeGrab(partLocalId, 0, remoteClient, surfaceArg);
  343. if(!part.PassTouches)
  344. return;
  345. }
  346. uint rootPartLocalId = grp.RootPart.LocalId;
  347. if (partLocalId != rootPartLocalId && (grp.RootPart.ScriptEvents & scriptEvents.touch_end) != 0)
  348. {
  349. EventManager.TriggerObjectDeGrab(rootPartLocalId, partLocalId, remoteClient, surfaceArg);
  350. }
  351. }
  352. /// <summary>
  353. /// Start spinning the given object
  354. /// </summary>
  355. /// <param name="objectID"></param>
  356. /// <param name="rotation"></param>
  357. /// <param name="remoteClient"></param>
  358. public virtual void ProcessSpinStart(UUID objectID, IClientAPI remoteClient)
  359. {
  360. SceneObjectGroup group = GetGroupByPrim(objectID);
  361. if (group != null)
  362. {
  363. if (Permissions.CanMoveObject(group, remoteClient))// && PermissionsMngr.)
  364. {
  365. group.SpinStart(remoteClient);
  366. }
  367. }
  368. }
  369. /// <summary>
  370. /// Spin the given object
  371. /// </summary>
  372. /// <param name="objectID"></param>
  373. /// <param name="rotation"></param>
  374. /// <param name="remoteClient"></param>
  375. public virtual void ProcessSpinObject(UUID objectID, Quaternion rotation, IClientAPI remoteClient)
  376. {
  377. SceneObjectGroup group = GetGroupByPrim(objectID);
  378. if (group != null)
  379. {
  380. if (Permissions.CanMoveObject(group, remoteClient))// && PermissionsMngr.)
  381. {
  382. group.SpinMovement(rotation, remoteClient);
  383. }
  384. // This is outside the above permissions condition
  385. // so that if the object is locked the client moving the object
  386. // get's it's position on the simulator even if it was the same as before
  387. // This keeps the moving user's client in sync with the rest of the world.
  388. group.SendGroupTerseUpdate();
  389. }
  390. }
  391. public virtual void ProcessSpinObjectStop(UUID objectID, IClientAPI remoteClient)
  392. {
  393. /* no op for now
  394. SceneObjectGroup group = GetGroupByPrim(objectID);
  395. if (group != null)
  396. {
  397. if (Permissions.CanMoveObject(group.UUID, remoteClient.AgentId))// && PermissionsMngr.)
  398. {
  399. // group.SpinMovement(rotation, remoteClient);
  400. }
  401. group.SendGroupTerseUpdate();
  402. }
  403. */
  404. }
  405. public void ProcessScriptReset(IClientAPI remoteClient, UUID objectID,
  406. UUID itemID)
  407. {
  408. SceneObjectPart part=GetSceneObjectPart(objectID);
  409. if (part == null)
  410. return;
  411. if (Permissions.CanResetScript(objectID, itemID, remoteClient.AgentId))
  412. {
  413. EventManager.TriggerScriptReset(part.LocalId, itemID);
  414. }
  415. }
  416. void ProcessViewerEffect(IClientAPI remoteClient, List<ViewerEffectEventHandlerArg> args)
  417. {
  418. // TODO: don't create new blocks if recycling an old packet
  419. bool discardableEffects = true;
  420. ViewerEffectPacket.EffectBlock[] effectBlockArray = new ViewerEffectPacket.EffectBlock[args.Count];
  421. for (int i = 0; i < args.Count; i++)
  422. {
  423. ViewerEffectPacket.EffectBlock effect = new ViewerEffectPacket.EffectBlock();
  424. effect.AgentID = args[i].AgentID;
  425. effect.Color = args[i].Color;
  426. effect.Duration = args[i].Duration;
  427. effect.ID = args[i].ID;
  428. effect.Type = args[i].Type;
  429. effect.TypeData = args[i].TypeData;
  430. effectBlockArray[i] = effect;
  431. if ((EffectType)effect.Type != EffectType.LookAt && (EffectType)effect.Type != EffectType.Beam)
  432. discardableEffects = false;
  433. //m_log.DebugFormat("[YYY]: VE {0} {1} {2}", effect.AgentID, effect.Duration, (EffectType)effect.Type);
  434. }
  435. ForEachScenePresence(sp =>
  436. {
  437. if (sp.ControllingClient.AgentId != remoteClient.AgentId)
  438. {
  439. if (!discardableEffects ||
  440. (discardableEffects && ShouldSendDiscardableEffect(remoteClient, sp)))
  441. {
  442. //m_log.DebugFormat("[YYY]: Sending to {0}", sp.UUID);
  443. sp.ControllingClient.SendViewerEffect(effectBlockArray);
  444. }
  445. //else
  446. // m_log.DebugFormat("[YYY]: Not sending to {0}", sp.UUID);
  447. }
  448. });
  449. }
  450. private bool ShouldSendDiscardableEffect(IClientAPI thisClient, ScenePresence other)
  451. {
  452. return Vector3.DistanceSquared(other.CameraPosition, thisClient.SceneAgent.AbsolutePosition) < 100;
  453. }
  454. private class DescendentsRequestData
  455. {
  456. public IClientAPI RemoteClient;
  457. public UUID FolderID;
  458. //public UUID OwnerID;
  459. public bool FetchFolders;
  460. public bool FetchItems;
  461. //public int SortOrder;
  462. }
  463. static private ConcurrentQueue<DescendentsRequestData> m_descendentsRequestQueue = new ConcurrentQueue<DescendentsRequestData>();
  464. static private Object m_descendentsRequestLock = new Object();
  465. static private bool m_descendentsRequestProcessing = false;
  466. /// <summary>
  467. /// Tell the client about the various child items and folders contained in the requested folder.
  468. /// </summary>
  469. /// <param name="remoteClient"></param>
  470. /// <param name="folderID"></param>
  471. /// <param name="ownerID"></param>
  472. /// <param name="fetchFolders"></param>
  473. /// <param name="fetchItems"></param>
  474. /// <param name="sortOrder"></param>
  475. public void HandleFetchInventoryDescendents(IClientAPI remoteClient, UUID folderID, UUID ownerID,
  476. bool fetchFolders, bool fetchItems, int sortOrder)
  477. {
  478. // m_log.DebugFormat(
  479. // "[USER INVENTORY]: HandleFetchInventoryDescendents() for {0}, folder={1}, fetchFolders={2}, fetchItems={3}, sortOrder={4}",
  480. // remoteClient.Name, folderID, fetchFolders, fetchItems, sortOrder);
  481. if (folderID == UUID.Zero)
  482. return;
  483. // FIXME MAYBE: We're not handling sortOrder!
  484. // TODO: This code for looking in the folder for the library should be folded somewhere else
  485. // so that this class doesn't have to know the details (and so that multiple libraries, etc.
  486. // can be handled transparently).
  487. InventoryFolderImpl fold = null;
  488. if (LibraryService != null && LibraryService.LibraryRootFolder != null)
  489. {
  490. if ((fold = LibraryService.LibraryRootFolder.FindFolder(folderID)) != null)
  491. {
  492. List<InventoryItemBase> its = fold.RequestListOfItems();
  493. List<InventoryFolderBase> fds = fold.RequestListOfFolders();
  494. remoteClient.SendInventoryFolderDetails(
  495. fold.Owner, folderID, its, fds,
  496. fold.Version, its.Count + fds.Count, fetchFolders, fetchItems);
  497. return;
  498. }
  499. }
  500. DescendentsRequestData req = new DescendentsRequestData();
  501. req.RemoteClient = remoteClient;
  502. req.FolderID = folderID;
  503. //req.OwnerID = ownerID;
  504. req.FetchFolders = fetchFolders;
  505. req.FetchItems = fetchItems;
  506. //req.SortOrder = sortOrder;
  507. m_descendentsRequestQueue.Enqueue(req);
  508. if (Monitor.TryEnter(m_descendentsRequestLock))
  509. {
  510. if (!m_descendentsRequestProcessing)
  511. {
  512. m_descendentsRequestProcessing = true;
  513. Util.FireAndForget(x => SendInventoryAsync());
  514. }
  515. Monitor.Exit(m_descendentsRequestLock);
  516. }
  517. }
  518. void SendInventoryAsync()
  519. {
  520. lock(m_descendentsRequestLock)
  521. {
  522. try
  523. {
  524. while(m_descendentsRequestQueue.TryDequeue(out DescendentsRequestData req))
  525. {
  526. if(!req.RemoteClient.IsActive)
  527. continue;
  528. SendInventoryUpdate(req.RemoteClient, new InventoryFolderBase(req.FolderID), req.FetchFolders, req.FetchItems);
  529. Thread.Sleep(50);
  530. }
  531. }
  532. catch (Exception e)
  533. {
  534. m_log.ErrorFormat("[AGENT INVENTORY]: Error in SendInventoryAsync(). Exception {0}", e);
  535. }
  536. m_descendentsRequestProcessing = false;
  537. }
  538. }
  539. /// <summary>
  540. /// Handle an inventory folder creation request from the client.
  541. /// </summary>
  542. /// <param name="remoteClient"></param>
  543. /// <param name="folderID"></param>
  544. /// <param name="folderType"></param>
  545. /// <param name="folderName"></param>
  546. /// <param name="parentID"></param>
  547. public void HandleCreateInventoryFolder(IClientAPI remoteClient, UUID folderID, ushort folderType,
  548. string folderName, UUID parentID)
  549. {
  550. InventoryFolderBase folder = new InventoryFolderBase(folderID, folderName, remoteClient.AgentId, (short)folderType, parentID, 1);
  551. if (!InventoryService.AddFolder(folder))
  552. {
  553. m_log.WarnFormat(
  554. "[AGENT INVENTORY]: Failed to create folder for user {0} {1}",
  555. remoteClient.Name, remoteClient.AgentId);
  556. }
  557. }
  558. /// <summary>
  559. /// Handle a client request to update the inventory folder
  560. /// </summary>
  561. ///
  562. /// FIXME: We call add new inventory folder because in the data layer, we happen to use an SQL REPLACE
  563. /// so this will work to rename an existing folder. Needless to say, to rely on this is very confusing,
  564. /// and needs to be changed.
  565. ///
  566. /// <param name="remoteClient"></param>
  567. /// <param name="folderID"></param>
  568. /// <param name="type"></param>
  569. /// <param name="name"></param>
  570. /// <param name="parentID"></param>
  571. public void HandleUpdateInventoryFolder(IClientAPI remoteClient, UUID folderID, ushort type, string name,
  572. UUID parentID)
  573. {
  574. // m_log.DebugFormat(
  575. // "[AGENT INVENTORY]: Updating inventory folder {0} {1} for {2} {3}", folderID, name, remoteClient.Name, remoteClient.AgentId);
  576. InventoryFolderBase folder = InventoryService.GetFolder(remoteClient.AgentId, folderID);
  577. if (folder != null)
  578. {
  579. folder.Name = name;
  580. folder.Type = (short)type;
  581. folder.ParentID = parentID;
  582. if (!InventoryService.UpdateFolder(folder))
  583. {
  584. m_log.ErrorFormat(
  585. "[AGENT INVENTORY]: Failed to update folder for user {0} {1}",
  586. remoteClient.Name, remoteClient.AgentId);
  587. }
  588. }
  589. }
  590. public void HandleMoveInventoryFolder(IClientAPI remoteClient, UUID folderID, UUID parentID)
  591. {
  592. InventoryFolderBase folder = InventoryService.GetFolder(remoteClient.AgentId, folderID);
  593. if (folder != null)
  594. {
  595. folder.ParentID = parentID;
  596. if (!InventoryService.MoveFolder(folder))
  597. m_log.WarnFormat("[AGENT INVENTORY]: could not move folder {0}", folderID);
  598. else
  599. m_log.DebugFormat("[AGENT INVENTORY]: folder {0} moved to parent {1}", folderID, parentID);
  600. }
  601. else
  602. {
  603. m_log.WarnFormat("[AGENT INVENTORY]: request to move folder {0} but folder not found", folderID);
  604. }
  605. }
  606. delegate void PurgeFolderDelegate(UUID userID, UUID folder);
  607. /// <summary>
  608. /// This should delete all the items and folders in the given directory.
  609. /// </summary>
  610. /// <param name="remoteClient"></param>
  611. /// <param name="folderID"></param>
  612. public void HandlePurgeInventoryDescendents(IClientAPI remoteClient, UUID folderID)
  613. {
  614. PurgeFolderDelegate d = PurgeFolderAsync;
  615. try
  616. {
  617. d.BeginInvoke(remoteClient.AgentId, folderID, PurgeFolderCompleted, d);
  618. }
  619. catch (Exception e)
  620. {
  621. m_log.WarnFormat("[AGENT INVENTORY]: Exception on purge folder for user {0}: {1}", remoteClient.AgentId, e.Message);
  622. }
  623. }
  624. private void PurgeFolderAsync(UUID userID, UUID folderID)
  625. {
  626. InventoryFolderBase folder = new InventoryFolderBase(folderID, userID);
  627. try
  628. {
  629. if (InventoryService.PurgeFolder(folder))
  630. m_log.DebugFormat("[AGENT INVENTORY]: folder {0} purged successfully", folderID);
  631. else
  632. m_log.WarnFormat("[AGENT INVENTORY]: could not purge folder {0}", folderID);
  633. }
  634. catch (Exception e)
  635. {
  636. m_log.WarnFormat("[AGENT INVENTORY]: Exception on async purge folder for user {0}: {1}", userID, e.Message);
  637. }
  638. }
  639. private void PurgeFolderCompleted(IAsyncResult iar)
  640. {
  641. PurgeFolderDelegate d = (PurgeFolderDelegate)iar.AsyncState;
  642. d.EndInvoke(iar);
  643. }
  644. }
  645. }