Exemplo n.º 1
0
 public WebSocketFrame(EFrameType frameType, bool maskFlag, byte[] message, bool isFinal)
 {
     FrameType = frameType;
     MaskFlag  = maskFlag;
     Message   = message;
     IsFinal   = isFinal;
 }
Exemplo n.º 2
0
        public static WebSocketFrame DecodeFrame(Stream inputStream)
        {
            using (EneterTrace.Entering())
            {
                try
                {
                    // Note: Do not enclose the BinaryReader with using because it will close the stream!!!
                    BinaryReader aReader = new BinaryReader(inputStream);

                    // Read the first 2 bytes.
                    byte[] aFirst2Bytes = aReader.ReadBytes(2);
                    if (aFirst2Bytes.Length < 2)
                    {
                        throw new EndOfStreamException("End of stream during reading first two bytes of web socket frame.");
                    }

                    // Get if final.
                    bool anIsFinal = (aFirst2Bytes[0] & 0x80) != 0;

                    // Get opcode.
                    EFrameType aFrameType = (EFrameType)(aFirst2Bytes[0] & 0xF);

                    // Get if masked.
                    bool anIsMasked = (aFirst2Bytes[1] & 0x80) != 0;

                    // Get the message length.
                    int aMessageLength = aFirst2Bytes[1] & 0x7F;
                    if (aMessageLength == 126)
                    {
                        // The length is encoded in next 2 bytes (16 bits).
                        ushort aLength = aReader.ReadUInt16();

                        // Websockets are in big endians, so convert it to little endian.
                        aMessageLength = ConvertEndian(aLength);
                    }
                    else
                    if (aMessageLength == 127)
                    {
                        // The length is encoded in next 8 bytes (64 bits).
                        ulong aLength = aReader.ReadUInt64();

                        // Websockets are in big endians, so convert it to little endian.
                        aLength = ConvertEndian(aLength);

                        aMessageLength = (int)aLength;
                    }

                    // Get mask bytes.
                    byte[] aMaskBytes = null;
                    if (anIsMasked)
                    {
                        aMaskBytes = aReader.ReadBytes(4);

                        if (aMaskBytes.Length < 4)
                        {
                            throw new EndOfStreamException("End of stream during reading web socket mask bytes.");
                        }
                    }

                    // Get the message data.
                    byte[] aMessageData = aReader.ReadBytes(aMessageLength);
                    if (aMessageData.Length < aMessageLength)
                    {
                        throw new EndOfStreamException("End of stream during reading message data from the web socket frame.");
                    }

                    // If mask was used then unmask data.
                    if (anIsMasked)
                    {
                        for (int i = 0; i < aMessageData.Length; ++i)
                        {
                            aMessageData[i] = (byte)(aMessageData[i] ^ aMaskBytes[i % 4]);
                        }
                    }

                    WebSocketFrame aFrame = new WebSocketFrame(aFrameType, anIsMasked, aMessageData, anIsFinal);

                    return(aFrame);
                }
                catch (EndOfStreamException)
                {
                    // End of the stream.
                    return(null);
                }
                catch (ObjectDisposedException)
                {
                    // End of the stream.
                    return(null);
                }
            }
        }