LLFileTransfer.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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(fileID, fileName, type);
  167. m_asset.Data = new byte[0];
  168. m_asset.Description = "empty";
  169. m_asset.Local = true;
  170. m_asset.Temporary = true;
  171. mXferID = Util.GetNextXferID();
  172. }
  173. public ulong XferID
  174. {
  175. get { return mXferID; }
  176. }
  177. public void RequestStartXfer(IClientAPI pRemoteClient)
  178. {
  179. if (!String.IsNullOrEmpty(m_asset.Name))
  180. {
  181. pRemoteClient.SendXferRequest(mXferID, m_asset.Type, m_asset.FullID, 0, Utils.StringToBytes(m_asset.Name));
  182. }
  183. else
  184. {
  185. pRemoteClient.SendXferRequest(mXferID, m_asset.Type, m_asset.FullID, 0, new byte[0]);
  186. }
  187. }
  188. /// <summary>
  189. /// Process transfer data received from the client.
  190. /// </summary>
  191. /// <param name="xferID"></param>
  192. /// <param name="packetID"></param>
  193. /// <param name="data"></param>
  194. public void XferReceive(IClientAPI remoteClient, ulong xferID, uint packetID, byte[] data)
  195. {
  196. if (mXferID == xferID)
  197. {
  198. if (m_asset.Data.Length > 1)
  199. {
  200. byte[] destinationArray = new byte[m_asset.Data.Length + data.Length];
  201. Array.Copy(m_asset.Data, 0, destinationArray, 0, m_asset.Data.Length);
  202. Array.Copy(data, 0, destinationArray, m_asset.Data.Length, data.Length);
  203. m_asset.Data = destinationArray;
  204. }
  205. else
  206. {
  207. byte[] buffer2 = new byte[data.Length - 4];
  208. Array.Copy(data, 4, buffer2, 0, data.Length - 4);
  209. m_asset.Data = buffer2;
  210. }
  211. remoteClient.SendConfirmXfer(xferID, packetID);
  212. if ((packetID & 0x80000000) != 0)
  213. {
  214. SendCompleteMessage(remoteClient);
  215. }
  216. }
  217. }
  218. protected void SendCompleteMessage(IClientAPI remoteClient)
  219. {
  220. m_complete = true;
  221. handlerUploadDone = UploadDone;
  222. if (handlerUploadDone != null)
  223. {
  224. handlerUploadDone(m_asset.Name, m_asset.FullID, mXferID, m_asset.Data, remoteClient);
  225. }
  226. }
  227. public void AbortUpload(IClientAPI remoteClient)
  228. {
  229. handlerAbort = UploadAborted;
  230. if (handlerAbort != null)
  231. {
  232. handlerAbort(m_asset.Name, m_asset.FullID, mXferID, remoteClient);
  233. }
  234. }
  235. }
  236. public class XferDownloadHandler
  237. {
  238. public IClientAPI Client;
  239. private bool complete;
  240. public byte[] Data = new byte[0];
  241. public int DataPointer = 0;
  242. public string FileName = String.Empty;
  243. public uint Packet = 0;
  244. public uint Serial = 1;
  245. public ulong XferID = 0;
  246. public XferDownloadHandler(string fileName, byte[] data, ulong xferID, IClientAPI client)
  247. {
  248. FileName = fileName;
  249. Data = data;
  250. XferID = xferID;
  251. Client = client;
  252. }
  253. public XferDownloadHandler()
  254. {
  255. }
  256. /// <summary>
  257. /// Start a transfer
  258. /// </summary>
  259. /// <returns>True if the transfer is complete, false if not</returns>
  260. public bool StartSend()
  261. {
  262. if (Data.Length < 1000)
  263. {
  264. // for now (testing) we only support files under 1000 bytes
  265. byte[] transferData = new byte[Data.Length + 4];
  266. Array.Copy(Utils.IntToBytes(Data.Length), 0, transferData, 0, 4);
  267. Array.Copy(Data, 0, transferData, 4, Data.Length);
  268. Client.SendXferPacket(XferID, 0 + 0x80000000, transferData);
  269. complete = true;
  270. }
  271. else
  272. {
  273. byte[] transferData = new byte[1000 + 4];
  274. Array.Copy(Utils.IntToBytes(Data.Length), 0, transferData, 0, 4);
  275. Array.Copy(Data, 0, transferData, 4, 1000);
  276. Client.SendXferPacket(XferID, 0, transferData);
  277. Packet++;
  278. DataPointer = 1000;
  279. }
  280. return complete;
  281. }
  282. /// <summary>
  283. /// Respond to an ack packet from the client
  284. /// </summary>
  285. /// <param name="packet"></param>
  286. /// <returns>True if the transfer is complete, false otherwise</returns>
  287. public bool AckPacket(uint packet)
  288. {
  289. if (!complete)
  290. {
  291. if ((Data.Length - DataPointer) > 1000)
  292. {
  293. byte[] transferData = new byte[1000];
  294. Array.Copy(Data, DataPointer, transferData, 0, 1000);
  295. Client.SendXferPacket(XferID, Packet, transferData);
  296. Packet++;
  297. DataPointer += 1000;
  298. }
  299. else
  300. {
  301. byte[] transferData = new byte[Data.Length - DataPointer];
  302. Array.Copy(Data, DataPointer, transferData, 0, Data.Length - DataPointer);
  303. uint endPacket = Packet |= (uint)0x80000000;
  304. Client.SendXferPacket(XferID, endPacket, transferData);
  305. Packet++;
  306. DataPointer += (Data.Length - DataPointer);
  307. complete = true;
  308. }
  309. }
  310. return complete;
  311. }
  312. }
  313. }