lltransfertargetfile.cpp 3.4 KB

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