1
0

LLFileTransfer.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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.Text;
  30. using System.Xml;
  31. using OpenMetaverse;
  32. using OpenMetaverse.Packets;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Client;
  35. using OpenSim.Framework.Communications.Cache;
  36. using OpenSim.Framework.Statistics;
  37. using OpenSim.Region.Interfaces;
  38. using OpenSim.Region.Environment.Scenes;
  39. namespace OpenSim.Region.ClientStack.LindenUDP
  40. {
  41. /// <summary>
  42. /// A work in progress, to contain the SL specific file transfer code that is currently in various region modules
  43. /// This file currently contains multiple classes that need to be split out into their own files.
  44. /// </summary>
  45. public class LLFileTransfer : IClientFileTransfer
  46. {
  47. protected IClientAPI m_clientAPI;
  48. /// Dictionary of handlers for uploading files from client
  49. /// TODO: Need to add cleanup code to remove handlers that have completed their upload
  50. protected Dictionary<ulong, XferUploadHandler> m_uploadHandlers;
  51. protected object m_uploadHandlersLock = new object();
  52. /// <summary>
  53. /// Dictionary of files ready to be sent to clients
  54. /// </summary>
  55. protected static Dictionary<string, byte[]> m_files;
  56. /// <summary>
  57. /// Dictionary of Download Transfers in progess
  58. /// </summary>
  59. protected Dictionary<ulong, XferDownloadHandler> m_downloadHandlers = new Dictionary<ulong, XferDownloadHandler>();
  60. public LLFileTransfer(IClientAPI clientAPI)
  61. {
  62. m_uploadHandlers = new Dictionary<ulong, XferUploadHandler>();
  63. m_clientAPI = clientAPI;
  64. m_clientAPI.OnXferReceive += XferReceive;
  65. m_clientAPI.OnAbortXfer += AbortXferUploadHandler;
  66. }
  67. public void Close()
  68. {
  69. if (m_clientAPI != null)
  70. {
  71. m_clientAPI.OnXferReceive -= XferReceive;
  72. m_clientAPI.OnAbortXfer -= AbortXferUploadHandler;
  73. m_clientAPI = null;
  74. }
  75. }
  76. #region Upload Handling
  77. public bool RequestUpload(string clientFileName, UploadComplete uploadCompleteCallback, UploadAborted abortCallback)
  78. {
  79. if ((String.IsNullOrEmpty(clientFileName)) || (uploadCompleteCallback == null))
  80. {
  81. return false;
  82. }
  83. XferUploadHandler uploader = new XferUploadHandler(m_clientAPI, clientFileName);
  84. return StartUpload(uploader, uploadCompleteCallback, abortCallback);
  85. }
  86. public bool RequestUpload(UUID fileID, UploadComplete uploadCompleteCallback, UploadAborted abortCallback)
  87. {
  88. if ((fileID == UUID.Zero) || (uploadCompleteCallback == null))
  89. {
  90. return false;
  91. }
  92. XferUploadHandler uploader = new XferUploadHandler(m_clientAPI, fileID);
  93. return StartUpload(uploader, uploadCompleteCallback, abortCallback);
  94. }
  95. private bool StartUpload(XferUploadHandler uploader, UploadComplete uploadCompleteCallback, UploadAborted abortCallback)
  96. {
  97. uploader.UploadDone += uploadCompleteCallback;
  98. uploader.UploadDone += RemoveXferUploadHandler;
  99. if (abortCallback != null)
  100. {
  101. uploader.UploadAborted += abortCallback;
  102. }
  103. lock (m_uploadHandlersLock)
  104. {
  105. if (!m_uploadHandlers.ContainsKey(uploader.XferID))
  106. {
  107. m_uploadHandlers.Add(uploader.XferID, uploader);
  108. uploader.RequestStartXfer(m_clientAPI);
  109. return true;
  110. }
  111. else
  112. {
  113. // something went wrong with the xferID allocation
  114. uploader.UploadDone -= uploadCompleteCallback;
  115. uploader.UploadDone -= RemoveXferUploadHandler;
  116. if (abortCallback != null)
  117. {
  118. uploader.UploadAborted -= abortCallback;
  119. }
  120. return false;
  121. }
  122. }
  123. }
  124. protected void AbortXferUploadHandler(IClientAPI remoteClient, ulong xferID)
  125. {
  126. lock (m_uploadHandlersLock)
  127. {
  128. if (m_uploadHandlers.ContainsKey(xferID))
  129. {
  130. m_uploadHandlers[xferID].AbortUpload(remoteClient);
  131. m_uploadHandlers.Remove(xferID);
  132. }
  133. }
  134. }
  135. protected void XferReceive(IClientAPI remoteClient, ulong xferID, uint packetID, byte[] data)
  136. {
  137. lock (m_uploadHandlersLock)
  138. {
  139. if (m_uploadHandlers.ContainsKey(xferID))
  140. {
  141. m_uploadHandlers[xferID].XferReceive(remoteClient, xferID, packetID, data);
  142. }
  143. }
  144. }
  145. protected void RemoveXferUploadHandler(string filename, UUID fileID, ulong transferID, byte[] fileData, IClientAPI remoteClient)
  146. {
  147. }
  148. #endregion
  149. }
  150. public class XferUploadHandler
  151. {
  152. private AssetBase m_asset;
  153. public event UploadComplete UploadDone;
  154. public event UploadAborted UploadAborted;
  155. private sbyte type = 0;
  156. public ulong mXferID;
  157. private UploadComplete handlerUploadDone;
  158. private UploadAborted handlerAbort;
  159. private bool m_complete = false;
  160. public bool UploadComplete
  161. {
  162. get { return m_complete; }
  163. }
  164. public XferUploadHandler(IClientAPI pRemoteClient, string pClientFilename)
  165. {
  166. Initialise(UUID.Zero, pClientFilename);
  167. }
  168. public XferUploadHandler(IClientAPI pRemoteClient, UUID fileID)
  169. {
  170. Initialise(fileID, String.Empty);
  171. }
  172. private void Initialise(UUID fileID, string fileName)
  173. {
  174. m_asset = new AssetBase();
  175. m_asset.FullID = fileID;
  176. m_asset.Type = type;
  177. m_asset.Data = new byte[0];
  178. m_asset.Name = fileName;
  179. m_asset.Description = "empty";
  180. m_asset.Local = true;
  181. m_asset.Temporary = true;
  182. mXferID = Util.GetNextXferID();
  183. }
  184. public ulong XferID
  185. {
  186. get { return mXferID; }
  187. }
  188. public void RequestStartXfer(IClientAPI pRemoteClient)
  189. {
  190. if (!String.IsNullOrEmpty(m_asset.Name))
  191. {
  192. pRemoteClient.SendXferRequest(mXferID, m_asset.Type, m_asset.FullID, 0, Utils.StringToBytes(m_asset.Name));
  193. }
  194. else
  195. {
  196. pRemoteClient.SendXferRequest(mXferID, m_asset.Type, m_asset.FullID, 0, new byte[0]);
  197. }
  198. }
  199. /// <summary>
  200. /// Process transfer data received from the client.
  201. /// </summary>
  202. /// <param name="xferID"></param>
  203. /// <param name="packetID"></param>
  204. /// <param name="data"></param>
  205. public void XferReceive(IClientAPI remoteClient, ulong xferID, uint packetID, byte[] data)
  206. {
  207. if (mXferID == xferID)
  208. {
  209. if (m_asset.Data.Length > 1)
  210. {
  211. byte[] destinationArray = new byte[m_asset.Data.Length + data.Length];
  212. Array.Copy(m_asset.Data, 0, destinationArray, 0, m_asset.Data.Length);
  213. Array.Copy(data, 0, destinationArray, m_asset.Data.Length, data.Length);
  214. m_asset.Data = destinationArray;
  215. }
  216. else
  217. {
  218. byte[] buffer2 = new byte[data.Length - 4];
  219. Array.Copy(data, 4, buffer2, 0, data.Length - 4);
  220. m_asset.Data = buffer2;
  221. }
  222. remoteClient.SendConfirmXfer(xferID, packetID);
  223. if ((packetID & 0x80000000) != 0)
  224. {
  225. SendCompleteMessage(remoteClient);
  226. }
  227. }
  228. }
  229. protected void SendCompleteMessage(IClientAPI remoteClient)
  230. {
  231. m_complete = true;
  232. handlerUploadDone = UploadDone;
  233. if (handlerUploadDone != null)
  234. {
  235. handlerUploadDone(m_asset.Name, m_asset.FullID, mXferID, m_asset.Data, remoteClient);
  236. }
  237. }
  238. public void AbortUpload(IClientAPI remoteClient)
  239. {
  240. handlerAbort = UploadAborted;
  241. if (handlerAbort != null)
  242. {
  243. handlerAbort(m_asset.Name, m_asset.FullID, mXferID, remoteClient);
  244. }
  245. }
  246. }
  247. public class XferDownloadHandler
  248. {
  249. public IClientAPI Client;
  250. private bool complete;
  251. public byte[] Data = new byte[0];
  252. public int DataPointer = 0;
  253. public string FileName = String.Empty;
  254. public uint Packet = 0;
  255. public uint Serial = 1;
  256. public ulong XferID = 0;
  257. public XferDownloadHandler(string fileName, byte[] data, ulong xferID, IClientAPI client)
  258. {
  259. FileName = fileName;
  260. Data = data;
  261. XferID = xferID;
  262. Client = client;
  263. }
  264. public XferDownloadHandler()
  265. {
  266. }
  267. /// <summary>
  268. /// Start a transfer
  269. /// </summary>
  270. /// <returns>True if the transfer is complete, false if not</returns>
  271. public bool StartSend()
  272. {
  273. if (Data.Length < 1000)
  274. {
  275. // for now (testing) we only support files under 1000 bytes
  276. byte[] transferData = new byte[Data.Length + 4];
  277. Array.Copy(Utils.IntToBytes(Data.Length), 0, transferData, 0, 4);
  278. Array.Copy(Data, 0, transferData, 4, Data.Length);
  279. Client.SendXferPacket(XferID, 0 + 0x80000000, transferData);
  280. complete = true;
  281. }
  282. else
  283. {
  284. byte[] transferData = new byte[1000 + 4];
  285. Array.Copy(Utils.IntToBytes(Data.Length), 0, transferData, 0, 4);
  286. Array.Copy(Data, 0, transferData, 4, 1000);
  287. Client.SendXferPacket(XferID, 0, transferData);
  288. Packet++;
  289. DataPointer = 1000;
  290. }
  291. return complete;
  292. }
  293. /// <summary>
  294. /// Respond to an ack packet from the client
  295. /// </summary>
  296. /// <param name="packet"></param>
  297. /// <returns>True if the transfer is complete, false otherwise</returns>
  298. public bool AckPacket(uint packet)
  299. {
  300. if (!complete)
  301. {
  302. if ((Data.Length - DataPointer) > 1000)
  303. {
  304. byte[] transferData = new byte[1000];
  305. Array.Copy(Data, DataPointer, transferData, 0, 1000);
  306. Client.SendXferPacket(XferID, Packet, transferData);
  307. Packet++;
  308. DataPointer += 1000;
  309. }
  310. else
  311. {
  312. byte[] transferData = new byte[Data.Length - DataPointer];
  313. Array.Copy(Data, DataPointer, transferData, 0, Data.Length - DataPointer);
  314. uint endPacket = Packet |= (uint)0x80000000;
  315. Client.SendXferPacket(XferID, endPacket, transferData);
  316. Packet++;
  317. DataPointer += (Data.Length - DataPointer);
  318. complete = true;
  319. }
  320. }
  321. return complete;
  322. }
  323. }
  324. }