LLFileTransfer.cs 13 KB

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