XferModule.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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.Agent.Xfer
  35. {
  36. public class XferModule : IRegionModule, IXfer
  37. {
  38. private Scene m_scene;
  39. public Dictionary<string, byte[]> NewFiles = new Dictionary<string, byte[]>();
  40. public Dictionary<ulong, XferDownLoad> Transfers = new Dictionary<ulong, XferDownLoad>();
  41. public XferModule()
  42. {
  43. }
  44. #region IRegionModule Members
  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. #endregion
  66. #region IXfer Members
  67. public bool AddNewFile(string fileName, byte[] data)
  68. {
  69. lock (NewFiles)
  70. {
  71. if (NewFiles.ContainsKey(fileName))
  72. {
  73. NewFiles[fileName] = data;
  74. }
  75. else
  76. {
  77. NewFiles.Add(fileName, data);
  78. }
  79. }
  80. return true;
  81. }
  82. #endregion
  83. public void NewClient(IClientAPI client)
  84. {
  85. client.OnRequestXfer += RequestXfer;
  86. client.OnConfirmXfer += AckPacket;
  87. }
  88. /// <summary>
  89. ///
  90. /// </summary>
  91. /// <param name="remoteClient"></param>
  92. /// <param name="xferID"></param>
  93. /// <param name="fileName"></param>
  94. public void RequestXfer(IClientAPI remoteClient, ulong xferID, string fileName)
  95. {
  96. lock (NewFiles)
  97. {
  98. if (NewFiles.ContainsKey(fileName))
  99. {
  100. if (!Transfers.ContainsKey(xferID))
  101. {
  102. byte[] fileData = NewFiles[fileName];
  103. XferDownLoad transaction = new XferDownLoad(fileName, fileData, xferID, remoteClient);
  104. Transfers.Add(xferID, transaction);
  105. NewFiles.Remove(fileName);
  106. if (transaction.StartSend())
  107. {
  108. Transfers.Remove(xferID);
  109. }
  110. }
  111. }
  112. }
  113. }
  114. public void AckPacket(IClientAPI remoteClient, ulong xferID, uint packet)
  115. {
  116. if (Transfers.ContainsKey(xferID))
  117. {
  118. if (Transfers[xferID].AckPacket(packet))
  119. {
  120. {
  121. Transfers.Remove(xferID);
  122. }
  123. }
  124. }
  125. }
  126. #region Nested type: XferDownLoad
  127. public class XferDownLoad
  128. {
  129. public IClientAPI Client;
  130. private bool complete;
  131. public byte[] Data = new byte[0];
  132. public int DataPointer = 0;
  133. public string FileName = String.Empty;
  134. public uint Packet = 0;
  135. public uint Serial = 1;
  136. public ulong XferID = 0;
  137. public XferDownLoad(string fileName, byte[] data, ulong xferID, IClientAPI client)
  138. {
  139. FileName = fileName;
  140. Data = data;
  141. XferID = xferID;
  142. Client = client;
  143. }
  144. public XferDownLoad()
  145. {
  146. }
  147. /// <summary>
  148. /// Start a transfer
  149. /// </summary>
  150. /// <returns>True if the transfer is complete, false if not</returns>
  151. public bool StartSend()
  152. {
  153. if (Data.Length < 1000)
  154. {
  155. // for now (testing ) we only support files under 1000 bytes
  156. byte[] transferData = new byte[Data.Length + 4];
  157. Array.Copy(Helpers.IntToBytes(Data.Length), 0, transferData, 0, 4);
  158. Array.Copy(Data, 0, transferData, 4, Data.Length);
  159. Client.SendXferPacket(XferID, 0 + 0x80000000, transferData);
  160. complete = true;
  161. }
  162. else
  163. {
  164. byte[] transferData = new byte[1000 + 4];
  165. Array.Copy(Helpers.IntToBytes(Data.Length), 0, transferData, 0, 4);
  166. Array.Copy(Data, 0, transferData, 4, 1000);
  167. Client.SendXferPacket(XferID, 0, transferData);
  168. Packet++;
  169. DataPointer = 1000;
  170. }
  171. return complete;
  172. }
  173. /// <summary>
  174. /// Respond to an ack packet from the client
  175. /// </summary>
  176. /// <param name="packet"></param>
  177. /// <returns>True if the transfer is complete, false otherwise</returns>
  178. public bool AckPacket(uint packet)
  179. {
  180. if (!complete)
  181. {
  182. if ((Data.Length - DataPointer) > 1000)
  183. {
  184. byte[] transferData = new byte[1000];
  185. Array.Copy(Data, DataPointer, transferData, 0, 1000);
  186. Client.SendXferPacket(XferID, Packet, transferData);
  187. Packet++;
  188. DataPointer += 1000;
  189. }
  190. else
  191. {
  192. byte[] transferData = new byte[Data.Length - DataPointer];
  193. Array.Copy(Data, DataPointer, transferData, 0, Data.Length - DataPointer);
  194. uint endPacket = Packet |= (uint) 0x80000000;
  195. Client.SendXferPacket(XferID, endPacket, transferData);
  196. Packet++;
  197. DataPointer += (Data.Length - DataPointer);
  198. complete = true;
  199. }
  200. }
  201. return complete;
  202. }
  203. }
  204. #endregion
  205. }
  206. }