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