如何在protobuf.net中反序列化UDPTunnel

时间:2014-06-18 18:50:08

标签: c# protobuf-net unityscript

我在Unity3d(c#scripting)中使用mumble做一个voip客户端代码,现在我能够成功连接到任何一个笨拙的公共服务器。但是当我尝试反序列化UDP隧道时,我会遇到很多例外情况,包括“无效的线型'”,“数字溢出”,“无效的字段”,“#39;' 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

1 个答案:

答案 0 :(得分:4)

我已经通过使用

解决了这个问题
var size = IPAddress.NetworkToHostOrder (_reader.ReadInt32 ());
var udpTunnel = new UDPTunnel { packet = _reader.ReadBytes(size) };

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