XferModule.cs 7.8 KB

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