lltransfersourceasset.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /**
  2. * @file lltransfersourceasset.cpp
  3. * @brief Transfer system for sending an asset.
  4. *
  5. * $LicenseInfo:firstyear=2006&license=viewergpl$
  6. *
  7. * Copyright (c) 2006-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "linden_common.h"
  33. #include "lltransfersourceasset.h"
  34. #include "lldatapacker.h"
  35. #include "lldir.h"
  36. #include "llfilesystem.h"
  37. #include "llmessage.h"
  38. LLTransferSourceAsset::LLTransferSourceAsset(const LLUUID& request_id, F32 prio)
  39. : LLTransferSource(LLTST_ASSET, request_id, prio),
  40. mGotResponse(false),
  41. mCurPos(0)
  42. {
  43. }
  44. void LLTransferSourceAsset::initTransfer()
  45. {
  46. if (gAssetStoragep)
  47. {
  48. // *HACK: asset transfers will only be coming from the viewer to the
  49. // simulator. This is subset of assets we allow to be simply pulled
  50. // straight from the asset system.
  51. if (LLAssetType::lookupIsAssetFetchByIDAllowed(mParams.getAssetType()))
  52. {
  53. LLUUID* tidp = new LLUUID(getID());
  54. gAssetStoragep->getAssetData(mParams.getAssetID(),
  55. mParams.getAssetType(),
  56. responderCallback, tidp, false);
  57. }
  58. else
  59. {
  60. llwarns << "Attempted to request blocked asset "
  61. << mParams.getAssetID() << ":"
  62. << LLAssetType::lookupHumanReadable(mParams.getAssetType())
  63. << llendl;
  64. sendTransferStatus(LLTS_ERROR);
  65. }
  66. }
  67. else
  68. {
  69. llwarns << "Attempted to request asset " << mParams.getAssetID() << ":"
  70. << LLAssetType::lookupHumanReadable(mParams.getAssetType())
  71. << " without an asset storage system !" << llendl;
  72. sendTransferStatus(LLTS_ERROR);
  73. }
  74. }
  75. F32 LLTransferSourceAsset::updatePriority()
  76. {
  77. return 0.f;
  78. }
  79. LLTSCode LLTransferSourceAsset::dataCallback(S32 packet_id, S32 max_bytes,
  80. U8** data_handle,
  81. S32& returned_bytes,
  82. bool& delete_returned)
  83. {
  84. if (!mGotResponse)
  85. {
  86. return LLTS_SKIP;
  87. }
  88. if (!gAssetStoragep)
  89. {
  90. llwarns << "Aborting transfer after asset storage shut down !"
  91. << llendl;
  92. return LLTS_ERROR;
  93. }
  94. LLFileSystem vf(mParams.getAssetID());
  95. if (!vf.getSize())
  96. {
  97. // Something bad happened with the asset request!
  98. return LLTS_ERROR;
  99. }
  100. if (packet_id != mLastPacketID + 1)
  101. {
  102. llwarns << "Cannot handle out of order file transfer !" << llendl;
  103. llassert(false);
  104. return LLTS_ERROR;
  105. }
  106. // grab a buffer from the right place in the file
  107. if (!vf.seek(mCurPos, 0))
  108. {
  109. llwarns << "Cannot seek to " << mCurPos << " length " << vf.getSize()
  110. << " while sending " << mParams.getAssetID() << llendl;
  111. return LLTS_ERROR;
  112. }
  113. delete_returned = true;
  114. U8* tmpp = new U8[max_bytes];
  115. *data_handle = tmpp;
  116. if (!vf.read(tmpp, max_bytes))
  117. {
  118. // Read failure, need to deal with it.
  119. delete[] tmpp;
  120. *data_handle = NULL;
  121. returned_bytes = 0;
  122. delete_returned = false;
  123. return LLTS_ERROR;
  124. }
  125. returned_bytes = vf.getLastBytesRead();
  126. mCurPos += returned_bytes;
  127. if (vf.eof())
  128. {
  129. if (!returned_bytes)
  130. {
  131. delete[] tmpp;
  132. *data_handle = NULL;
  133. returned_bytes = 0;
  134. delete_returned = false;
  135. }
  136. return LLTS_DONE;
  137. }
  138. return LLTS_OK;
  139. }
  140. void LLTransferSourceAsset::completionCallback(LLTSCode status)
  141. {
  142. // No matter what happens, all we want to do is close the vfile if
  143. // we have got it open.
  144. }
  145. void LLTransferSourceAsset::packParams(LLDataPacker& dp) const
  146. {
  147. mParams.packParams(dp);
  148. }
  149. bool LLTransferSourceAsset::unpackParams(LLDataPacker &dp)
  150. {
  151. return mParams.unpackParams(dp);
  152. }
  153. void LLTransferSourceAsset::responderCallback(const LLUUID& uuid,
  154. LLAssetType::EType type,
  155. void* user_data, S32 result,
  156. LLExtStat ext_status)
  157. {
  158. LLUUID* tidp = (LLUUID*)user_data;
  159. LLUUID transfer_id = *(tidp);
  160. delete tidp;
  161. tidp = NULL;
  162. if (!gAssetStoragep)
  163. {
  164. llwarns << "Aborting transfer after asset storage shut down !"
  165. << llendl;
  166. return;
  167. }
  168. LLTransferSourceAsset* tsap =
  169. (LLTransferSourceAsset*)gTransferManager.findTransferSource(transfer_id);
  170. if (!tsap)
  171. {
  172. llwarns << "Aborting transfer " << transfer_id
  173. << " callback, transfer source went away" << llendl;
  174. return;
  175. }
  176. if (result)
  177. {
  178. llwarns << "AssetStorage: Error "
  179. << gAssetStoragep->getErrorString(result)
  180. << ", downloading uuid: " << uuid << llendl;
  181. }
  182. LLTSCode status;
  183. tsap->mGotResponse = true;
  184. if (LL_ERR_NOERR == result)
  185. {
  186. // Everything's OK.
  187. LLFileSystem vf(uuid);
  188. tsap->mSize = vf.getSize();
  189. status = LLTS_OK;
  190. }
  191. else
  192. {
  193. // Uh oh, something bad happened when we tried to get this asset!
  194. switch (result)
  195. {
  196. case LL_ERR_ASSET_REQUEST_NOT_IN_DATABASE:
  197. status = LLTS_UNKNOWN_SOURCE;
  198. break;
  199. default:
  200. status = LLTS_ERROR;
  201. }
  202. }
  203. tsap->sendTransferStatus(status);
  204. }
  205. LLTransferSourceParamsAsset::LLTransferSourceParamsAsset()
  206. : LLTransferSourceParams(LLTST_ASSET),
  207. mAssetType(LLAssetType::AT_NONE)
  208. {
  209. }
  210. void LLTransferSourceParamsAsset::setAsset(const LLUUID& asset_id,
  211. LLAssetType::EType asset_type)
  212. {
  213. mAssetID = asset_id;
  214. mAssetType = asset_type;
  215. }
  216. void LLTransferSourceParamsAsset::packParams(LLDataPacker& dp) const
  217. {
  218. dp.packUUID(mAssetID, "AssetID");
  219. dp.packS32(mAssetType, "AssetType");
  220. }
  221. bool LLTransferSourceParamsAsset::unpackParams(LLDataPacker& dp)
  222. {
  223. S32 tmp_at;
  224. dp.unpackUUID(mAssetID, "AssetID");
  225. dp.unpackS32(tmp_at, "AssetType");
  226. mAssetType = (LLAssetType::EType)tmp_at;
  227. return true;
  228. }