llxfermanager.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /**
  2. * @file llxfermanager.h
  3. * @brief definition of LLXferManager class for a keeping track of
  4. * multiple xfers
  5. *
  6. * $LicenseInfo:firstyear=2001&license=viewergpl$
  7. *
  8. * Copyright (c) 2001-2009, Linden Research, Inc.
  9. *
  10. * Second Life Viewer Source Code
  11. * The source code in this file ("Source Code") is provided by Linden Lab
  12. * to you under the terms of the GNU General Public License, version 2.0
  13. * ("GPL"), unless you have obtained a separate licensing agreement
  14. * ("Other License"), formally executed by you and Linden Lab. Terms of
  15. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  16. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  17. *
  18. * There are special exceptions to the terms and conditions of the GPL as
  19. * it is applied to this Source Code. View the full text of the exception
  20. * in the file doc/FLOSS-exception.txt in this software distribution, or
  21. * online at
  22. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  23. *
  24. * By copying, modifying or distributing this software, you acknowledge
  25. * that you have read and understood your obligations described above,
  26. * and agree to abide by those obligations.
  27. *
  28. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  29. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  30. * COMPLETENESS OR PERFORMANCE.
  31. * $/LicenseInfo$
  32. */
  33. #ifndef LL_LLXFERMANAGER_H
  34. #define LL_LLXFERMANAGER_H
  35. /**
  36. * this manager keeps both a send list and a receive list; anything with a
  37. * LLXferManager can send and receive files via messages
  38. */
  39. #include <deque>
  40. #include "llassetstorage.h"
  41. #include "lldir.h" // For ELLPath
  42. #include "llthrottle.h"
  43. #include "llxfer.h"
  44. class LLHostStatus
  45. {
  46. public:
  47. LLHostStatus()
  48. : mNumActive(0),
  49. mNumPending(0)
  50. {
  51. }
  52. virtual ~LLHostStatus() {}
  53. public:
  54. LLHost mHost;
  55. S32 mNumActive;
  56. S32 mNumPending;
  57. };
  58. // Class stores ack information, to be put on list so we can throttle xfer
  59. // rate.
  60. class LLXferAckInfo
  61. {
  62. public:
  63. LLXferAckInfo(U32 dummy = 0)
  64. {
  65. mID = 0;
  66. mPacketNum = -1;
  67. }
  68. U64 mID;
  69. S32 mPacketNum;
  70. LLHost mRemoteHost;
  71. };
  72. class LLXferManager
  73. {
  74. protected:
  75. LOG_CLASS(LLXferManager);
  76. public:
  77. // This enumeration is useful in the requestFile() to specify if an xfer
  78. // must happen asap.
  79. enum
  80. {
  81. LOW_PRIORITY = 0,
  82. HIGH_PRIORITY = 1,
  83. };
  84. LLXferManager();
  85. ~LLXferManager();
  86. LL_INLINE void setHardLimitOutgoingXfersPerCircuit(S32 max)
  87. {
  88. mHardLimitOutgoingXfersPerCircuit = max;
  89. }
  90. LL_INLINE void setUseAckThrottling(bool use)
  91. {
  92. mUseAckThrottling = use;
  93. }
  94. void setAckThrottleBPS(F32 bps);
  95. // List management routines
  96. typedef std::deque<LLXfer*> xfer_list_t;
  97. LLXfer* findXferByID(U64 id, xfer_list_t& list);
  98. void removeXfer(LLXfer* delp, xfer_list_t& list);
  99. LLHostStatus* findHostStatus(const LLHost& host);
  100. S32 numActiveXfers(const LLHost& host);
  101. S32 numPendingXfers(const LLHost& host);
  102. void changeNumActiveXfers(const LLHost& host, S32 delta);
  103. LL_INLINE void setMaxOutgoingXfersPerCircuit(S32 max_num)
  104. {
  105. mMaxOutgoingXfersPerCircuit = max_num;
  106. }
  107. LL_INLINE void setMaxIncomingXfers(S32 max_num)
  108. {
  109. mMaxIncomingXfers = max_num;
  110. }
  111. void updateHostStatus();
  112. void printHostStatus();
  113. // General utility routines
  114. void registerCallbacks(LLMessageSystem* mesgsys);
  115. U64 getNextID();
  116. #if 0 // Not used
  117. LL_INLINE S32 encodePacketNum(S32 packet_num, bool is_eof)
  118. {
  119. return is_eof ? (packet_num | 0x80000000) : packet_num;
  120. }
  121. #endif
  122. LL_INLINE S32 decodePacketNum(S32 packet_num)
  123. {
  124. return packet_num & 0x0FFFFFFF;
  125. }
  126. LL_INLINE bool isLastPacket(S32 packet_num)
  127. {
  128. return (packet_num & 0x80000000) != 0;
  129. }
  130. // File requesting routine
  131. U64 requestFile(const std::string& local_filename,
  132. const std::string& remote_filename,
  133. ELLPath remote_path, const LLHost& remote_host,
  134. bool delete_remote_on_completion,
  135. void (*callback)(void**, S32, LLExtStat), void** user_data,
  136. bool is_priority = false, bool use_big_packets = false);
  137. // VFile requesting
  138. void requestVFile(const LLUUID& local_id, const LLUUID& remote_id,
  139. LLAssetType::EType type, const LLHost& remote_host,
  140. void (*callback)(void**, S32, LLExtStat),
  141. void** user_data, bool is_priority = false);
  142. // When arbitrary files are requested to be transfered (by giving a dir of
  143. // LL_PATH_NONE) they must be "expected", but having something pre-
  144. // authorize them. This pair of functions maintains a pre-authorized list.
  145. // The first function adds something to the list, the second checks if is
  146. // authorized, removing it if so. In this way, a file is only authorized
  147. // for a single use.
  148. void expectFileForTransfer(const std::string& filename);
  149. bool validateFileForTransfer(const std::string& filename);
  150. // Same idea, but for the viewer about to call InitiateDownload to track
  151. // what it requested.
  152. void expectFileForRequest(const std::string& filename);
  153. bool validateFileForRequest(const std::string& filename);
  154. void processReceiveData(LLMessageSystem* mesgsys, void** user_data);
  155. void sendConfirmPacket(LLMessageSystem* mesgsys, U64 id, S32 packetnum,
  156. const LLHost& remote_host);
  157. // File sending routines
  158. void processFileRequest(LLMessageSystem* mesgsys, void** user_data);
  159. void processConfirmation(LLMessageSystem* mesgsys, void** user_data);
  160. void retransmitUnackedPackets();
  161. // Error handling
  162. void abortRequestById(U64 xfer_id, S32 result_code);
  163. void processAbort(LLMessageSystem* mesgsys, void** user_data);
  164. bool isHostFlooded(const LLHost& host);
  165. protected:
  166. // Implementation methods
  167. void startPendingDownloads();
  168. void addToList(LLXfer* xferp, xfer_list_t& xfer_list, bool is_priority);
  169. public:
  170. xfer_list_t mSendList;
  171. xfer_list_t mReceiveList;
  172. typedef std::list<LLHostStatus*> status_list_t;
  173. status_list_t mOutgoingHosts;
  174. protected:
  175. S32 mMaxIncomingXfers;
  176. S32 mMaxOutgoingXfersPerCircuit;
  177. // At this limit, kill off the connection
  178. S32 mHardLimitOutgoingXfersPerCircuit;
  179. std::deque<LLXferAckInfo> mXferAckQueue;
  180. LLThrottle mAckThrottle;
  181. // Files that are authorized to transfer out
  182. std::multiset<std::string> mExpectedTransfers;
  183. // Files that are authorized to be downloaded on top of
  184. std::multiset<std::string> mExpectedRequests;
  185. // Use ack throttling to cap file xfer bandwidth
  186. bool mUseAckThrottling;
  187. };
  188. extern LLXferManager* gXferManagerp;
  189. // Message system callbacks
  190. void process_confirm_packet(LLMessageSystem* mesgsys, void** user_data);
  191. void process_request_xfer(LLMessageSystem* mesgsys, void** user_data);
  192. void continue_file_receive(LLMessageSystem* mesgsys, void** user_data);
  193. void process_abort_xfer(LLMessageSystem* mesgsys, void** user_data);
  194. #endif