TryDecodeHeader() public static method

public static TryDecodeHeader ( byte buffer, int &dataStart, int end, bool &isFin, OpCode &opCode, bool &isMasked, int &payloadLength, byte mask ) : bool
buffer byte
dataStart int
end int
isFin bool
opCode OpCode
isMasked bool
payloadLength int
mask byte
return bool
Example #1
0
        public void TestTryDecodeFragmentedUnmaskedText()
        {
            int start = 0;
            var mask  = new byte[4];

            var frame0 = new byte[] { 0x01, 0x03, 0x48, 0x65, 0x6c }; // "Hel"

            Assert.IsTrue(FrameDecoder.TryDecodeHeader(frame0, ref start, frame0.Length, out bool isFin, out OpCode opCode, out bool isMasked, out int payloadLength, mask));
            Assert.IsFalse(isFin);
            Assert.AreEqual(OpCode.Text, opCode);
            Assert.IsFalse(isMasked);
            Assert.AreEqual(3, payloadLength);

            var decodePayloadStart = start;

            Assert.IsTrue(FrameDecoder.TryDecodePayload(frame0, ref start, frame0.Length, payloadLength, isMasked, mask));
            Assert.AreEqual("Hel", Encoding.UTF8.GetString(frame0, decodePayloadStart, payloadLength));

            var frame1 = new byte[] { 0x80, 0x02, 0x6c, 0x6f }; // "lo"

            start = 0;
            Assert.IsTrue(FrameDecoder.TryDecodeHeader(frame1, ref start, frame1.Length, out isFin, out opCode, out isMasked, out payloadLength, mask));
            Assert.IsTrue(isFin);
            Assert.AreEqual(OpCode.Continuation, opCode);
            Assert.IsFalse(isMasked);
            Assert.AreEqual(2, payloadLength);

            decodePayloadStart = start;
            Assert.IsTrue(FrameDecoder.TryDecodePayload(frame1, ref start, frame1.Length, payloadLength, isMasked, mask));
            Assert.AreEqual("lo", Encoding.UTF8.GetString(frame1, decodePayloadStart, payloadLength));
        }
Example #2
0
        public void TestTryDecodeMaskedText()
        {
            int start = 0;
            var mask  = new byte[4];

            var frame = new byte[] { 0x81, 0x85, 0x37, 0xfa, 0x21, 0x3d, 0x7f, 0x9f, 0x4d, 0x51, 0x58 }; // "Hello"

            Assert.IsTrue(FrameDecoder.TryDecodeHeader(frame, ref start, frame.Length, out bool isFin, out OpCode opCode, out bool isMasked, out int payloadLength, mask));
            Assert.IsTrue(isFin);
            Assert.AreEqual(OpCode.Text, opCode);
            Assert.IsTrue(isMasked);
            Assert.AreEqual(5, payloadLength);

            var decodePayloadStart = start;

            Assert.IsTrue(FrameDecoder.TryDecodePayload(frame, ref start, frame.Length, payloadLength, isMasked, mask));
            Assert.AreEqual("Hello", Encoding.UTF8.GetString(frame, decodePayloadStart, payloadLength));
        }
Example #3
0
        public void TestTryDecodeUnmaskedText()
        {
            var frame = new byte[] { 0x81, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f }; // "Hello"

            int start = 0;
            var mask  = new byte[4];

            Assert.IsTrue(FrameDecoder.TryDecodeHeader(frame, ref start, frame.Length, out bool isFin, out OpCode opCode, out bool isMasked, out int payloadLength, mask));
            Assert.IsTrue(isFin);
            Assert.AreEqual(OpCode.Text, opCode);
            Assert.IsFalse(isMasked);
            Assert.AreEqual(5, payloadLength);

            var decodePayloadStart = start;

            Assert.IsTrue(FrameDecoder.TryDecodePayload(frame, ref start, frame.Length, payloadLength, isMasked, mask));
            Assert.AreEqual("Hello", Encoding.UTF8.GetString(frame, decodePayloadStart, payloadLength));
        }