1
0

XferModule.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. */
  28. using System;
  29. using System.Collections.Generic;
  30. using libsecondlife;
  31. using Nini.Config;
  32. using OpenSim.Framework;
  33. using OpenSim.Region.Environment.Interfaces;
  34. using OpenSim.Region.Environment.Scenes;
  35. namespace OpenSim.Region.Environment.Modules
  36. {
  37. public class XferModule : IRegionModule, IXfer
  38. {
  39. public Dictionary<string, byte[]> NewFiles = new Dictionary<string, byte[]>();
  40. public Dictionary<ulong, XferDownLoad> Transfers = new Dictionary<ulong, XferDownLoad>();
  41. private Scene m_scene;
  42. public XferModule()
  43. {
  44. }
  45. public void Initialise(Scene scene, IConfigSource config)
  46. {
  47. m_scene = scene;
  48. m_scene.EventManager.OnNewClient += NewClient;
  49. m_scene.RegisterModuleInterface<IXfer>(this);
  50. }
  51. public void PostInitialise()
  52. {
  53. }
  54. public void Close()
  55. {
  56. }
  57. public string Name
  58. {
  59. get { return "XferModule"; }
  60. }
  61. public bool IsSharedModule
  62. {
  63. get { return false; }
  64. }
  65. public void NewClient(IClientAPI client)
  66. {
  67. client.OnRequestXfer += RequestXfer;
  68. client.OnConfirmXfer += AckPacket;
  69. }
  70. /// <summary>
  71. ///
  72. /// </summary>
  73. /// <param name="remoteClient"></param>
  74. /// <param name="xferID"></param>
  75. /// <param name="fileName"></param>
  76. public void RequestXfer(IClientAPI remoteClient, ulong xferID, string fileName)
  77. {
  78. lock (NewFiles)
  79. {
  80. if (NewFiles.ContainsKey(fileName))
  81. {
  82. if (!Transfers.ContainsKey(xferID))
  83. {
  84. byte[] fileData = NewFiles[fileName];
  85. XferDownLoad transaction = new XferDownLoad(fileName, fileData, xferID, remoteClient);
  86. Transfers.Add(xferID, transaction);
  87. NewFiles.Remove(fileName);
  88. if (transaction.StartSend())
  89. {
  90. Transfers.Remove(xferID);
  91. }
  92. }
  93. }
  94. }
  95. }
  96. public void AckPacket(IClientAPI remoteClient, ulong xferID, uint packet)
  97. {
  98. if (Transfers.ContainsKey(xferID))
  99. {
  100. if (Transfers[xferID].AckPacket(packet))
  101. {
  102. {
  103. Transfers.Remove(xferID);
  104. }
  105. }
  106. }
  107. }
  108. public bool AddNewFile(string fileName, byte[] data)
  109. {
  110. lock (NewFiles)
  111. {
  112. if (NewFiles.ContainsKey(fileName))
  113. {
  114. NewFiles[fileName] = data;
  115. }
  116. else
  117. {
  118. NewFiles.Add(fileName, data);
  119. }
  120. }
  121. return true;
  122. }
  123. public class XferDownLoad
  124. {
  125. public byte[] Data = new byte[0];
  126. public string FileName = String.Empty;
  127. public ulong XferID = 0;
  128. public int DataPointer = 0;
  129. public uint Packet = 0;
  130. public IClientAPI Client;
  131. public uint Serial = 1;
  132. private bool complete;
  133. public XferDownLoad(string fileName, byte[] data, ulong xferID, IClientAPI client)
  134. {
  135. FileName = fileName;
  136. Data = data;
  137. XferID = xferID;
  138. Client = client;
  139. }
  140. public XferDownLoad()
  141. {
  142. }
  143. /// <summary>
  144. /// Start a transfer
  145. /// </summary>
  146. /// <returns>True if the transfer is complete, false if not</returns>
  147. public bool StartSend()
  148. {
  149. if (Data.Length < 1000)
  150. {
  151. // for now (testing ) we only support files under 1000 bytes
  152. byte[] transferData = new byte[Data.Length + 4];
  153. Array.Copy(Helpers.IntToBytes(Data.Length), 0, transferData, 0, 4);
  154. Array.Copy(Data, 0, transferData, 4, Data.Length);
  155. Client.SendXferPacket(XferID, 0 + 0x80000000, transferData);
  156. complete = true;
  157. }
  158. else
  159. {
  160. byte[] transferData = new byte[1000 + 4];
  161. Array.Copy(Helpers.IntToBytes(Data.Length), 0, transferData, 0, 4);
  162. Array.Copy(Data, 0, transferData, 4, 1000);
  163. Client.SendXferPacket(XferID, 0, transferData);
  164. Packet++;
  165. DataPointer = 1000;
  166. }
  167. return complete;
  168. }
  169. /// <summary>
  170. /// Respond to an ack packet from the client
  171. /// </summary>
  172. /// <param name="packet"></param>
  173. /// <returns>True if the transfer is complete, false otherwise</returns>
  174. public bool AckPacket(uint packet)
  175. {
  176. if (!complete)
  177. {
  178. if ((Data.Length - DataPointer) > 1000)
  179. {
  180. byte[] transferData = new byte[1000];
  181. Array.Copy(Data, DataPointer, transferData, 0, 1000);
  182. Client.SendXferPacket(XferID, Packet, transferData);
  183. Packet++;
  184. DataPointer += 1000;
  185. }
  186. else
  187. {
  188. byte[] transferData = new byte[Data.Length - DataPointer];
  189. Array.Copy(Data, DataPointer, transferData, 0, Data.Length - DataPointer);
  190. uint endPacket = Packet |= (uint) 0x80000000;
  191. Client.SendXferPacket(XferID, endPacket, transferData);
  192. Packet++;
  193. DataPointer += (Data.Length - DataPointer);
  194. complete = true;
  195. }
  196. }
  197. return complete;
  198. }
  199. }
  200. }
  201. }