HGSceneCommunicationService.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /**
  2. * Copyright (c) 2008, Contributors. All rights reserved.
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * * Neither the name of the Organizations nor the names of Individual
  14. * Contributors may be used to endorse or promote products derived from
  15. * this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  20. * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  21. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  22. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  23. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  25. * OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. */
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Net;
  31. using System.Reflection;
  32. using System.Threading;
  33. using OpenMetaverse;
  34. using log4net;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Communications;
  37. using OpenSim.Framework.Communications.Cache;
  38. using OpenSim.Region.Environment.Scenes;
  39. using OpenSim.Region.Environment;
  40. using OpenSim.Region.Interfaces;
  41. using OSD = OpenMetaverse.StructuredData.OSD;
  42. namespace OpenSim.Region.Environment.Scenes.Hypergrid
  43. {
  44. public class HGSceneCommunicationService : SceneCommunicationService
  45. {
  46. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  47. private IHyperlink m_hg;
  48. public HGSceneCommunicationService(CommunicationsManager commsMan, IHyperlink hg) : base(commsMan)
  49. {
  50. m_hg = hg;
  51. }
  52. /// <summary>
  53. /// Try to teleport an agent to a new region.
  54. /// </summary>
  55. /// <param name="remoteClient"></param>
  56. /// <param name="RegionHandle"></param>
  57. /// <param name="position"></param>
  58. /// <param name="lookAt"></param>
  59. /// <param name="flags"></param>
  60. public override void RequestTeleportToLocation(ScenePresence avatar, ulong regionHandle, Vector3 position,
  61. Vector3 lookAt, uint teleportFlags)
  62. {
  63. if (!avatar.Scene.Permissions.CanTeleport(avatar.UUID))
  64. return;
  65. bool destRegionUp = false;
  66. IEventQueue eq = avatar.Scene.RequestModuleInterface<IEventQueue>();
  67. if (regionHandle == m_regionInfo.RegionHandle)
  68. {
  69. // Teleport within the same region
  70. if (position.X < 0 || position.X > Constants.RegionSize || position.Y < 0 || position.Y > Constants.RegionSize || position.Z < 0)
  71. {
  72. Vector3 emergencyPos = new Vector3(128, 128, 128);
  73. m_log.WarnFormat(
  74. "[HGSceneCommService]: RequestTeleportToLocation() was given an illegal position of {0} for avatar {1}, {2}. Substituting {3}",
  75. position, avatar.Name, avatar.UUID, emergencyPos);
  76. position = emergencyPos;
  77. }
  78. // TODO: Get proper AVG Height
  79. float localAVHeight = 1.56f;
  80. float posZLimit = (float)avatar.Scene.GetLandHeight((int)position.X, (int)position.Y);
  81. float newPosZ = posZLimit + localAVHeight;
  82. if (posZLimit >= (position.Z - (localAVHeight / 2)) && !(Single.IsInfinity(newPosZ) || Single.IsNaN(newPosZ)))
  83. {
  84. position.Z = newPosZ;
  85. }
  86. // Only send this if the event queue is null
  87. if (eq == null)
  88. avatar.ControllingClient.SendTeleportLocationStart();
  89. avatar.ControllingClient.SendLocalTeleport(position, lookAt, teleportFlags);
  90. avatar.Teleport(position);
  91. }
  92. else
  93. {
  94. RegionInfo reg = RequestNeighbouringRegionInfo(regionHandle);
  95. if (reg != null)
  96. {
  97. uint newRegionX = (uint)(reg.RegionHandle >> 40);
  98. uint newRegionY = (((uint)(reg.RegionHandle)) >> 8);
  99. uint oldRegionX = (uint)(m_regionInfo.RegionHandle >> 40);
  100. uint oldRegionY = (((uint)(m_regionInfo.RegionHandle)) >> 8);
  101. ///
  102. /// Hypergrid mod start
  103. ///
  104. ///
  105. bool isHyperLink = m_hg.IsHyperlinkRegion(reg.RegionHandle);
  106. bool isHomeUser = true;
  107. ulong realHandle = regionHandle;
  108. CachedUserInfo uinfo = m_commsProvider.UserProfileCacheService.GetUserDetails(avatar.UUID);
  109. if (uinfo != null)
  110. {
  111. isHomeUser = HGNetworkServersInfo.Singleton.IsLocalUser(uinfo.UserProfile);
  112. realHandle = m_hg.FindRegionHandle(regionHandle);
  113. Console.WriteLine("XXX ---- home user? " + isHomeUser + " --- hyperlink? " + isHyperLink + " --- real handle: " + realHandle.ToString());
  114. }
  115. ///
  116. /// Hypergrid mod stop
  117. ///
  118. ///
  119. if (eq == null)
  120. avatar.ControllingClient.SendTeleportLocationStart();
  121. if (reg.RemotingAddress != "" && reg.RemotingPort != 0)
  122. {
  123. // region is remote. see if it is up
  124. destRegionUp = m_commsProvider.InterRegion.CheckRegion(reg.RemotingAddress, reg.RemotingPort);
  125. }
  126. else
  127. {
  128. // assume local regions are always up
  129. destRegionUp = true;
  130. }
  131. // Let's do DNS resolution only once in this process, please!
  132. // This may be a costly operation. The reg.ExternalEndPoint field is not a passive field,
  133. // it's actually doing a lot of work.
  134. IPEndPoint endPoint = reg.ExternalEndPoint;
  135. if (endPoint.Address == null)
  136. {
  137. // Couldn't resolve the name. Can't TP, because the viewer wants IP addresses.
  138. destRegionUp = false;
  139. }
  140. if (destRegionUp)
  141. {
  142. // Fixing a bug where teleporting while sitting results in the avatar ending up removed from
  143. // both regions
  144. if (avatar.ParentID != (uint)0)
  145. avatar.StandUp();
  146. if (!avatar.ValidateAttachments())
  147. {
  148. avatar.ControllingClient.SendTeleportFailed("Inconsistent attachment state");
  149. return;
  150. }
  151. // the avatar.Close below will clear the child region list. We need this below for (possibly)
  152. // closing the child agents, so save it here (we need a copy as it is Clear()-ed).
  153. //List<ulong> childRegions = new List<ulong>(avatar.GetKnownRegionList());
  154. // Compared to ScenePresence.CrossToNewRegion(), there's no obvious code to handle a teleport
  155. // failure at this point (unlike a border crossing failure). So perhaps this can never fail
  156. // once we reach here...
  157. //avatar.Scene.RemoveCapsHandler(avatar.UUID);
  158. // Let's close some agents
  159. if (isHyperLink) // close them all except this one
  160. {
  161. List<ulong> regions = new List<ulong>(avatar.KnownChildRegionHandles);
  162. regions.Remove(avatar.Scene.RegionInfo.RegionHandle);
  163. SendCloseChildAgentConnections(avatar.UUID, regions);
  164. }
  165. else // close just a few
  166. avatar.CloseChildAgents(newRegionX, newRegionY);
  167. string capsPath = String.Empty;
  168. AgentCircuitData agent = avatar.ControllingClient.RequestClientInfo();
  169. agent.BaseFolder = UUID.Zero;
  170. agent.InventoryFolder = UUID.Zero;
  171. agent.startpos = position;
  172. agent.child = true;
  173. if (Util.IsOutsideView(oldRegionX, newRegionX, oldRegionY, newRegionY) || isHyperLink)
  174. {
  175. Thread.Sleep(1000);
  176. // brand new agent
  177. agent.CapsPath = Util.GetRandomCapsPath();
  178. if (!m_commsProvider.InterRegion.InformRegionOfChildAgent(reg.RegionHandle, agent))
  179. {
  180. avatar.ControllingClient.SendTeleportFailed("Destination is not accepting teleports.");
  181. return;
  182. }
  183. Thread.Sleep(2000);
  184. // TODO Should construct this behind a method
  185. capsPath =
  186. "http://" + reg.ExternalHostName + ":" + reg.HttpPort
  187. + "/CAPS/" + agent.CapsPath + "0000/";
  188. if (eq != null)
  189. {
  190. OSD Item = EventQueueHelper.EnableSimulator(realHandle, endPoint);
  191. eq.Enqueue(Item, avatar.UUID);
  192. Item = EventQueueHelper.EstablishAgentCommunication(avatar.UUID, endPoint.ToString(), capsPath);
  193. eq.Enqueue(Item, avatar.UUID);
  194. }
  195. else
  196. {
  197. avatar.ControllingClient.InformClientOfNeighbour(realHandle, endPoint);
  198. // TODO: make Event Queue disablable!
  199. }
  200. }
  201. else
  202. {
  203. // child agent already there
  204. agent.CapsPath = avatar.Scene.GetChildSeed(avatar.UUID, reg.RegionHandle);
  205. capsPath = "http://" + reg.ExternalHostName + ":" + reg.HttpPort
  206. + "/CAPS/" + agent.CapsPath + "0000/";
  207. }
  208. m_commsProvider.InterRegion.ExpectAvatarCrossing(reg.RegionHandle, avatar.ControllingClient.AgentId,
  209. position, false);
  210. //if (!m_commsProvider.InterRegion.ExpectAvatarCrossing(reg.RegionHandle, avatar.ControllingClient.AgentId,
  211. // position, false))
  212. //{
  213. // avatar.ControllingClient.SendTeleportFailed("Problem with destination.");
  214. // // We should close that agent we just created over at destination...
  215. // List<ulong> lst = new List<ulong>();
  216. // lst.Add(realHandle);
  217. // SendCloseChildAgentAsync(avatar.UUID, lst);
  218. // return;
  219. //}
  220. Thread.Sleep(2000);
  221. m_log.DebugFormat(
  222. "[CAPS]: Sending new CAPS seed url {0} to client {1}", agent.CapsPath, avatar.UUID);
  223. ///
  224. /// Hypergrid mod: realHandle instead of reg.RegionHandle
  225. ///
  226. ///
  227. if (eq != null)
  228. {
  229. OSD Item = EventQueueHelper.TeleportFinishEvent(realHandle, 13, endPoint,
  230. 4, teleportFlags, capsPath, avatar.UUID);
  231. eq.Enqueue(Item, avatar.UUID);
  232. }
  233. else
  234. {
  235. avatar.ControllingClient.SendRegionTeleport(realHandle, 13, endPoint, 4,
  236. teleportFlags, capsPath);
  237. }
  238. ///
  239. /// Hypergrid mod stop
  240. ///
  241. avatar.MakeChildAgent();
  242. Thread.Sleep(3000);
  243. avatar.CrossAttachmentsIntoNewRegion(reg.RegionHandle, true);
  244. if (KiPrimitive != null)
  245. {
  246. KiPrimitive(avatar.LocalId);
  247. }
  248. //avatar.Close();
  249. // Finally, let's close this previously-known-as-root agent, when the jump is outside the view zone
  250. ///
  251. /// Hypergrid mod: extra check for isHyperLink
  252. ///
  253. if (Util.IsOutsideView(oldRegionX, newRegionX, oldRegionY, newRegionY) || isHyperLink)
  254. {
  255. CloseConnection(avatar.UUID);
  256. }
  257. // if (teleport success) // seems to be always success here
  258. // the user may change their profile information in other region,
  259. // so the userinfo in UserProfileCache is not reliable any more, delete it
  260. if (avatar.Scene.NeedSceneCacheClear(avatar.UUID) || isHyperLink)
  261. {
  262. m_commsProvider.UserProfileCacheService.RemoveUser(avatar.UUID);
  263. m_log.InfoFormat("[HGSceneCommService]: User {0} is going to another region, profile cache removed", avatar.UUID);
  264. }
  265. }
  266. else
  267. {
  268. avatar.ControllingClient.SendTeleportFailed("Remote Region appears to be down");
  269. }
  270. }
  271. else
  272. {
  273. // TP to a place that doesn't exist (anymore)
  274. // Inform the viewer about that
  275. avatar.ControllingClient.SendTeleportFailed("The region you tried to teleport to doesn't exist anymore");
  276. // and set the map-tile to '(Offline)'
  277. uint regX, regY;
  278. Utils.LongToUInts(regionHandle, out regX, out regY);
  279. MapBlockData block = new MapBlockData();
  280. block.X = (ushort)(regX / Constants.RegionSize);
  281. block.Y = (ushort)(regY / Constants.RegionSize);
  282. block.Access = 254; // == not there
  283. List<MapBlockData> blocks = new List<MapBlockData>();
  284. blocks.Add(block);
  285. avatar.ControllingClient.SendMapBlock(blocks, 0);
  286. }
  287. }
  288. }
  289. }
  290. }