Esempio n. 1
0
        public virtual void TestSingleFrame()
        {
            RpcUtil.RpcFrameDecoder decoder = new RpcUtil.RpcFrameDecoder();
            // Test "Length field is not received yet"
            ByteBuffer    buffer        = ByteBuffer.Allocate(1);
            ChannelBuffer buf           = new ByteBufferBackedChannelBuffer(buffer);
            ChannelBuffer channelBuffer = (ChannelBuffer)decoder.Decode(Org.Mockito.Mockito.Mock
                                                                        <ChannelHandlerContext>(), Org.Mockito.Mockito.Mock <Org.Jboss.Netty.Channel.Channel
                                                                                                                             >(), buf);

            Assert.True(channelBuffer == null);
            // Test all bytes are not received yet
            byte[] fragment = new byte[4 + 9];
            fragment[0] = unchecked ((byte)(1 << 7));
            // final fragment
            fragment[1] = 0;
            fragment[2] = 0;
            fragment[3] = unchecked ((byte)10);
            // fragment size = 10 bytes
            Assert.True(XDR.IsLastFragment(fragment));
            Assert.True(XDR.FragmentSize(fragment) == 10);
            buffer = ByteBuffer.Allocate(4 + 9);
            buffer.Put(fragment);
            buffer.Flip();
            buf           = new ByteBufferBackedChannelBuffer(buffer);
            channelBuffer = (ChannelBuffer)decoder.Decode(Org.Mockito.Mockito.Mock <ChannelHandlerContext
                                                                                    >(), Org.Mockito.Mockito.Mock <Org.Jboss.Netty.Channel.Channel>(), buf);
            Assert.True(channelBuffer == null);
        }
Esempio n. 2
0
            public override void MessageReceived(ChannelHandlerContext ctx, MessageEvent e)
            {
                ChannelBuffer buf = (ChannelBuffer)e.GetMessage();

                // Read reply
                if (!ValidMessageLength(buf.ReadableBytes()))
                {
                    e.GetChannel().Close();
                    return;
                }
                // handling fragment header for TCP, 4 bytes.
                byte[] fragmentHeader = Arrays.CopyOfRange(buf.Array(), 0, 4);
                int    fragmentSize   = XDR.FragmentSize(fragmentHeader);
                bool   isLast         = XDR.IsLastFragment(fragmentHeader);

                System.Diagnostics.Debug.Assert((fragmentSize == 28 && isLast == true));
                XDR xdr = new XDR();

                xdr.WriteFixedOpaque(Arrays.CopyOfRange(buf.Array(), 4, buf.ReadableBytes()));
                RpcReply reply = RpcReply.Read(xdr);

                if (reply.GetState() == RpcReply.ReplyState.MsgAccepted)
                {
                    RpcAcceptedReply acceptedReply = (RpcAcceptedReply)reply;
                    Handle(acceptedReply, xdr);
                }
                else
                {
                    RpcDeniedReply deniedReply = (RpcDeniedReply)reply;
                    Handle(deniedReply);
                }
                e.GetChannel().Close();
            }
Esempio n. 3
0
        public virtual void TestMultipleFrames()
        {
            RpcUtil.RpcFrameDecoder decoder = new RpcUtil.RpcFrameDecoder();
            // Test multiple frames
            byte[] fragment1 = new byte[4 + 10];
            fragment1[0] = 0;
            // not final fragment
            fragment1[1] = 0;
            fragment1[2] = 0;
            fragment1[3] = unchecked ((byte)10);
            // fragment size = 10 bytes
            NUnit.Framework.Assert.IsFalse(XDR.IsLastFragment(fragment1));
            Assert.True(XDR.FragmentSize(fragment1) == 10);
            // decoder should wait for the final fragment
            ByteBuffer buffer = ByteBuffer.Allocate(4 + 10);

            buffer.Put(fragment1);
            buffer.Flip();
            ChannelBuffer buf           = new ByteBufferBackedChannelBuffer(buffer);
            ChannelBuffer channelBuffer = (ChannelBuffer)decoder.Decode(Org.Mockito.Mockito.Mock
                                                                        <ChannelHandlerContext>(), Org.Mockito.Mockito.Mock <Org.Jboss.Netty.Channel.Channel
                                                                                                                             >(), buf);

            Assert.True(channelBuffer == null);
            byte[] fragment2 = new byte[4 + 10];
            fragment2[0] = unchecked ((byte)(1 << 7));
            // final fragment
            fragment2[1] = 0;
            fragment2[2] = 0;
            fragment2[3] = unchecked ((byte)10);
            // fragment size = 10 bytes
            Assert.True(XDR.IsLastFragment(fragment2));
            Assert.True(XDR.FragmentSize(fragment2) == 10);
            buffer = ByteBuffer.Allocate(4 + 10);
            buffer.Put(fragment2);
            buffer.Flip();
            buf           = new ByteBufferBackedChannelBuffer(buffer);
            channelBuffer = (ChannelBuffer)decoder.Decode(Org.Mockito.Mockito.Mock <ChannelHandlerContext
                                                                                    >(), Org.Mockito.Mockito.Mock <Org.Jboss.Netty.Channel.Channel>(), buf);
            Assert.True(channelBuffer != null);
            // Complete frame should have to total size 10+10=20
            Assert.Equal(20, channelBuffer.ReadableBytes());
        }
Esempio n. 4
0
            protected override object Decode(ChannelHandlerContext ctx, Org.Jboss.Netty.Channel.Channel
                                             channel, ChannelBuffer buf)
            {
                if (buf.ReadableBytes() < 4)
                {
                    return(null);
                }
                buf.MarkReaderIndex();
                byte[] fragmentHeader = new byte[4];
                buf.ReadBytes(fragmentHeader);
                int  length = XDR.FragmentSize(fragmentHeader);
                bool isLast = XDR.IsLastFragment(fragmentHeader);

                if (buf.ReadableBytes() < length)
                {
                    buf.ResetReaderIndex();
                    return(null);
                }
                ChannelBuffer newFragment = buf.ReadSlice(length);

                if (currentFrame == null)
                {
                    currentFrame = newFragment;
                }
                else
                {
                    currentFrame = ChannelBuffers.WrappedBuffer(currentFrame, newFragment);
                }
                if (isLast)
                {
                    ChannelBuffer completeFrame = currentFrame;
                    currentFrame = null;
                    return(completeFrame);
                }
                else
                {
                    return(null);
                }
            }