HGSceneCommunicationService.cs 19 KB

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