2014-06-18 41 views
3

我在Unity3d(c#腳本)中嘟vo着一個voip客戶端代碼,現在我能夠成功連接到任何一個拙劣的公共服務器。但是,當我嘗試反序列化一個UDP隧道時,我得到了很多例外,包括'無效wiretype','數字溢出','無效字段','endofstream','錯誤的組被終止'和bla bla ...所有在這個特定的路線。如何反序列化protobuf.net中的UDPTunnel

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

其中_ssl是SslStream

這是我用

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

完整的方法

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在

+0

你回來了你發送的相同字節嗎?直到你知道你獲得了正確的數據爲止:其他的事情都沒有任何意義。 –

回答

3

我有過去,這現在我不知道爲什麼Deserializewithlengthprefix不工作,因爲據我所知,這些行是做同樣的事情。