我正在 Unity3d(c# 脚本)中使用 mumble 编写 voip 客户端代码,现在我能够成功连接到任何 mumble 公共(public)服务器。但是当我尝试反序列化一个 UDP 隧道时,我得到了很多异常,包括“无效的线路类型”、“数字溢出”、“无效字段”、“endofstream”、“错误的组已结束”和 bla bla ......所有的在这个特定的行。

var udpTunnel = Serializer.DeserializeWithLengthPrefix<UDPTunnel> (_ssl, PrefixStyle.Fixed32BigEndian);

其中 _ssl 是 SslStream

这是我的完整方法
nternal void ProcessTcpData ()
{
        try {

                var masg = IPAddress.NetworkToHostOrder (_reader.ReadInt16 ());
                MessageType messageType = (MessageType)masg;
                Debug.Log ("Received message type: " + messageType);

                switch (messageType) {
                case MessageType.Version:
                        _mc.RemoteVersion = Serializer.DeserializeWithLengthPrefix<Version> (_ssl,
PrefixStyle.Fixed32BigEndian);
                        break;
                case MessageType.CryptSetup:
                        var cryptSetup = Serializer.DeserializeWithLengthPrefix<CryptSetup> (_ssl,
PrefixStyle.Fixed32BigEndian);
                        ProcessCryptSetup (cryptSetup);
                        break;
                case MessageType.CodecVersion:
                        _mc.CodecVersion = Serializer.DeserializeWithLengthPrefix<CodecVersion> (_ssl,
PrefixStyle.Fixed32BigEndian);
                        break;
                case MessageType.ChannelState:
                        _mc.ChannelState = Serializer.DeserializeWithLengthPrefix<ChannelState> (_ssl,
PrefixStyle.Fixed32BigEndian);
                        break;
                case MessageType.PermissionQuery:
                        _mc.PermissionQuery = Serializer.DeserializeWithLengthPrefix<PermissionQuery> (_ssl,
PrefixStyle.Fixed32BigEndian);
                        break;
                case MessageType.UserState:
                        _mc.UserState = Serializer.DeserializeWithLengthPrefix<UserState> (_ssl,
PrefixStyle.Fixed32BigEndian);
                        break;
                case MessageType.ServerSync:
                        _mc.ServerSync = Serializer.DeserializeWithLengthPrefix<ServerSync> (_ssl,
PrefixStyle.Fixed32BigEndian);
                        _mc.ConnectionSetupFinished = true;
                        break;
                case MessageType.ServerConfig:
                        _mc.ServerConfig = Serializer.DeserializeWithLengthPrefix<ServerConfig> (_ssl,
PrefixStyle.Fixed32BigEndian);
                        _validConnection = true; // handshake complete
                        break;
                case MessageType.TextMessage:
                        var textMessage = Serializer.DeserializeWithLengthPrefix<TextMessage> (_ssl, PrefixStyle.Fixed32BigEndian);
                        break;
                case MessageType.UDPTunnel:
                        if (_validConnection) {
                                var udpTunnel = Serializer.DeserializeWithLengthPrefix<UDPTunnel> (_ssl, PrefixStyle.Fixed32BigEndian);
                        }
                        break;
                case MessageType.Ping:
                        var ping = Serializer.DeserializeWithLengthPrefix<MumbleProto.Ping> (_ssl, PrefixStyle.Fixed32BigEndian);
                        Debug.Log ("Received ping: " + ping.timestamp + ", udp: " + ping.udp_packets + ", tcp:" +
                                ping.tcp_packets);
                        break;
                case MessageType.Reject:
                        var reject = Serializer.DeserializeWithLengthPrefix<Reject> (_ssl,
PrefixStyle.Fixed32BigEndian);
                        _validConnection = false;
                        _errorCallback ("Mumble server reject: " + reject.reason, true);
                        break;
                default:
                        _errorCallback ("Message type " + messageType + " not implemented", true);
                        break;
                }
                if (_validConnection) {
                        Debug.Log ("Handshake Complete:\tconnection is valid");
                }
        } catch (Exception ex) {
                Debug.LogException (ex);
        }
}

_reader 是一个 BinaryReader

最佳答案

我已经通过使用

var size = IPAddress.NetworkToHostOrder (_reader.ReadInt32 ());
var udpTunnel = new UDPTunnel { packet = _reader.ReadBytes(size) };

现在我不知道为什么 Deserializewithlengthprefix 不起作用,因为据我所知,这些行正在做同样的事情。

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

10-13 06:26