1
0

HGSceneCommunicationService.cs 18 KB

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