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."); }
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."); }
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(); }