llxfer.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /**
  2. * @file llxfer.cpp
  3. * @brief implementation of LLXfer class for a single xfer.
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-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 "llxfer.h"
  34. #include "llextendedstatus.h"
  35. #include "llmath.h"
  36. // Number of bytes sent in each message
  37. constexpr U32 LL_XFER_CHUNK_SIZE = 1000;
  38. LLXfer::LLXfer(S32 chunk_size)
  39. {
  40. init(chunk_size);
  41. }
  42. LLXfer::~LLXfer()
  43. {
  44. cleanup();
  45. }
  46. void LLXfer::init(S32 chunk_size)
  47. {
  48. mID = 0;
  49. mPacketNum = -1; // There is a pre-increment before sending packet 0
  50. mXferSize = 0;
  51. mStatus = e_LL_XFER_UNINITIALIZED;
  52. mWaitingForACK = false;
  53. mCallback = NULL;
  54. mCallbackDataHandle = NULL;
  55. mCallbackResult = 0;
  56. mBufferContainsEOF = false;
  57. mBuffer = NULL;
  58. mBufferLength = 0;
  59. mBufferStartOffset = 0;
  60. mRetries = 0;
  61. if (chunk_size < 1)
  62. {
  63. chunk_size = LL_XFER_CHUNK_SIZE;
  64. }
  65. mChunkSize = chunk_size;
  66. }
  67. void LLXfer::cleanup()
  68. {
  69. if (mBuffer)
  70. {
  71. delete[] mBuffer;
  72. mBuffer = NULL;
  73. }
  74. }
  75. S32 LLXfer::startSend(U64 xfer_id, const LLHost& remote_host)
  76. {
  77. llwarns << "Default, no-operation version called for " << getFileName()
  78. << llendl;
  79. return -1;
  80. }
  81. void LLXfer::closeFileHandle()
  82. {
  83. llwarns << "Default, no-operation version called for " << getFileName()
  84. << llendl;
  85. }
  86. S32 LLXfer::reopenFileHandle()
  87. {
  88. llwarns << "Default, no-operation version called for " << getFileName()
  89. << llendl;
  90. return -1;
  91. }
  92. void LLXfer::setXferSize(S32 xfer_size)
  93. {
  94. mXferSize = xfer_size;
  95. }
  96. S32 LLXfer::startDownload()
  97. {
  98. llwarns << "Default, no-operation version called for " << getFileName()
  99. << llendl;
  100. return -1;
  101. }
  102. S32 LLXfer::receiveData(char* datap, S32 data_size)
  103. {
  104. S32 retval = 0;
  105. if ((S32)mBufferLength + data_size > getMaxBufferSize())
  106. {
  107. // Write existing data to disk if it is larger than the buffer size
  108. retval = flush();
  109. }
  110. if (!retval)
  111. {
  112. if (datap != NULL)
  113. {
  114. // Append new data to mBuffer
  115. memcpy(&mBuffer[mBufferLength], datap, data_size);
  116. mBufferLength += data_size;
  117. }
  118. else
  119. {
  120. llerrs << "NULL data passed in receiveData" << llendl;
  121. }
  122. }
  123. return (retval);
  124. }
  125. S32 LLXfer::flush()
  126. {
  127. // Only files have somewhere to flush to if we get called with a flush it
  128. // means we've blown past our allocated buffer size
  129. return -1;
  130. }
  131. S32 LLXfer::suck(S32 start_position)
  132. {
  133. llwarns << "Attempted to send a packet outside the buffer bounds"
  134. << llendl;
  135. return -1;
  136. }
  137. void LLXfer::sendPacket(S32 packet_num)
  138. {
  139. char fdata_buf[LL_XFER_LARGE_PAYLOAD + 4];
  140. S32 fdata_size = mChunkSize;
  141. bool last_packet = false;
  142. S32 num_copy = 0;
  143. // If the desired packet is not in our current buffered excerpt from the
  144. // file
  145. if ((U32)packet_num * fdata_size < mBufferStartOffset ||
  146. llmin((U32)mXferSize, (U32)((packet_num + 1) * fdata_size)) >
  147. mBufferStartOffset + mBufferLength)
  148. {
  149. if (suck(packet_num * fdata_size)) // returns non-zero on failure
  150. {
  151. abort(LL_ERR_EOF);
  152. return;
  153. }
  154. }
  155. S32 desired_read_position = packet_num * fdata_size - mBufferStartOffset;
  156. fdata_size = llmin((S32)mBufferLength - desired_read_position, mChunkSize);
  157. if (fdata_size < 0)
  158. {
  159. llwarns << "Negative data size in transfer send: aborting." << llendl;
  160. abort(LL_ERR_EOF);
  161. return;
  162. }
  163. if (mBufferContainsEOF &&
  164. (U32)(desired_read_position + fdata_size) >= (U32)mBufferLength)
  165. {
  166. last_packet = true;
  167. }
  168. if (packet_num)
  169. {
  170. num_copy = llmin(fdata_size, (S32)sizeof(fdata_buf));
  171. num_copy = llmin(num_copy,
  172. (S32)(mBufferLength - desired_read_position));
  173. if (num_copy > 0)
  174. {
  175. memcpy(fdata_buf, &mBuffer[desired_read_position], num_copy);
  176. }
  177. }
  178. else
  179. {
  180. // If we are the first packet, encode size as an additional S32 at
  181. // start of data.
  182. num_copy = llmin(fdata_size, (S32)(sizeof(fdata_buf) - sizeof(S32)));
  183. num_copy = llmin(num_copy,
  184. (S32)(mBufferLength - desired_read_position));
  185. if (num_copy > 0)
  186. {
  187. memcpy(fdata_buf + sizeof(S32), &mBuffer[desired_read_position],
  188. num_copy);
  189. }
  190. fdata_size += sizeof(S32);
  191. htonmemcpy(fdata_buf, &mXferSize, MVT_S32, sizeof(S32));
  192. }
  193. S32 encoded_packetnum = encodePacketNum(packet_num, last_packet);
  194. if (fdata_size)
  195. {
  196. // Send the packet
  197. LLMessageSystem* msg = gMessageSystemp;
  198. msg->newMessageFast(_PREHASH_SendXferPacket);
  199. msg->nextBlockFast(_PREHASH_XferID);
  200. msg->addU64Fast(_PREHASH_ID, mID);
  201. msg->addU32Fast(_PREHASH_Packet, encoded_packetnum);
  202. msg->nextBlockFast(_PREHASH_DataPacket);
  203. msg->addBinaryDataFast(_PREHASH_Data, &fdata_buf, fdata_size);
  204. S32 bytes_sent = msg->sendMessage(mRemoteHost);
  205. if (!bytes_sent)
  206. {
  207. abort(LL_ERR_CIRCUIT_GONE);
  208. return;
  209. }
  210. ACKTimer.reset();
  211. mWaitingForACK = true;
  212. }
  213. if (last_packet)
  214. {
  215. mStatus = e_LL_XFER_COMPLETE;
  216. }
  217. else
  218. {
  219. mStatus = e_LL_XFER_IN_PROGRESS;
  220. }
  221. }
  222. void LLXfer::sendNextPacket()
  223. {
  224. mRetries = 0;
  225. sendPacket(++mPacketNum);
  226. }
  227. void LLXfer::resendLastPacket()
  228. {
  229. ++mRetries;
  230. sendPacket(mPacketNum);
  231. }
  232. S32 LLXfer::processEOF()
  233. {
  234. S32 retval = 0;
  235. mStatus = e_LL_XFER_COMPLETE;
  236. if (mCallbackResult == LL_ERR_NOERR)
  237. {
  238. llinfos << "Transfer from " << mRemoteHost << " complete: "
  239. << getFileName() << llendl;
  240. }
  241. else
  242. {
  243. llinfos << "Transfer from " << mRemoteHost
  244. << " failed or aborted with error code " << mCallbackResult
  245. << ": " << getFileName() << llendl;
  246. }
  247. if (mCallback)
  248. {
  249. mCallback(mCallbackDataHandle, mCallbackResult, LLExtStat::NONE);
  250. }
  251. return retval;
  252. }
  253. S32 LLXfer::encodePacketNum(S32 packet_num, bool is_eof)
  254. {
  255. return is_eof ? (packet_num | 0x80000000) : packet_num;
  256. }
  257. void LLXfer::abort (S32 result_code)
  258. {
  259. mCallbackResult = result_code;
  260. llinfos << "Aborting transfer from: " << mRemoteHost << " - named: "
  261. << getFileName() << " - error: " << result_code << llendl;
  262. if (result_code != LL_ERR_CIRCUIT_GONE)
  263. {
  264. LLMessageSystem* msg = gMessageSystemp;
  265. msg->newMessageFast(_PREHASH_AbortXfer);
  266. msg->nextBlockFast(_PREHASH_XferID);
  267. msg->addU64Fast(_PREHASH_ID, mID);
  268. msg->addS32Fast(_PREHASH_Result, result_code);
  269. msg->sendMessage(mRemoteHost);
  270. }
  271. mStatus = e_LL_XFER_ABORTED;
  272. }
  273. std::string LLXfer::getFileName()
  274. {
  275. return U64_to_str(mID);
  276. }
  277. U32 LLXfer::getXferTypeTag()
  278. {
  279. return 0;
  280. }
  281. S32 LLXfer::getMaxBufferSize()
  282. {
  283. return mXferSize;
  284. }
  285. std::ostream& operator<<(std::ostream& os, LLXfer& hh)
  286. {
  287. os << hh.getFileName();
  288. return os;
  289. }