|
| 1 | +// Copyright (c) CodeEx.cn & webmote. All rights reserved. |
| 2 | +// Licensed under theApache License. |
| 3 | +// See LICENSE file in the project root for full license information. |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Diagnostics.Contracts; |
| 7 | +using System.Text; |
| 8 | +using DotNetty.Buffers; |
| 9 | +using DotNetty.Transport.Channels; |
| 10 | + |
| 11 | +namespace DotNetty.Codecs.Thrift |
| 12 | +{ |
| 13 | + /// |
| 14 | + /// A decoder that splits the received {@link ByteBuf}s dynamically by the |
| 15 | + /// value of the Thrift Frame Buffers |
| 16 | + /// Base 1 integer length field in the message. |
| 17 | + /// For example: |
| 18 | + /// |
| 19 | + /// BEFORE DECODE (304 bytes) AFTER DECODE (300 bytes) |
| 20 | + /// +------------+---------------+ +---------------+ |
| 21 | + /// | Length | Thrift Data |----->| Thrift Data | |
| 22 | + /// | 0x0000AC02 | (300 bytes) | | (300 bytes) | |
| 23 | + /// +------------+---------------+ +---------------+ |
| 24 | + /// |
| 25 | + public class ThriftFrameDecoder : ByteToMessageDecoder |
| 26 | + { |
| 27 | + private ThriftOptions _opts; |
| 28 | + // todo: maxFrameLength + safe skip + fail-fast option (just like LengthFieldBasedFrameDecoder) |
| 29 | + public ThriftFrameDecoder(ThriftOptions options = null) |
| 30 | + { |
| 31 | + _opts = options ?? ThriftOptions.DefaultOptions; |
| 32 | + } |
| 33 | + |
| 34 | + protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List<object> output) |
| 35 | + { |
| 36 | + input.MarkReaderIndex(); |
| 37 | + |
| 38 | + int preIndex = input.ReaderIndex; |
| 39 | + int length = ReadRawVarint32(input); |
| 40 | + |
| 41 | + if (preIndex == input.ReaderIndex) |
| 42 | + { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + if (length < 0) |
| 47 | + { |
| 48 | + throw new CorruptedFrameException($"Negative length: {length}"); |
| 49 | + } |
| 50 | + |
| 51 | + if (input.ReadableBytes < length) |
| 52 | + { |
| 53 | + input.ResetReaderIndex(); |
| 54 | + } |
| 55 | + else |
| 56 | + { |
| 57 | + if (length > _opts.MaxFrameSize) |
| 58 | + { |
| 59 | + input.SetReaderIndex(length); |
| 60 | + context.FireExceptionCaught(new TooLongFrameException($"frame length ({length}) exceeds the allowed maximum ({_opts.MaxFrameSize})")); |
| 61 | + } |
| 62 | + IByteBuffer byteBuffer = input.ReadSlice(length); |
| 63 | + output.Add(byteBuffer.Retain()); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + static int ReadRawVarint32(IByteBuffer buffer) |
| 68 | + { |
| 69 | + Contract.Requires(buffer != null); |
| 70 | + |
| 71 | + if (!buffer.IsReadable()) |
| 72 | + { |
| 73 | + return 0; |
| 74 | + } |
| 75 | + |
| 76 | + buffer.MarkReaderIndex(); |
| 77 | + if (buffer.ReadableBytes >= 4) |
| 78 | + { |
| 79 | + return buffer.ReadInt(); |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + buffer.ResetReaderIndex(); |
| 84 | + return 0; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments