protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List <object> output) { IByteBuffer nextHeader = null; do { if (input.ReadableBytes > 1) { try { nextHeader = AMQPParser.Next(input); } catch (Exception ex) { if (ex is MalformedMessageException || ex is IndexOutOfRangeException) { input.ResetReaderIndex(); if (nextHeader != null) { nextHeader.Release(); nextHeader = null; } } else { throw ex; } } } if (nextHeader != null) { input.ReadBytes(nextHeader, nextHeader.Capacity); try { AMQPHeader header = AMQPParser.Decode(nextHeader); output.Add(header); } catch (Exception e) { input.ResetReaderIndex(); context.Channel.Pipeline.Remove(this); throw e; } finally { nextHeader.Release(); } } }while (input.ReadableBytes > 1 && nextHeader != null); }
public static AMQPHeader Decode(IByteBuffer buf) { long length = buf.ReadInt() & 0xffffffffL; int doff = buf.ReadByte() & 0xff; int type = buf.ReadByte() & 0xff; int channel = buf.ReadShort() & 0xffff; // TODO check condition if (length == 8 && doff == 2 && (type == 0 || type == 1) && channel == 0) { if (buf.ReadableBytes == 0) { return(new AMQPPing()); } else { throw new MalformedMessageException("Received malformed ping-header with invalid length"); } } // PTOROCOL-HEADER if (length == 1095586128 && (doff == 3 || doff == 0) && type == 1 && channel == 0) { if (buf.ReadableBytes == 0) { return(new AMQPProtoHeader(doff)); } else { throw new MalformedMessageException("Received malformed protocol-header with invalid length"); } } if (length != buf.ReadableBytes + 8) { throw new MalformedMessageException("Received malformed header with invalid length"); } AMQPHeader header = null; if (type == 0) { header = AMQPFactory.getAMQP(buf); } else if (type == 1) { header = AMQPFactory.getSASL(buf); } else { throw new MalformedMessageException("Received malformed header with invalid type: " + type); } header.Dott = doff; header.HeaderType = type; header.Channel = channel; if (header.Code.HasValue && header.Code.Value == HeaderCodes.TRANSFER) { List <AMQPSection> sections = new List <AMQPSection>(); while (buf.ReadableBytes > 0) { sections.Add(AMQPFactory.getSection(buf)); } ((AMQPTransfer)header).addSections(sections.ToArray()); } return(header); }
public static IByteBuffer Encode(AMQPHeader header) { IByteBuffer buf = null; if (header is AMQPProtoHeader) { buf = Unpooled.Buffer(8); buf.WriteBytes(ASCIIEncoding.ASCII.GetBytes("AMQP")); buf.WriteByte(((AMQPProtoHeader)header).ProtocolId); buf.WriteByte(((AMQPProtoHeader)header).VersionMajor); buf.WriteByte(((AMQPProtoHeader)header).VersionMinor); buf.WriteByte(((AMQPProtoHeader)header).VersionRevision); return(buf); } if (header is AMQPPing) { buf = Unpooled.Buffer(8); buf.WriteInt(8); buf.WriteByte(header.Dott); buf.WriteByte(header.HeaderType); buf.WriteShort(header.Channel); return(buf); } int length = 8; TLVList arguments = header.getArguments(); length += arguments.getLength(); Dictionary <SectionCodes, AMQPSection> sections = null; if (header.Code.HasValue && header.Code.Value == HeaderCodes.TRANSFER) { sections = ((AMQPTransfer)header).getSections(); foreach (KeyValuePair <SectionCodes, AMQPSection> entry in sections) { length += entry.Value.getValue().getLength(); } } buf = Unpooled.Buffer(length); buf.WriteInt(length); int doff = header.Dott; buf.WriteByte(doff); int type = header.HeaderType; buf.WriteByte(type); int channel = header.Channel; buf.WriteShort(channel); buf.WriteBytes(arguments.getBytes()); if (sections != null) { foreach (KeyValuePair <SectionCodes, AMQPSection> entry in sections) { buf.WriteBytes(entry.Value.getValue().getBytes()); } } return(buf); }