예제 #1
0
        public WebSocketFrame(WebSocket webSocket, WebSocketFrameTypes type, byte[] data, UInt64 pos, UInt64 length, bool isFinal, bool useExtensions)
        {
            this.Type          = type;
            this.IsFinal       = isFinal;
            this.UseExtensions = useExtensions;

            if (data != null)
            {
                this.Data = new byte[length];
                Array.Copy(data, (int)pos, this.Data, 0, (int)length);
            }
            else
            {
                data = NoData;
            }

            // First byte: Final Bit + Rsv flags + OpCode
            byte finalBit = (byte)(IsFinal ? 0x80 : 0x0);

            this.Header = (byte)(finalBit | (byte)Type);

            if (this.UseExtensions && webSocket != null && webSocket.Extensions != null)
            {
                for (int i = 0; i < webSocket.Extensions.Length; ++i)
                {
                    var ext = webSocket.Extensions[i];
                    if (ext != null)
                    {
                        this.Header |= ext.GetFrameHeader(this, this.Header);
                        this.Data    = ext.Encode(this);
                    }
                }
            }
        }
        public WebSocketFrame(WebSocket webSocket, WebSocketFrameTypes type, byte[] data, UInt64 pos, UInt64 length, bool isFinal, bool useExtensions)
        {
            this.Type = type;
            this.IsFinal = isFinal;
            this.UseExtensions = useExtensions;

            if (data != null)
            {
                this.Data = new byte[length];
                Array.Copy(data, (int)pos, this.Data, 0, (int)length);
            }
            else
                data = NoData;

            // First byte: Final Bit + Rsv flags + OpCode
            byte finalBit = (byte)(IsFinal ? 0x80 : 0x0);
            this.Header = (byte)(finalBit | (byte)Type);

            if (this.UseExtensions && webSocket != null && webSocket.Extensions != null)
            {
                for (int i = 0; i < webSocket.Extensions.Length; ++i)
                {
                    var ext = webSocket.Extensions[i];
                    if (ext != null)
                    {
                        this.Header |= ext.GetFrameHeader(this, this.Header);
                        this.Data = ext.Encode(this);
                    }
                }
            }
        }
        internal void Read(Stream stream)
        {
            // For the complete documentation for this section see:
            // http://tools.ietf.org/html/rfc6455#section-5.2

            byte header = (byte)stream.ReadByte();

            // The first byte is the Final Bit and the type of the frame
            IsFinal = (header & 0x80) != 0;
            Type = (WebSocketFrameTypes)(header & 0xF);

            header = (byte)stream.ReadByte();

            // The secound byte is the Mask Bit and the length of the payload data
            HasMask = (header & 0x80) != 0;

            // if 0-125, that is the payload length.
            Length = (UInt64)(header & 127);

            // If 126, the following 2 bytes interpreted as a 16-bit unsigned integer are the payload length.
            if (Length == 126)
            {
                byte[] rawLen = new byte[2];
                stream.ReadBuffer(rawLen);

                if (BitConverter.IsLittleEndian)
                    Array.Reverse(rawLen, 0, rawLen.Length);

                Length = (UInt64)BitConverter.ToUInt16(rawLen, 0);
            }
            else if (Length == 127)
            {
                // If 127, the following 8 bytes interpreted as a 64-bit unsigned integer (the
                // most significant bit MUST be 0) are the payload length.

                byte[] rawLen = new byte[8];
                stream.ReadBuffer(rawLen);

                if (BitConverter.IsLittleEndian)
                    Array.Reverse(rawLen, 0, rawLen.Length);

                Length = (UInt64)BitConverter.ToUInt64(rawLen, 0);
            }

            // Read the Mask, if has any
            if (HasMask)
            {
                Mask = new byte[4];
                stream.Read(Mask, 0, 4);
            }

            Data = new byte[Length];
            for (UInt64 i = 0; i < Length; ++i)
            {
                Data[i] = (byte)stream.ReadByte();
                if (HasMask)
                    Data[i] = (byte)(Data[i] ^ Mask[i % 4]);
            }
        }
        /// <summary>
        /// Assembles all fragments into a final frame. Call this on the last fragment of a frame.
        /// </summary>
        /// <param name="fragments">The list of previously downloaded and parsed fragments of the frame</param>
        internal void Assemble(List<WebSocketFrameReader> fragments)
        {
            // this way the following algorithms will handle this fragment's data too
            fragments.Add(this);

            UInt64 finalLength = 0;
            for (int i = 0; i < fragments.Count; ++i)
                finalLength += fragments[i].Length;

            byte[] buffer = new byte[finalLength];
            UInt64 pos = 0;
            for (int i = 0; i < fragments.Count; ++i)
            {
                Array.Copy(fragments[i].Data, 0, buffer, (int)pos, (int)fragments[i].Length);
                pos += fragments[i].Length;
            }

            // All fragments of a message are of the same type, as set by the first fragment's opcode.
            this.Type = fragments[0].Type;
            this.Length = finalLength;
            this.Data = buffer;
        }
예제 #5
0
 public WebSocketFrame(WebSocket webSocket, WebSocketFrameTypes type, byte[] data, bool isFinal, bool useExtensions)
     : this(webSocket, type, data, 0, data != null ? (UInt64)data.Length : 0, isFinal, useExtensions)
 {
 }
예제 #6
0
 public WebSocketFrame(WebSocket webSocket, WebSocketFrameTypes type, byte[] data)
     : this(webSocket, type, data, true)
 {
 }
 public WebSocketFrame(WebSocket webSocket, WebSocketFrameTypes type, byte[] data, bool isFinal, bool useExtensions)
     : this(webSocket, type, data, 0, data != null ? (UInt64)data.Length : 0, isFinal, useExtensions)
 {
 }
 public WebSocketFrame(WebSocket webSocket, WebSocketFrameTypes type, byte[] data)
     :this(webSocket, type, data, true)
 { }