PageRenderTime 52ms CodeModel.GetById 37ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llmessage/lltransfersourceasset.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 254 lines | 175 code | 41 blank | 38 comment | 15 complexity | dc52c2d7580204aed88cdbdc28dba244 MD5 | raw file
  1/** 
  2 * @file lltransfersourceasset.cpp
  3 * @brief Transfer system for sending an asset.
  4 *
  5 * $LicenseInfo:firstyear=2006&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27#include "linden_common.h"
 28
 29#include "lltransfersourceasset.h"
 30
 31#include "llerror.h"
 32#include "message.h"
 33#include "lldatapacker.h"
 34#include "lldir.h"
 35#include "llvfile.h"
 36
 37LLTransferSourceAsset::LLTransferSourceAsset(const LLUUID &request_id, const F32 priority) :
 38	LLTransferSource(LLTST_ASSET, request_id, priority),
 39	mGotResponse(FALSE),
 40	mCurPos(0)
 41{
 42}
 43
 44LLTransferSourceAsset::~LLTransferSourceAsset()
 45{
 46}
 47
 48
 49void LLTransferSourceAsset::initTransfer()
 50{
 51	if (gAssetStorage)
 52	{
 53		// *HACK: asset transfers will only be coming from the viewer
 54		// to the simulator. This is subset of assets we allow to be
 55		// simply pulled straight from the asset system.
 56		LLUUID* tidp;
 57		if(LLAssetType::lookupIsAssetFetchByIDAllowed(mParams.getAssetType()))
 58		{
 59			tidp = new LLUUID(getID());
 60			gAssetStorage->getAssetData(
 61				mParams.getAssetID(),
 62				mParams.getAssetType(),
 63				LLTransferSourceAsset::responderCallback,
 64				tidp,
 65				FALSE);
 66		}
 67		else
 68		{
 69			llwarns << "Attempted to request blocked asset "
 70				<< mParams.getAssetID() << ":"
 71				<< LLAssetType::lookupHumanReadable(mParams.getAssetType())
 72				<< llendl;
 73			sendTransferStatus(LLTS_ERROR);
 74		}
 75	}
 76	else
 77	{
 78		llwarns << "Attempted to request asset " << mParams.getAssetID()
 79			<< ":" << LLAssetType::lookupHumanReadable(mParams.getAssetType())
 80			<< " without an asset system!" << llendl;
 81		sendTransferStatus(LLTS_ERROR);
 82	}
 83}
 84
 85F32 LLTransferSourceAsset::updatePriority()
 86{
 87	return 0.f;
 88}
 89
 90LLTSCode LLTransferSourceAsset::dataCallback(const S32 packet_id,
 91											const S32 max_bytes,
 92											U8 **data_handle,
 93											S32 &returned_bytes,
 94											BOOL &delete_returned)
 95{
 96	//llinfos << "LLTransferSourceAsset::dataCallback" << llendl;
 97	if (!mGotResponse)
 98	{
 99		return LLTS_SKIP;
100	}
101
102	LLVFile vf(gAssetStorage->mVFS, mParams.getAssetID(), mParams.getAssetType(), LLVFile::READ);
103
104	if (!vf.getSize())
105	{
106		// Something bad happened with the asset request!
107		return LLTS_ERROR;
108	}
109
110	if (packet_id != mLastPacketID + 1)
111	{
112		llerrs << "Can't handle out of order file transfer yet!" << llendl;
113	}
114
115	// grab a buffer from the right place in the file
116	if (!vf.seek(mCurPos, 0))
117	{
118		llwarns << "LLTransferSourceAsset Can't seek to " << mCurPos << " length " << vf.getSize() << llendl;
119		llwarns << "While sending " << mParams.getAssetID() << llendl;
120		return LLTS_ERROR;
121	}
122	
123	delete_returned = TRUE;
124	U8 *tmpp = new U8[max_bytes];
125	*data_handle = tmpp;
126	if (!vf.read(tmpp, max_bytes))		/* Flawfinder: Ignore */
127	{
128		// Read failure, need to deal with it.
129		delete[] tmpp;
130		*data_handle = NULL;
131		returned_bytes = 0;
132		delete_returned = FALSE;
133		return LLTS_ERROR;
134	}
135
136	returned_bytes = vf.getLastBytesRead();
137	mCurPos += returned_bytes;
138
139
140	if (vf.eof())
141	{
142		if (!returned_bytes)
143		{
144			delete[] tmpp;
145			*data_handle = NULL;
146			returned_bytes = 0;
147			delete_returned = FALSE;
148		}
149		return LLTS_DONE;
150	}
151
152	return LLTS_OK;
153}
154
155void LLTransferSourceAsset::completionCallback(const LLTSCode status)
156{
157	// No matter what happens, all we want to do is close the vfile if
158	// we've got it open.
159}
160
161void LLTransferSourceAsset::packParams(LLDataPacker& dp) const
162{
163	//llinfos << "LLTransferSourceAsset::packParams" << llendl;
164	mParams.packParams(dp);
165}
166
167BOOL LLTransferSourceAsset::unpackParams(LLDataPacker &dp)
168{
169	//llinfos << "LLTransferSourceAsset::unpackParams" << llendl;
170	return mParams.unpackParams(dp);
171}
172
173
174void LLTransferSourceAsset::responderCallback(LLVFS *vfs, const LLUUID& uuid, LLAssetType::EType type,
175											  void *user_data, S32 result, LLExtStat ext_status )
176{
177	LLUUID *tidp = ((LLUUID*) user_data);
178	LLUUID transfer_id = *(tidp);
179	delete tidp;
180	tidp = NULL;
181
182	LLTransferSourceAsset *tsap = (LLTransferSourceAsset *)	gTransferManager.findTransferSource(transfer_id);
183
184	if (!tsap)
185	{
186		llinfos << "Aborting transfer " << transfer_id << " callback, transfer source went away" << llendl;
187		return;
188	}
189
190	if (result)
191	{
192		llinfos << "AssetStorage: Error " << gAssetStorage->getErrorString(result) << " downloading uuid " << uuid << llendl;
193	}
194
195	LLTSCode status;
196
197	tsap->mGotResponse = TRUE;
198	if (LL_ERR_NOERR == result)
199	{
200		// Everything's OK.
201		LLVFile vf(gAssetStorage->mVFS, uuid, type, LLVFile::READ);
202		tsap->mSize = vf.getSize();
203		status = LLTS_OK;
204	}
205	else
206	{
207		// Uh oh, something bad happened when we tried to get this asset!
208		switch (result)
209		{
210		case LL_ERR_ASSET_REQUEST_NOT_IN_DATABASE:
211			status = LLTS_UNKNOWN_SOURCE;
212			break;
213		default:
214			status = LLTS_ERROR;
215		}
216	}
217
218	tsap->sendTransferStatus(status);
219}
220
221
222
223LLTransferSourceParamsAsset::LLTransferSourceParamsAsset()
224	: LLTransferSourceParams(LLTST_ASSET),
225
226	  mAssetType(LLAssetType::AT_NONE)
227{
228}
229
230void LLTransferSourceParamsAsset::setAsset(const LLUUID &asset_id, const LLAssetType::EType asset_type)
231{
232	mAssetID = asset_id;
233	mAssetType = asset_type;
234}
235
236void LLTransferSourceParamsAsset::packParams(LLDataPacker &dp) const
237{
238	dp.packUUID(mAssetID, "AssetID");
239	dp.packS32(mAssetType, "AssetType");
240}
241
242
243BOOL LLTransferSourceParamsAsset::unpackParams(LLDataPacker &dp)
244{
245	S32 tmp_at;
246
247	dp.unpackUUID(mAssetID, "AssetID");
248	dp.unpackS32(tmp_at, "AssetType");
249
250	mAssetType = (LLAssetType::EType)tmp_at;
251
252	return TRUE;
253}
254