Exemple #1
0
        public static AmpMessage CreateResponseMessage(int serviceId, ushort messageId)
        {
            AmpMessage message = new AmpMessage
            {
                ServiceId         = serviceId,
                MessageId         = messageId,
                Version           = 1,
                CodecType         = 0,
                InvokeMessageType = InvokeMessageType.Response
            };

            return(message);
        }
Exemple #2
0
        public static AmpMessage CreateResponseMessage(string requestId)
        {
            var        data    = requestId.Split('|');
            AmpMessage message = new AmpMessage
            {
                ServiceId         = int.Parse(data[0]),
                MessageId         = ushort.Parse(data[1]),
                Version           = 1,
                CodecType         = 0,
                InvokeMessageType = InvokeMessageType.Response
            };

            return(message);
        }
Exemple #3
0
        public static AmpMessage CreateRequestMessage(int serviceId, ushort messageId, bool withOutResponse = false)
        {
            AmpMessage msg = new AmpMessage
            {
                ServiceId         = serviceId,
                MessageId         = messageId,
                Version           = 1,
                CodecType         = 0,
                InvokeMessageType =
                    withOutResponse ? InvokeMessageType.InvokeWithoutResponse : InvokeMessageType.Request
            };

            return(msg);
        }
Exemple #4
0
        protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List <object> output)
        {
            if (input.ReadableBytes == 0)
            {
                return;
            }

            var msg = new AmpMessage {
                Version = input.ReadByte()
            };

            int headLength;

            if (msg.Version == 0)
            {
                headLength = AmpMessage.VERSION_0_HEAD_LENGTH;
                if (input.ReadableBytes < AmpMessage.VERSION_0_HEAD_LENGTH - 1)
                {
                    throw new RpcCodecException($"decode error ,ReadableBytes={input.ReadableBytes+1},HEAD_LENGTH={AmpMessage.VERSION_0_HEAD_LENGTH}");
                }
            }
            else if (msg.Version == 1)
            {
                headLength = AmpMessage.VERSION_1_HEAD_LENGTH;
                if (input.ReadableBytes < AmpMessage.VERSION_1_HEAD_LENGTH - 1)
                {
                    throw new RpcCodecException($"decode error ,ReadableBytes={input.ReadableBytes+1},HEAD_LENGTH={AmpMessage.VERSION_1_HEAD_LENGTH}");
                }
            }
            else
            {
                throw new RpcCodecException($"decode error ,{msg.Version} is not support");
            }

            var length = input.ReadInt();

            msg.Sequence = input.ReadInt();
            var type = input.ReadByte();

            msg.InvokeMessageType = (InvokeMessageType)Enum.ToObject(typeof(InvokeMessageType), type);


            msg.ServiceId = msg.Version == 0 ? input.ReadUnsignedShort() : input.ReadInt();


            msg.MessageId = input.ReadUnsignedShort();
            msg.Code      = input.ReadInt();

            if (msg.Version == 1)
            {
                var codeType = input.ReadByte();
                if (codeType != this._serializer.CodecType)
                {
                    throw  new RpcCodecException($"CodecType:{codeType} is not Match {this._serializer.CodecType}");
                }
                msg.CodecType = (CodecType)Enum.ToObject(typeof(CodecType), codeType);
            }
            else
            {
                msg.CodecType = CodecType.Protobuf;
            }

            int left = length - headLength;

            if (left > 0)
            {
                if (left > input.ReadableBytes)
                {
                    throw new RpcCodecException("message not long enough!");
                }
                msg.Data = new byte[left];
                input.ReadBytes(msg.Data);
            }
            output.Add(msg);
        }