Esempio n. 1
0
    public static StunMessageClass Parse(Bits bits)
    {
      if (bits.Count != 2)
      {
        throw new ArgumentException("The number of bits must be 2.", nameof(bits));
      }

      if (Request.Bits.SequenceEqual(bits.ToBitArray()))
      {
        return Request;
      }

      if (SuccessResponse.Bits.SequenceEqual(bits.ToBitArray()))
      {
        return SuccessResponse;
      }

      if (FailureResponse.Bits.SequenceEqual(bits.ToBitArray()))
      {
        return FailureResponse;
      }

      if (Indication.Bits.SequenceEqual(bits.ToBitArray()))
      {
        return Indication;
      }

      throw new InvalidOperationException("Invalid class.");
    }
Esempio n. 2
0
    public static StunMessageMethod Parse(Bits bits)
    {
      if (bits.Count != 12)
      {
        throw new ArgumentException("The number of bits must be 12.", nameof(bits));
      }

      if (Binding.Bits.SequenceEqual(bits.ToBitArray()))
      {
        return Binding;
      }

      throw new InvalidOperationException("Invalid method.");
    }
Esempio n. 3
0
    public StunMessageType(StunMessageMethod method, StunMessageClass @class)
    {
      if (!method.PermittedClasses.Contains(@class))
      {
        throw new ArgumentException($"The class {@class} is not permitted by the method {method}.", nameof(@class));
      }
      
      Name = method.Name + " " + @class.Name;

      var methodBits = new Bits(method.Bits);
      var classBits = new Bits(@class.Bits);
      var bits = new Bits(14);
      bits.AddBits(methodBits.PopBits(5));
      bits.AddBit(classBits.Pop());
      bits.AddBits(methodBits.PopBits(3));
      bits.AddBit(classBits.Pop());
      bits.AddBits(methodBits);

      Bits = bits.ToBitArray();
    }