c# - protobuf-net 无法反序列化我的类

标签 c# protobuf-net

我想序列化一个“播放器”类并通过我的网络流将它发送到客户端。

玩家类

    [ProtoMember(1)]
    public int flag;
    [ProtoMember(2)]
    public Int16 id;
    [ProtoMember(3)]
    public MyVector3 CharPos;
    [ProtoMember(7)]
    public bool spawned;

MyVector3(由于protobuf不支持Vector3的序列化)

[ProtoContract]
public class MyVector3
{
    [ProtoMember(4)]
    public float X { get; set; }

    [ProtoMember(5)]
    public float Y { get; set; }

    [ProtoMember(6)]
    public float Z { get; set; }

    public MyVector3()
    {
        this.X = 0.0f;
        this.Y = 0.0f;
        this.Z = 0.0f;
    }

    public MyVector3(float x, float y, float z)
    {
        this.X = x;
        this.Y = y;
        this.Z = z;
    }

    public static implicit operator Vector3(MyVector3 v)
    {
        return new Vector3(v.X, v.Y, v.Z);
    }

    public static implicit operator MyVector3(Vector3 v)
    {
        return new MyVector3(v.X, v.Y, v.Z);
    }
}

序列化和反序列化函数

public byte[] serialize(Object obj)
    {
        if (obj == null)
        {
            return null;
        }
        MemoryStream ms = new MemoryStream();
        Serializer.SerializeWithLengthPrefix(ms,obj,PrefixStyle.Base128);
        return ms.ToArray();
    }

    public Player deserialize(NetworkStream inc)
    {
        Player obj = Serializer.DeserializeWithLengthPrefix<Player>(inc,PrefixStyle.Base128);
        return obj;
    }

测试函数(实际上不起作用!)

    static void Main(string[] args)
    {
        TcpListener serverSocket = new TcpListener(8888);
        TcpClient clientSocket = default(TcpClient);

        Serialize ser = new Serialize();

        Player temp = new Player();

        serverSocket.Start();

        while (true)
        {
            clientSocket = serverSocket.AcceptTcpClient();
            using (NetworkStream networkStream = clientSocket.GetStream())
            {
                DeserializeTest(ser,networkStream);
                SerializeTest(ser, networkStream);
            }
        }

    }

    static void SerializeTest(Serialize ser,NetworkStream networkStream)
    {
        Player temp = new Player();
        BinaryWriter writer = new BinaryWriter(networkStream);
        writer.Write(ser.serialize(temp));
        networkStream.Flush();
    }

    static void DeserializeTest(Serialize ser, NetworkStream networkStream)
    {
        Player temp = new Player();
        temp = (Player)ser.deserialize(networkStream);
        Console.WriteLine(temp.flag.ToString() + temp.CharPos.ToString());
        networkStream.Flush();
    }
}

现在发生了什么:

DeserializeTest 启动时,protobuf 尝试反序列化来自 networkStream 的数据,整个函数有点“卡住”并开始循环。 当我序列化数据(在客户端中)并将其发送到反序列化的服务器(此代码)时,一切都很顺利。

最佳答案

网络流在关闭之前不会结束,默认情况下 protobuf(由 Google 定义)消耗到流的末尾。它正在等待更多数据,或者等待您的流最终结束。如果您要发送多条消息,或者只想保持流打开,请将 SerializeDeserialize 替换为 SerializeWithLengthPrefixDeserializeWithLengthPrefix。这将添加额外的信息,让它在不关闭流的情况下获得单独的消息。重要的是管道的两端都知道他们正在使用这个变体。


完整示例(这是使用核心 .NET,但现在应该可以毫无问题地进行翻译):

using System;
using System.IO;
using System.Net.Sockets;
using System.Threading;
using ProtoBuf;
[ProtoContract]
public class Player
{
    [ProtoMember(1)] public int flag;
    [ProtoMember(2)] public Int16 id;
    [ProtoMember(3, DataFormat = DataFormat.Group)] public MyVector3 CharPos;
    [ProtoMember(7)] public bool spawned;
}

public struct MyVector3
{
    public readonly float X, Y, Z;
    public MyVector3(float x, float y, float z)
    {
        X = x;
        Y = y;
        Z = z;
    }
    public override string ToString()
    {
        return string.Format("({0},{1},{2})", X, Y, Z);
    }
}

static class Program
{
    static ManualResetEvent evt = new ManualResetEvent(false);
    static void Main(string[] args)
    {
        var player = new Player() {CharPos = new MyVector3(1, 2, 3), flag=123, id=456, spawned=true};
        ThreadPool.QueueUserWorkItem(x =>
        {
            Console.WriteLine("client: waiting for server");
            evt.WaitOne();
            Console.WriteLine("client: opening connection");
            using (var client = new TcpClient("localhost", 15000))
            using (var ns = client.GetStream())
            {
                serialize(ns, player);
                ns.Flush();
                Console.WriteLine("client: wrote player");

                Console.WriteLine("client: waiting for response");
                while (ns.ReadByte() >= 0)
                {
                    Console.WriteLine("client: receiving...");
                }
                Console.WriteLine("client: connection closed by server");
                ns.Close();
            }
        });
        TcpListener serverSocket = new TcpListener(15000);
        TcpClient clientSocket;

        serverSocket.Start();

        Console.WriteLine("server: accepting connections");
        evt.Set();
        while (true)
        {
            Console.WriteLine("server: waiting for client...");
            clientSocket = serverSocket.AcceptTcpClient();
            Console.WriteLine("server: got client");
            using (NetworkStream networkStream = clientSocket.GetStream())
            {
                var fromNetwork = deserialize(networkStream);
                Console.WriteLine("server: got player");
                Console.WriteLine("> flag: {0}", fromNetwork.flag);
                Console.WriteLine("> id: {0}", fromNetwork.id);
                Console.WriteLine("> spawned: {0}", fromNetwork.spawned);
                Console.WriteLine("> pos: {0}", fromNetwork.CharPos);
            }
        }

    }
    public static void serialize(Stream dest, Player player)
    {
        if (player == null) throw new ArgumentNullException();
        Serializer.SerializeWithLengthPrefix(dest, player, PrefixStyle.Base128);
    }

    public static Player deserialize(Stream inc)
    {
        Player obj = Serializer.DeserializeWithLengthPrefix<Player>(inc, PrefixStyle.Base128);
        return obj;
    }
}

这给出:

client: waiting for server
server: accepting connections
server: waiting for client...
client: opening connection
server: got client
client: wrote player
client: waiting for response
server: got player
> flag: 123
> id: 456
> spawned: True
> pos: (1,2,3)
client: connection closed by server
server: waiting for client...

关于c# - protobuf-net 无法反序列化我的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8901287/

相关文章:

c# - protobuf-net 隐式合约

protobuf-net - protobuf-net 中日期时间的 .proto 消息是什么

c# - C++ AMP计算与WPF渲染显卡双用性能

c# - 查找嵌套集合的最大深度/级别

c# - 如何从任务的“分配给”字段获取 SPuser 对象

c# - 为什么我不能将 [SuppressMessage(…)] 中的常量与 ReSharper 一起使用?

c# - Protocol Buffer : consecutive serialize and deserialize to/from socket

c# - 嵌套自身的类的 Protobuf-net 序列化

c# - RollingFileAppender 中的先前文件名

.net - protobuf -net 不知道如何序列化类型为 object 的数据成员