HGSceneCommunicationService.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 log4net;
  34. using OpenMetaverse;
  35. using OSD = OpenMetaverse.StructuredData.OSD;
  36. using OpenSim.Framework;
  37. using OpenSim.Framework.Communications;
  38. using OpenSim.Framework.Communications.Cache;
  39. using OpenSim.Framework.Communications.Capabilities;
  40. using OpenSim.Region.Environment.Scenes;
  41. using OpenSim.Region.Environment;
  42. using OpenSim.Region.Interfaces;
  43. namespace OpenSim.Region.Environment.Scenes.Hypergrid
  44. {
  45. public class HGSceneCommunicationService : SceneCommunicationService
  46. {
  47. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  48. public readonly IHyperlink m_hg;
  49. public HGSceneCommunicationService(CommunicationsManager commsMan, IHyperlink hg) : base(commsMan)
  50. {
  51. m_hg = hg;
  52. }
  53. /// <summary>
  54. /// Try to teleport an agent to a new region.
  55. /// </summary>
  56. /// <param name="remoteClient"></param>
  57. /// <param name="RegionHandle"></param>
  58. /// <param name="position"></param>
  59. /// <param name="lookAt"></param>
  60. /// <param name="flags"></param>
  61. public override void RequestTeleportToLocation(ScenePresence avatar, ulong regionHandle, Vector3 position,
  62. Vector3 lookAt, uint teleportFlags)
  63. {
  64. if (!avatar.Scene.Permissions.CanTeleport(avatar.UUID))
  65. return;
  66. bool destRegionUp = true;
  67. IEventQueue eq = avatar.Scene.RequestModuleInterface<IEventQueue>();
  68. if (regionHandle == m_regionInfo.RegionHandle)
  69. {
  70. // Teleport within the same region
  71. if (position.X < 0 || position.X > Constants.RegionSize || position.Y < 0 || position.Y > Constants.RegionSize || position.Z < 0)
  72. {
  73. Vector3 emergencyPos = new Vector3(128, 128, 128);
  74. m_log.WarnFormat(
  75. "[HGSceneCommService]: RequestTeleportToLocation() was given an illegal position of {0} for avatar {1}, {2}. Substituting {3}",
  76. position, avatar.Name, avatar.UUID, emergencyPos);
  77. position = emergencyPos;
  78. }
  79. // TODO: Get proper AVG Height
  80. float localAVHeight = 1.56f;
  81. float posZLimit = (float)avatar.Scene.GetLandHeight((int)position.X, (int)position.Y);
  82. float newPosZ = posZLimit + localAVHeight;
  83. if (posZLimit >= (position.Z - (localAVHeight / 2)) && !(Single.IsInfinity(newPosZ) || Single.IsNaN(newPosZ)))
  84. {
  85. position.Z = newPosZ;
  86. }
  87. // Only send this if the event queue is null
  88. if (eq == null)
  89. avatar.ControllingClient.SendTeleportLocationStart();
  90. avatar.ControllingClient.SendLocalTeleport(position, lookAt, teleportFlags);
  91. avatar.Teleport(position);
  92. }
  93. else
  94. {
  95. RegionInfo reg = RequestNeighbouringRegionInfo(regionHandle);
  96. if (reg != null)
  97. {
  98. uint newRegionX = (uint)(reg.RegionHandle >> 40);
  99. uint newRegionY = (((uint)(reg.RegionHandle)) >> 8);
  100. uint oldRegionX = (uint)(m_regionInfo.RegionHandle >> 40);
  101. uint oldRegionY = (((uint)(m_regionInfo.RegionHandle)) >> 8);
  102. ///
  103. /// Hypergrid mod start
  104. ///
  105. ///
  106. bool isHyperLink = m_hg.IsHyperlinkRegion(reg.RegionHandle);
  107. bool isHomeUser = true;
  108. ulong realHandle = regionHandle;
  109. CachedUserInfo uinfo = m_commsProvider.UserProfileCacheService.GetUserDetails(avatar.UUID);
  110. if (uinfo != null)
  111. {
  112. isHomeUser = HGNetworkServersInfo.Singleton.IsLocalUser(uinfo.UserProfile);
  113. realHandle = m_hg.FindRegionHandle(regionHandle);
  114. Console.WriteLine("XXX ---- home user? " + isHomeUser + " --- hyperlink? " + isHyperLink + " --- real handle: " + realHandle.ToString());
  115. }
  116. ///
  117. /// Hypergrid mod stop
  118. ///
  119. ///
  120. if (eq == null)
  121. avatar.ControllingClient.SendTeleportLocationStart();
  122. // Let's do DNS resolution only once in this process, please!
  123. // This may be a costly operation. The reg.ExternalEndPoint field is not a passive field,
  124. // it's actually doing a lot of work.
  125. IPEndPoint endPoint = reg.ExternalEndPoint;
  126. if (endPoint.Address == null)
  127. {
  128. // Couldn't resolve the name. Can't TP, because the viewer wants IP addresses.
  129. destRegionUp = false;
  130. }
  131. if (destRegionUp)
  132. {
  133. // Fixing a bug where teleporting while sitting results in the avatar ending up removed from
  134. // both regions
  135. if (avatar.ParentID != (uint)0)
  136. avatar.StandUp();
  137. if (!avatar.ValidateAttachments())
  138. {
  139. avatar.ControllingClient.SendTeleportFailed("Inconsistent attachment state");
  140. return;
  141. }
  142. // the avatar.Close below will clear the child region list. We need this below for (possibly)
  143. // closing the child agents, so save it here (we need a copy as it is Clear()-ed).
  144. //List<ulong> childRegions = new List<ulong>(avatar.GetKnownRegionList());
  145. // Compared to ScenePresence.CrossToNewRegion(), there's no obvious code to handle a teleport
  146. // failure at this point (unlike a border crossing failure). So perhaps this can never fail
  147. // once we reach here...
  148. //avatar.Scene.RemoveCapsHandler(avatar.UUID);
  149. string capsPath = String.Empty;
  150. AgentCircuitData agentCircuit = avatar.ControllingClient.RequestClientInfo();
  151. agentCircuit.BaseFolder = UUID.Zero;
  152. agentCircuit.InventoryFolder = UUID.Zero;
  153. agentCircuit.startpos = position;
  154. agentCircuit.child = true;
  155. if (Util.IsOutsideView(oldRegionX, newRegionX, oldRegionY, newRegionY))
  156. {
  157. // brand new agent, let's create a new caps seed
  158. agentCircuit.CapsPath = CapsUtil.GetRandomCapsObjectPath();
  159. }
  160. //if (!m_commsProvider.InterRegion.InformRegionOfChildAgent(reg.RegionHandle, agentCircuit))
  161. if (!m_interregionCommsOut.SendCreateChildAgent(reg.RegionHandle, agentCircuit))
  162. {
  163. avatar.ControllingClient.SendTeleportFailed("Destination is not accepting teleports.");
  164. return;
  165. }
  166. // Let's close some agents
  167. if (isHyperLink) // close them all except this one
  168. {
  169. List<ulong> regions = new List<ulong>(avatar.KnownChildRegionHandles);
  170. regions.Remove(avatar.Scene.RegionInfo.RegionHandle);
  171. SendCloseChildAgentConnections(avatar.UUID, regions);
  172. }
  173. else // close just a few
  174. avatar.CloseChildAgents(newRegionX, newRegionY);
  175. if (Util.IsOutsideView(oldRegionX, newRegionX, oldRegionY, newRegionY) || isHyperLink)
  176. {
  177. capsPath
  178. = "http://"
  179. + reg.ExternalHostName
  180. + ":"
  181. + reg.HttpPort
  182. + CapsUtil.GetCapsSeedPath(agentCircuit.CapsPath);
  183. if (eq != null)
  184. {
  185. OSD Item = EventQueueHelper.EnableSimulator(realHandle, endPoint);
  186. eq.Enqueue(Item, avatar.UUID);
  187. // ES makes the client send a UseCircuitCode message to the destination,
  188. // which triggers a bunch of things there.
  189. // So let's wait
  190. Thread.Sleep(2000);
  191. Item = EventQueueHelper.EstablishAgentCommunication(avatar.UUID, endPoint.ToString(), capsPath);
  192. eq.Enqueue(Item, avatar.UUID);
  193. }
  194. else
  195. {
  196. avatar.ControllingClient.InformClientOfNeighbour(realHandle, endPoint);
  197. // TODO: make Event Queue disablable!
  198. }
  199. }
  200. else
  201. {
  202. // child agent already there
  203. agentCircuit.CapsPath = avatar.Scene.CapsModule.GetChildSeed(avatar.UUID, reg.RegionHandle);
  204. capsPath = "http://" + reg.ExternalHostName + ":" + reg.HttpPort
  205. + "/CAPS/" + agentCircuit.CapsPath + "0000/";
  206. }
  207. //m_commsProvider.InterRegion.ExpectAvatarCrossing(reg.RegionHandle, avatar.ControllingClient.AgentId,
  208. // position, false);
  209. //if (!m_commsProvider.InterRegion.ExpectAvatarCrossing(reg.RegionHandle, avatar.ControllingClient.AgentId,
  210. // position, false))
  211. //{
  212. // avatar.ControllingClient.SendTeleportFailed("Problem with destination.");
  213. // // We should close that agent we just created over at destination...
  214. // List<ulong> lst = new List<ulong>();
  215. // lst.Add(realHandle);
  216. // SendCloseChildAgentAsync(avatar.UUID, lst);
  217. // return;
  218. //}
  219. SetInTransit(avatar.UUID);
  220. // Let's send a full update of the agent. This is a synchronous call.
  221. AgentData agent = new AgentData();
  222. avatar.CopyTo(agent);
  223. agent.Position = position;
  224. agent.CallbackURI = "http://" + m_regionInfo.ExternalHostName + ":" + m_regionInfo.HttpPort +
  225. "/agent/" + avatar.UUID.ToString() + "/" + avatar.Scene.RegionInfo.RegionHandle.ToString() + "/release/";
  226. m_interregionCommsOut.SendChildAgentUpdate(reg.RegionHandle, agent);
  227. m_log.DebugFormat(
  228. "[CAPS]: Sending new CAPS seed url {0} to client {1}", agentCircuit.CapsPath, avatar.UUID);
  229. ///
  230. /// Hypergrid mod: realHandle instead of reg.RegionHandle
  231. ///
  232. ///
  233. if (eq != null)
  234. {
  235. OSD Item = EventQueueHelper.TeleportFinishEvent(realHandle, 13, endPoint,
  236. 4, teleportFlags, capsPath, avatar.UUID);
  237. eq.Enqueue(Item, avatar.UUID);
  238. }
  239. else
  240. {
  241. avatar.ControllingClient.SendRegionTeleport(realHandle, 13, endPoint, 4,
  242. teleportFlags, capsPath);
  243. }
  244. ///
  245. /// Hypergrid mod stop
  246. ///
  247. // TeleportFinish makes the client send CompleteMovementIntoRegion (at the destination), which
  248. // trigers a whole shebang of things there, including MakeRoot. So let's wait for confirmation
  249. // that the client contacted the destination before we send the attachments and close things here.
  250. if (!WaitForCallback(avatar.UUID))
  251. {
  252. // Client never contacted destination. Let's restore everything back
  253. avatar.ControllingClient.SendTeleportFailed("Problems connecting to destination.");
  254. ResetFromTransit(avatar.UUID);
  255. // Yikes! We should just have a ref to scene here.
  256. avatar.Scene.InformClientOfNeighbours(avatar);
  257. // Finally, kill the agent we just created at the destination.
  258. m_interregionCommsOut.SendCloseAgent(reg.RegionHandle, avatar.UUID);
  259. return;
  260. }
  261. // Can't go back from here
  262. if (KiPrimitive != null)
  263. {
  264. KiPrimitive(avatar.LocalId);
  265. }
  266. avatar.MakeChildAgent();
  267. // CrossAttachmentsIntoNewRegion is a synchronous call. We shouldn't need to wait after it
  268. avatar.CrossAttachmentsIntoNewRegion(reg.RegionHandle, true);
  269. // Finally, let's close this previously-known-as-root agent, when the jump is outside the view zone
  270. ///
  271. /// Hypergrid mod: extra check for isHyperLink
  272. ///
  273. if (Util.IsOutsideView(oldRegionX, newRegionX, oldRegionY, newRegionY) || isHyperLink)
  274. {
  275. Thread.Sleep(5000);
  276. avatar.Close();
  277. CloseConnection(avatar.UUID);
  278. }
  279. // if (teleport success) // seems to be always success here
  280. // the user may change their profile information in other region,
  281. // so the userinfo in UserProfileCache is not reliable any more, delete it
  282. if (avatar.Scene.NeedSceneCacheClear(avatar.UUID) || isHyperLink)
  283. {
  284. m_commsProvider.UserProfileCacheService.RemoveUser(avatar.UUID);
  285. m_log.DebugFormat(
  286. "[HGSceneCommService]: User {0} is going to another region, profile cache removed",
  287. avatar.UUID);
  288. }
  289. }
  290. else
  291. {
  292. avatar.ControllingClient.SendTeleportFailed("Remote Region appears to be down");
  293. }
  294. }
  295. else
  296. {
  297. // TP to a place that doesn't exist (anymore)
  298. // Inform the viewer about that
  299. avatar.ControllingClient.SendTeleportFailed("The region you tried to teleport to doesn't exist anymore");
  300. // and set the map-tile to '(Offline)'
  301. uint regX, regY;
  302. Utils.LongToUInts(regionHandle, out regX, out regY);
  303. MapBlockData block = new MapBlockData();
  304. block.X = (ushort)(regX / Constants.RegionSize);
  305. block.Y = (ushort)(regY / Constants.RegionSize);
  306. block.Access = 254; // == not there
  307. List<MapBlockData> blocks = new List<MapBlockData>();
  308. blocks.Add(block);
  309. avatar.ControllingClient.SendMapBlock(blocks, 0);
  310. }
  311. }
  312. }
  313. }
  314. }