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 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); }
private void ReadHeader(IBufferReader readerBuffer) { switch (_headerSize) { case 1: _packageLength = readerBuffer.ReadByte(); break; case 2: _packageLength = readerBuffer.ReadInt16(); break; case 4: _packageLength = readerBuffer.ReadInt32(); break; default: ThrowHelper.ThrowLengthFieldConfigErrorException(); break; } }
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); }
public static ViofoGpsPoint Parse(IBufferReader reader, uint expectedSize) { long start = reader.Position; uint size = reader.ReadUintBE(); string type = reader.ReadString(4); string magic = reader.ReadString(4); if (expectedSize != size || type != "free" || magic != "GPS ") { return(null); } ViofoGpsPoint gps = new ViofoGpsPoint(); //# checking for weird Azdome 0xAA XOR "encrypted" GPS data. //This portion is a quick fix. int payload_size = 254; byte c = reader.ReadByte(); if (c == 0x05) { if (size < 254) { payload_size = (int)+size; } reader.Position += 5; //??? byte[] payload = reader.ReadBuffer(payload_size); } else if ((char)c == 'L') { const uint OFFSET_V2 = 48, OFFSET_V1 = 16; reader.Position = start + OFFSET_V2; //# Datetime data int hour = (int)reader.ReadUintLE(); int minute = (int)reader.ReadUintLE(); int second = (int)reader.ReadUintLE(); int year = (int)reader.ReadUintLE(); int month = (int)reader.ReadUintLE(); int day = (int)reader.ReadUintLE(); try { gps.Date = new DateTime(2000 + year, month, day, hour, minute, second); } catch (Exception err) { Debug.WriteLine(err.ToString()); return(null); } //# Coordinate data char active = (char)reader.ReadByte(); gps.IsActive = (active == 'A'); gps.Latitude_hemisphere = (char)reader.ReadByte(); gps.Longtitude_hemisphere = (char)reader.ReadByte(); gps.Unknown = reader.ReadByte(); float lat = reader.ReadFloatLE(); gps.Latitude = FixCoordinate(lat, gps.Latitude_hemisphere); float lon = reader.ReadFloatLE(); gps.Longtitude = FixCoordinate(lon, gps.Longtitude_hemisphere); gps.Speed = reader.ReadFloatLE(); gps.Bearing = reader.ReadFloatLE(); return(gps); } return(null); }