XferModule.cs 7.8 KB

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