コード例 #1
0
ファイル: network_data.cpp プロジェクト: Cassie90/ClanLib
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.to_string().length();
	case NetGameEventValue::binary:
		return 1 + 2 + value.to_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
ファイル: network_data.cpp プロジェクト: Cassie90/ClanLib
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.to_uinteger();
		return 5;
	case NetGameEventValue::integer:
		*d = 3;
		*reinterpret_cast<int*>(d + 1) = value.to_integer();
		return 5;
	case NetGameEventValue::number:
		*d = 4;
		*reinterpret_cast<float*>(d + 1) = value.to_number();
		return 5;
	case NetGameEventValue::boolean:
		*d = value.to_boolean() ? 6 : 5;
		return 1;
	case NetGameEventValue::string:
		{
			std::string s = value.to_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.to_ucharacter();
		return 2;
	case NetGameEventValue::character:
		*d = 10;
		*reinterpret_cast<char*>(d + 1) = value.to_character();
		return 2;
	case NetGameEventValue::binary:
		{
			DataBuffer s = value.to_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");
	}
}