EventQueueHelper.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 OpenSim 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.Net;
  29. using OpenMetaverse;
  30. using OpenMetaverse.Packets;
  31. using OpenMetaverse.StructuredData;
  32. namespace OpenSim.Region.Environment
  33. {
  34. public class EventQueueHelper
  35. {
  36. private EventQueueHelper() {} // no construction possible, it's an utility class
  37. private static byte[] regionHandleToByteArray(ulong regionHandle)
  38. {
  39. // Reverse endianness of RegionHandle
  40. return new byte[]
  41. {
  42. (byte)((regionHandle >> 56) % 256),
  43. (byte)((regionHandle >> 48) % 256),
  44. (byte)((regionHandle >> 40) % 256),
  45. (byte)((regionHandle >> 32) % 256),
  46. (byte)((regionHandle >> 24) % 256),
  47. (byte)((regionHandle >> 16) % 256),
  48. (byte)((regionHandle >> 8) % 256),
  49. (byte)(regionHandle % 256)
  50. };
  51. }
  52. public static OSD buildEvent(string eventName, OSD eventBody)
  53. {
  54. OSDMap llsdEvent = new OSDMap(2);
  55. llsdEvent.Add("message", new OSDString(eventName));
  56. llsdEvent.Add("body", eventBody);
  57. return llsdEvent;
  58. }
  59. public static OSD EnableSimulator(ulong Handle, IPEndPoint endPoint)
  60. {
  61. OSDMap llsdSimInfo = new OSDMap(3);
  62. llsdSimInfo.Add("Handle", new OSDBinary(regionHandleToByteArray(Handle)));
  63. llsdSimInfo.Add("IP", new OSDBinary(endPoint.Address.GetAddressBytes()));
  64. llsdSimInfo.Add("Port", new OSDInteger(endPoint.Port));
  65. OSDArray arr = new OSDArray(1);
  66. arr.Add(llsdSimInfo);
  67. OSDMap llsdBody = new OSDMap(1);
  68. llsdBody.Add("SimulatorInfo", arr);
  69. return buildEvent("EnableSimulator", llsdBody);
  70. }
  71. public static OSD DisableSimulator(ulong Handle)
  72. {
  73. //OSDMap llsdSimInfo = new OSDMap(1);
  74. //llsdSimInfo.Add("Handle", new OSDBinary(regionHandleToByteArray(Handle)));
  75. //OSDArray arr = new OSDArray(1);
  76. //arr.Add(llsdSimInfo);
  77. OSDMap llsdBody = new OSDMap(0);
  78. //llsdBody.Add("SimulatorInfo", arr);
  79. return buildEvent("DisableSimulator", llsdBody);
  80. }
  81. public static OSD CrossRegion(ulong Handle, Vector3 pos, Vector3 lookAt,
  82. IPEndPoint newRegionExternalEndPoint,
  83. string capsURL, UUID AgentID, UUID SessionID)
  84. {
  85. OSDArray LookAtArr = new OSDArray(3);
  86. LookAtArr.Add(OSD.FromReal(lookAt.X));
  87. LookAtArr.Add(OSD.FromReal(lookAt.Y));
  88. LookAtArr.Add(OSD.FromReal(lookAt.Z));
  89. OSDArray PositionArr = new OSDArray(3);
  90. PositionArr.Add(OSD.FromReal(pos.X));
  91. PositionArr.Add(OSD.FromReal(pos.Y));
  92. PositionArr.Add(OSD.FromReal(pos.Z));
  93. OSDMap InfoMap = new OSDMap(2);
  94. InfoMap.Add("LookAt", LookAtArr);
  95. InfoMap.Add("Position", PositionArr);
  96. OSDArray InfoArr = new OSDArray(1);
  97. InfoArr.Add(InfoMap);
  98. OSDMap AgentDataMap = new OSDMap(2);
  99. AgentDataMap.Add("AgentID", OSD.FromUUID(AgentID));
  100. AgentDataMap.Add("SessionID", OSD.FromUUID(SessionID));
  101. OSDArray AgentDataArr = new OSDArray(1);
  102. AgentDataArr.Add(AgentDataMap);
  103. OSDMap RegionDataMap = new OSDMap(4);
  104. RegionDataMap.Add("RegionHandle", OSD.FromBinary(regionHandleToByteArray(Handle)));
  105. RegionDataMap.Add("SeedCapability", OSD.FromString(capsURL));
  106. RegionDataMap.Add("SimIP", OSD.FromBinary(newRegionExternalEndPoint.Address.GetAddressBytes()));
  107. RegionDataMap.Add("SimPort", OSD.FromInteger(newRegionExternalEndPoint.Port));
  108. OSDArray RegionDataArr = new OSDArray(1);
  109. RegionDataArr.Add(RegionDataMap);
  110. OSDMap llsdBody = new OSDMap(3);
  111. llsdBody.Add("Info", InfoArr);
  112. llsdBody.Add("AgentData", AgentDataArr);
  113. llsdBody.Add("RegionData", RegionDataArr);
  114. return buildEvent("CrossedRegion", llsdBody);
  115. }
  116. public static OSD TeleportFinishEvent(
  117. ulong regionHandle, byte simAccess, IPEndPoint regionExternalEndPoint,
  118. uint locationID, uint flags, string capsURL, UUID AgentID)
  119. {
  120. OSDMap info = new OSDMap();
  121. info.Add("AgentID", OSD.FromUUID(AgentID));
  122. info.Add("LocationID", OSD.FromInteger(4)); // TODO what is this?
  123. info.Add("RegionHandle", OSD.FromBinary(regionHandleToByteArray(regionHandle)));
  124. info.Add("SeedCapability", OSD.FromString(capsURL));
  125. info.Add("SimAccess", OSD.FromInteger(simAccess));
  126. info.Add("SimIP", OSD.FromBinary(regionExternalEndPoint.Address.GetAddressBytes()));
  127. info.Add("SimPort", OSD.FromInteger(regionExternalEndPoint.Port));
  128. info.Add("TeleportFlags", OSD.FromBinary(1L << 4)); // AgentManager.TeleportFlags.ViaLocation
  129. OSDArray infoArr = new OSDArray();
  130. infoArr.Add(info);
  131. OSDMap body = new OSDMap();
  132. body.Add("Info", infoArr);
  133. return buildEvent("TeleportFinish", body);
  134. }
  135. public static OSD ScriptRunningReplyEvent(UUID objectID, UUID itemID, bool running, bool mono)
  136. {
  137. OSDMap script = new OSDMap();
  138. script.Add("ObjectID", OSD.FromUUID(objectID));
  139. script.Add("ItemID", OSD.FromUUID(itemID));
  140. script.Add("Running", OSD.FromBoolean(running));
  141. script.Add("Mono", OSD.FromBoolean(mono));
  142. OSDArray scriptArr = new OSDArray();
  143. scriptArr.Add(script);
  144. OSDMap body = new OSDMap();
  145. body.Add("Script", scriptArr);
  146. return buildEvent("ScriptRunningReply", body);
  147. }
  148. public static OSD EstablishAgentCommunication(UUID agentID, string simIpAndPort, string seedcap)
  149. {
  150. OSDMap body = new OSDMap(3);
  151. body.Add("agent-id", new OSDUUID(agentID));
  152. body.Add("sim-ip-and-port", new OSDString(simIpAndPort));
  153. body.Add("seed-capability", new OSDString(seedcap));
  154. return buildEvent("EstablishAgentCommunication", body);
  155. }
  156. public static OSD KeepAliveEvent()
  157. {
  158. return buildEvent("FAKEEVENT", new OSDMap());
  159. }
  160. public static OSD AgentParams(UUID agentID, bool checkEstate, int godLevel, bool limitedToEstate)
  161. {
  162. OSDMap body = new OSDMap(4);
  163. body.Add("agent_id", new OSDUUID(agentID));
  164. body.Add("check_estate", new OSDInteger(checkEstate ? 1 : 0));
  165. body.Add("god_level", new OSDInteger(godLevel));
  166. body.Add("limited_to_estate", new OSDInteger(limitedToEstate ? 1 : 0));
  167. return body;
  168. }
  169. public static OSD InstantMessageParams(UUID fromAgent, string message, UUID toAgent,
  170. string fromName, byte dialog, uint timeStamp, bool offline, int parentEstateID,
  171. Vector3 position, uint ttl, UUID transactionID, bool fromGroup, byte[] binaryBucket)
  172. {
  173. OSDMap messageParams = new OSDMap(15);
  174. messageParams.Add("type", new OSDInteger((int)dialog));
  175. OSDArray positionArray = new OSDArray(3);
  176. positionArray.Add(OSD.FromReal(position.X));
  177. positionArray.Add(OSD.FromReal(position.Y));
  178. positionArray.Add(OSD.FromReal(position.Z));
  179. messageParams.Add("position", positionArray);
  180. messageParams.Add("region_id", new OSDUUID(UUID.Zero));
  181. messageParams.Add("to_id", new OSDUUID(toAgent));
  182. messageParams.Add("source", new OSDInteger(0));
  183. OSDMap data = new OSDMap(1);
  184. data.Add("binary_bucket", OSD.FromBinary(binaryBucket));
  185. messageParams.Add("data", data);
  186. messageParams.Add("message", new OSDString(message));
  187. messageParams.Add("id", new OSDUUID(transactionID));
  188. messageParams.Add("from_name", new OSDString(fromName));
  189. messageParams.Add("timestamp", new OSDInteger((int)timeStamp));
  190. messageParams.Add("offline", new OSDInteger(offline ? 1 : 0));
  191. messageParams.Add("parent_estate_id", new OSDInteger(parentEstateID));
  192. messageParams.Add("ttl", new OSDInteger((int)ttl));
  193. messageParams.Add("from_id", new OSDUUID(fromAgent));
  194. messageParams.Add("from_group", new OSDInteger(fromGroup ? 1 : 0));
  195. return messageParams;
  196. }
  197. public static OSD InstantMessage(UUID fromAgent, string message, UUID toAgent,
  198. string fromName, byte dialog, uint timeStamp, bool offline, int parentEstateID,
  199. Vector3 position, uint ttl, UUID transactionID, bool fromGroup, byte[] binaryBucket,
  200. bool checkEstate, int godLevel, bool limitedToEstate)
  201. {
  202. OSDMap im = new OSDMap(2);
  203. im.Add("message_params", InstantMessageParams(fromAgent, message, toAgent,
  204. fromName, dialog, timeStamp, offline, parentEstateID,
  205. position, ttl, transactionID, fromGroup, binaryBucket));
  206. im.Add("agent_params", AgentParams(fromAgent, checkEstate, godLevel, limitedToEstate));
  207. return im;
  208. }
  209. public static OSD ChatterboxInvitation(UUID sessionID, string sessionName,
  210. UUID fromAgent, string message, UUID toAgent, string fromName, byte dialog,
  211. uint timeStamp, bool offline, int parentEstateID, Vector3 position,
  212. uint ttl, UUID transactionID, bool fromGroup, byte[] binaryBucket)
  213. {
  214. OSDMap body = new OSDMap(5);
  215. body.Add("session_id", new OSDUUID(sessionID));
  216. body.Add("from_name", new OSDString(fromName));
  217. body.Add("session_name", new OSDString(sessionName));
  218. body.Add("from_id", new OSDUUID(fromAgent));
  219. body.Add("instantmessage", InstantMessage(fromAgent, message, toAgent,
  220. fromName, dialog, timeStamp, offline, parentEstateID, position,
  221. ttl, transactionID, fromGroup, binaryBucket, true, 0, true));
  222. OSDMap chatterboxInvitation = new OSDMap(2);
  223. chatterboxInvitation.Add("message", new OSDString("ChatterBoxInvitation"));
  224. chatterboxInvitation.Add("body", body);
  225. return chatterboxInvitation;
  226. }
  227. }
  228. }