00001
00032 #ifndef LL_LLMESSAGETEMPLATE_H
00033 #define LL_LLMESSAGETEMPLATE_H
00034
00035 #include "lldarray.h"
00036 #include "message.h"
00037 #include "llstl.h"
00038
00039 class LLMsgVarData
00040 {
00041 public:
00042 LLMsgVarData() : mName(NULL), mSize(-1), mDataSize(-1), mData(NULL), mType(MVT_U8)
00043 {
00044 }
00045
00046 LLMsgVarData(const char *name, EMsgVariableType type) : mSize(-1), mDataSize(-1), mData(NULL), mType(type)
00047 {
00048 mName = (char *)name;
00049 }
00050
00051 ~LLMsgVarData()
00052 {
00053
00054 }
00055
00056 void deleteData()
00057 {
00058 delete[] mData;
00059 mData = NULL;
00060 }
00061
00062 void addData(const void *indata, S32 size, EMsgVariableType type, S32 data_size = -1);
00063
00064 char *getName() const { return mName; }
00065 S32 getSize() const { return mSize; }
00066 void *getData() { return (void*)mData; }
00067 const void *getData() const { return (const void*)mData; }
00068 S32 getDataSize() const { return mDataSize; }
00069 EMsgVariableType getType() const { return mType; }
00070
00071 protected:
00072 char *mName;
00073 S32 mSize;
00074 S32 mDataSize;
00075
00076 U8 *mData;
00077 EMsgVariableType mType;
00078 };
00079
00080 class LLMsgBlkData
00081 {
00082 public:
00083 LLMsgBlkData(const char *name, S32 blocknum) : mOffset(-1), mBlockNumber(blocknum), mTotalSize(-1)
00084 {
00085 mName = (char *)name;
00086 }
00087
00088 ~LLMsgBlkData()
00089 {
00090 for (msg_var_data_map_t::iterator iter = mMemberVarData.begin();
00091 iter != mMemberVarData.end(); iter++)
00092 {
00093 iter->deleteData();
00094 }
00095 }
00096
00097 void addVariable(const char *name, EMsgVariableType type)
00098 {
00099 LLMsgVarData tmp(name,type);
00100 mMemberVarData[name] = tmp;
00101 }
00102
00103 void addData(char *name, const void *data, S32 size, EMsgVariableType type, S32 data_size = -1)
00104 {
00105 LLMsgVarData* temp = &mMemberVarData[name];
00106 temp->addData(data, size, type, data_size);
00107 }
00108
00109 S32 mOffset;
00110 S32 mBlockNumber;
00111 typedef LLDynamicArrayIndexed<LLMsgVarData, const char *, 8> msg_var_data_map_t;
00112 msg_var_data_map_t mMemberVarData;
00113 char *mName;
00114 S32 mTotalSize;
00115 };
00116
00117 class LLMsgData
00118 {
00119 public:
00120 LLMsgData(const char *name) : mTotalSize(-1)
00121 {
00122 mName = (char *)name;
00123 }
00124 ~LLMsgData()
00125 {
00126 for_each(mMemberBlocks.begin(), mMemberBlocks.end(), DeletePairedPointer());
00127 }
00128
00129 void addBlock(LLMsgBlkData *blockp)
00130 {
00131 mMemberBlocks[blockp->mName] = blockp;
00132 }
00133
00134 void addDataFast(char *blockname, char *varname, const void *data, S32 size, EMsgVariableType type, S32 data_size = -1);
00135
00136 public:
00137 S32 mOffset;
00138 typedef std::map<char*, LLMsgBlkData*> msg_blk_data_map_t;
00139 msg_blk_data_map_t mMemberBlocks;
00140 char *mName;
00141 S32 mTotalSize;
00142 };
00143
00144
00145 class LLMessageVariable
00146 {
00147 public:
00148 LLMessageVariable() : mName(NULL), mType(MVT_NULL), mSize(-1)
00149 {
00150 }
00151
00152 LLMessageVariable(char *name) : mType(MVT_NULL), mSize(-1)
00153 {
00154 mName = name;
00155 }
00156
00157 LLMessageVariable(const char *name, const EMsgVariableType type, const S32 size) : mType(type), mSize(size)
00158 {
00159 mName = LLMessageStringTable::getInstance()->getString(name);
00160 }
00161
00162 ~LLMessageVariable() {}
00163
00164 friend std::ostream& operator<<(std::ostream& s, LLMessageVariable &msg);
00165
00166 EMsgVariableType getType() const { return mType; }
00167 S32 getSize() const { return mSize; }
00168 char *getName() const { return mName; }
00169 protected:
00170 char *mName;
00171 EMsgVariableType mType;
00172 S32 mSize;
00173 };
00174
00175
00176 typedef enum e_message_block_type
00177 {
00178 MBT_NULL,
00179 MBT_SINGLE,
00180 MBT_MULTIPLE,
00181 MBT_VARIABLE,
00182 MBT_EOF
00183 } EMsgBlockType;
00184
00185 class LLMessageBlock
00186 {
00187 public:
00188 LLMessageBlock(const char *name, EMsgBlockType type, S32 number = 1) : mType(type), mNumber(number), mTotalSize(0)
00189 {
00190 mName = LLMessageStringTable::getInstance()->getString(name);
00191 }
00192
00193 ~LLMessageBlock()
00194 {
00195 for_each(mMemberVariables.begin(), mMemberVariables.end(), DeletePointer());
00196 }
00197
00198 void addVariable(char *name, const EMsgVariableType type, const S32 size)
00199 {
00200 LLMessageVariable** varp = &mMemberVariables[name];
00201 if (*varp != NULL)
00202 {
00203 llerrs << name << " has already been used as a variable name!" << llendl;
00204 }
00205 *varp = new LLMessageVariable(name, type, size);
00206 if (((*varp)->getType() != MVT_VARIABLE)
00207 &&(mTotalSize != -1))
00208 {
00209 mTotalSize += (*varp)->getSize();
00210 }
00211 else
00212 {
00213 mTotalSize = -1;
00214 }
00215 }
00216
00217 EMsgVariableType getVariableType(char *name)
00218 {
00219 return (mMemberVariables[name])->getType();
00220 }
00221
00222 S32 getVariableSize(char *name)
00223 {
00224 return (mMemberVariables[name])->getSize();
00225 }
00226
00227 const LLMessageVariable* getVariable(char* name) const
00228 {
00229 message_variable_map_t::const_iterator iter = mMemberVariables.find(name);
00230 return iter != mMemberVariables.end()? *iter : NULL;
00231 }
00232
00233 friend std::ostream& operator<<(std::ostream& s, LLMessageBlock &msg);
00234
00235 typedef LLDynamicArrayIndexed<LLMessageVariable*, const char *, 8> message_variable_map_t;
00236 message_variable_map_t mMemberVariables;
00237 char *mName;
00238 EMsgBlockType mType;
00239 S32 mNumber;
00240 S32 mTotalSize;
00241 };
00242
00243
00244 enum EMsgFrequency
00245 {
00246 MFT_NULL = 0,
00247 MFT_HIGH = 1,
00248 MFT_MEDIUM = 2,
00249 MFT_LOW = 4
00250 };
00251
00252 typedef enum e_message_trust
00253 {
00254 MT_TRUST,
00255 MT_NOTRUST
00256 } EMsgTrust;
00257
00258 enum EMsgEncoding
00259 {
00260 ME_UNENCODED,
00261 ME_ZEROCODED
00262 };
00263
00264 enum EMsgDeprecation
00265 {
00266 MD_NOTDEPRECATED,
00267 MD_UDPDEPRECATED,
00268 MD_DEPRECATED
00269 };
00270
00271 class LLMessageTemplate
00272 {
00273 public:
00274 LLMessageTemplate(const char *name, U32 message_number, EMsgFrequency freq)
00275 :
00276
00277 mName(NULL),
00278 mFrequency(freq),
00279 mTrust(MT_NOTRUST),
00280 mEncoding(ME_ZEROCODED),
00281 mDeprecation(MD_NOTDEPRECATED),
00282 mMessageNumber(message_number),
00283 mTotalSize(0),
00284 mReceiveCount(0),
00285 mReceiveBytes(0),
00286 mReceiveInvalid(0),
00287 mDecodeTimeThisFrame(0.f),
00288 mTotalDecoded(0),
00289 mTotalDecodeTime(0.f),
00290 mMaxDecodeTimePerMsg(0.f),
00291 mBanFromTrusted(false),
00292 mBanFromUntrusted(false),
00293 mHandlerFunc(NULL),
00294 mUserData(NULL)
00295 {
00296 mName = LLMessageStringTable::getInstance()->getString(name);
00297 }
00298
00299 ~LLMessageTemplate()
00300 {
00301 for_each(mMemberBlocks.begin(), mMemberBlocks.end(), DeletePointer());
00302 }
00303
00304 void addBlock(LLMessageBlock *blockp)
00305 {
00306 LLMessageBlock** member_blockp = &mMemberBlocks[blockp->mName];
00307 if (*member_blockp != NULL)
00308 {
00309 llerrs << "Block " << blockp->mName
00310 << "has already been used as a block name!" << llendl;
00311 }
00312 *member_blockp = blockp;
00313 if ( (mTotalSize != -1)
00314 &&(blockp->mTotalSize != -1)
00315 &&( (blockp->mType == MBT_SINGLE)
00316 ||(blockp->mType == MBT_MULTIPLE)))
00317 {
00318 mTotalSize += blockp->mNumber*blockp->mTotalSize;
00319 }
00320 else
00321 {
00322 mTotalSize = -1;
00323 }
00324 }
00325
00326 LLMessageBlock *getBlock(char *name)
00327 {
00328 return mMemberBlocks[name];
00329 }
00330
00331
00332 void setTrust(EMsgTrust t)
00333 {
00334 mTrust = t;
00335 }
00336
00337 EMsgTrust getTrust(void) const
00338 {
00339 return mTrust;
00340 }
00341
00342
00343 void setEncoding(EMsgEncoding e)
00344 {
00345 mEncoding = e;
00346 }
00347 EMsgEncoding getEncoding() const
00348 {
00349 return mEncoding;
00350 }
00351
00352 void setDeprecation(EMsgDeprecation d)
00353 {
00354 mDeprecation = d;
00355 }
00356
00357 EMsgDeprecation getDeprecation() const
00358 {
00359 return mDeprecation;
00360 }
00361
00362 void setHandlerFunc(void (*handler_func)(LLMessageSystem *msgsystem, void **user_data), void **user_data)
00363 {
00364 mHandlerFunc = handler_func;
00365 mUserData = user_data;
00366 }
00367
00368 BOOL callHandlerFunc(LLMessageSystem *msgsystem) const
00369 {
00370 if (mHandlerFunc)
00371 {
00372 mHandlerFunc(msgsystem, mUserData);
00373 return TRUE;
00374 }
00375 return FALSE;
00376 }
00377
00378 bool isBanned(bool trustedSource) const
00379 {
00380 return trustedSource ? mBanFromTrusted : mBanFromUntrusted;
00381 }
00382
00383 friend std::ostream& operator<<(std::ostream& s, LLMessageTemplate &msg);
00384
00385 const LLMessageBlock* getBlock(char* name) const
00386 {
00387 message_block_map_t::const_iterator iter = mMemberBlocks.find(name);
00388 return iter != mMemberBlocks.end()? *iter : NULL;
00389 }
00390
00391 public:
00392 typedef LLDynamicArrayIndexed<LLMessageBlock*, char*, 8> message_block_map_t;
00393 message_block_map_t mMemberBlocks;
00394 char *mName;
00395 EMsgFrequency mFrequency;
00396 EMsgTrust mTrust;
00397 EMsgEncoding mEncoding;
00398 EMsgDeprecation mDeprecation;
00399 U32 mMessageNumber;
00400 S32 mTotalSize;
00401 U32 mReceiveCount;
00402 U32 mReceiveBytes;
00403 U32 mReceiveInvalid;
00404 F32 mDecodeTimeThisFrame;
00405 U32 mTotalDecoded;
00406 F32 mTotalDecodeTime;
00407 F32 mMaxDecodeTimePerMsg;
00408
00409 bool mBanFromTrusted;
00410 bool mBanFromUntrusted;
00411
00412 private:
00413
00414 void (*mHandlerFunc)(LLMessageSystem *msgsystem, void **user_data);
00415 void **mUserData;
00416 };
00417
00418 #endif // LL_LLMESSAGETEMPLATE_H