使用Protobuf-net反序列化二进制文件时,无效的线型

时间:2014-04-07 11:50:14

标签: c# deserialization protobuf-net

可能导致此错误的原因是什么?

Invalid wire-type; this usually means you have over-written a file without truncating or setting the length; see http://stackoverflow.com/q/2152978/23354

我按照上面的帮助链接,但设置FileMode.Truncate没有帮助......

问题总是发生在被反序列化的一个特定文件中的相同位置。我已多次重新创建该文件。

  [ProtoContract]
    public class TickRecord
    {
        [ProtoMember(1, DataFormat = DataFormat.FixedSize)]
        public DateTime DT;
        [ProtoMember(2)]
        public double BidPrice;
        [ProtoMember(3)]
        public double AskPrice;
        [ProtoMember(4, DataFormat = DataFormat.FixedSize)]
        public int BidSize;
        [ProtoMember(5, DataFormat = DataFormat.FixedSize)]
        public int AskSize;

        public TickRecord(DateTime DT, double BidPrice, double AskPrice, int BidSize, int AskSize)
        {
            this.DT = DT;
            this.BidPrice = BidPrice;
            this.AskPrice = AskPrice;
            this.BidSize = BidSize;
            this.AskSize = AskSize;

        }

        public TickRecord()
        {
        }
}

编辑JON SKEET

这很有效。但是当我使用我的真实数据(我无法分享)时,我的问题就出现了。我反序列化的大多数文件都可以正常工作,但其中一个文件没有....

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProtoBuf;
using System.IO;
using System.Diagnostics;

namespace BinTest3
{


    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Serialize_Click(object sender, EventArgs e)
        {

            FileStream outBin = null;

            string binFileName = @"C:\binfile.dft";
            outBin = File.Create(binFileName, 2048, FileOptions.None);

            DateTime d = DateTime.Now;

            TickRecord tr = new TickRecord(d, 1.02, 1.03,200,300);

            for (int i =0; i < 20000000; i++)
            {
                tr.BidPrice += 1;
                Serializer.SerializeWithLengthPrefix(outBin, tr, PrefixStyle.Base128);
            }

            outBin.Close();
            label1.Text = "Done ";
        }

        private void Deserialize_Click(object sender, EventArgs e)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();

            FileStream fs;
            string binFileName = @"C:\binfile.dft";

            byte[] data = File.ReadAllBytes(binFileName);
            MemoryStream ms = new MemoryStream(data);

            long skipRate =10;
            int count = 0;
            TickRecord tr;

            long skip = (38*skipRate);
            try
            {

                while ((tr = Serializer.DeserializeWithLengthPrefix<TickRecord>(ms, PrefixStyle.Base128)) != null) //fs.Length > fs.Position)
                {
                    count++;


                    ms.Seek(skip, SeekOrigin.Current);

                }
            }
            catch (Exception)
            {

            }

            ms.Close();

            sw.Stop();
            label1.Text = "Time taken: " + sw.Elapsed + " Count: " + count.ToString("n0");

        }
    }


    [ProtoContract]
    public class TickRecord
    {

        [ProtoMember(1, DataFormat = DataFormat.FixedSize)]
        public DateTime DT;
        [ProtoMember(2)]
        public double BidPrice;
        [ProtoMember(3)]
        public double AskPrice;
        [ProtoMember(4, DataFormat = DataFormat.FixedSize)]
        public int BidSize;
        [ProtoMember(5, DataFormat = DataFormat.FixedSize)]
        public int AskSize;

        public TickRecord()
        {

        }

        public TickRecord(DateTime DT, double BidPrice, double AskPrice, int BidSize, int AskSize)
        {
            this.DT = DT;
            this.BidPrice = BidPrice;
            this.AskPrice = AskPrice;
            this.BidSize = BidSize;
            this.AskSize = AskSize;

        }



    }
}

编辑2问题在序列化 - 它不像我想象的那样固定:

private void Once_Click(object sender, EventArgs e)
{
    FileStream outBin = null;

    string binFileName = @"C:\binfile.dft";
    outBin = File.Create(binFileName, 2048, FileOptions.None);

    DateTime d = DateTime.Now;
    long lastPosition = 0;

    TickRecord tr = new TickRecord(d, 1.02, 1.03, 200, 300);
    Serializer.SerializeWithLengthPrefix(outBin, tr, PrefixStyle.Base128);
    Console.WriteLine("outBin.Position: " + outBin.Position + " lastPosition: " + lastPosition + " diff: " + (outBin.Position-lastPosition));
    lastPosition = outBin.Position;
    tr = new TickRecord(d, 1.02, 0.0, 200, 300);
    Serializer.SerializeWithLengthPrefix(outBin, tr, PrefixStyle.Base128);
    Console.WriteLine("outBin.Position: " + outBin.Position + " lastPosition: " + lastPosition + " diff: " + (outBin.Position - lastPosition));


    outBin.Close();
    label1.Text = "Done ";

}

[ProtoContract] 公共类TickRecord {

    [ProtoMember(1, DataFormat = DataFormat.FixedSize)]
    public DateTime DT;
    [ProtoMember(2, DataFormat = DataFormat.FixedSize)]
    public double BidPrice;
    [ProtoMember(3, DataFormat = DataFormat.FixedSize)]
    public double AskPrice;
    [ProtoMember(4, DataFormat = DataFormat.FixedSize)]
    public int BidSize;
    [ProtoMember(5, DataFormat = DataFormat.FixedSize)]
    public int AskSize;

public TickRecord()
{

}

public TickRecord(DateTime DT, double BidPrice, double AskPrice, int BidSize, int AskSize)
{
    this.DT = DT;
    this.BidPrice = BidPrice;
    this.AskPrice = AskPrice;
    this.BidSize = BidSize;
    this.AskSize = AskSize;

}

}

这给出了:

outBin.Position: 38 lastPosition: 0 diff: 38
outBin.Position: 67 lastPosition: 38 diff: 29

因此,当它变为ZERO时,变量的大小似乎不同。

如何使ZERO double与非零双倍相同?

1 个答案:

答案 0 :(得分:3)

您需要IsRequired属性。

ZERO值double和int字段与非零字段的序列化不同。所以虽然我指定了FixedSize - 但事实并非如此。

以下内容创建了一个真正的FixedSize记录:

 [ProtoContract]
    public class TickRecord
    {
        [ProtoMember(1, DataFormat = DataFormat.FixedSize, IsRequired = true)]
        public DateTime DT;
        [ProtoMember(2, DataFormat = DataFormat.FixedSize, IsRequired = true)]
        public double BidPrice;
        [ProtoMember(3, DataFormat = DataFormat.FixedSize, IsRequired = true)]
        public double AskPrice;
        [ProtoMember(4, DataFormat = DataFormat.FixedSize, IsRequired = true)]
        public int BidSize;
        [ProtoMember(5, DataFormat = DataFormat.FixedSize, IsRequired = true)]
        public int AskSize;