Scene.PacketHandlers.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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. bool oldSelect = part.IsSelected;
  236. part.IsSelected = false;
  237. if (oldgprSelect != part.ParentGroup.IsSelected)
  238. {
  239. if (!part.ParentGroup.IsAttachment )
  240. EventManager.TriggerParcelPrimCountTainted();
  241. }
  242. // restore targetOmega
  243. if (oldSelect && part.AngularVelocity != Vector3.Zero)
  244. part.ScheduleTerseUpdate();
  245. }
  246. public virtual void ProcessMoneyTransferRequest(UUID source, UUID destination, int amount,
  247. int transactiontype, string description)
  248. {
  249. EventManager.MoneyTransferArgs args = new EventManager.MoneyTransferArgs(source, destination, amount,
  250. transactiontype, description);
  251. EventManager.TriggerMoneyTransfer(this, args);
  252. }
  253. public virtual void ProcessParcelBuy(UUID agentId, UUID groupId, bool final, bool groupOwned,
  254. bool removeContribution, int parcelLocalID, int parcelArea, int parcelPrice, bool authenticated)
  255. {
  256. EventManager.LandBuyArgs args = new EventManager.LandBuyArgs(agentId, groupId, final, groupOwned,
  257. removeContribution, parcelLocalID, parcelArea,
  258. parcelPrice, authenticated);
  259. // First, allow all validators a stab at it
  260. m_eventManager.TriggerValidateLandBuy(this, args);
  261. // Then, check validation and transfer
  262. m_eventManager.TriggerLandBuy(this, args);
  263. }
  264. public virtual void ProcessObjectGrab(uint localID, Vector3 offsetPos, IClientAPI remoteClient, List<SurfaceTouchEventArgs> surfaceArgs)
  265. {
  266. SceneObjectPart part = GetSceneObjectPart(localID);
  267. if (part == null)
  268. return;
  269. SceneObjectGroup obj = part.ParentGroup;
  270. SurfaceTouchEventArgs surfaceArg = null;
  271. if (surfaceArgs != null && surfaceArgs.Count > 0)
  272. surfaceArg = surfaceArgs[0];
  273. // Currently only grab/touch for the single prim
  274. // the client handles rez correctly
  275. obj.ObjectGrabHandler(localID, offsetPos, remoteClient);
  276. // If the touched prim handles touches, deliver it
  277. if ((part.ScriptEvents & scriptEvents.touch_start) != 0)
  278. EventManager.TriggerObjectGrab(part.LocalId, 0, offsetPos, remoteClient, surfaceArg);
  279. // Deliver to the root prim if the touched prim doesn't handle touches
  280. // or if we're meant to pass on touches anyway.
  281. if (((part.ScriptEvents & scriptEvents.touch_start) == 0) ||
  282. (part.PassTouches && (part.LocalId != obj.RootPart.LocalId)))
  283. {
  284. EventManager.TriggerObjectGrab(obj.RootPart.LocalId, part.LocalId, offsetPos, remoteClient, surfaceArg);
  285. }
  286. }
  287. public virtual void ProcessObjectGrabUpdate(
  288. UUID objectID, Vector3 offset, Vector3 pos, IClientAPI remoteClient, List<SurfaceTouchEventArgs> surfaceArgs)
  289. {
  290. SceneObjectPart part = GetSceneObjectPart(objectID);
  291. if (part == null)
  292. return;
  293. SceneObjectGroup group = part.ParentGroup;
  294. if(group == null || group.IsDeleted)
  295. return;
  296. if (Permissions.CanMoveObject(group, remoteClient))
  297. {
  298. group.GrabMovement(objectID, offset, pos, remoteClient);
  299. }
  300. // This is outside the above permissions condition
  301. // so that if the object is locked the client moving the object
  302. // get's it's position on the simulator even if it was the same as before
  303. // This keeps the moving user's client in sync with the rest of the world.
  304. group.SendGroupTerseUpdate();
  305. SurfaceTouchEventArgs surfaceArg = null;
  306. if (surfaceArgs != null && surfaceArgs.Count > 0)
  307. surfaceArg = surfaceArgs[0];
  308. Vector3 grabOffset = pos - part.AbsolutePosition;
  309. // If the touched prim handles touches, deliver it
  310. if ((part.ScriptEvents & scriptEvents.touch) != 0)
  311. EventManager.TriggerObjectGrabbing(part.LocalId, 0, grabOffset, remoteClient, surfaceArg);
  312. // Deliver to the root prim if the touched prim doesn't handle touches
  313. // or if we're meant to pass on touches anyway.
  314. if (((part.ScriptEvents & scriptEvents.touch) == 0) ||
  315. (part.PassTouches && (part.LocalId != group.RootPart.LocalId)))
  316. EventManager.TriggerObjectGrabbing(group.RootPart.LocalId, part.LocalId, grabOffset, remoteClient, surfaceArg);
  317. }
  318. public virtual void ProcessObjectDeGrab(uint localID, IClientAPI remoteClient, List<SurfaceTouchEventArgs> surfaceArgs)
  319. {
  320. SceneObjectPart part = GetSceneObjectPart(localID);
  321. if (part == null)
  322. return;
  323. SceneObjectGroup grp = part.ParentGroup;
  324. SurfaceTouchEventArgs surfaceArg = null;
  325. if (surfaceArgs != null && surfaceArgs.Count > 0)
  326. surfaceArg = surfaceArgs[0];
  327. // If the touched prim handles touches, deliver it
  328. if ((part.ScriptEvents & scriptEvents.touch_end) != 0)
  329. EventManager.TriggerObjectDeGrab(part.LocalId, 0, remoteClient, surfaceArg);
  330. // if not or PassTouchs, send it also to root.
  331. if (((part.ScriptEvents & scriptEvents.touch_end) == 0) ||
  332. (part.PassTouches && (part.LocalId != grp.RootPart.LocalId)))
  333. {
  334. EventManager.TriggerObjectDeGrab(grp.RootPart.LocalId, part.LocalId, remoteClient, surfaceArg);
  335. }
  336. }
  337. /// <summary>
  338. /// Start spinning the given object
  339. /// </summary>
  340. /// <param name="objectID"></param>
  341. /// <param name="rotation"></param>
  342. /// <param name="remoteClient"></param>
  343. public virtual void ProcessSpinStart(UUID objectID, IClientAPI remoteClient)
  344. {
  345. SceneObjectGroup group = GetGroupByPrim(objectID);
  346. if (group != null)
  347. {
  348. if (Permissions.CanMoveObject(group, remoteClient))// && PermissionsMngr.)
  349. {
  350. group.SpinStart(remoteClient);
  351. }
  352. }
  353. }
  354. /// <summary>
  355. /// Spin the given object
  356. /// </summary>
  357. /// <param name="objectID"></param>
  358. /// <param name="rotation"></param>
  359. /// <param name="remoteClient"></param>
  360. public virtual void ProcessSpinObject(UUID objectID, Quaternion rotation, IClientAPI remoteClient)
  361. {
  362. SceneObjectGroup group = GetGroupByPrim(objectID);
  363. if (group != null)
  364. {
  365. if (Permissions.CanMoveObject(group, remoteClient))// && PermissionsMngr.)
  366. {
  367. group.SpinMovement(rotation, remoteClient);
  368. }
  369. // This is outside the above permissions condition
  370. // so that if the object is locked the client moving the object
  371. // get's it's position on the simulator even if it was the same as before
  372. // This keeps the moving user's client in sync with the rest of the world.
  373. group.SendGroupTerseUpdate();
  374. }
  375. }
  376. public virtual void ProcessSpinObjectStop(UUID objectID, IClientAPI remoteClient)
  377. {
  378. /* no op for now
  379. SceneObjectGroup group = GetGroupByPrim(objectID);
  380. if (group != null)
  381. {
  382. if (Permissions.CanMoveObject(group.UUID, remoteClient.AgentId))// && PermissionsMngr.)
  383. {
  384. // group.SpinMovement(rotation, remoteClient);
  385. }
  386. group.SendGroupTerseUpdate();
  387. }
  388. */
  389. }
  390. public void ProcessScriptReset(IClientAPI remoteClient, UUID objectID,
  391. UUID itemID)
  392. {
  393. SceneObjectPart part=GetSceneObjectPart(objectID);
  394. if (part == null)
  395. return;
  396. if (Permissions.CanResetScript(objectID, itemID, remoteClient.AgentId))
  397. {
  398. EventManager.TriggerScriptReset(part.LocalId, itemID);
  399. }
  400. }
  401. void ProcessViewerEffect(IClientAPI remoteClient, List<ViewerEffectEventHandlerArg> args)
  402. {
  403. // TODO: don't create new blocks if recycling an old packet
  404. bool discardableEffects = true;
  405. ViewerEffectPacket.EffectBlock[] effectBlockArray = new ViewerEffectPacket.EffectBlock[args.Count];
  406. for (int i = 0; i < args.Count; i++)
  407. {
  408. ViewerEffectPacket.EffectBlock effect = new ViewerEffectPacket.EffectBlock();
  409. effect.AgentID = args[i].AgentID;
  410. effect.Color = args[i].Color;
  411. effect.Duration = args[i].Duration;
  412. effect.ID = args[i].ID;
  413. effect.Type = args[i].Type;
  414. effect.TypeData = args[i].TypeData;
  415. effectBlockArray[i] = effect;
  416. if ((EffectType)effect.Type != EffectType.LookAt && (EffectType)effect.Type != EffectType.Beam)
  417. discardableEffects = false;
  418. //m_log.DebugFormat("[YYY]: VE {0} {1} {2}", effect.AgentID, effect.Duration, (EffectType)effect.Type);
  419. }
  420. ForEachScenePresence(sp =>
  421. {
  422. if (sp.ControllingClient.AgentId != remoteClient.AgentId)
  423. {
  424. if (!discardableEffects ||
  425. (discardableEffects && ShouldSendDiscardableEffect(remoteClient, sp)))
  426. {
  427. //m_log.DebugFormat("[YYY]: Sending to {0}", sp.UUID);
  428. sp.ControllingClient.SendViewerEffect(effectBlockArray);
  429. }
  430. //else
  431. // m_log.DebugFormat("[YYY]: Not sending to {0}", sp.UUID);
  432. }
  433. });
  434. }
  435. private bool ShouldSendDiscardableEffect(IClientAPI thisClient, ScenePresence other)
  436. {
  437. return Vector3.DistanceSquared(other.CameraPosition, thisClient.SceneAgent.AbsolutePosition) < 100;
  438. }
  439. private class DescendentsRequestData
  440. {
  441. public IClientAPI RemoteClient;
  442. public UUID FolderID;
  443. //public UUID OwnerID;
  444. public bool FetchFolders;
  445. public bool FetchItems;
  446. //public int SortOrder;
  447. }
  448. static private ConcurrentQueue<DescendentsRequestData> m_descendentsRequestQueue = new ConcurrentQueue<DescendentsRequestData>();
  449. static private Object m_descendentsRequestLock = new Object();
  450. static private bool m_descendentsRequestProcessing = false;
  451. /// <summary>
  452. /// Tell the client about the various child items and folders contained in the requested folder.
  453. /// </summary>
  454. /// <param name="remoteClient"></param>
  455. /// <param name="folderID"></param>
  456. /// <param name="ownerID"></param>
  457. /// <param name="fetchFolders"></param>
  458. /// <param name="fetchItems"></param>
  459. /// <param name="sortOrder"></param>
  460. public void HandleFetchInventoryDescendents(IClientAPI remoteClient, UUID folderID, UUID ownerID,
  461. bool fetchFolders, bool fetchItems, int sortOrder)
  462. {
  463. // m_log.DebugFormat(
  464. // "[USER INVENTORY]: HandleFetchInventoryDescendents() for {0}, folder={1}, fetchFolders={2}, fetchItems={3}, sortOrder={4}",
  465. // remoteClient.Name, folderID, fetchFolders, fetchItems, sortOrder);
  466. if (folderID == UUID.Zero)
  467. return;
  468. // FIXME MAYBE: We're not handling sortOrder!
  469. // TODO: This code for looking in the folder for the library should be folded somewhere else
  470. // so that this class doesn't have to know the details (and so that multiple libraries, etc.
  471. // can be handled transparently).
  472. InventoryFolderImpl fold = null;
  473. if (LibraryService != null && LibraryService.LibraryRootFolder != null)
  474. {
  475. if ((fold = LibraryService.LibraryRootFolder.FindFolder(folderID)) != null)
  476. {
  477. List<InventoryItemBase> its = fold.RequestListOfItems();
  478. List<InventoryFolderBase> fds = fold.RequestListOfFolders();
  479. remoteClient.SendInventoryFolderDetails(
  480. fold.Owner, folderID, its, fds,
  481. fold.Version, its.Count + fds.Count, fetchFolders, fetchItems);
  482. return;
  483. }
  484. }
  485. DescendentsRequestData req = new DescendentsRequestData();
  486. req.RemoteClient = remoteClient;
  487. req.FolderID = folderID;
  488. //req.OwnerID = ownerID;
  489. req.FetchFolders = fetchFolders;
  490. req.FetchItems = fetchItems;
  491. //req.SortOrder = sortOrder;
  492. m_descendentsRequestQueue.Enqueue(req);
  493. if (Monitor.TryEnter(m_descendentsRequestLock))
  494. {
  495. if (!m_descendentsRequestProcessing)
  496. {
  497. m_descendentsRequestProcessing = true;
  498. Util.FireAndForget(x => SendInventoryAsync());
  499. }
  500. Monitor.Exit(m_descendentsRequestLock);
  501. }
  502. }
  503. void SendInventoryAsync()
  504. {
  505. lock(m_descendentsRequestLock)
  506. {
  507. try
  508. {
  509. while(m_descendentsRequestQueue.TryDequeue(out DescendentsRequestData req))
  510. {
  511. if(!req.RemoteClient.IsActive)
  512. continue;
  513. SendInventoryUpdate(req.RemoteClient, new InventoryFolderBase(req.FolderID), req.FetchFolders, req.FetchItems);
  514. Thread.Sleep(50);
  515. }
  516. }
  517. catch (Exception e)
  518. {
  519. m_log.ErrorFormat("[AGENT INVENTORY]: Error in SendInventoryAsync(). Exception {0}", e);
  520. }
  521. m_descendentsRequestProcessing = false;
  522. }
  523. }
  524. /// <summary>
  525. /// Handle an inventory folder creation request from the client.
  526. /// </summary>
  527. /// <param name="remoteClient"></param>
  528. /// <param name="folderID"></param>
  529. /// <param name="folderType"></param>
  530. /// <param name="folderName"></param>
  531. /// <param name="parentID"></param>
  532. public void HandleCreateInventoryFolder(IClientAPI remoteClient, UUID folderID, ushort folderType,
  533. string folderName, UUID parentID)
  534. {
  535. InventoryFolderBase folder = new InventoryFolderBase(folderID, folderName, remoteClient.AgentId, (short)folderType, parentID, 1);
  536. if (!InventoryService.AddFolder(folder))
  537. {
  538. m_log.WarnFormat(
  539. "[AGENT INVENTORY]: Failed to create folder for user {0} {1}",
  540. remoteClient.Name, remoteClient.AgentId);
  541. }
  542. }
  543. /// <summary>
  544. /// Handle a client request to update the inventory folder
  545. /// </summary>
  546. ///
  547. /// FIXME: We call add new inventory folder because in the data layer, we happen to use an SQL REPLACE
  548. /// so this will work to rename an existing folder. Needless to say, to rely on this is very confusing,
  549. /// and needs to be changed.
  550. ///
  551. /// <param name="remoteClient"></param>
  552. /// <param name="folderID"></param>
  553. /// <param name="type"></param>
  554. /// <param name="name"></param>
  555. /// <param name="parentID"></param>
  556. public void HandleUpdateInventoryFolder(IClientAPI remoteClient, UUID folderID, ushort type, string name,
  557. UUID parentID)
  558. {
  559. // m_log.DebugFormat(
  560. // "[AGENT INVENTORY]: Updating inventory folder {0} {1} for {2} {3}", folderID, name, remoteClient.Name, remoteClient.AgentId);
  561. InventoryFolderBase folder = InventoryService.GetFolder(remoteClient.AgentId, folderID);
  562. if (folder != null)
  563. {
  564. folder.Name = name;
  565. folder.Type = (short)type;
  566. folder.ParentID = parentID;
  567. if (!InventoryService.UpdateFolder(folder))
  568. {
  569. m_log.ErrorFormat(
  570. "[AGENT INVENTORY]: Failed to update folder for user {0} {1}",
  571. remoteClient.Name, remoteClient.AgentId);
  572. }
  573. }
  574. }
  575. public void HandleMoveInventoryFolder(IClientAPI remoteClient, UUID folderID, UUID parentID)
  576. {
  577. InventoryFolderBase folder = InventoryService.GetFolder(remoteClient.AgentId, folderID);
  578. if (folder != null)
  579. {
  580. folder.ParentID = parentID;
  581. if (!InventoryService.MoveFolder(folder))
  582. m_log.WarnFormat("[AGENT INVENTORY]: could not move folder {0}", folderID);
  583. else
  584. m_log.DebugFormat("[AGENT INVENTORY]: folder {0} moved to parent {1}", folderID, parentID);
  585. }
  586. else
  587. {
  588. m_log.WarnFormat("[AGENT INVENTORY]: request to move folder {0} but folder not found", folderID);
  589. }
  590. }
  591. delegate void PurgeFolderDelegate(UUID userID, UUID folder);
  592. /// <summary>
  593. /// This should delete all the items and folders in the given directory.
  594. /// </summary>
  595. /// <param name="remoteClient"></param>
  596. /// <param name="folderID"></param>
  597. public void HandlePurgeInventoryDescendents(IClientAPI remoteClient, UUID folderID)
  598. {
  599. PurgeFolderDelegate d = PurgeFolderAsync;
  600. try
  601. {
  602. d.BeginInvoke(remoteClient.AgentId, folderID, PurgeFolderCompleted, d);
  603. }
  604. catch (Exception e)
  605. {
  606. m_log.WarnFormat("[AGENT INVENTORY]: Exception on purge folder for user {0}: {1}", remoteClient.AgentId, e.Message);
  607. }
  608. }
  609. private void PurgeFolderAsync(UUID userID, UUID folderID)
  610. {
  611. InventoryFolderBase folder = new InventoryFolderBase(folderID, userID);
  612. try
  613. {
  614. if (InventoryService.PurgeFolder(folder))
  615. m_log.DebugFormat("[AGENT INVENTORY]: folder {0} purged successfully", folderID);
  616. else
  617. m_log.WarnFormat("[AGENT INVENTORY]: could not purge folder {0}", folderID);
  618. }
  619. catch (Exception e)
  620. {
  621. m_log.WarnFormat("[AGENT INVENTORY]: Exception on async purge folder for user {0}: {1}", userID, e.Message);
  622. }
  623. }
  624. private void PurgeFolderCompleted(IAsyncResult iar)
  625. {
  626. PurgeFolderDelegate d = (PurgeFolderDelegate)iar.AsyncState;
  627. d.EndInvoke(iar);
  628. }
  629. }
  630. }