XferModule.cs 9.4 KB

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