示例#1
0
	unsigned int NetGameNetworkData::get_encoded_length(const NetGameEventValue &value)
	{
		switch (value.get_type())
		{
		case NetGameEventValue::null:
		case NetGameEventValue::boolean:
			return 1;
		case NetGameEventValue::character:
		case NetGameEventValue::ucharacter:
			return 2;
		case NetGameEventValue::uinteger:
		case NetGameEventValue::integer:
		case NetGameEventValue::number:
			return 5;
		case NetGameEventValue::string:
			return 1 + 2 + value.get_string().length();
		case NetGameEventValue::binary:
			return 1 + 2 + value.get_binary().get_size();
		case NetGameEventValue::complex:
		{
			unsigned l = 2;
			for (unsigned int i = 0; i < value.get_member_count(); i++)
				l += get_encoded_length(value.get_member(i));
			return l;
		}
		default:
			throw Exception("Unknown game event value type");
		}
	}
示例#2
0
	unsigned int NetGameNetworkData::encode_value(unsigned char *d, const NetGameEventValue &value)
	{
		switch (value.get_type())
		{
		case NetGameEventValue::null:
			*d = 1;
			return 1;
		case NetGameEventValue::uinteger:
			*d = 2;
			*reinterpret_cast<unsigned int*>(d + 1) = value.get_uinteger();
			return 5;
		case NetGameEventValue::integer:
			*d = 3;
			*reinterpret_cast<int*>(d + 1) = value.get_integer();
			return 5;
		case NetGameEventValue::number:
			*d = 4;
			*reinterpret_cast<float*>(d + 1) = value.get_number();
			return 5;
		case NetGameEventValue::boolean:
			*d = value.get_boolean() ? 6 : 5;
			return 1;
		case NetGameEventValue::string:
		{
			std::string s = value.get_string();
			*d = 7;
			*reinterpret_cast<unsigned short*>(d + 1) = s.length();
			memcpy(d + 3, s.data(), s.length());
			return 3 + s.length();
		}
		case NetGameEventValue::complex:
		{
			d[0] = 8;
			unsigned l = 1;
			for (unsigned int i = 0; i < value.get_member_count(); i++)
				l += encode_value(d + l, value.get_member(i));
			d[l] = 0;
			l++;
			return l;
		}
		case NetGameEventValue::ucharacter:
			*d = 9;
			*reinterpret_cast<unsigned char*>(d + 1) = value.get_ucharacter();
			return 2;
		case NetGameEventValue::character:
			*d = 10;
			*reinterpret_cast<char*>(d + 1) = value.get_character();
			return 2;
		case NetGameEventValue::binary:
		{
			DataBuffer s = value.get_binary();
			*d = 11;
			*reinterpret_cast<unsigned short*>(d + 1) = s.get_size();
			memcpy(d + 3, s.get_data(), s.get_size());
			return 3 + s.get_size();
		}
		default:
			throw Exception("Unknown game event value type");
		}
	}
示例#3
0
	std::string NetGameEventValue::to_string(const NetGameEventValue &v)
	{
		switch (v.get_type())
		{
		case NetGameEventValue::null:
			return "null";
		case NetGameEventValue::integer:
			return StringHelp::int_to_text(v.get_integer());
		case NetGameEventValue::uinteger:
			return StringHelp::uint_to_text(v.get_uinteger());
		case NetGameEventValue::character:
			return StringHelp::int_to_text(static_cast<int>(v.get_character()));
		case NetGameEventValue::ucharacter:
			return StringHelp::uint_to_text(static_cast<unsigned int>(v.get_ucharacter()));
		case NetGameEventValue::string:
			return "\"" + v.get_string() + "\"";
		case NetGameEventValue::boolean:
			return v.get_boolean() ? "true" : "false";
		case NetGameEventValue::number:
			return StringHelp::float_to_text(v.get_number());
		case NetGameEventValue::complex:
		{
			std::string str;
			str += "[";
			for (unsigned int j = 0; j < v.get_member_count(); j++)
			{
				if (j > 0)
					str += ",";
				str += to_string(v.get_member(j));
			}
			str += "]";
			return str;
		}
		default:
			return "??" + StringHelp::int_to_text(v.get_type());
		}
	}