PageRenderTime 28ms CodeModel.GetById 1ms app.highlight 20ms RepoModel.GetById 2ms app.codeStats 0ms

/indra/llmessage/lltransfertargetfile.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 131 lines | 82 code | 17 blank | 32 comment | 12 complexity | 6b4605c4e1ba91f6b613ae16c1bcb569 MD5 | raw file
  1/** 
  2 * @file lltransfertargetfile.cpp
  3 * @brief Transfer system for receiving a file.
  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 "lltransfertargetfile.h"
 30#include "llerror.h"
 31
 32
 33
 34
 35LLTransferTargetFile::LLTransferTargetFile(
 36	const LLUUID& uuid,
 37	LLTransferSourceType src_type) :
 38	LLTransferTarget(LLTTT_FILE, uuid, src_type),
 39	mFP(NULL)
 40{
 41}
 42
 43LLTransferTargetFile::~LLTransferTargetFile()
 44{
 45	if (mFP)
 46	{
 47		llerrs << "LLTransferTargetFile::~LLTransferTargetFile - Should have been cleaned up in completion callback" << llendl;
 48		fclose(mFP);
 49		mFP = NULL;
 50	}
 51}
 52
 53// virtual
 54bool LLTransferTargetFile::unpackParams(LLDataPacker& dp)
 55{
 56	// we can safely ignore this call
 57	return true;
 58}
 59
 60void LLTransferTargetFile::applyParams(const LLTransferTargetParams &params)
 61{
 62	if (params.getType() != mType)
 63	{
 64		llwarns << "Target parameter type doesn't match!" << llendl;
 65		return;
 66	}
 67	
 68	mParams = (LLTransferTargetParamsFile &)params;
 69}
 70
 71LLTSCode LLTransferTargetFile::dataCallback(const S32 packet_id, U8 *in_datap, const S32 in_size)
 72{
 73	//llinfos << "LLTransferTargetFile::dataCallback" << llendl;
 74	//llinfos << "Packet: " << packet_id << llendl;
 75
 76	if (!mFP)
 77	{
 78		mFP = LLFile::fopen(mParams.mFilename, "wb");		/* Flawfinder: ignore */		
 79
 80		if (!mFP)
 81		{
 82			llwarns << "Failure opening " << mParams.mFilename << " for write by LLTransferTargetFile" << llendl;
 83			return LLTS_ERROR;
 84		}
 85	}
 86	if (!in_size)
 87	{
 88		return LLTS_OK;
 89	}
 90
 91	S32 count = (S32)fwrite(in_datap, 1, in_size, mFP);
 92	if (count != in_size)
 93	{
 94		llwarns << "Failure in LLTransferTargetFile::dataCallback!" << llendl;
 95		return LLTS_ERROR;
 96	}
 97	return LLTS_OK;
 98}
 99
100void LLTransferTargetFile::completionCallback(const LLTSCode status)
101{
102	llinfos << "LLTransferTargetFile::completionCallback" << llendl;
103	if (mFP)
104	{
105		fclose(mFP);
106	}
107
108	// Still need to gracefully handle error conditions.
109	switch (status)
110	{
111	case LLTS_DONE:
112		break;
113	case LLTS_ABORT:
114	case LLTS_ERROR:
115		// We're aborting this transfer, we don't want to keep this file.
116		llwarns << "Aborting file transfer for " << mParams.mFilename << llendl;
117		if (mFP)
118		{
119			// Only need to remove file if we successfully opened it.
120			LLFile::remove(mParams.mFilename);
121		}
122	default:
123		break;
124	}
125
126	mFP = NULL;
127	if (mParams.mCompleteCallback)
128	{
129		mParams.mCompleteCallback(status, mParams.mUserData);
130	}
131}