/// <summary> /// 处理收到的包 /// </summary> private void ProcessReceviedPackage() { CMessage message = null; while ((message = m_Receiver.PopReadyToHandlerPackage()) != null) { // 如果是伪造的消息,则创建时会导致读写位置到包的末尾了 // 因此需要在这里重置归0 message.Body.ResetPosition(); // 包ID int OpCode = message.OpCode; // 反射出类型 Type type = CPackageManager.Instance.ReflectionClassNameByActionID(OpCode); //安全处理 if (type == null) { CLOG.E("Reflection class name by OpCode error! OpCode={0} reflection class name = null!! please check the code", OpCode); return; } // 得到该类型的处理委托 DelegatePackageHandler DP = CPackageManager.Instance.GetPackageHandler(type); // 创建反射类型实例 CPackageBase package = Activator.CreateInstance(type) as CPackageBase; //安全处理 if (package == null) { CLOG.E("create package instance error! OpCode = {0} type = {1}", OpCode, type.ToString()); return; } // 从message的身体中获取数据实例化对象 try { package.FromMessage(ref message); } catch (Exception ex) { CLOG.E("from message error! Exception!! OpCode = {0} type={1} message={2} ", OpCode, type.ToString(), message.ToString()); CLOG.E(ex.ToString()); return; } // 调用委托,传入参数 if (DP != null) { DP(package); } else { CLOG.W("ths OpCode {0} was not register handler!", OpCode); } } }
/// <summary> /// 从body中获取数据,写入参数中去 /// </summary> /// <typeparam name="T">泛型</typeparam> public T PopData <T>(ref CMessage msg) { Type TType = typeof(T); if (TType == typeof(byte)) { return(( T )( Object )PopByte()); } if (TType == typeof(sbyte)) { return(( T )( Object )PopSByte()); } if (TType == typeof(bool)) { return(( T )( Object )PopBoolean()); } if (TType == typeof(short)) { return(( T )( Object )PopShort()); } if (TType == typeof(ushort)) { return(( T )( Object )PopUShort()); } if (TType == typeof(int)) { return(( T )( Object )PopInt()); } if (TType == typeof(uint)) { return(( T )( Object )PopUInt()); } if (TType == typeof(float)) { return(( T )( Object )PopFloat()); } if (TType == typeof(long)) { return(( T )( Object )PopLong()); } if (TType == typeof(ulong)) { return(( T )( Object )PopULong()); } if (TType == typeof(string)) { return(( T )( Object )PopString()); } // 如果反射对象是 CPackageBase 的子类 if (TType.IsSubclassOf(typeof(CPackageBase))) { // 创建反射类型实例 CPackageBase MemberPackageBase = Activator.CreateInstance(TType) as CPackageBase; MemberPackageBase.FromMessage(ref msg); return(( T )( Object )MemberPackageBase); } // 如果反射对象是 List if (TType.IsGenericType) { Type[] GenericTypes = TType.GetGenericArguments(); object[] param = { msg }; return(( T )( Object )CFunction.CallGenericArrayFunction("PopList", this, GenericTypes, param)); } // 如果反射对象是数组 if (TType.IsArray) { Type ElementType = TType.GetElementType(); object[] param = { msg }; return(( T )( Object )CFunction.CallGenericFunction("PopArray", this, ElementType, param)); } CLOG.E("the type {0} has no serilize methon", TType.ToString()); return(default(T)); }