public void Decode(Channel ch, ByteArray input) { if (input.Length == 0) { throw new ProtocolCodecException("Unexpected empty packet data:no readable bytes available!"); } if (endpoint.LogLevel == Logging.LogLevel.DEBUG) { if (input.Length > 0x400) { string[] messages = new string[] { "Data Read: Size > 1024, dump omitted" }; this.log.Info(messages); } else { string[] textArray2 = new string[] { "Data Read: " + HexDumpFormatter.HexDump(input) }; this.log.Info(textArray2); } } input.Position = 0; while (input.Length > 0) { if (this.ReadState == PacketReadState.WAIT_NEW_PACKET) { input = this.HandleNewPacket(input); } else { if (this.ReadState == PacketReadState.WAIT_PACKET_HEADER) { input = this.HandlePakcetHeader(input); continue; } if (this.ReadState == PacketReadState.WAIT_PACKET_HEADER_FRAGMETN) { input = this.HandlePacketHeaderFragment(input); continue; } if (this.ReadState == PacketReadState.WAIT_DATA) { input = this.HandlePacketData(ch, input); continue; } if (this.ReadState == PacketReadState.INVALID_DATA) { input = this.HandleInvalidData(input); } } } }
private ByteArray HandlePakcetHeader(ByteArray data) { string[] messages = new string[1]; object[] objArray1 = new object[] { "Handling Packet Header. Data Length: ", data.Length }; messages[0] = string.Concat(objArray1); this.log.Debug(messages); if (data.Length >= PAKCET_BINARY_LENGTH) { short magic = data.ReadShort(); if (magic != MAGIC) { object[] objArray2 = new object[] { "Unexpected header magic short: ", magic, "\n", HexDumpFormatter.HexDump(data) }; throw new ProtocolCodecException(string.Concat(objArray2)); } //packet header type byte type = data.ReadByte(); //packet header compressed byte compressed = data.ReadByte(); //Packet header length int length = data.ReadInt(); PacketHeader header = new PacketHeader(type, compressed, length); pendingPacket.Header = header; this.fsm.ApplyTransition(PacketReadTransition.PacketHeaderReceived); if (data.Length > PAKCET_BINARY_LENGTH) { return(this.ResizeByteArray(data, PAKCET_BINARY_LENGTH, data.Length - PAKCET_BINARY_LENGTH)); } data = this.EMPTY_BUFFER; return(data); } this.fsm.ApplyTransition(PacketReadTransition.IncompletePacketHeader); this.pendingPacket.Content.WriteBytes(data.Bytes); data = this.EMPTY_BUFFER; return(data); }