PageRenderTime 91ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llmessage/llmessagetemplate.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 421 lines | 330 code | 60 blank | 31 comment | 18 complexity | a04d8627eb8a9c12f1626b0b792e9c2d MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llmessagetemplate.h
  3. * @brief Declaration of the message template classes.
  4. *
  5. * $LicenseInfo:firstyear=2007&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. #ifndef LL_LLMESSAGETEMPLATE_H
  27. #define LL_LLMESSAGETEMPLATE_H
  28. #include "lldarray.h"
  29. #include "message.h" // TODO: babbage: Remove...
  30. #include "llstat.h"
  31. #include "llstl.h"
  32. class LLMsgVarData
  33. {
  34. public:
  35. LLMsgVarData() : mName(NULL), mSize(-1), mDataSize(-1), mData(NULL), mType(MVT_U8)
  36. {
  37. }
  38. LLMsgVarData(const char *name, EMsgVariableType type) : mSize(-1), mDataSize(-1), mData(NULL), mType(type)
  39. {
  40. mName = (char *)name;
  41. }
  42. ~LLMsgVarData()
  43. {
  44. // copy constructor just copies the mData pointer, so only delete mData explicitly
  45. }
  46. void deleteData()
  47. {
  48. delete[] mData;
  49. mData = NULL;
  50. }
  51. void addData(const void *indata, S32 size, EMsgVariableType type, S32 data_size = -1);
  52. char *getName() const { return mName; }
  53. S32 getSize() const { return mSize; }
  54. void *getData() { return (void*)mData; }
  55. const void *getData() const { return (const void*)mData; }
  56. S32 getDataSize() const { return mDataSize; }
  57. EMsgVariableType getType() const { return mType; }
  58. protected:
  59. char *mName;
  60. S32 mSize;
  61. S32 mDataSize;
  62. U8 *mData;
  63. EMsgVariableType mType;
  64. };
  65. class LLMsgBlkData
  66. {
  67. public:
  68. LLMsgBlkData(const char *name, S32 blocknum) : mBlockNumber(blocknum), mTotalSize(-1)
  69. {
  70. mName = (char *)name;
  71. }
  72. ~LLMsgBlkData()
  73. {
  74. for (msg_var_data_map_t::iterator iter = mMemberVarData.begin();
  75. iter != mMemberVarData.end(); iter++)
  76. {
  77. iter->deleteData();
  78. }
  79. }
  80. void addVariable(const char *name, EMsgVariableType type)
  81. {
  82. LLMsgVarData tmp(name,type);
  83. mMemberVarData[name] = tmp;
  84. }
  85. void addData(char *name, const void *data, S32 size, EMsgVariableType type, S32 data_size = -1)
  86. {
  87. LLMsgVarData* temp = &mMemberVarData[name]; // creates a new entry if one doesn't exist
  88. temp->addData(data, size, type, data_size);
  89. }
  90. S32 mBlockNumber;
  91. typedef LLDynamicArrayIndexed<LLMsgVarData, const char *, 8> msg_var_data_map_t;
  92. msg_var_data_map_t mMemberVarData;
  93. char *mName;
  94. S32 mTotalSize;
  95. };
  96. class LLMsgData
  97. {
  98. public:
  99. LLMsgData(const char *name) : mTotalSize(-1)
  100. {
  101. mName = (char *)name;
  102. }
  103. ~LLMsgData()
  104. {
  105. for_each(mMemberBlocks.begin(), mMemberBlocks.end(), DeletePairedPointer());
  106. }
  107. void addBlock(LLMsgBlkData *blockp)
  108. {
  109. mMemberBlocks[blockp->mName] = blockp;
  110. }
  111. void addDataFast(char *blockname, char *varname, const void *data, S32 size, EMsgVariableType type, S32 data_size = -1);
  112. public:
  113. typedef std::map<char*, LLMsgBlkData*> msg_blk_data_map_t;
  114. msg_blk_data_map_t mMemberBlocks;
  115. char *mName;
  116. S32 mTotalSize;
  117. };
  118. // LLMessage* classes store the template of messages
  119. class LLMessageVariable
  120. {
  121. public:
  122. LLMessageVariable() : mName(NULL), mType(MVT_NULL), mSize(-1)
  123. {
  124. }
  125. LLMessageVariable(char *name) : mType(MVT_NULL), mSize(-1)
  126. {
  127. mName = name;
  128. }
  129. LLMessageVariable(const char *name, const EMsgVariableType type, const S32 size) : mType(type), mSize(size)
  130. {
  131. mName = LLMessageStringTable::getInstance()->getString(name);
  132. }
  133. ~LLMessageVariable() {}
  134. friend std::ostream& operator<<(std::ostream& s, LLMessageVariable &msg);
  135. EMsgVariableType getType() const { return mType; }
  136. S32 getSize() const { return mSize; }
  137. char *getName() const { return mName; }
  138. protected:
  139. char *mName;
  140. EMsgVariableType mType;
  141. S32 mSize;
  142. };
  143. typedef enum e_message_block_type
  144. {
  145. MBT_NULL,
  146. MBT_SINGLE,
  147. MBT_MULTIPLE,
  148. MBT_VARIABLE,
  149. MBT_EOF
  150. } EMsgBlockType;
  151. class LLMessageBlock
  152. {
  153. public:
  154. LLMessageBlock(const char *name, EMsgBlockType type, S32 number = 1) : mType(type), mNumber(number), mTotalSize(0)
  155. {
  156. mName = LLMessageStringTable::getInstance()->getString(name);
  157. }
  158. ~LLMessageBlock()
  159. {
  160. for_each(mMemberVariables.begin(), mMemberVariables.end(), DeletePointer());
  161. }
  162. void addVariable(char *name, const EMsgVariableType type, const S32 size)
  163. {
  164. LLMessageVariable** varp = &mMemberVariables[name];
  165. if (*varp != NULL)
  166. {
  167. llerrs << name << " has already been used as a variable name!" << llendl;
  168. }
  169. *varp = new LLMessageVariable(name, type, size);
  170. if (((*varp)->getType() != MVT_VARIABLE)
  171. &&(mTotalSize != -1))
  172. {
  173. mTotalSize += (*varp)->getSize();
  174. }
  175. else
  176. {
  177. mTotalSize = -1;
  178. }
  179. }
  180. EMsgVariableType getVariableType(char *name)
  181. {
  182. return (mMemberVariables[name])->getType();
  183. }
  184. S32 getVariableSize(char *name)
  185. {
  186. return (mMemberVariables[name])->getSize();
  187. }
  188. const LLMessageVariable* getVariable(char* name) const
  189. {
  190. message_variable_map_t::const_iterator iter = mMemberVariables.find(name);
  191. return iter != mMemberVariables.end()? *iter : NULL;
  192. }
  193. friend std::ostream& operator<<(std::ostream& s, LLMessageBlock &msg);
  194. typedef LLDynamicArrayIndexed<LLMessageVariable*, const char *, 8> message_variable_map_t;
  195. message_variable_map_t mMemberVariables;
  196. char *mName;
  197. EMsgBlockType mType;
  198. S32 mNumber;
  199. S32 mTotalSize;
  200. };
  201. enum EMsgFrequency
  202. {
  203. MFT_NULL = 0, // value is size of message number in bytes
  204. MFT_HIGH = 1,
  205. MFT_MEDIUM = 2,
  206. MFT_LOW = 4
  207. };
  208. typedef enum e_message_trust
  209. {
  210. MT_TRUST,
  211. MT_NOTRUST
  212. } EMsgTrust;
  213. enum EMsgEncoding
  214. {
  215. ME_UNENCODED,
  216. ME_ZEROCODED
  217. };
  218. enum EMsgDeprecation
  219. {
  220. MD_NOTDEPRECATED,
  221. MD_UDPDEPRECATED,
  222. MD_UDPBLACKLISTED,
  223. MD_DEPRECATED
  224. };
  225. class LLMessageTemplate
  226. {
  227. public:
  228. LLMessageTemplate(const char *name, U32 message_number, EMsgFrequency freq)
  229. :
  230. //mMemberBlocks(),
  231. mName(NULL),
  232. mFrequency(freq),
  233. mTrust(MT_NOTRUST),
  234. mEncoding(ME_ZEROCODED),
  235. mDeprecation(MD_NOTDEPRECATED),
  236. mMessageNumber(message_number),
  237. mTotalSize(0),
  238. mReceiveCount(0),
  239. mReceiveBytes(0),
  240. mReceiveInvalid(0),
  241. mDecodeTimeThisFrame(0.f),
  242. mTotalDecoded(0),
  243. mTotalDecodeTime(0.f),
  244. mMaxDecodeTimePerMsg(0.f),
  245. mBanFromTrusted(false),
  246. mBanFromUntrusted(false),
  247. mHandlerFunc(NULL),
  248. mUserData(NULL)
  249. {
  250. mName = LLMessageStringTable::getInstance()->getString(name);
  251. }
  252. ~LLMessageTemplate()
  253. {
  254. for_each(mMemberBlocks.begin(), mMemberBlocks.end(), DeletePointer());
  255. }
  256. void addBlock(LLMessageBlock *blockp)
  257. {
  258. LLMessageBlock** member_blockp = &mMemberBlocks[blockp->mName];
  259. if (*member_blockp != NULL)
  260. {
  261. llerrs << "Block " << blockp->mName
  262. << "has already been used as a block name!" << llendl;
  263. }
  264. *member_blockp = blockp;
  265. if ( (mTotalSize != -1)
  266. &&(blockp->mTotalSize != -1)
  267. &&( (blockp->mType == MBT_SINGLE)
  268. ||(blockp->mType == MBT_MULTIPLE)))
  269. {
  270. mTotalSize += blockp->mNumber*blockp->mTotalSize;
  271. }
  272. else
  273. {
  274. mTotalSize = -1;
  275. }
  276. }
  277. LLMessageBlock *getBlock(char *name)
  278. {
  279. return mMemberBlocks[name];
  280. }
  281. // Trusted messages can only be recieved on trusted circuits.
  282. void setTrust(EMsgTrust t)
  283. {
  284. mTrust = t;
  285. }
  286. EMsgTrust getTrust(void) const
  287. {
  288. return mTrust;
  289. }
  290. // controls for how the message should be encoded
  291. void setEncoding(EMsgEncoding e)
  292. {
  293. mEncoding = e;
  294. }
  295. EMsgEncoding getEncoding() const
  296. {
  297. return mEncoding;
  298. }
  299. void setDeprecation(EMsgDeprecation d)
  300. {
  301. mDeprecation = d;
  302. }
  303. EMsgDeprecation getDeprecation() const
  304. {
  305. return mDeprecation;
  306. }
  307. void setHandlerFunc(void (*handler_func)(LLMessageSystem *msgsystem, void **user_data), void **user_data)
  308. {
  309. mHandlerFunc = handler_func;
  310. mUserData = user_data;
  311. }
  312. BOOL callHandlerFunc(LLMessageSystem *msgsystem) const
  313. {
  314. if (mHandlerFunc)
  315. {
  316. LLPerfBlock msg_cb_time("msg_cb", mName);
  317. mHandlerFunc(msgsystem, mUserData);
  318. return TRUE;
  319. }
  320. return FALSE;
  321. }
  322. bool isUdpBanned() const
  323. {
  324. return mDeprecation == MD_UDPBLACKLISTED;
  325. }
  326. void banUdp();
  327. bool isBanned(bool trustedSource) const
  328. {
  329. return trustedSource ? mBanFromTrusted : mBanFromUntrusted;
  330. }
  331. friend std::ostream& operator<<(std::ostream& s, LLMessageTemplate &msg);
  332. const LLMessageBlock* getBlock(char* name) const
  333. {
  334. message_block_map_t::const_iterator iter = mMemberBlocks.find(name);
  335. return iter != mMemberBlocks.end()? *iter : NULL;
  336. }
  337. public:
  338. typedef LLDynamicArrayIndexed<LLMessageBlock*, char*, 8> message_block_map_t;
  339. message_block_map_t mMemberBlocks;
  340. char *mName;
  341. EMsgFrequency mFrequency;
  342. EMsgTrust mTrust;
  343. EMsgEncoding mEncoding;
  344. EMsgDeprecation mDeprecation;
  345. U32 mMessageNumber;
  346. S32 mTotalSize;
  347. U32 mReceiveCount; // how many of this template have been received since last reset
  348. U32 mReceiveBytes; // How many bytes received
  349. U32 mReceiveInvalid; // How many "invalid" packets
  350. F32 mDecodeTimeThisFrame; // Total seconds spent decoding this frame
  351. U32 mTotalDecoded; // Total messages successfully decoded
  352. F32 mTotalDecodeTime; // Total time successfully decoding messages
  353. F32 mMaxDecodeTimePerMsg;
  354. bool mBanFromTrusted;
  355. bool mBanFromUntrusted;
  356. private:
  357. // message handler function (this is set by each application)
  358. void (*mHandlerFunc)(LLMessageSystem *msgsystem, void **user_data);
  359. void **mUserData;
  360. };
  361. #endif // LL_LLMESSAGETEMPLATE_H