XferModule.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 System.Reflection;
  30. using Nini.Config;
  31. using log4net;
  32. using OpenMetaverse;
  33. using OpenSim.Framework;
  34. using OpenSim.Region.Framework.Interfaces;
  35. using OpenSim.Region.Framework.Scenes;
  36. namespace OpenSim.Region.CoreModules.Agent.Xfer
  37. {
  38. public class XferModule : IRegionModule, IXfer
  39. {
  40. private Scene m_scene;
  41. private Dictionary<string, FileData> NewFiles = new Dictionary<string, FileData>();
  42. private Dictionary<ulong, XferDownLoad> Transfers = new Dictionary<ulong, XferDownLoad>();
  43. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. public struct XferRequest
  45. {
  46. public IClientAPI remoteClient;
  47. public ulong xferID;
  48. public string fileName;
  49. public DateTime timeStamp;
  50. }
  51. private class FileData
  52. {
  53. public byte[] Data;
  54. public int Count;
  55. }
  56. #region IRegionModule Members
  57. public void Initialise(Scene scene, IConfigSource config)
  58. {
  59. m_scene = scene;
  60. m_scene.EventManager.OnNewClient += NewClient;
  61. m_scene.RegisterModuleInterface<IXfer>(this);
  62. }
  63. public void PostInitialise()
  64. {
  65. }
  66. public void Close()
  67. {
  68. }
  69. public string Name
  70. {
  71. get { return "XferModule"; }
  72. }
  73. public bool IsSharedModule
  74. {
  75. get { return false; }
  76. }
  77. #endregion
  78. #region IXfer Members
  79. /// <summary>
  80. /// Let the Xfer module know about a file that the client is about to request.
  81. /// Caller is responsible for making sure that the file is here before
  82. /// the client starts the XferRequest.
  83. /// </summary>
  84. /// <param name="fileName"></param>
  85. /// <param name="data"></param>
  86. /// <returns></returns>
  87. public bool AddNewFile(string fileName, byte[] data)
  88. {
  89. lock (NewFiles)
  90. {
  91. if (NewFiles.ContainsKey(fileName))
  92. {
  93. NewFiles[fileName].Count++;
  94. NewFiles[fileName].Data = data;
  95. }
  96. else
  97. {
  98. FileData fd = new FileData();
  99. fd.Count = 1;
  100. fd.Data = data;
  101. NewFiles.Add(fileName, fd);
  102. }
  103. }
  104. return true;
  105. }
  106. #endregion
  107. public void NewClient(IClientAPI client)
  108. {
  109. client.OnRequestXfer += RequestXfer;
  110. client.OnConfirmXfer += AckPacket;
  111. client.OnAbortXfer += AbortXfer;
  112. }
  113. /// <summary>
  114. ///
  115. /// </summary>
  116. /// <param name="remoteClient"></param>
  117. /// <param name="xferID"></param>
  118. /// <param name="fileName"></param>
  119. public void RequestXfer(IClientAPI remoteClient, ulong xferID, string fileName)
  120. {
  121. lock (NewFiles)
  122. {
  123. if (NewFiles.ContainsKey(fileName))
  124. {
  125. if (!Transfers.ContainsKey(xferID))
  126. {
  127. byte[] fileData = NewFiles[fileName].Data;
  128. XferDownLoad transaction = new XferDownLoad(fileName, fileData, xferID, remoteClient);
  129. Transfers.Add(xferID, transaction);
  130. if (transaction.StartSend())
  131. RemoveXferData(xferID);
  132. // The transaction for this file is either complete or on its way
  133. RemoveOrDecrement(fileName);
  134. }
  135. }
  136. else
  137. m_log.WarnFormat("[Xfer]: {0} not found", fileName);
  138. }
  139. }
  140. public void AckPacket(IClientAPI remoteClient, ulong xferID, uint packet)
  141. {
  142. lock (NewFiles) // This is actually to lock Transfers
  143. {
  144. if (Transfers.ContainsKey(xferID))
  145. {
  146. XferDownLoad dl = Transfers[xferID];
  147. if (Transfers[xferID].AckPacket(packet))
  148. {
  149. RemoveXferData(xferID);
  150. RemoveOrDecrement(dl.FileName);
  151. }
  152. }
  153. }
  154. }
  155. private void RemoveXferData(ulong xferID)
  156. {
  157. // NewFiles must be locked!
  158. if (Transfers.ContainsKey(xferID))
  159. {
  160. XferModule.XferDownLoad xferItem = Transfers[xferID];
  161. //string filename = xferItem.FileName;
  162. Transfers.Remove(xferID);
  163. xferItem.Data = new byte[0]; // Clear the data
  164. xferItem.DataPointer = 0;
  165. }
  166. }
  167. public void AbortXfer(IClientAPI remoteClient, ulong xferID)
  168. {
  169. lock (NewFiles)
  170. {
  171. if (Transfers.ContainsKey(xferID))
  172. RemoveOrDecrement(Transfers[xferID].FileName);
  173. RemoveXferData(xferID);
  174. }
  175. }
  176. private void RemoveOrDecrement(string fileName)
  177. {
  178. // NewFiles must be locked
  179. if (NewFiles.ContainsKey(fileName))
  180. {
  181. if (NewFiles[fileName].Count == 1)
  182. NewFiles.Remove(fileName);
  183. else
  184. NewFiles[fileName].Count--;
  185. }
  186. }
  187. #region Nested type: XferDownLoad
  188. public class XferDownLoad
  189. {
  190. public IClientAPI Client;
  191. private bool complete;
  192. public byte[] Data = new byte[0];
  193. public int DataPointer = 0;
  194. public string FileName = String.Empty;
  195. public uint Packet = 0;
  196. public uint Serial = 1;
  197. public ulong XferID = 0;
  198. public XferDownLoad(string fileName, byte[] data, ulong xferID, IClientAPI client)
  199. {
  200. FileName = fileName;
  201. Data = data;
  202. XferID = xferID;
  203. Client = client;
  204. }
  205. public XferDownLoad()
  206. {
  207. }
  208. /// <summary>
  209. /// Start a transfer
  210. /// </summary>
  211. /// <returns>True if the transfer is complete, false if not</returns>
  212. public bool StartSend()
  213. {
  214. if (Data.Length < 1000)
  215. {
  216. // for now (testing) we only support files under 1000 bytes
  217. byte[] transferData = new byte[Data.Length + 4];
  218. Array.Copy(Utils.IntToBytes(Data.Length), 0, transferData, 0, 4);
  219. Array.Copy(Data, 0, transferData, 4, Data.Length);
  220. Client.SendXferPacket(XferID, 0 + 0x80000000, transferData);
  221. complete = true;
  222. }
  223. else
  224. {
  225. byte[] transferData = new byte[1000 + 4];
  226. Array.Copy(Utils.IntToBytes(Data.Length), 0, transferData, 0, 4);
  227. Array.Copy(Data, 0, transferData, 4, 1000);
  228. Client.SendXferPacket(XferID, 0, transferData);
  229. Packet++;
  230. DataPointer = 1000;
  231. }
  232. return complete;
  233. }
  234. /// <summary>
  235. /// Respond to an ack packet from the client
  236. /// </summary>
  237. /// <param name="packet"></param>
  238. /// <returns>True if the transfer is complete, false otherwise</returns>
  239. public bool AckPacket(uint packet)
  240. {
  241. if (!complete)
  242. {
  243. if ((Data.Length - DataPointer) > 1000)
  244. {
  245. byte[] transferData = new byte[1000];
  246. Array.Copy(Data, DataPointer, transferData, 0, 1000);
  247. Client.SendXferPacket(XferID, Packet, transferData);
  248. Packet++;
  249. DataPointer += 1000;
  250. }
  251. else
  252. {
  253. byte[] transferData = new byte[Data.Length - DataPointer];
  254. Array.Copy(Data, DataPointer, transferData, 0, Data.Length - DataPointer);
  255. uint endPacket = Packet |= (uint) 0x80000000;
  256. Client.SendXferPacket(XferID, endPacket, transferData);
  257. Packet++;
  258. DataPointer += (Data.Length - DataPointer);
  259. complete = true;
  260. }
  261. }
  262. return complete;
  263. }
  264. }
  265. #endregion
  266. }
  267. }