Exemplo n.º 1
0
        public override IEnumerable <ResponseFrame> Process(byte[] buffer, int size, Stream stream, IProtoBufComporessor compressor)
        {
            Init(buffer, size);

            while (AreMore())
            {
                byte b = GetByte();

                switch (ByteIdx)
                {
                case 0: TmpFrameHeader.Version = b; break;

                case 1: TmpFrameHeader.Flags = b; break;

                case 2: TmpFrameHeader.StreamId = b; break;

                case 3: TmpFrameHeader.Opcode = b; break;

                case 4:
                {
                    TmpFrameHeader.Len[0] = b;
                } break;

                case 5: TmpFrameHeader.Len[1] = b; break;

                case 6: TmpFrameHeader.Len[2] = b; break;

                case 7: TmpFrameHeader.Len[3] = b;
                    _bodyLen = TypeInterpreter.BytesToInt32(TmpFrameHeader.Len, 0);
                    TmpFrame = TmpFrameHeader.MakeFrame(new BufferedProtoBuf(_bodyLen, ((TmpFrameHeader.Flags & 0x01) == 0x01) ? compressor : null));
                    yield return(TmpFrame);

                    break;

                default:
                {
                    TmpFrame.RawStream.WriteByte(b);
                }
                break;
                }
                ByteIdx++;
                if (ByteIdx - 8 >= _bodyLen)
                {
                    ByteIdx        = 0;
                    _bodyLen       = int.MaxValue;
                    TmpFrameHeader = new FrameHeader();
                }
            }
        }
Exemplo n.º 2
0
        virtual public IEnumerable <ResponseFrame> Process(byte[] buffer, int size, Stream stream, IProtoBufComporessor compressor)
        {
            Init(buffer, size);

            int bodyLen = int.MaxValue;

            while (AreMore())
            {
                byte b = GetByte();

                switch (ByteIdx)
                {
                case 0: TmpFrameHeader.Version = b; break;

                case 1: TmpFrameHeader.Flags = b; break;

                case 2: TmpFrameHeader.StreamId = b; break;

                case 3: TmpFrameHeader.Opcode = b; break;

                case 4:
                {
                    TmpFrameHeader.Len[0] = b;
                } break;

                case 5: TmpFrameHeader.Len[1] = b; break;

                case 6: TmpFrameHeader.Len[2] = b; break;

                case 7: TmpFrameHeader.Len[3] = b;
                    bodyLen  = TypeInterpreter.BytesToInt32(TmpFrameHeader.Len, 0);
                    TmpFrame = TmpFrameHeader.MakeFrame(new StreamProtoBuf(stream, ((TmpFrameHeader.Flags & 0x01) == 0x01) ? compressor : null));
                    yield return(TmpFrame);

                    break;

                default:
                    throw new DriverInternalError("Invalid state");
                }

                ByteIdx++;
                if (ByteIdx >= FrameHeader.Size)
                {
                    ByteIdx        = 0;
                    TmpFrameHeader = new FrameHeader();
                }
            }
        }
Exemplo n.º 3
0
        public ResponseFrame MakeFrame(IProtoBuf stream)
        {
            var bodyLen = TypeInterpreter.BytesToInt32(Len, 0);

            if (MaxFrameSize - 8 < bodyLen)
            {
                throw new DriverInternalError("Frame length mismatch");
            }

            var frame = new ResponseFrame()
            {
                FrameHeader = this, RawStream = stream
            };

            return(frame);
        }
Exemplo n.º 4
0
        public object ConvertFrom(byte[] decimalBuf)
        {
            if (decimalBuf.Length > 16)
            {
                throw new ArgumentOutOfRangeException(
                          "this java.math.BigDecimal is too big to fit into System.Decimal. Think about using other TypeAdapter for java.math.BigDecimal (e.g. J#, IKVM,...)");
            }

            var scaleBytes = new byte[4];

            for (int i = 0; i < 4; i++)
            {
                scaleBytes[i] = decimalBuf[i];
            }

            var bigIntBytes = new byte[decimalBuf.Length - 4];

            for (int i = 0; i < bigIntBytes.Length; i++)
            {
                bigIntBytes[i] = decimalBuf[i + 4];
            }

            bool isNegative = (bigIntBytes[0] & 0x80) != 0;

            var bytes = new byte[12];

            if (isNegative)
            {
                for (int i = 0; i < 12; i++)
                {
                    bytes[i] = 0xff;
                }
            }

            int offset = 12 - bigIntBytes.Length;

            for (int i = 0; i < bigIntBytes.Length; i++)
            {
                bytes[offset + i] = bigIntBytes[i];
            }

            var lowB  = new byte[4];
            var midB  = new byte[4];
            var highB = new byte[4];

            Array.Copy(bytes, 8, lowB, 0, 4);
            Array.Copy(bytes, 4, midB, 0, 4);
            Array.Copy(bytes, 0, highB, 0, 4);

            Array.Reverse(lowB);
            Array.Reverse(midB);
            Array.Reverse(highB);

            uint low   = BitConverter.ToUInt32(lowB, 0);
            uint mid   = BitConverter.ToUInt32(midB, 0);
            uint high  = BitConverter.ToUInt32(highB, 0);
            var  scale = (byte)TypeInterpreter.BytesToInt32(scaleBytes, 0);

            if (isNegative)
            {
                low  = ~low;
                mid  = ~mid;
                high = ~high;

                high += (mid == 0xFFFFFFF && low == 0xFFFFFFF) ? 1u : 0;
                mid  += (low == 0xFFFFFFF) ? 1u : 0;
                low  += 1;
            }
            return(new decimal((int)low, (int)mid, (int)high, isNegative, scale));
        }