public SocketModel GetMessage() { if (this.GetSocketMessageCount() <= 0) { return((SocketModel)null); } SocketModel message = this.messages[0]; this.messages.RemoveAt(0); return(message); }
/// <summary> /// 解码 /// </summary> /// <param name="value"></param> /// <returns></returns> private SocketModel mDecode(byte[] value) { ByteArray byteArray = new ByteArray(value); SocketModel socketModel = new SocketModel(); byte num1; byteArray.read(out num1); int num2; byteArray.read(out num2); socketModel.type = num1; socketModel.command = num2; if (byteArray.Readnable) { byte[] numArray; byteArray.read(out numArray, byteArray.Length - byteArray.Position); //序列化消息 //socketModel.message = SerializeUtil.decode(numArray); socketModel.message = SerializeUtil.ProtoBufDecode <SocketModel>(numArray); } byteArray.Close(); return(socketModel); }
/// <summary> /// 循环处理接收报文,粘/拆包 /// </summary> private void OnData() { byte[] numArray = this.Decode(ref this.cache); //为空就说明不再读取 if (numArray == null) { this.isReading = false; } else { //新建一个协议基础模型,真正解码 SocketModel socketModel = this.mDecode(numArray); if (socketModel == null) { this.isReading = false; } else { this.messages.Add(socketModel); this.OnData(); } } }