/// <summary> /// Decodes the specified data and returns the <see cref="ResonanceTranscodingInformation.Message"/> as type T. /// </summary> /// <typeparam name="T">Type of expected message.</typeparam> /// <param name="data">The encoded data.</param> /// <returns></returns> public T Decode <T>(byte[] data) { ResonanceDecodingInformation info = new ResonanceDecodingInformation(); Decode(data, info); return((T)info.Message); }
/// <summary> /// Decodes the specified data and populates the specified decoding information. /// </summary> /// <param name="data">The encoded data.</param> /// <param name="info">The decoding information object to populate.</param> public virtual void Decode(byte[] data, ResonanceDecodingInformation info) { try { using (MemoryStream ms = new MemoryStream(data)) { using (BinaryReader reader = new BinaryReader(ms)) { _headerTranscoder.Decode(reader, info); OnTranscodingInformationDecoded(info); if (info.Type != ResonanceTranscodingInformationType.KeepAliveRequest && info.Type != ResonanceTranscodingInformationType.KeepAliveResponse && info.Type != ResonanceTranscodingInformationType.Disconnect && info.Type != ResonanceTranscodingInformationType.MessageSyncACK && !info.HasError) { ms.Position = info.ActualMessageStreamPosition; byte[] msgData = reader.ReadBytes((int)(ms.Length - ms.Position)); if (info.IsCompressed) { msgData = DecompressMessageData(msgData); } if (EncryptionConfiguration.Enabled) { msgData = DecryptMessageData(msgData); } using (MemoryStream msgMs = new MemoryStream(msgData)) { info.Message = Decode(msgMs); } } } } } catch (Exception ex) { info.DecoderException = ex; throw; } }
/// <summary> /// Decodes the header using the specified binary reader and populates the specified decoding information. /// </summary> /// <param name="reader">The binary reader.</param> /// <param name="info">The decoding information.</param> public virtual void Decode(BinaryReader reader, ResonanceDecodingInformation info) { info.ProtocolVersion = reader.ReadByte(); info.Transcoding = reader.ReadShortASCII(); info.IsCompressed = reader.ReadBoolean(); info.Token = reader.ReadShortASCII(); info.Type = (ResonanceTranscodingInformationType)reader.ReadByte(); String rpcSignatureString = reader.ReadShortASCII(); if (!String.IsNullOrEmpty(rpcSignatureString)) { info.RPCSignature = RPCSignature.FromString(rpcSignatureString); } byte timeout = reader.ReadByte(); if (timeout > 0) { info.Timeout = timeout; } if (info.Type == ResonanceTranscodingInformationType.Response || info.Type == ResonanceTranscodingInformationType.MessageSyncACK) { info.Completed = reader.ReadBoolean(); info.HasError = reader.ReadBoolean(); info.ErrorMessage = reader.ReadUTF8(); } else if (info.Type == ResonanceTranscodingInformationType.Disconnect) { info.ErrorMessage = reader.ReadUTF8(); } info.ActualMessageStreamPosition = reader.ReadUInt32(); if (info.ProtocolVersion >= ProtocolVersion) { //Add new fields here... } }
/// <summary> /// Called when the transcoding information is first available. /// </summary> /// <param name="info">The transcoding information.</param> protected virtual void OnTranscodingInformationDecoded(ResonanceDecodingInformation info) { //Leave it. }