/// <summary> /// 压入一个包到收线程来伪造服务器消息 /// </summary> /// <param name="msg">消息对象</param> public void FakeMessageToReceive(ref CMessage msg) { m_Receiver.PushReadyToHandlerPackage(ref msg); }
/// <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)); }
/// <summary> /// 返回收到的数据 /// </summary> /// <returns></returns> private CMessage RecevieData( ) { // 得到Socket对象 Socket _Socket = m_NetworkRef.SocketInstance.Socket; if (_Socket == null || !m_NetworkRef.IsConnected) { return(null); } byte[] Buff = new byte[4]; int length; // 收4个字节的Head try { length = _Socket.Receive(Buff); } catch (Exception ex) { CLOG.V(ex.ToString()); CNetwork.Instance.DisConnect(); return(null); } if (4 != length) { CLOG.V("receive packge length error! length = {0} ", length); return(null); } //取OpCode ushort Opcode = BitConverter.ToUInt16(Buff, 0); //取BodyLength ushort BodyLength = BitConverter.ToUInt16(Buff, 2); //新建包体缓存区 Buff = new byte[BodyLength]; int HasReceivedLength = 0; while (HasReceivedLength < BodyLength) { try { int recSize = _Socket.Receive(Buff, HasReceivedLength, BodyLength - HasReceivedLength, SocketFlags.None); HasReceivedLength += recSize; } catch (Exception ex) { CLOG.V(ex.ToString()); CNetwork.Instance.DisConnect(); //主动断网 return(null); } } CLOG.V("package length is {0} has received {1} data", BodyLength, HasReceivedLength); if (BodyLength != HasReceivedLength || BodyLength != Buff.Length) { CLOG.E("receivce data error!"); return(null); } CMessage message = new CMessage(Opcode, Buff, BodyLength); return(message); }
/// <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()); }