1
0

LLFileTransfer.cs 13 KB

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