public AmpMessage Decode(IBufferReader reader) { if (reader.ReadableBytes == 0) { return(null); } AmpMessage msg = new AmpMessage(); if (reader.ReadableBytes < 10) { throw new Rpc.Exceptions.RpcCodecException("消息不正确,小于头长度"); } msg.Version = reader.ReadByte(); int length = reader.ReadInt(); msg.Sequence = reader.ReadInt(); byte type = reader.ReadByte(); msg.InvokeMessageType = InvokeMessageTypeParser.Parse(type); msg.ServiceId = reader.ReadUShort(); msg.MessageId = reader.ReadUShort(); int left = length - 14; if (left > 0) { if (left > reader.ReadableBytes) { throw new Rpc.Exceptions.RpcCodecException("消息长度不正确"); } msg.Data = new byte[left]; reader.ReadBytes(msg.Data); } return(msg); }
public CommandLineMessage Parse(IBufferReader reader) { byte[] buffer = new byte[reader.ReadableBytes]; reader.ReadBytes(buffer); string content = Encoding.UTF8.GetString(buffer); var arr = content.Split(new string[] { SPLITER }, StringSplitOptions.RemoveEmptyEntries); if (arr.Length == 0) { return(new CommandLineMessage(string.Empty)); } if (arr.Length == 1) { return(new CommandLineMessage(arr[0])); } return(new CommandLineMessage(arr[0], arr.Skip(1).ToArray())); }
public AmpMessage Decode(IBufferReader reader) { if (reader.ReadableBytes == 0) { return(null); } AmpMessage msg = new AmpMessage(); if (reader.ReadableBytes < AmpMessage.HEAD_LENGTH) { throw new Rpc.Exceptions.RpcCodecException($"decode error ,ReadableBytes={reader.ReadableBytes},HEAD_LENGTH={AmpMessage.HEAD_LENGTH}"); } msg.Version = reader.ReadByte(); int length = reader.ReadInt(); msg.Sequence = reader.ReadInt(); byte type = reader.ReadByte(); msg.InvokeMessageType = Rpc.Utils.ParseUtils.ParseMessageType(type); msg.ServiceId = reader.ReadUShort(); msg.MessageId = reader.ReadUShort(); msg.Code = reader.ReadInt(); int left = length - AmpMessage.HEAD_LENGTH; if (left > 0) { if (left > reader.ReadableBytes) { throw new Rpc.Exceptions.RpcCodecException("消息长度不正确"); } msg.Data = new byte[left]; reader.ReadBytes(msg.Data); } return(msg); }
public AvenueMessage Decode(IBufferReader input) { var message = new AvenueMessage(); //开始读取头 message.Flag = input.ReadByte(); if (message.Flag != AvenueConstans.TYPE_REQUEST && message.Flag != AvenueConstans.TYPE_RESPONSE) { throw new RpcCodecException("package_type_error"); } var headlen = input.ReadByte(); message.Version = input.ReadByte(); if (message.Version != AvenueConstans.VERSION_1) { throw new RpcCodecException("package_version_error"); } input.ReadByte(); //包长 int packetLen = input.ReadInt(); int serviceId = input.ReadInt(); if (serviceId < 0) { throw new RpcCodecException("package_serviceid_error"); } int msgId = input.ReadInt(); if (msgId != 0) { if (serviceId == 0) { throw new RpcCodecException("package_msgid_error"); } } message.ServiceId = serviceId; message.MsgId = msgId; message.Sequence = input.ReadInt(); //下4位为 optional //context input.ReadByte(); byte mustReach = input.ReadByte(); byte format = input.ReadByte(); byte encoding = input.ReadByte(); if (mustReach != AvenueConstans.MUSTREACH_NO && mustReach != AvenueConstans.MUSTREACH_YES) { throw new RpcCodecException("package_mustreach_error"); } if (format != AvenueConstans.FORMAT_TLV) { throw new RpcCodecException("package_format_error"); } if (encoding != AvenueConstans.ENCODING_GBK && encoding != AvenueConstans.ENCODING_UTF8) { throw new RpcCodecException("package_encoding_error"); } message.MustReach = mustReach; message.Format = format; message.Encoding = encoding; // 优先级实际没有用 input.ReadInt(); //signature input.ReadBytes(new byte[16]); if (headlen > AvenueConstans.STANDARD_HEADLEN) { message.XHead = new byte[headlen - AvenueConstans.STANDARD_HEADLEN]; input.ReadBytes(message.XHead); } //开始读取Body message.Body = new byte[packetLen - headlen]; input.ReadBytes(message.Body); return(message); }
public AmpMessage Parse(IBufferReader reader) { if (reader.ReadableBytes == 0) { return(null); } var msg = new AmpMessage { Version = reader.ReadByte() }; int headLength; if (msg.Version == 0) { headLength = AmpMessage.VERSION_0_HEAD_LENGTH; if (reader.ReadableBytes < AmpMessage.VERSION_0_HEAD_LENGTH - 1) { throw new RpcCodecException($"decode error ,ReadableBytes={reader.ReadableBytes+1},HEAD_LENGTH={AmpMessage.VERSION_0_HEAD_LENGTH}"); } } else if (msg.Version == 1) { headLength = AmpMessage.VERSION_1_HEAD_LENGTH; if (reader.ReadableBytes < AmpMessage.VERSION_1_HEAD_LENGTH - 1) { throw new RpcCodecException($"decode error ,ReadableBytes={reader.ReadableBytes+1},HEAD_LENGTH={AmpMessage.VERSION_1_HEAD_LENGTH}"); } } else { throw new RpcCodecException($"decode error ,{msg.Version} is not support"); } var length = reader.ReadInt(); msg.Sequence = reader.ReadInt(); var type = reader.ReadByte(); msg.InvokeMessageType = (InvokeMessageType)Enum.ToObject(typeof(InvokeMessageType), type); msg.ServiceId = msg.Version == 0 ? reader.ReadUShort() : reader.ReadInt(); msg.MessageId = reader.ReadUShort(); msg.Code = reader.ReadInt(); if (msg.Version == 1) { byte codeType = reader.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 > reader.ReadableBytes) { throw new RpcCodecException("message not long enough!"); } msg.Data = new byte[left]; reader.ReadBytes(msg.Data); } return(msg); }