1
0

HGSceneCommunicationService.cs 19 KB

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