protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List <object> output) { // Wait until the length prefix is available. if (input.ReadableBytes < 5) { return; } input.MarkReaderIndex(); // Check the magic number. byte magicNumber = input.ReadByte(); if (magicNumber != 'F') { input.ResetReaderIndex(); throw new CorruptedFrameException("Invalid magic number: " + magicNumber); } // Wait until the whole data is available. int dataLength = input.ReadInt(); if (input.ReadableBytes < dataLength) { input.ResetReaderIndex(); return; } // Convert the received data into a new BigIntere. byte[] decoded = new byte[dataLength]; input.ReadBytes(decoded); output.Add(new BigInteger(decoded)); }
protected void Decode2(IChannelHandlerContext context, IByteBuffer input, List <object> output) { int readable = input.ReadableBytes; if (readable < 4) { logger.Debug("数据过短 {}", readable); return; } input.MarkReaderIndex(); int packageLength = input.ReadInt(); if (packageLength == 0) { throw new Exception("包长度不能为0"); } //半包 if (packageLength > input.ReadableBytes) { logger.Debug("数据不够一个数据包 pl={} ,rl={}", packageLength, input.ReadableBytes); input.ResetReaderIndex(); } else { IByteBuffer buffer = input.ReadBytes(packageLength); ConsumerBuffer consumerBuffer = new ConsumerBuffer(); consumerBuffer.Buffer = buffer; consumerBuffer.EndIndex = buffer.WriterIndex; output.Add(consumerBuffer); } }
public override IPacket ReadHeader(IByteBuffer buf) { int length = -1; int opcode = -1; try { length = ByteBufUtils.ReadVarInt(buf); // mark point before opcode buf.MarkReaderIndex(); opcode = ByteBufUtils.ReadVarInt(buf); return(inboundCodecs.Find(opcode)); } catch (IOException) { throw new UnknownPacketException("Failed to read packet data (corrupt?)", opcode, length); } catch (IllegalOpcodeException) { // go back to before opcode, so that skipping length doesn't skip too much buf.ResetReaderIndex(); throw new UnknownPacketException("Opcode received is not a registered codec on the server!", opcode, length); } }
public void ReaderIndexAndMarks() { IByteBuffer wrapped = Unpooled.Buffer(16); try { wrapped.SetWriterIndex(14); wrapped.SetReaderIndex(2); wrapped.MarkWriterIndex(); wrapped.MarkReaderIndex(); IByteBuffer slice = wrapped.Slice(4, 4); Assert.Equal(0, slice.ReaderIndex); Assert.Equal(4, slice.WriterIndex); slice.SetReaderIndex(slice.ReaderIndex + 1); slice.ResetReaderIndex(); Assert.Equal(0, slice.ReaderIndex); slice.SetWriterIndex(slice.WriterIndex - 1); slice.ResetWriterIndex(); Assert.Equal(0, slice.WriterIndex); } finally { wrapped.Release(); } }
protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List <object> output) { if (input.ReadableBytes < 2) { return; } input.MarkReaderIndex(); byte magic1 = input.ReadByte(); byte magic2 = input.ReadByte(); bool isFlashPolicyRequest = (magic1 == '<' && magic2 == 'p'); if (isFlashPolicyRequest) { input.SkipBytes(input.ReadableBytes); // Make sure no downstream handler can interfere with sending our policy response removeAllPipelineHandlers(context.Channel.Pipeline); // Write the policy and close the connection context.WriteAndFlushAsync(FlashPolicy.GetBytes()).ContinueWith(delegate { context.CloseAsync(); }); } else { // Remove ourselves context.Channel.Pipeline.Remove(this); } }
public static bool TryReadVarInt32(this IByteBuffer buffer, out int result) { buffer.MarkReaderIndex(); var numBytes = (byte)0; result = 0; byte read; do { if (!buffer.IsReadable()) { buffer.ResetReaderIndex(); return(false); } read = buffer.ReadByte(); var value = read & VarIntContentMask; result |= value << (VarIntContentBytesCount * numBytes); numBytes++; if (numBytes <= VarInt32MaxBytes) { continue; } buffer.ResetReaderIndex(); return(false); } while ((read & VarIntIndexMask) != 0); return(true); }
public void Decode(IByteBuffer input, List <SSMessage> output) { input = input.WithOrder(ByteOrder.LittleEndian); output.Clear(); SSHead head = new SSHead(); while (input.ReadableBytes >= Constant.MIN_SS_HEAD_LEN) { input.MarkReaderIndex(); head.length = input.ReadInt(); head.msgid = input.ReadShort(); head.dest_type = input.ReadByte(); head.dest_id = input.ReadLong(); if (head.RealLength() > input.ReadableBytes) { input.ResetReaderIndex(); return; } if (head.RealLength() > Constant.MAX_MSG_LEN) { throw new TooLongFrameException(""); } IByteBuffer buffer = input.SliceAndRetain(head.RealLength()); var bytes = buffer.ReadSlice(buffer.ReadableBytes).ToArray(); output.Add(new SSMessage(ProtoBuffDecode.NewMessage(head.msgid, bytes), Platform.GetMilliSeconds())); buffer.Release(); } }
void TestDiscardMarks(int capacity) { IByteBuffer buf = this.NewBuffer(capacity); buf.WriteShort(1); buf.SkipBytes(1); buf.MarkReaderIndex(); buf.MarkWriterIndex(); Assert.True(buf.Release()); IByteBuffer buf2 = this.NewBuffer(capacity); Assert.Same(UnwrapIfNeeded(buf), UnwrapIfNeeded(buf2)); buf2.WriteShort(1); buf2.ResetReaderIndex(); buf2.ResetWriterIndex(); Assert.Equal(0, buf2.ReaderIndex); Assert.Equal(0, buf2.WriterIndex); Assert.True(buf2.Release()); }
protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List <object> output) { if (input.ReadableBytes > 0 && input.IsReadable()) { input.MarkReaderIndex(); int opcode = input.ReadByte(); Log.Logger.Debug("File Request: Opcode: {0}", opcode); if (opcode is 0 or 1) { if (input.ReadableBytes < 3) { input.ResetReaderIndex(); return; } int index = input.ReadByte(); int file = input.ReadUnsignedShort(); bool priority = opcode == 1; Log.Logger.Debug("File Request: Index: {0}, File: {1} Priority: {2}", index, file, priority); _ = context.Channel.WriteAndFlushAsync(new JS5Request(index, file, priority, _encryptionKey)); } else if (opcode is 2 or 3 or 6) { input.SkipBytes(3); }
protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List <object> output) { if (input.ReadableBytes < 5) { return; } input.MarkReaderIndex(); int magicNumber = input.ReadByte(); if (magicNumber != 'F') { input.ResetReaderIndex(); throw new Exception($"Invalid magic number: {magicNumber}"); } int dataLength = input.ReadInt(); if (input.ReadableBytes < dataLength) { input.ResetReaderIndex(); return; } var decoded = new byte[dataLength]; input.ReadBytes(decoded); output.Add(new BigInteger(decoded)); }
protected internal override void decode(IChannelHandlerContext ctx, IByteBuffer @in, List <object> @out) { @in.MarkReaderIndex(); ProtocolCode protocolCode = decodeProtocolCode(@in); if (null != protocolCode) { byte protocolVersion = decodeProtocolVersion(@in); if (ctx.Channel.GetAttribute(Connection.PROTOCOL).Get() == null) { ctx.Channel.GetAttribute(Connection.PROTOCOL).Set(protocolCode); if (DEFAULT_ILLEGAL_PROTOCOL_VERSION_LENGTH != protocolVersion) { ctx.Channel.GetAttribute(Connection.VERSION).Set(protocolVersion); } } Protocol protocol = ProtocolManager.getProtocol(protocolCode); if (null != protocol) { @in.ResetReaderIndex(); protocol.Decoder.decode(ctx, @in, @out); } else { throw new CodecException("Unknown protocol code: [" + protocolCode + "] while decode in ProtocolDecoder."); } } }
// todo: maxFrameLength + safe skip + fail-fast option (just like LengthFieldBasedFrameDecoder) protected internal override void Decode(IChannelHandlerContext context, IByteBuffer input, List <object> output) { _ = input.MarkReaderIndex(); int preIndex = input.ReaderIndex; int length = ReadRawVarint32(input); if (0u >= (uint)(preIndex - input.ReaderIndex)) { return; } uint uLen = (uint)length; if (uLen > SharedConstants.TooBigOrNegative) { ThrowCorruptedFrameException_Negative_length(length); } if ((uint)input.ReadableBytes >= uLen) { IByteBuffer byteBuffer = input.ReadSlice(length); output.Add(byteBuffer.Retain()); } else { _ = input.ResetReaderIndex(); } }
// todo: maxFrameLength + safe skip + fail-fast option (just like LengthFieldBasedFrameDecoder) protected internal override void Decode(IChannelHandlerContext context, IByteBuffer input, List <object> output) { input.MarkReaderIndex(); int preIndex = input.ReaderIndex; int length = ReadRawVarint32(input); if (preIndex == input.ReaderIndex) { return; } if (length < 0) { throw new CorruptedFrameException($"Negative length: {length}"); } if (input.ReadableBytes < length) { input.ResetReaderIndex(); } else { IByteBuffer byteBuffer = input.ReadSlice(length); output.Add(byteBuffer.Retain()); } }
protected override byte PeekByte(int offset) { _byteBuffer.MarkReaderIndex(); _byteBuffer.SkipBytes(offset); byte result = _byteBuffer.ReadByte(); _byteBuffer.ResetReaderIndex(); return(result); }
protected override void Decode(IChannelHandlerContext ctx, IByteBuffer buffer, List <object> output) { buffer.MarkReaderIndex(); if (buffer.ReadableBytes < 6) { // If the incoming data is less than 6 bytes, it's junk. return; } byte delimiter = buffer.ReadByte(); buffer.ResetReaderIndex(); if (delimiter == 60) { string policy = "<?xml version=\"1.0\"?>\r\n" + "<!DOCTYPE cross-domain-policy SYSTEM \"/xml/dtds/cross-domain-policy.dtd\">\r\n" + "<cross-domain-policy>\r\n" + "<allow-access-from domain=\"*\" to-ports=\"*\" />\r\n" + "</cross-domain-policy>\0)"; ctx.Channel.WriteAndFlushAsync(Unpooled.CopiedBuffer(StringUtil.GetEncoding().GetBytes(policy))); } else { buffer.MarkReaderIndex(); int length = buffer.ReadInt(); if (buffer.ReadableBytes < length) { buffer.ResetReaderIndex(); return; } if (length < 0) { return; } var messageBuffer = buffer.ReadBytes(length); output.Add(new Request(length, messageBuffer.ReadShort(), messageBuffer)); } }
protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List <object> output) { input.MarkReaderIndex(); if (input.ReadableBytes < 6) { return; } byte delimeter = input.ReadByte(); input.ResetReaderIndex(); if (delimeter == 60) { string policy = "<?xml version=\"1.0\"?>\r\n" + "<!DOCTYPE cross-domain-policy SYSTEM \"/xml/dtds/cross-domain-policy.dtd\">\r\n" + "<cross-domain-policy>\r\n" + "<allow-access-from domain=\"*\" to-ports=\"*\" />\r\n" + "</cross-domain-policy>\0)"; context.WriteAndFlushAsync(Unpooled.CopiedBuffer(Encoding.Default.GetBytes(policy))); } else { input.MarkReaderIndex(); int length = input.ReadInt(); if (input.ReadableBytes < length) { input.ResetReaderIndex(); return; } IByteBuffer newBuf = input.ReadBytes(length); if (length < 0) { return; } output.Add(new ClientPacket(newBuf)); } }
public override RequestPacket DecodeResponse(IByteBuffer input) { var inputStream = new TarsInputStream(input); var result = new RequestPacket { Version = TryDecode(() => VersionHandler.DecodeVersion(inputStream)) }; var handler = GetTupByVersion(result.Version); TryDecode(() => handler.DecodeResponse(inputStream, result)); input.MarkReaderIndex(); return(result); }
public (long length, string typeName, object msg) Decode(IByteBuffer input) { var readableBytes = input.ReadableBytes; if (readableBytes < HeaderLength) { return(0, null, null); } input.MarkReaderIndex(); var magic = input.ReadIntLE(); var metaLength = input.ReadIntLE(); var bodyLength = input.ReadIntLE(); var totalLength = HeaderLength + metaLength + bodyLength; if (readableBytes < totalLength) { input.ResetReaderIndex(); return(0, null, null); } var nameLength = input.ReadByte(); var name = input.ReadString(nameLength, Encoding.UTF8); if (!MessageTypes.TryGetValue(name, out var messageType)) { throw new Exception($"Message:{name} not found"); } var metaBodyLength = metaLength - 1 - name.Length; var metaBody = ArrayPool <byte> .Shared.Rent(metaBodyLength); try { input.ReadBytes(metaBody, 0, metaBodyLength); var meta = JsonSerializer.Deserialize(new ReadOnlySpan <byte>(metaBody, 0, metaBodyLength), messageType); var body = Empty; if (bodyLength > 0) { body = new byte[bodyLength]; input.ReadBytes(body); } var msg = new RpcMessage(meta as RpcMeta, body); return(totalLength, meta.GetType().Name, msg); } finally { ArrayPool <byte> .Shared.Return(metaBody); } }
/** * Puts the bytes from the specified buffer into this packet's buffer, in * reverse. * @param buffer The source {@link ByteBuf}. * @throws IllegalStateException if the builder is not in byte access mode. */ public void PutBytesReverse(IByteBuffer buffer) { byte[] bytes = new byte[buffer.ReadableBytes]; buffer.MarkReaderIndex(); try { buffer.ReadBytes(bytes); } finally { buffer.ResetReaderIndex(); } PutBytesReverse(bytes); }
protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List <object> output) { input.MarkReaderIndex(); if (input.ReadableBytes < 6) { return; } byte delimeter = input.ReadByte(); input.ResetReaderIndex(); if (delimeter == 60) { context.WriteAndFlushAsync(Unpooled.CopiedBuffer(PolicyRequest)); } else { input.MarkReaderIndex(); int length = input.ReadInt(); if (input.ReadableBytes < length) { input.ResetReaderIndex(); return; } IByteBuffer buffer = input.ReadBytes(length); if (length < 0) { return; } ClientPacket clientMessage = new ClientPacket(buffer.ReadBytes(length)); output.Add(clientMessage); } }
public ChannelBufferStream(IByteBuffer buffer, int length) { _buffer = buffer ?? throw new NullReferenceException("buffer"); if (length < 0) { throw new ArgumentException("length:" + length); } if (length > buffer.ReadableBytes) { throw new IndexOutOfRangeException(); } this._buffer = buffer; startIndex = buffer.ReaderIndex; endIndex = startIndex + length; buffer.MarkReaderIndex(); }
//TCP 데이터 수신시. //Decode(IChannelHandlerContext ctx, IByteBuffer msg, List<object> list) 에서, //IByteBuffer msg 는 ByteToMessageDecoder가 내부적으로 가지고 있는 누적버퍼이다. protected override void Decode(IChannelHandlerContext ctx, IByteBuffer msg, List <object> list) { //헤더부분(4 + 4 + 2 + 2 byte)도 다 못읽어왔을 경우! 12byte는 얼마든지 변경 가능. if (msg.ReadableBytes < 12) { return; //다시 되돌려서 패킷을 이어서 더 받아오게끔 하는 부분! } //readerIndex 저장. msg.MarkReaderIndex(); //이 예제에서 packet_size는, 헤더12byte를 제외한, 순수한 패킷 자체의 데이터크기를 나타낸다. //앞으로 받아올 패킷크기를 불러온다! int(4byte) 만큼 읽었으므로 readerIndex 0 -> 4로 증가됨. int packet_size = msg.ReadIntLE(); //msg 버퍼 객체로부터 4byte만큼, LittleEndian 방식으로 Int값을 불러옴. //isAvailablePacketSize() 함수로 패킷 크기 체크. if (!isAvailablePacketSize(packet_size)) { //비정상적인 데이터가 도달하였다면. Debug.Log("[TCP_InBoundHandler] Packet손상!! packet_size = " + packet_size + ""); //연결 종료 ctx.Channel.CloseAsync(); //채널이 닫힌다면 msg는 release 된다. return; } //아직 버퍼에 데이터가 충분하지 않다면 다시 되돌려서 패킷을 이어서 더 받아오게끔 하는 부분! if (msg.ReadableBytes < packet_size + 8) //<- 이미 4byte만큼 읽었으므로 헤더크기 12에서 4를 뺀값. { //정상적으로 데이터를 다 읽은것이 아니므로 아까 MarkReaderIndex로 저장했던 0값으로 readerIndex 4 -> 0로 초기화함. msg.ResetReaderIndex(); return; //다시 되돌려서 패킷을 이어서 더 받아오게끔 하는 부분! } //패킷을 완성할만큼 충분한 데이터를 받아온 경우 else { //패킷 완성후, 다음 데이터가 없는지 체크 if (!isCompleteSizePacket(packet_size, ctx, msg)) { //다음 데이터가 없다면 다시 이어서 받는다. return; } } }
public static byte[] ToArray(this IByteBuffer buffer, out int offset, out int count) { if (buffer.HasArray) { offset = buffer.ArrayOffset; count = buffer.ReadableBytes; return(buffer.Array); } offset = 0; count = buffer.ReadableBytes; var bytes = new byte[count]; buffer.MarkReaderIndex(); buffer.ReadBytes(bytes); buffer.ResetReaderIndex(); return(bytes); }
protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List <object> output) { int headerLength = sizeof(int) * 2; if (input.ReadableBytes < headerLength) { return; } input.MarkReaderIndex(); int totalLength = input.ReadInt(); if (totalLength < 0) { throw new CorruptedFrameException($"Negative length: {totalLength}"); } int ptlCode = input.ReadInt(); Type type = PtlCodeHelper.Instance.GetType(ptlCode); if (type == null) { throw new CorruptedFrameException($"ptl code error: {ptlCode}"); } if (input.ReadableBytes < totalLength - headerLength) { //半包 input.ResetReaderIndex(); return; } int bodyLength = totalLength - headerLength; var bodyBuffer = input.ReadSlice(bodyLength); var message = Activator.CreateInstance(type); if (message is IMessage msg) { msg.MergeFrom(bodyBuffer.Array, bodyBuffer.ArrayOffset + bodyBuffer.ReaderIndex, bodyBuffer.ReadableBytes); output.Add(msg); } }
protected override Object Decode(IChannelHandlerContext ctx, IByteBuffer input) { // var x = base.Decode(ctx, input); IByteBuffer bbin = input; if (bbin == null) { return(null); } bbin.MarkReaderIndex(); FastPacket customMsg = null; if (!FastPacket.Parse(bbin, out customMsg)) { bbin.ResetReaderIndex(); return(null); } return(customMsg); }
public void DiscardReadBytes3() { IByteBuffer a = Unpooled.WrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); IByteBuffer b = Unpooled.WrappedBuffer( Unpooled.WrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, 5), Unpooled.WrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 5, 5)); a.SkipBytes(6); a.MarkReaderIndex(); b.SkipBytes(6); b.MarkReaderIndex(); Assert.Equal(a.ReaderIndex, b.ReaderIndex); a.SetReaderIndex(a.ReaderIndex - 1); b.SetReaderIndex(b.ReaderIndex - 1); Assert.Equal(a.ReaderIndex, b.ReaderIndex); a.SetWriterIndex(a.WriterIndex - 1); a.MarkWriterIndex(); b.SetWriterIndex(b.WriterIndex - 1); b.MarkWriterIndex(); Assert.Equal(a.WriterIndex, b.WriterIndex); a.SetWriterIndex(a.WriterIndex + 1); b.SetWriterIndex(b.WriterIndex + 1); Assert.Equal(a.WriterIndex, b.WriterIndex); Assert.True(ByteBufferUtil.Equals(a, b)); // now discard a.DiscardReadBytes(); b.DiscardReadBytes(); Assert.Equal(a.ReaderIndex, b.ReaderIndex); Assert.Equal(a.WriterIndex, b.WriterIndex); Assert.True(ByteBufferUtil.Equals(a, b)); a.ResetReaderIndex(); b.ResetReaderIndex(); Assert.Equal(a.ReaderIndex, b.ReaderIndex); a.ResetWriterIndex(); b.ResetWriterIndex(); Assert.Equal(a.WriterIndex, b.WriterIndex); Assert.True(ByteBufferUtil.Equals(a, b)); a.Release(); b.Release(); }
private static int getNext(IByteBuffer buf) { buf.MarkReaderIndex(); int length = buf.ReadInt(); if (length == 1095586128) { int protocolId = buf.ReadByte(); int versionMajor = buf.ReadByte(); int versionMinor = buf.ReadByte(); int versionRevision = buf.ReadByte(); if ((protocolId == 0 || protocolId == 3) && versionMajor == 1 && versionMinor == 0 && versionRevision == 0) { buf.ResetReaderIndex(); return(8); } } buf.ResetReaderIndex(); return(length); }
protected int ExtractSequenceId(IByteBuffer messageBuffer) { try { messageBuffer.MarkReaderIndex(); using (TTransport inputTransport = new TChannelBufferInputTransport(messageBuffer)) { using (TProtocol inputProtocol = this.ProtocolFactory.GetInputProtocolFactory().GetProtocol(inputTransport)) { TMessage message = inputProtocol.ReadMessageBegin(); messageBuffer.ResetReaderIndex(); return(message.SeqID); } } } catch (Exception t) { throw new TTransportException(TTransportException.ExceptionType.Unknown, $"Could not find sequenceId in Thrift message{Environment.NewLine}{ t.Message}"); } }
/// <summary> /// Decode the header of a message if available /// </summary> /// <param name="context">The socket context</param> /// <param name="input">The input buffer.</param> /// <param name="output">List containing decoded messages (if any)</param> protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List <object> output) { if (input.ReadableBytes < 4) { return; } // Mark is used by ResetReaderIndex input.MarkReaderIndex(); // It is possible to call ReadInt here since it decodes the int as big-endian (network order) int length = input.ReadInt(); if (input.ReadableBytes < length) { input.ResetReaderIndex(); return; } output.Add(input.ReadBytes(length)); }
protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List <object> output) { if (input.GetByte(0) == 60) { context.WriteAndFlushAsync(Unpooled.CopiedBuffer(Encoding.UTF8.GetBytes("<?xml version=\"1.0\"?>\r\n" + "<!DOCTYPE cross-domain-policy SYSTEM \"http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd\">\r\n" + "<cross-domain-policy>\r\n" + "<allow-access-from domain=\"*\" to-ports=\"*\" />\r\n" + "</cross-domain-policy>\0"))); } else { input.MarkReaderIndex(); int length = input.ReadInt(); if (length <= input.ReadableBytes) { output.Add(new ClientMessage(input.ReadBytes(length))); } } }