Пример #1
0
 public bool TryParse(byte[] buffer)
 {
     try
     {
         int offset = 0;
         MessageType = (MessageType)NetBinaryUtil.ReadShort(buffer, ref offset);
         int messageLength = (ushort)NetBinaryUtil.ReadShort(buffer, ref offset);
         if (MagicCookie != NetBinaryUtil.ReadUInt(buffer, ref offset))
         {
             return(false);
         }
         TransactionId = NetBinaryUtil.ReadBytes(buffer, TransactionIdSize, ref offset);
         Attributes.Clear();
         while (offset < messageLength + 20)
         {
             var attr = GetAttribute(buffer, ref offset);
             if (attr != null)
             {
                 Attributes.Add(attr);
             }
         }
         return(true);
     }
     catch
     {
         return(false);
     }
 }
Пример #2
0
        protected override void ReadBody(byte[] buf, int offset, int size)
        {
            offset++;
            var family = buf[offset++];
            var port   = (int)(NetBinaryUtil.ReadUShort(buf, ref offset) ^ (ushort)(StunMessage.MagicCookie >> 16));

            byte[] address = NetBinaryUtil.ReadBytes(buf, family == 1 ? 4 : 16, ref offset);
            for (int i = 0; i < address.Length; i++)
            {
                address[i] = (byte)(address[i] ^ buf[i + 4]);
            }
            EndPoint = new IPEndPoint(new IPAddress(address), port);
        }