2

我正在尝试学习 C# 中的套接字,并决定创建一个多人游戏进行练习。虽然我在套接字发送方面已经取得了很大进展,但我遇到了一个奇怪的问题,即在从另一个客户端接收并反序列化我的类后,布尔值总是变为真。

问题出现在这里:

void OnDataReceived(object sender, ConnectionEventArgs e)
{
    Connection con = (Connection)e.SyncResult.AsyncState;
    Game.ScoreBoard[currentPlayer] = Serializer.ToScoreCard(con.buffer); //Here
    ...
}

Game.ScoreBoard[currentPlayer].Local总是为真,我完全不确定问题出在哪里。其他值似乎工作正常。Connection 是一个包含 IP、套接字和管理连接等的类。缓冲区大小当前为 30 000,因为我尝试扩大它以确保这不是问题。

以下是课堂上的相关信息:

public ScoreCard(SerializationInfo info, StreamingContext context)
{
    name = (string)info.GetValue("Name", typeof(string));
    played = (bool[])info.GetValue("Played", typeof(bool[]));
    scores = (int[])info.GetValue("Scores", typeof(int[]));
    bonusScore = (int)info.GetValue("bonusScore", typeof(int));
    local = (bool)info.GetValue("Local", typeof(bool));
}

public void GetObjectData(SerializationInfo info, StreamingContext context)
{
    info.AddValue("Scores", scores, typeof(int[]));
    info.AddValue("Played", played, typeof(bool[]));
    info.AddValue("Name", name, typeof(string));
    info.AddValue("bonusScore", bonusScore, typeof(int));
    info.AddValue("Local", local, typeof(bool));
}

这是序列化程序类:

static class Serializer
{
    public static byte[] ToArray(object data)
    {
        MemoryStream stream = new MemoryStream();
        BinaryFormatter b = new BinaryFormatter();
        b.Serialize(stream, data);
        return stream.ToArray();
    }

    public static ScoreCard ToScoreCard(byte[] data)
    {
        ScoreCard sc;
        MemoryStream stream = new MemoryStream(data);
        BinaryFormatter b = new BinaryFormatter();
        sc = (ScoreCard)b.Deserialize(stream);
        return sc;
    }
}

我不知道该怎么做,或者即使提供的信息足以让您解决我的问题。如有必要,我可以提供更多信息。我只是觉得奇怪的是,似乎只有那个布尔值无法正常工作。

编辑:我发现了问题,像往常一样,这是一个简单的愚蠢错误。哦,好吧,我至少学会了制作自己的二进制格式化程序。多谢你们 :)

4

1 回答 1

1

BinaryFormatter 的性能很糟糕。它序列化的每个对象都有大量开销,根据我的经验,一个格式化程序需要> 40秒来序列化二进制对象,而自定义二进制编写器可以在< 1秒内完成。

您可能要考虑这一点:

static class Serializer
{
    public static byte[] MakePacket(ScoreCard data)
    {
        MemoryStream stream = new MemoryStream();
        using (StreamWriter sw = new StreamWriter(stream)) {
            sw.Write(1); // This indicates "version one" of your data format - you can modify the code to support multiple versions by using this
            sw.Write(data.Name);
            sw.Write(data.scores.Length);
            foreach (int score in data.scores) {
                sw.Write(score);
            }
            sw.Write(data.played.Length);
            foreach (bool played in data.played) {
                sw.Write(played );
            }
            sw.Write(data.bonusScore);
            sw.Write(data.local);
        }
        return stream.ToArray();
    }

    public static ScoreCard ReadPacket(byte[] data)
    {
        ScoreCard sc = new ScoreCard();
        MemoryStream stream = new MemoryStream(data);
        using (StreamReader sr = new StreamReader(stream)) {
            int ver = sr.ReadInt32();
            switch (ver) {
                case 1:
                    sc.name = sr.ReadString();
                    sc.scores = new int[sr.ReadInt32()];
                    for (int i = 0; i < sc.scores.Length; i++) {
                        sc.scores[i] = sr.ReadInt32();
                    }
                    sc.played = new bool[sr.ReadInt32()];
                    for (int i = 0; i < sc.scores.Length; i++) {
                        sc.played [i] = sr.ReadBool();
                    }
                    sc.BonusScore = sr.ReadInt32();
                    sc.Local = sr.ReadBool();
                    break;
                default: 
                    throw new Exception("Unrecognized network protocol");
            }
        }
        return sc;
    }
}
于 2012-07-23T22:37:31.147 回答