PageRenderTime 129ms CodeModel.GetById 0ms RepoModel.GetById 0ms 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
Possible License(s): LGPL-2.1
  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. #include "linden_common.h"
  27. #include "lltransfertargetfile.h"
  28. #include "llerror.h"
  29. LLTransferTargetFile::LLTransferTargetFile(
  30. const LLUUID& uuid,
  31. LLTransferSourceType src_type) :
  32. LLTransferTarget(LLTTT_FILE, uuid, src_type),
  33. mFP(NULL)
  34. {
  35. }
  36. LLTransferTargetFile::~LLTransferTargetFile()
  37. {
  38. if (mFP)
  39. {
  40. llerrs << "LLTransferTargetFile::~LLTransferTargetFile - Should have been cleaned up in completion callback" << llendl;
  41. fclose(mFP);
  42. mFP = NULL;
  43. }
  44. }
  45. // virtual
  46. bool LLTransferTargetFile::unpackParams(LLDataPacker& dp)
  47. {
  48. // we can safely ignore this call
  49. return true;
  50. }
  51. void LLTransferTargetFile::applyParams(const LLTransferTargetParams &params)
  52. {
  53. if (params.getType() != mType)
  54. {
  55. llwarns << "Target parameter type doesn't match!" << llendl;
  56. return;
  57. }
  58. mParams = (LLTransferTargetParamsFile &)params;
  59. }
  60. LLTSCode LLTransferTargetFile::dataCallback(const S32 packet_id, U8 *in_datap, const S32 in_size)
  61. {
  62. //llinfos << "LLTransferTargetFile::dataCallback" << llendl;
  63. //llinfos << "Packet: " << packet_id << llendl;
  64. if (!mFP)
  65. {
  66. mFP = LLFile::fopen(mParams.mFilename, "wb"); /* Flawfinder: ignore */
  67. if (!mFP)
  68. {
  69. llwarns << "Failure opening " << mParams.mFilename << " for write by LLTransferTargetFile" << llendl;
  70. return LLTS_ERROR;
  71. }
  72. }
  73. if (!in_size)
  74. {
  75. return LLTS_OK;
  76. }
  77. S32 count = (S32)fwrite(in_datap, 1, in_size, mFP);
  78. if (count != in_size)
  79. {
  80. llwarns << "Failure in LLTransferTargetFile::dataCallback!" << llendl;
  81. return LLTS_ERROR;
  82. }
  83. return LLTS_OK;
  84. }
  85. void LLTransferTargetFile::completionCallback(const LLTSCode status)
  86. {
  87. llinfos << "LLTransferTargetFile::completionCallback" << llendl;
  88. if (mFP)
  89. {
  90. fclose(mFP);
  91. }
  92. // Still need to gracefully handle error conditions.
  93. switch (status)
  94. {
  95. case LLTS_DONE:
  96. break;
  97. case LLTS_ABORT:
  98. case LLTS_ERROR:
  99. // We're aborting this transfer, we don't want to keep this file.
  100. llwarns << "Aborting file transfer for " << mParams.mFilename << llendl;
  101. if (mFP)
  102. {
  103. // Only need to remove file if we successfully opened it.
  104. LLFile::remove(mParams.mFilename);
  105. }
  106. default:
  107. break;
  108. }
  109. mFP = NULL;
  110. if (mParams.mCompleteCallback)
  111. {
  112. mParams.mCompleteCallback(status, mParams.mUserData);
  113. }
  114. }