PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llmessage/lldispatcher.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 147 lines | 91 code | 13 blank | 43 comment | 12 complexity | de9243eac3ee220c29ddee47211b0740 MD5 | raw file
  1/** 
  2 * @file lldispatcher.cpp
  3 * @brief Implementation of the dispatcher object.
  4 *
  5 * $LicenseInfo:firstyear=2004&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#include "linden_common.h"
 28
 29#include "lldispatcher.h"
 30
 31#include <algorithm>
 32#include "llstl.h"
 33#include "message.h"
 34
 35///----------------------------------------------------------------------------
 36/// Class lldispatcher
 37///----------------------------------------------------------------------------
 38
 39
 40LLDispatcher::LLDispatcher()
 41{
 42}
 43
 44LLDispatcher::~LLDispatcher()
 45{
 46}
 47
 48bool LLDispatcher::isHandlerPresent(const key_t& name) const
 49{
 50	if(mHandlers.find(name) != mHandlers.end())
 51	{
 52		return true;
 53	}
 54	return false;
 55}
 56
 57void LLDispatcher::copyAllHandlerNames(keys_t& names) const
 58{
 59	// copy the names onto the vector we are given
 60	std::transform(
 61		mHandlers.begin(),
 62		mHandlers.end(),
 63		std::back_insert_iterator<keys_t>(names),
 64		llselect1st<dispatch_map_t::value_type>());
 65}
 66
 67bool LLDispatcher::dispatch(
 68	const key_t& name,
 69	const LLUUID& invoice,
 70	const sparam_t& strings) const
 71{
 72	dispatch_map_t::const_iterator it = mHandlers.find(name);
 73	if(it != mHandlers.end())
 74	{
 75		LLDispatchHandler* func = (*it).second;
 76		return (*func)(this, name, invoice, strings);
 77	}
 78	llwarns << "Unable to find handler for Generic message: " << name << llendl;
 79	return false;
 80}
 81
 82LLDispatchHandler* LLDispatcher::addHandler(
 83	const key_t& name, LLDispatchHandler* func)
 84{
 85	dispatch_map_t::iterator it = mHandlers.find(name);
 86	LLDispatchHandler* old_handler = NULL;
 87	if(it != mHandlers.end())
 88	{
 89		old_handler = (*it).second;
 90		mHandlers.erase(it);
 91	}
 92	if(func)
 93	{
 94		// only non-null handlers so that we don't have to worry about
 95		// it later.
 96		mHandlers.insert(dispatch_map_t::value_type(name, func));
 97	}
 98	return old_handler;
 99}
100
101// static
102bool LLDispatcher::unpackMessage(
103		LLMessageSystem* msg,
104		LLDispatcher::key_t& method,
105		LLUUID& invoice,
106		LLDispatcher::sparam_t& parameters)
107{
108	char buf[MAX_STRING];	/*Flawfinder: ignore*/
109	msg->getStringFast(_PREHASH_MethodData, _PREHASH_Method, method);
110	msg->getUUIDFast(_PREHASH_MethodData, _PREHASH_Invoice, invoice);
111	S32 size;
112	S32 count = msg->getNumberOfBlocksFast(_PREHASH_ParamList);
113	for (S32 i = 0; i < count; ++i)
114	{
115		// we treat the SParam as binary data (since it might be an 
116		// LLUUID in compressed form which may have embedded \0's,)
117		size = msg->getSizeFast(_PREHASH_ParamList, i, _PREHASH_Parameter);
118		if (size >= 0)
119		{
120			msg->getBinaryDataFast(
121				_PREHASH_ParamList, _PREHASH_Parameter,
122				buf, size, i, MAX_STRING-1);
123
124			// If the last byte of the data is 0x0, this is either a normally
125			// packed string, or a binary packed UUID (which for these messages
126			// are packed with a 17th byte 0x0).  Unpack into a std::string
127			// without the trailing \0, so "abc\0" becomes std::string("abc", 3)
128			// which matches const char* "abc".
129			if (size > 0
130				&& buf[size-1] == 0x0)
131			{
132				// special char*/size constructor because UUIDs may have embedded
133				// 0x0 bytes.
134				std::string binary_data(buf, size-1);
135				parameters.push_back(binary_data);
136			}
137			else
138			{
139				// This is either a NULL string, or a string that was packed 
140				// incorrectly as binary data, without the usual trailing '\0'.
141				std::string string_data(buf, size);
142				parameters.push_back(string_data);
143			}
144		}
145	}
146	return true;
147}