/indra/llmessage/lltransfermanager.h

https://bitbucket.org/lindenlab/viewer-beta/ · C++ Header · 499 lines · 325 code · 112 blank · 62 comment · 0 complexity · 4dccf85ddbc014fbbc86791f85b5363f MD5 · raw file

  1. /**
  2. * @file lltransfermanager.h
  3. * @brief Improved transfer mechanism for moving data through the
  4. * message system.
  5. *
  6. * $LicenseInfo:firstyear=2006&license=viewerlgpl$
  7. * Second Life Viewer Source Code
  8. * Copyright (C) 2010, Linden Research, Inc.
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation;
  13. * version 2.1 of the License only.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  25. * $/LicenseInfo$
  26. */
  27. #ifndef LL_LLTRANSFERMANAGER_H
  28. #define LL_LLTRANSFERMANAGER_H
  29. #include <map>
  30. #include <list>
  31. #include "llhost.h"
  32. #include "lluuid.h"
  33. #include "llthrottle.h"
  34. #include "llpriqueuemap.h"
  35. #include "llassettype.h"
  36. //
  37. // Definition of the manager class for the new LLXfer replacement.
  38. // Provides prioritized, bandwidth-throttled transport of arbitrary
  39. // binary data between host/circuit combos
  40. //
  41. typedef enum e_transfer_channel_type
  42. {
  43. LLTCT_UNKNOWN = 0,
  44. LLTCT_MISC,
  45. LLTCT_ASSET,
  46. LLTCT_NUM_TYPES
  47. } LLTransferChannelType;
  48. typedef enum e_transfer_source_type
  49. {
  50. LLTST_UNKNOWN = 0,
  51. LLTST_FILE,
  52. LLTST_ASSET,
  53. LLTST_SIM_INV_ITEM, // Simulator specific, may not be handled
  54. LLTST_SIM_ESTATE, // Simulator specific, may not be handled
  55. LLTST_NUM_TYPES
  56. } LLTransferSourceType;
  57. typedef enum e_transfer_target_type
  58. {
  59. LLTTT_UNKNOWN = 0,
  60. LLTTT_FILE,
  61. LLTTT_VFILE,
  62. LLTTT_NUM_TYPES
  63. } LLTransferTargetType;
  64. // Errors are negative, expected values are positive.
  65. typedef enum e_status_codes
  66. {
  67. LLTS_OK = 0,
  68. LLTS_DONE = 1,
  69. LLTS_SKIP = 2,
  70. LLTS_ABORT = 3,
  71. LLTS_ERROR = -1,
  72. LLTS_UNKNOWN_SOURCE = -2, // Equivalent of a 404
  73. LLTS_INSUFFICIENT_PERMISSIONS = -3 // Not enough permissions
  74. } LLTSCode;
  75. // Types of requests for estate wide information
  76. typedef enum e_estate_type
  77. {
  78. ET_Covenant = 0,
  79. ET_NONE = -1
  80. } EstateAssetType;
  81. class LLMessageSystem;
  82. class LLDataPacker;
  83. class LLTransferConnection;
  84. class LLTransferSourceChannel;
  85. class LLTransferTargetChannel;
  86. class LLTransferSourceParams;
  87. class LLTransferTargetParams;
  88. class LLTransferSource;
  89. class LLTransferTarget;
  90. class LLTransferManager
  91. {
  92. public:
  93. LLTransferManager();
  94. virtual ~LLTransferManager();
  95. void init();
  96. void cleanup();
  97. void updateTransfers(); // Called per frame to push packets out on the various different channels.
  98. void cleanupConnection(const LLHost &host);
  99. LLTransferSourceChannel *getSourceChannel(const LLHost &host, const LLTransferChannelType stype);
  100. LLTransferTargetChannel *getTargetChannel(const LLHost &host, const LLTransferChannelType stype);
  101. LLTransferSource *findTransferSource(const LLUUID &transfer_id);
  102. BOOL isValid() const { return mValid; }
  103. static void processTransferRequest(LLMessageSystem *mesgsys, void **);
  104. static void processTransferInfo(LLMessageSystem *mesgsys, void **);
  105. static void processTransferPacket(LLMessageSystem *mesgsys, void **);
  106. static void processTransferAbort(LLMessageSystem *mesgsys, void **);
  107. static void reliablePacketCallback(void **, S32 result);
  108. S32 getTransferBitsIn(const LLTransferChannelType tctype) const { return mTransferBitsIn[tctype]; }
  109. S32 getTransferBitsOut(const LLTransferChannelType tctype) const { return mTransferBitsOut[tctype]; }
  110. void resetTransferBitsIn(const LLTransferChannelType tctype) { mTransferBitsIn[tctype] = 0; }
  111. void resetTransferBitsOut(const LLTransferChannelType tctype) { mTransferBitsOut[tctype] = 0; }
  112. void addTransferBitsIn(const LLTransferChannelType tctype, const S32 bits) { mTransferBitsIn[tctype] += bits; }
  113. void addTransferBitsOut(const LLTransferChannelType tctype, const S32 bits) { mTransferBitsOut[tctype] += bits; }
  114. protected:
  115. LLTransferConnection *getTransferConnection(const LLHost &host);
  116. BOOL removeTransferConnection(const LLHost &host);
  117. protected:
  118. // Convenient typedefs
  119. typedef std::map<LLHost, LLTransferConnection *> host_tc_map;
  120. BOOL mValid;
  121. LLHost mHost;
  122. S32 mTransferBitsIn[LLTTT_NUM_TYPES];
  123. S32 mTransferBitsOut[LLTTT_NUM_TYPES];
  124. // We keep a map between each host and LLTransferConnection.
  125. host_tc_map mTransferConnections;
  126. };
  127. //
  128. // Keeps tracks of all channels to/from a particular host.
  129. //
  130. class LLTransferConnection
  131. {
  132. public:
  133. LLTransferConnection(const LLHost &host);
  134. virtual ~LLTransferConnection();
  135. void updateTransfers();
  136. LLTransferSourceChannel *getSourceChannel(const LLTransferChannelType type);
  137. LLTransferTargetChannel *getTargetChannel(const LLTransferChannelType type);
  138. // Convenient typedefs
  139. typedef std::list<LLTransferSourceChannel *>::iterator tsc_iter;
  140. typedef std::list<LLTransferTargetChannel *>::iterator ttc_iter;
  141. friend class LLTransferManager;
  142. protected:
  143. LLHost mHost;
  144. std::list<LLTransferSourceChannel *> mTransferSourceChannels;
  145. std::list<LLTransferTargetChannel *> mTransferTargetChannels;
  146. };
  147. //
  148. // A channel which is pushing data out.
  149. //
  150. class LLTransferSourceChannel
  151. {
  152. public:
  153. LLTransferSourceChannel(const LLTransferChannelType channel_type,
  154. const LLHost &host);
  155. virtual ~LLTransferSourceChannel();
  156. void updateTransfers();
  157. void updatePriority(LLTransferSource *tsp, const F32 priority);
  158. void addTransferSource(LLTransferSource *sourcep);
  159. LLTransferSource *findTransferSource(const LLUUID &transfer_id);
  160. BOOL deleteTransfer(LLTransferSource *tsp);
  161. void setThrottleID(const S32 throttle_id) { mThrottleID = throttle_id; }
  162. LLTransferChannelType getChannelType() const { return mChannelType; }
  163. LLHost getHost() const { return mHost; }
  164. protected:
  165. typedef std::list<LLTransferSource *>::iterator ts_iter;
  166. LLTransferChannelType mChannelType;
  167. LLHost mHost;
  168. LLPriQueueMap<LLTransferSource*> mTransferSources;
  169. // The throttle that this source channel should use
  170. S32 mThrottleID;
  171. };
  172. //
  173. // A channel receiving data from a source.
  174. //
  175. class LLTransferTargetChannel
  176. {
  177. public:
  178. LLTransferTargetChannel(const LLTransferChannelType channel_type, const LLHost &host);
  179. virtual ~LLTransferTargetChannel();
  180. void requestTransfer(const LLTransferSourceParams &source_params,
  181. const LLTransferTargetParams &target_params,
  182. const F32 priority);
  183. LLTransferTarget *findTransferTarget(const LLUUID &transfer_id);
  184. BOOL deleteTransfer(LLTransferTarget *ttp);
  185. LLTransferChannelType getChannelType() const { return mChannelType; }
  186. LLHost getHost() const { return mHost; }
  187. protected:
  188. void sendTransferRequest(LLTransferTarget *targetp,
  189. const LLTransferSourceParams &params,
  190. const F32 priority);
  191. void addTransferTarget(LLTransferTarget *targetp);
  192. friend class LLTransferTarget;
  193. friend class LLTransferManager;
  194. protected:
  195. typedef std::list<LLTransferTarget *>::iterator tt_iter;
  196. LLTransferChannelType mChannelType;
  197. LLHost mHost;
  198. std::list<LLTransferTarget *> mTransferTargets;
  199. };
  200. class LLTransferSourceParams
  201. {
  202. public:
  203. LLTransferSourceParams(const LLTransferSourceType type) : mType(type) { }
  204. virtual ~LLTransferSourceParams();
  205. virtual void packParams(LLDataPacker &dp) const = 0;
  206. virtual BOOL unpackParams(LLDataPacker &dp) = 0;
  207. LLTransferSourceType getType() const { return mType; }
  208. protected:
  209. LLTransferSourceType mType;
  210. };
  211. //
  212. // LLTransferSource is an interface, all transfer sources should be derived from it.
  213. //
  214. typedef LLTransferSource *(*LLTransferSourceCreateFunc)(const LLUUID &id, const F32 priority);
  215. class LLTransferSource
  216. {
  217. public:
  218. LLUUID getID() { return mID; }
  219. friend class LLTransferManager;
  220. friend class LLTransferSourceChannel;
  221. protected:
  222. LLTransferSource(const LLTransferSourceType source_type,
  223. const LLUUID &request_id,
  224. const F32 priority);
  225. virtual ~LLTransferSource();
  226. void sendTransferStatus(LLTSCode status); // When you've figured out your transfer status, do this
  227. virtual void initTransfer() = 0;
  228. virtual F32 updatePriority() = 0;
  229. virtual LLTSCode dataCallback(const S32 packet_id,
  230. const S32 max_bytes,
  231. U8 **datap,
  232. S32 &returned_bytes,
  233. BOOL &delete_returned) = 0;
  234. // The completionCallback is GUARANTEED to be called before the destructor.
  235. virtual void completionCallback(const LLTSCode status) = 0;
  236. virtual void packParams(LLDataPacker& dp) const = 0;
  237. virtual BOOL unpackParams(LLDataPacker& dp) = 0;
  238. virtual S32 getNextPacketID() { return mLastPacketID + 1; }
  239. virtual void setLastPacketID(const S32 packet_id) { mLastPacketID = packet_id; }
  240. // For now, no self-induced priority changes
  241. F32 getPriority() { return mPriority; }
  242. void setPriority(const F32 pri) { mPriority = pri; }
  243. virtual void abortTransfer(); // DON'T USE THIS ONE, used internally by LLTransferManager
  244. static LLTransferSource *createSource(const LLTransferSourceType stype,
  245. const LLUUID &request_id,
  246. const F32 priority);
  247. static void registerSourceType(const LLTransferSourceType stype, LLTransferSourceCreateFunc);
  248. static void sSetPriority(LLTransferSource *&tsp, const F32 priority);
  249. static F32 sGetPriority(LLTransferSource *&tsp);
  250. protected:
  251. typedef std::map<LLTransferSourceType, LLTransferSourceCreateFunc> stype_scfunc_map;
  252. static stype_scfunc_map sSourceCreateMap;
  253. LLTransferSourceType mType;
  254. LLUUID mID;
  255. LLTransferSourceChannel *mChannelp;
  256. F32 mPriority;
  257. S32 mSize;
  258. S32 mLastPacketID;
  259. };
  260. class LLTransferTargetParams
  261. {
  262. public:
  263. LLTransferTargetParams(const LLTransferTargetType type) : mType(type) {}
  264. LLTransferTargetType getType() const { return mType; }
  265. protected:
  266. LLTransferTargetType mType;
  267. };
  268. class LLTransferPacket
  269. {
  270. // Used for storing a packet that's being delivered later because it's out of order.
  271. // ONLY should be accessed by the following two classes, for now.
  272. friend class LLTransferTarget;
  273. friend class LLTransferManager;
  274. protected:
  275. LLTransferPacket(const S32 packet_id, const LLTSCode status, const U8 *datap, const S32 size);
  276. virtual ~LLTransferPacket();
  277. protected:
  278. S32 mPacketID;
  279. LLTSCode mStatus;
  280. U8 *mDatap;
  281. S32 mSize;
  282. };
  283. class LLTransferTarget
  284. {
  285. public:
  286. LLTransferTarget(
  287. LLTransferTargetType target_type,
  288. const LLUUID& transfer_id,
  289. LLTransferSourceType source_type);
  290. virtual ~LLTransferTarget();
  291. // Accessors
  292. LLUUID getID() const { return mID; }
  293. LLTransferTargetType getType() const { return mType; }
  294. LLTransferTargetChannel *getChannel() const { return mChannelp; }
  295. LLTransferSourceType getSourceType() const { return mSourceType; }
  296. // Static functionality
  297. static LLTransferTarget* createTarget(
  298. LLTransferTargetType target_type,
  299. const LLUUID& request_id,
  300. LLTransferSourceType source_type);
  301. // friends
  302. friend class LLTransferManager;
  303. friend class LLTransferTargetChannel;
  304. protected:
  305. // Implementation
  306. virtual bool unpackParams(LLDataPacker& dp) = 0;
  307. virtual void applyParams(const LLTransferTargetParams &params) = 0;
  308. virtual LLTSCode dataCallback(const S32 packet_id, U8 *in_datap, const S32 in_size) = 0;
  309. // The completionCallback is GUARANTEED to be called before the destructor, so all handling
  310. // of errors/aborts should be done here.
  311. virtual void completionCallback(const LLTSCode status) = 0;
  312. void abortTransfer();
  313. virtual S32 getNextPacketID() { return mLastPacketID + 1; }
  314. virtual void setLastPacketID(const S32 packet_id) { mLastPacketID = packet_id; }
  315. void setSize(const S32 size) { mSize = size; }
  316. void setGotInfo(const BOOL got_info) { mGotInfo = got_info; }
  317. BOOL gotInfo() const { return mGotInfo; }
  318. bool addDelayedPacket(
  319. const S32 packet_id,
  320. const LLTSCode status,
  321. U8* datap,
  322. const S32 size);
  323. protected:
  324. typedef std::map<S32, LLTransferPacket *> transfer_packet_map;
  325. typedef std::map<S32, LLTransferPacket *>::iterator tpm_iter;
  326. LLTransferTargetType mType;
  327. LLTransferSourceType mSourceType;
  328. LLUUID mID;
  329. LLTransferTargetChannel *mChannelp;
  330. BOOL mGotInfo;
  331. S32 mSize;
  332. S32 mLastPacketID;
  333. transfer_packet_map mDelayedPacketMap; // Packets that are waiting because of missing/out of order issues
  334. };
  335. // Hack, here so it's publicly available even though LLTransferSourceInvItem is only available on the simulator
  336. class LLTransferSourceParamsInvItem: public LLTransferSourceParams
  337. {
  338. public:
  339. LLTransferSourceParamsInvItem();
  340. virtual ~LLTransferSourceParamsInvItem() {}
  341. /*virtual*/ void packParams(LLDataPacker &dp) const;
  342. /*virtual*/ BOOL unpackParams(LLDataPacker &dp);
  343. void setAgentSession(const LLUUID &agent_id, const LLUUID &session_id);
  344. void setInvItem(const LLUUID &owner_id, const LLUUID &task_id, const LLUUID &item_id);
  345. void setAsset(const LLUUID &asset_id, const LLAssetType::EType at);
  346. LLUUID getAgentID() const { return mAgentID; }
  347. LLUUID getSessionID() const { return mSessionID; }
  348. LLUUID getOwnerID() const { return mOwnerID; }
  349. LLUUID getTaskID() const { return mTaskID; }
  350. LLUUID getItemID() const { return mItemID; }
  351. LLUUID getAssetID() const { return mAssetID; }
  352. LLAssetType::EType getAssetType() const { return mAssetType; }
  353. protected:
  354. LLUUID mAgentID;
  355. LLUUID mSessionID;
  356. LLUUID mOwnerID;
  357. LLUUID mTaskID;
  358. LLUUID mItemID;
  359. LLUUID mAssetID;
  360. LLAssetType::EType mAssetType;
  361. };
  362. // Hack, here so it's publicly available even though LLTransferSourceEstate is only available on the simulator
  363. class LLTransferSourceParamsEstate: public LLTransferSourceParams
  364. {
  365. public:
  366. LLTransferSourceParamsEstate();
  367. virtual ~LLTransferSourceParamsEstate() {}
  368. /*virtual*/ void packParams(LLDataPacker &dp) const;
  369. /*virtual*/ BOOL unpackParams(LLDataPacker &dp);
  370. void setAgentSession(const LLUUID &agent_id, const LLUUID &session_id);
  371. void setEstateAssetType(const EstateAssetType etype);
  372. void setAsset(const LLUUID &asset_id, const LLAssetType::EType at);
  373. LLUUID getAgentID() const { return mAgentID; }
  374. LLUUID getSessionID() const { return mSessionID; }
  375. EstateAssetType getEstateAssetType() const { return mEstateAssetType; }
  376. LLUUID getAssetID() const { return mAssetID; }
  377. LLAssetType::EType getAssetType() const { return mAssetType; }
  378. protected:
  379. LLUUID mAgentID;
  380. LLUUID mSessionID;
  381. EstateAssetType mEstateAssetType;
  382. // these are set on the sim based on estateinfotype
  383. LLUUID mAssetID;
  384. LLAssetType::EType mAssetType;
  385. };
  386. extern LLTransferManager gTransferManager;
  387. #endif//LL_LLTRANSFERMANAGER_H