public virtual void test_createUsingByte()
 {
     Assert.False(ProtocolSwitch.create(0).isOn(0));
     Assert.True(ProtocolSwitch.create(1).isOn(0));
     Assert.True(ProtocolSwitch.create(2).isOn(1));
     Assert.True(ProtocolSwitch.create(4).isOn(2));
     Assert.True(ProtocolSwitch.create(8).isOn(3));
     Assert.True(ProtocolSwitch.create(3).isOn(0));
     Assert.True(ProtocolSwitch.create(3).isOn(1));
     Assert.False(ProtocolSwitch.create(3).isOn(2));
     Assert.True(ProtocolSwitch.create(64).isOn(6));
     Assert.False(ProtocolSwitch.create(64).isOn(0));
     Assert.True(ProtocolSwitch.create(127).isOn(0));
     Assert.True(ProtocolSwitch.create(127).isOn(1));
     Assert.True(ProtocolSwitch.create(127).isOn(6));
     Assert.False(ProtocolSwitch.create(127).isOn(7));
     Assert.True(ProtocolSwitch.create(255).isOn(0));
     Assert.True(ProtocolSwitch.create(255).isOn(1));
     Assert.True(ProtocolSwitch.create(255).isOn(7));
     try
     {
         ProtocolSwitch.create(511);
         Assert.Null("Should not reach here!");
     }
     catch (System.ArgumentOutOfRangeException)
     {
     }
 }
Example #2
0
        /// <summary>
        /// Convert application request object to remoting request command.
        /// </summary>
        /// <param name="request"> </param>
        /// <param name="conn"> </param>
        /// <param name="timeoutMillis">
        /// @return </param>
        /// <exception cref="CodecException"> </exception>
        //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
        //ORIGINAL LINE: protected RemotingCommand toRemotingCommand(Object request, Connection conn, InvokeContext invokeContext, int timeoutMillis) throws exception.SerializationException
        protected internal virtual RemotingCommand toRemotingCommand(object request, Connection conn, InvokeContext invokeContext, int timeoutMillis)
        {
            RpcRequestCommand command = (RpcRequestCommand)CommandFactory.createRequestCommand(request);

            if (null != invokeContext)
            {
                // set client custom serializer for request command if not null
                object clientCustomSerializer = invokeContext.get(InvokeContext.BOLT_CUSTOM_SERIALIZER);
                if (null != clientCustomSerializer)
                {
                    try
                    {
                        command.Serializer = ((byte?)clientCustomSerializer).Value;
                    }
                    catch (System.InvalidCastException)
                    {
                        //JAVA TO C# CONVERTER WARNING: The .NET Type.FullName property will not always yield results identical to the Java Class.getName method:
                        throw new System.ArgumentException("Illegal custom serializer [" + clientCustomSerializer + "], the type of value should be [byte], but now is [" + clientCustomSerializer.GetType().FullName + "].");
                    }
                }

                // enable crc by default, user can disable by set invoke context `false` for key `InvokeContext.BOLT_CRC_SWITCH`
                bool?crcSwitch = (bool)invokeContext.get(InvokeContext.BOLT_CRC_SWITCH, ProtocolSwitch.CRC_SWITCH_DEFAULT_VALUE);
                if (null != crcSwitch && crcSwitch.HasValue && crcSwitch.Value)
                {
                    command.ProtocolSwitch = ProtocolSwitch.create(new int[] { ProtocolSwitch.CRC_SWITCH_INDEX });
                }
            }
            else
            {
                // enable crc by default, if there is no invoke context.
                command.ProtocolSwitch = ProtocolSwitch.create(new int[] { ProtocolSwitch.CRC_SWITCH_INDEX });
            }
            command.Timeout = timeoutMillis;
            //JAVA TO C# CONVERTER WARNING: The .NET Type.FullName property will not always yield results identical to the Java Class.getName method:
            command.RequestClass  = request.GetType();
            command.InvokeContext = invokeContext;
            command.serialize();
            logDebugInfo(command);
            return(command);
        }
        public virtual void test_createUsingIndex()
        {
            for (int i = 0; i < 7; ++i)
            {
                Assert.True(ProtocolSwitch.create(new int[] { i }).isOn(i));
            }

            int size = 7;

            int[] a = new int[size];
            for (int i = 0; i < size; ++i)
            {
                a[i] = i;
            }
            ProtocolSwitch status = ProtocolSwitch.create(a);

            for (int i = 0; i < size; ++i)
            {
                Assert.True(status.isOn(i));
            }
        }
        /// <seealso cref= CommandDecoder#decode(IChannelHandlerContext, IByteBuffer, List<object>) </seealso>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: @Override public void decode(io.netty.channel.IChannelHandlerContext ctx, io.netty.buffer.IByteBuffer in, java.util.List<object><Object> out) throws Exception
        public virtual void decode(IChannelHandlerContext ctx, IByteBuffer @in, List <object> @out)
        {
            // the less length between response header and request header
            if (@in.ReadableBytes >= lessLen)
            {
                @in.MarkReaderIndex();
                byte protocol = @in.ReadByte();
                @in.ResetReaderIndex();
                if (protocol == RpcProtocolV2.PROTOCOL_CODE)
                {
                    /*
                     * ver: version for protocol
                     * type: request/response/request oneway
                     * cmdcode: code for remoting command
                     * ver2:version for remoting command
                     * requestId: id of request
                     * codec: code for codec
                     * switch: function switch
                     * (req)timeout: request timeout
                     * (resp)respStatus: response status
                     * classLen: length of request or response class name
                     * headerLen: length of header
                     * contentLen: length of content
                     * className
                     * header
                     * content
                     */
                    if (@in.ReadableBytes > 2 + 1)
                    {
                        int startIndex = @in.ReaderIndex;
                        @in.MarkReaderIndex();
                        @in.ReadByte();                         //protocol code
                        byte version = @in.ReadByte();          //protocol version
                        byte type    = @in.ReadByte();          //type
                        if (type == RpcCommandType.REQUEST || type == RpcCommandType.REQUEST_ONEWAY)
                        {
                            //decode request
                            if (@in.ReadableBytes >= RpcProtocolV2.RequestHeaderLength - 3)
                            {
                                short  cmdCode             = @in.ReadShort();
                                byte   ver2                = @in.ReadByte();
                                int    requestId           = @in.ReadInt();
                                byte   serializer          = @in.ReadByte();
                                byte   protocolSwitchValue = @in.ReadByte();
                                int    timeout             = @in.ReadInt();
                                short  classLen            = @in.ReadShort();
                                short  headerLen           = @in.ReadShort();
                                int    contentLen          = @in.ReadInt();
                                byte[] clazz               = null;
                                byte[] header              = null;
                                byte[] content             = null;

                                // decide the at-least bytes length for each version
                                int  lengthAtLeastForV1 = classLen + headerLen + contentLen;
                                bool crcSwitchOn        = ProtocolSwitch.isOn(ProtocolSwitch.CRC_SWITCH_INDEX, protocolSwitchValue);
                                int  lengthAtLeastForV2 = classLen + headerLen + contentLen;
                                if (crcSwitchOn)
                                {
                                    lengthAtLeastForV2 += 4;                                     // crc int
                                }

                                // continue read
                                if ((version == RpcProtocolV2.PROTOCOL_VERSION_1 && @in.ReadableBytes >= lengthAtLeastForV1) || (version == RpcProtocolV2.PROTOCOL_VERSION_2 && @in.ReadableBytes >= lengthAtLeastForV2))
                                {
                                    if (classLen > 0)
                                    {
                                        clazz = new byte[classLen];
                                        @in.ReadBytes(clazz);
                                    }
                                    if (headerLen > 0)
                                    {
                                        header = new byte[headerLen];
                                        @in.ReadBytes(header);
                                    }
                                    if (contentLen > 0)
                                    {
                                        content = new byte[contentLen];
                                        @in.ReadBytes(content);
                                    }
                                    if (version == RpcProtocolV2.PROTOCOL_VERSION_2 && crcSwitchOn)
                                    {
                                        checkCRC(@in, startIndex);
                                    }
                                }
                                else
                                {                                 // not enough data
                                    @in.ResetReaderIndex();
                                    return;
                                }
                                RequestCommand command;
                                if (cmdCode == CommandCode_Fields.HEARTBEAT_VALUE)
                                {
                                    command = new HeartbeatCommand();
                                }
                                else
                                {
                                    command = createRequestCommand(cmdCode);
                                }
                                command.Type           = type;
                                command.Version        = ver2;
                                command.Id             = requestId;
                                command.Serializer     = serializer;
                                command.ProtocolSwitch = ProtocolSwitch.create(protocolSwitchValue);
                                command.Timeout        = timeout;
                                command.Clazz          = clazz;
                                command.Header         = header;
                                command.Content        = content;

                                @out.Add(command);
                            }
                            else
                            {
                                @in.ResetReaderIndex();
                            }
                        }
                        else if (type == RpcCommandType.RESPONSE)
                        {
                            //decode response
                            if (@in.ReadableBytes >= RpcProtocolV2.ResponseHeaderLength - 3)
                            {
                                short  cmdCode             = @in.ReadShort();
                                byte   ver2                = @in.ReadByte();
                                int    requestId           = @in.ReadInt();
                                byte   serializer          = @in.ReadByte();
                                byte   protocolSwitchValue = @in.ReadByte();
                                short  status              = @in.ReadShort();
                                short  classLen            = @in.ReadShort();
                                short  headerLen           = @in.ReadShort();
                                int    contentLen          = @in.ReadInt();
                                byte[] clazz               = null;
                                byte[] header              = null;
                                byte[] content             = null;

                                // decide the at-least bytes length for each version
                                int  lengthAtLeastForV1 = classLen + headerLen + contentLen;
                                bool crcSwitchOn        = ProtocolSwitch.isOn(ProtocolSwitch.CRC_SWITCH_INDEX, protocolSwitchValue);
                                int  lengthAtLeastForV2 = classLen + headerLen + contentLen;
                                if (crcSwitchOn)
                                {
                                    lengthAtLeastForV2 += 4;                                     // crc int
                                }

                                // continue read
                                if ((version == RpcProtocolV2.PROTOCOL_VERSION_1 && @in.ReadableBytes >= lengthAtLeastForV1) || (version == RpcProtocolV2.PROTOCOL_VERSION_2 && @in.ReadableBytes >= lengthAtLeastForV2))
                                {
                                    if (classLen > 0)
                                    {
                                        clazz = new byte[classLen];
                                        @in.ReadBytes(clazz);
                                    }
                                    if (headerLen > 0)
                                    {
                                        header = new byte[headerLen];
                                        @in.ReadBytes(header);
                                    }
                                    if (contentLen > 0)
                                    {
                                        content = new byte[contentLen];
                                        @in.ReadBytes(content);
                                    }
                                    if (version == RpcProtocolV2.PROTOCOL_VERSION_2 && crcSwitchOn)
                                    {
                                        checkCRC(@in, startIndex);
                                    }
                                }
                                else
                                {                                 // not enough data
                                    @in.ResetReaderIndex();
                                    return;
                                }
                                ResponseCommand command;
                                if (cmdCode == CommandCode_Fields.HEARTBEAT_VALUE)
                                {
                                    command = new HeartbeatAckCommand();
                                }
                                else
                                {
                                    command = createResponseCommand(cmdCode);
                                }
                                command.Type               = type;
                                command.Version            = ver2;
                                command.Id                 = requestId;
                                command.Serializer         = serializer;
                                command.ProtocolSwitch     = ProtocolSwitch.create(protocolSwitchValue);
                                command.ResponseStatus     = (ResponseStatus)status;
                                command.Clazz              = clazz;
                                command.Header             = header;
                                command.Content            = content;
                                command.ResponseTimeMillis = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
                                command.ResponseHost       = (IPEndPoint)ctx.Channel.RemoteAddress;

                                @out.Add(command);
                            }
                            else
                            {
                                @in.ResetReaderIndex();
                            }
                        }
                        else
                        {
                            string emsg = "Unknown command type: " + type;
                            logger.LogError(emsg);
                            throw new Exception(emsg);
                        }
                    }
                }
                else
                {
                    string emsg = "Unknown protocol: " + protocol;
                    logger.LogError(emsg);
                    throw new Exception(emsg);
                }
            }
        }