示例#1
0
//===================================encoder==============================
bool RedisCommandEncoder::Encode(Buffer& buf, const RedisCommandFrame& cmd)
{
    buf.Printf("*%d\r\n", cmd.GetArguments().size() + 1);
    buf.Printf("$%d\r\n", cmd.GetCommand().size());
    buf.Write(cmd.GetCommand().data(), cmd.GetCommand().size());
    buf.Write("\r\n", 2);
    for (uint32 i = 0; i < cmd.GetArguments().size(); i++)
    {
        const std::string* arg = cmd.GetArgument(i);
        buf.Printf("$%d\r\n", arg->size());
        buf.Write(arg->data(), arg->size());
        buf.Write("\r\n", 2);
    }
    return true;
}
示例#2
0
int RedisCommandManager::HandlerCommandFrame(RedisCommandFrame& frame,
        uint32 connectionID)
{
	Buffer* name = frame.GetArgument(0);
	if (NULL == name)
	{
		snprintf(m_szErrMsg, MAX_REDIS_ERR_MSG_LEN, "NULL command name.");
		return -1;
	}
	std::string namestr = name->AsString();
	namestr = string_tolower(namestr);
	RedisCommandConfigTable::iterator cfgfound = m_cfg_table.find(namestr);
	if (cfgfound == m_cfg_table.end())
	{
		snprintf(m_szErrMsg, MAX_REDIS_ERR_MSG_LEN, "not supported command:%s",
		        namestr.c_str());
		return -1;
	}
	RedisCommandConfig cfg = cfgfound->second;
	if (cfg.arity > 0 && cfg.arity != frame.GetArguments().size())
	{
		snprintf(m_szErrMsg, MAX_REDIS_ERR_MSG_LEN,
		        "wrong number of arguments for '%s' command", namestr.c_str());
		return -1;
	}

	RedisCommandHandlerArray::iterator it = m_handler_array.begin();
	while (it != m_handler_array.end())
	{
		RedisCommandHandler* handler = *it;
		if (NULL != handler)
		{
			handler->OnRedisCommand(cfg.type, connectionID, frame);
		}
		it++;
	}
	return 0;
}