LLFileTransfer.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 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 += AbortXferHandler;
  66. }
  67. public void Close()
  68. {
  69. if (m_clientAPI != null)
  70. {
  71. m_clientAPI.OnXferReceive -= XferReceive;
  72. m_clientAPI.OnAbortXfer -= AbortXferHandler;
  73. m_clientAPI = null;
  74. }
  75. }
  76. public bool RequestUpload(string clientFileName, UploadComplete uploadCompleteCallback, UploadAborted abortCallback)
  77. {
  78. if ((String.IsNullOrEmpty(clientFileName)) || (uploadCompleteCallback == null))
  79. {
  80. return false;
  81. }
  82. XferUploadHandler uploader = new XferUploadHandler(m_clientAPI, clientFileName);
  83. return StartUpload(uploader, uploadCompleteCallback, abortCallback);
  84. }
  85. public bool RequestUpload(UUID fileID, UploadComplete uploadCompleteCallback, UploadAborted abortCallback)
  86. {
  87. if ((fileID == UUID.Zero) || (uploadCompleteCallback == null))
  88. {
  89. return false;
  90. }
  91. XferUploadHandler uploader = new XferUploadHandler(m_clientAPI, fileID);
  92. return StartUpload(uploader, uploadCompleteCallback, abortCallback);
  93. }
  94. private bool StartUpload(XferUploadHandler uploader, UploadComplete uploadCompleteCallback, UploadAborted abortCallback)
  95. {
  96. uploader.UploadDone += uploadCompleteCallback;
  97. uploader.UploadDone += RemoveXferHandler;
  98. if (abortCallback != null)
  99. {
  100. uploader.UploadAborted += abortCallback;
  101. }
  102. lock (m_uploadHandlersLock)
  103. {
  104. if (!m_uploadHandlers.ContainsKey(uploader.XferID))
  105. {
  106. m_uploadHandlers.Add(uploader.XferID, uploader);
  107. uploader.RequestStartXfer(m_clientAPI);
  108. return true;
  109. }
  110. else
  111. {
  112. // something went wrong with the xferID allocation
  113. uploader.UploadDone -= uploadCompleteCallback;
  114. uploader.UploadDone -= RemoveXferHandler;
  115. if (abortCallback != null)
  116. {
  117. uploader.UploadAborted -= abortCallback;
  118. }
  119. return false;
  120. }
  121. }
  122. }
  123. protected void AbortXferHandler(IClientAPI remoteClient, ulong xferID)
  124. {
  125. lock (m_uploadHandlersLock)
  126. {
  127. if (m_uploadHandlers.ContainsKey(xferID))
  128. {
  129. m_uploadHandlers[xferID].AbortUpload(remoteClient);
  130. m_uploadHandlers.Remove(xferID);
  131. }
  132. }
  133. }
  134. protected void XferReceive(IClientAPI remoteClient, ulong xferID, uint packetID, byte[] data)
  135. {
  136. lock (m_uploadHandlersLock)
  137. {
  138. if (m_uploadHandlers.ContainsKey(xferID))
  139. {
  140. m_uploadHandlers[xferID].XferReceive(remoteClient, xferID, packetID, data);
  141. }
  142. }
  143. }
  144. protected void RemoveXferHandler(string filename, UUID fileID, ulong transferID, byte[] fileData, IClientAPI remoteClient)
  145. {
  146. }
  147. }
  148. public class XferUploadHandler
  149. {
  150. private AssetBase m_asset;
  151. public event UploadComplete UploadDone;
  152. public event UploadAborted UploadAborted;
  153. private sbyte type = 0;
  154. public ulong mXferID;
  155. private UploadComplete handlerUploadDone;
  156. private UploadAborted handlerAbort;
  157. private bool m_complete = false;
  158. public bool UploadComplete
  159. {
  160. get { return m_complete; }
  161. }
  162. public XferUploadHandler(IClientAPI pRemoteClient, string pClientFilename)
  163. {
  164. Initialise(UUID.Zero, pClientFilename);
  165. }
  166. public XferUploadHandler(IClientAPI pRemoteClient, UUID fileID)
  167. {
  168. Initialise(fileID, String.Empty);
  169. }
  170. private void Initialise(UUID fileID, string fileName)
  171. {
  172. m_asset = new AssetBase();
  173. m_asset.FullID = fileID;
  174. m_asset.Type = type;
  175. m_asset.Data = new byte[0];
  176. m_asset.Name = fileName;
  177. m_asset.Description = "empty";
  178. m_asset.Local = true;
  179. m_asset.Temporary = true;
  180. mXferID = Util.GetNextXferID();
  181. }
  182. public ulong XferID
  183. {
  184. get { return mXferID; }
  185. }
  186. public void RequestStartXfer(IClientAPI pRemoteClient)
  187. {
  188. if (!String.IsNullOrEmpty(m_asset.Name))
  189. {
  190. pRemoteClient.SendXferRequest(mXferID, m_asset.Type, m_asset.FullID, 0, Utils.StringToBytes(m_asset.Name));
  191. }
  192. else
  193. {
  194. pRemoteClient.SendXferRequest(mXferID, m_asset.Type, m_asset.FullID, 0, new byte[0]);
  195. }
  196. }
  197. /// <summary>
  198. /// Process transfer data received from the client.
  199. /// </summary>
  200. /// <param name="xferID"></param>
  201. /// <param name="packetID"></param>
  202. /// <param name="data"></param>
  203. public void XferReceive(IClientAPI remoteClient, ulong xferID, uint packetID, byte[] data)
  204. {
  205. if (mXferID == xferID)
  206. {
  207. if (m_asset.Data.Length > 1)
  208. {
  209. byte[] destinationArray = new byte[m_asset.Data.Length + data.Length];
  210. Array.Copy(m_asset.Data, 0, destinationArray, 0, m_asset.Data.Length);
  211. Array.Copy(data, 0, destinationArray, m_asset.Data.Length, data.Length);
  212. m_asset.Data = destinationArray;
  213. }
  214. else
  215. {
  216. byte[] buffer2 = new byte[data.Length - 4];
  217. Array.Copy(data, 4, buffer2, 0, data.Length - 4);
  218. m_asset.Data = buffer2;
  219. }
  220. remoteClient.SendConfirmXfer(xferID, packetID);
  221. if ((packetID & 0x80000000) != 0)
  222. {
  223. SendCompleteMessage(remoteClient);
  224. }
  225. }
  226. }
  227. protected void SendCompleteMessage(IClientAPI remoteClient)
  228. {
  229. m_complete = true;
  230. handlerUploadDone = UploadDone;
  231. if (handlerUploadDone != null)
  232. {
  233. handlerUploadDone(m_asset.Name, m_asset.FullID, mXferID, m_asset.Data, remoteClient);
  234. }
  235. }
  236. public void AbortUpload(IClientAPI remoteClient)
  237. {
  238. handlerAbort = UploadAborted;
  239. if (handlerAbort != null)
  240. {
  241. handlerAbort(m_asset.Name, m_asset.FullID, mXferID, remoteClient);
  242. }
  243. }
  244. }
  245. public class XferDownloadHandler
  246. {
  247. public IClientAPI Client;
  248. private bool complete;
  249. public byte[] Data = new byte[0];
  250. public int DataPointer = 0;
  251. public string FileName = String.Empty;
  252. public uint Packet = 0;
  253. public uint Serial = 1;
  254. public ulong XferID = 0;
  255. public XferDownloadHandler(string fileName, byte[] data, ulong xferID, IClientAPI client)
  256. {
  257. FileName = fileName;
  258. Data = data;
  259. XferID = xferID;
  260. Client = client;
  261. }
  262. public XferDownloadHandler()
  263. {
  264. }
  265. /// <summary>
  266. /// Start a transfer
  267. /// </summary>
  268. /// <returns>True if the transfer is complete, false if not</returns>
  269. public bool StartSend()
  270. {
  271. if (Data.Length < 1000)
  272. {
  273. // for now (testing) we only support files under 1000 bytes
  274. byte[] transferData = new byte[Data.Length + 4];
  275. Array.Copy(Utils.IntToBytes(Data.Length), 0, transferData, 0, 4);
  276. Array.Copy(Data, 0, transferData, 4, Data.Length);
  277. Client.SendXferPacket(XferID, 0 + 0x80000000, transferData);
  278. complete = true;
  279. }
  280. else
  281. {
  282. byte[] transferData = new byte[1000 + 4];
  283. Array.Copy(Utils.IntToBytes(Data.Length), 0, transferData, 0, 4);
  284. Array.Copy(Data, 0, transferData, 4, 1000);
  285. Client.SendXferPacket(XferID, 0, transferData);
  286. Packet++;
  287. DataPointer = 1000;
  288. }
  289. return complete;
  290. }
  291. /// <summary>
  292. /// Respond to an ack packet from the client
  293. /// </summary>
  294. /// <param name="packet"></param>
  295. /// <returns>True if the transfer is complete, false otherwise</returns>
  296. public bool AckPacket(uint packet)
  297. {
  298. if (!complete)
  299. {
  300. if ((Data.Length - DataPointer) > 1000)
  301. {
  302. byte[] transferData = new byte[1000];
  303. Array.Copy(Data, DataPointer, transferData, 0, 1000);
  304. Client.SendXferPacket(XferID, Packet, transferData);
  305. Packet++;
  306. DataPointer += 1000;
  307. }
  308. else
  309. {
  310. byte[] transferData = new byte[Data.Length - DataPointer];
  311. Array.Copy(Data, DataPointer, transferData, 0, Data.Length - DataPointer);
  312. uint endPacket = Packet |= (uint)0x80000000;
  313. Client.SendXferPacket(XferID, endPacket, transferData);
  314. Packet++;
  315. DataPointer += (Data.Length - DataPointer);
  316. complete = true;
  317. }
  318. }
  319. return complete;
  320. }
  321. }
  322. }