Exemple #1
0
        /// <summary>
        /// 通过网络包创建Message对象
        /// </summary>
        /// <param name="package"></param>
        /// <returns></returns>
        public static CMessage CreateMessage(CPackageBase package)
        {
            // 将物理对象转化为Message
            CMessage message = new CMessage();

            //填充OpCode
            Type type = package.GetType();

            Object[] attrs = type.GetCustomAttributes(false);
            foreach (Object item in attrs)
            {
                // 符合特性的才注册
                if (item.GetType() == typeof(CAttrPackage))
                {
                    message.OpCode = (item as CAttrPackage).OpCode;
                    break;
                }
            }

            // 填充身体数据
            package.ToMessage(ref message);

            //设置长度
            message.BodyLength = ( ushort )message.Body.Position;

            return(message);
        }
Exemple #2
0
        /// <summary>
        /// 压入数据
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="data">要压入的值></param>
        /// <param name="msg">要压入的目标消息></param>
        public void PushData <T>(T data, ref CMessage msg)
        {
            Type dataType = typeof(T);

            if (dataType == typeof(byte))
            {
                PushByte(( byte )( Object )data);
                return;
            }

            if (dataType == typeof(sbyte))
            {
                PushByte(( sbyte )( Object )data);
                return;
            }

            if (dataType == typeof(bool))
            {
                PushBoolean(( bool )( Object )data);
                return;
            }

            if (dataType == typeof(int))
            {
                PushInt(( int )( Object )data);
                return;
            }

            if (dataType == typeof(uint))
            {
                PushInt(( uint )( Object )data);
                return;
            }

            if (dataType == typeof(short))
            {
                PushShort(( short )( Object )data);
                return;
            }

            if (dataType == typeof(ushort))
            {
                PushInt(( ushort )( Object )data);
                return;
            }

            if (dataType == typeof(float))
            {
                PushFloat(( float )( Object )data);
                return;
            }

            if (dataType == typeof(long))
            {
                PushLong(( long )( Object )data);
                return;
            }

            if (dataType == typeof(ulong))
            {
                PushLong(( ulong )( Object )data);
                return;
            }

            if (dataType == typeof(string))
            {
                PushString(( string )( Object )data);
                return;
            }

            // 如果反射对象是 CPackageBase 的子类
            if (data is CPackageBase)
            {
                CPackageBase tempData = ( CPackageBase )( Object )data;
                if (tempData == null)
                {
                    return;
                }
                tempData.ToMessage(ref msg);
                return;
            }

            // 如果反射对象是 List
            if (dataType.IsGenericType)
            {
                Type[]   GenericType = dataType.GetGenericArguments();
                object[] param       = { data, msg };
                CFunction.CallGenericArrayFunction("PushList", this, GenericType, param);
                return;
            }

            // 如果反射对象是数组
            if (dataType.IsArray)
            {
                Type     ElementType = dataType.GetElementType();
                object[] param       = { data, msg };
                CFunction.CallGenericFunction("PushArray", this, ElementType, param);
                return;
            }

            CLOG.E("the type {0} has no serilize methon", dataType.ToString());
        }