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