Ejemplo n.º 1
0
        internal WebSocketFrame(Fin fin, Opcode opcode, PayloadData payloadData, bool compressed, bool mask)
        {
            _fin    = fin;
            _opcode = opcode;

            _rsv1 = opcode.IsData() && compressed ? Rsv.On : Rsv.Off;
            _rsv2 = Rsv.Off;
            _rsv3 = Rsv.Off;

            var len = payloadData.Length;

            if (len < 126)
            {
                _payloadLength    = (byte)len;
                _extPayloadLength = WebSocket.EmptyBytes;
            }
            else if (len < 0x010000)
            {
                _payloadLength    = (byte)126;
                _extPayloadLength = ((ushort)len).InternalToByteArray(ByteOrder.Big);
            }
            else
            {
                _payloadLength    = (byte)127;
                _extPayloadLength = len.InternalToByteArray(ByteOrder.Big);
            }

            if (mask)
            {
                _mask       = Mask.On;
                _maskingKey = CreateMaskingKey();
                payloadData.Mask(_maskingKey);
            }
            else
            {
                _mask       = Mask.Off;
                _maskingKey = WebSocket.EmptyBytes;
            }

            _payloadData = payloadData;
        }
Ejemplo n.º 2
0
        private void close(ushort code, string reason)
        {
            var data = new List <byte>(code.ToBytes(ByteOrder.BIG));

            if (!reason.IsNullOrEmpty())
            {
                var buffer = Encoding.UTF8.GetBytes(reason);
                data.AddRange(buffer);
            }

            var payloadData = new PayloadData(data.ToArray());

            if (payloadData.Length > 125)
            {
                var msg = "A Close frame must have a payload length of 125 bytes or less.";
                onError(msg);
                return;
            }

            close(payloadData);
        }
Ejemplo n.º 3
0
        internal WebSocketFrame(
            Fin fin, Opcode opcode, Mask mask, PayloadData payload, bool compressed)
        {
            _fin    = fin;
            _rsv1   = isData(opcode) && compressed ? Rsv.On : Rsv.Off;
            _rsv2   = Rsv.Off;
            _rsv3   = Rsv.Off;
            _opcode = opcode;
            _mask   = mask;

            var len = payload.Length;

            if (len < 126)
            {
                _payloadLength    = (byte)len;
                _extPayloadLength = new byte[0];
            }
            else if (len < 0x010000)
            {
                _payloadLength    = (byte)126;
                _extPayloadLength = ((ushort)len).InternalToByteArray(ByteOrder.Big);
            }
            else
            {
                _payloadLength    = (byte)127;
                _extPayloadLength = len.InternalToByteArray(ByteOrder.Big);
            }

            if (mask == Mask.Mask)
            {
                _maskingKey = createMaskingKey();
                payload.Mask(_maskingKey);
            }
            else
            {
                _maskingKey = new byte[0];
            }

            _payloadData = payload;
        }
Ejemplo n.º 4
0
        private void close(PayloadData data)
        {
      #if DEBUG
            Console.WriteLine("WS: Info@close: Current thread IsBackground ?: {0}", Thread.CurrentThread.IsBackground);
      #endif
            lock (_forClose)
            {
                // Whether the closing handshake has been started already ?
                if (_readyState == WsState.CLOSING ||
                    _readyState == WsState.CLOSED)
                {
                    return;
                }

                // Whether the closing handshake as server is started before the connection has been established ?
                if (_readyState == WsState.CONNECTING && !_client)
                {
                    sendResponseHandshake(HttpStatusCode.BadRequest);
                    onClose(new CloseEventArgs(data));

                    return;
                }

                _readyState = WsState.CLOSING;
            }

            // Whether a close status code that must not be set for send is used ?
            if (!canSendAsCloseFrame(data))
            {
                onClose(new CloseEventArgs(data));
                return;
            }

            closeHandshake(data);
      #if DEBUG
            Console.WriteLine("WS: Info@close: Exits close method.");
      #endif
        }
Ejemplo n.º 5
0
        public WsFrame(Fin fin, Opcode opcode, Mask mask, PayloadData payload, bool compressed)
        {
            this.Fin    = fin;
            this.Rsv1   = ((!WsFrame.isData(opcode) || !compressed) ? Rsv.Off : Rsv.On);
            this.Rsv2   = Rsv.Off;
            this.Rsv3   = Rsv.Off;
            this.Opcode = opcode;
            this.Mask   = mask;
            ulong length = payload.Length;
            byte  b      = (length >= 126UL) ? ((length >= 65536UL) ? 127 : 126) : ((byte)length);

            this.PayloadLen    = b;
            this.ExtPayloadLen = ((b >= 126) ? ((b != 126) ? length.ToByteArrayInternally(ByteOrder.Big) : ((ushort)length).ToByteArrayInternally(ByteOrder.Big)) : new byte[0]);
            bool flag = mask == Mask.Mask;

            byte[] maskingKey = (!flag) ? new byte[0] : WsFrame.createMaskingKey();
            this.MaskingKey = maskingKey;
            if (flag)
            {
                payload.Mask(maskingKey);
            }
            this.PayloadData = payload;
        }
Ejemplo n.º 6
0
        internal WebSocketFrame(Fin fin, Opcode opcode, PayloadData payloadData, bool compressed, bool mask)
        {
            _fin    = fin;
            _rsv1   = ((isData(opcode) && compressed) ? Rsv.On : Rsv.Off);
            _rsv2   = Rsv.Off;
            _rsv3   = Rsv.Off;
            _opcode = opcode;
            ulong length = payloadData.Length;

            if (length < 126)
            {
                _payloadLength    = (byte)length;
                _extPayloadLength = new byte[0];
            }
            else if (length < 65536)
            {
                _payloadLength    = 126;
                _extPayloadLength = ((ushort)length).InternalToByteArray(ByteOrder.Big);
            }
            else
            {
                _payloadLength    = 127;
                _extPayloadLength = length.InternalToByteArray(ByteOrder.Big);
            }
            if (mask)
            {
                _mask       = Mask.Mask;
                _maskingKey = createMaskingKey();
                payloadData.Mask(_maskingKey);
            }
            else
            {
                _mask       = Mask.Unmask;
                _maskingKey = new byte[0];
            }
            _payloadData = payloadData;
        }
Ejemplo n.º 7
0
        private void pong(PayloadData data)
        {
            var frame = createFrame(Fin.FINAL, Opcode.PONG, data);

            send(frame);
        }
Ejemplo n.º 8
0
 internal CloseEventArgs(PayloadData payloadData, bool clean)
 {
     _payloadData = payloadData;
     _clean       = clean;
 }
Ejemplo n.º 9
0
 private WsFrame createFrame(Fin fin, Opcode opcode, PayloadData payloadData)
 {
     return(_client
      ? new WsFrame(fin, opcode, payloadData)
      : new WsFrame(fin, opcode, Mask.UNMASK, payloadData));
 }
Ejemplo n.º 10
0
 internal CloseEventArgs(PayloadData payloadData)
 {
     _payloadData = payloadData;
 }
Ejemplo n.º 11
0
 internal CloseEventArgs(ushort code, string reason, bool clean)
 {
     _payloadData = new PayloadData(code, reason);
     _clean       = clean;
 }
Ejemplo n.º 12
0
        public void Print()
        {
            byte[] buffer;
            long   count, i, j;
            int    countDigit, remainder;
            string countFmt, extPayloadLen, headerFmt, topLineFmt, bottomLineFmt, payloadData, spFmt;

            switch (ExtPayloadLen.Length)
            {
            case 2:
                extPayloadLen = ExtPayloadLen.To <ushort>(ByteOrder.BIG).ToString();
                break;

            case 8:
                extPayloadLen = ExtPayloadLen.To <ulong>(ByteOrder.BIG).ToString();
                break;

            default:
                extPayloadLen = String.Empty;
                break;
            }

            if (((Opcode.TEXT | Opcode.PING | Opcode.PONG) & Opcode) == Opcode &&
                Masked == Mask.UNMASK &&
                PayloadLength > 0)
            {
                payloadData = Encoding.UTF8.GetString(PayloadData.ToBytes());
            }
            else
            {
                payloadData = BitConverter.ToString(PayloadData.ToBytes());
            }

            headerFmt = @"
 WsFrame:

 FIN={0}, RSV1={1}, RSV2={2}, RSV3={3}, Opcode={4},
 MASK={5}, Payload Len={6}, Extended Payload Len={7},
 Masking Key ={8},
 Payload Data={9}";

            buffer    = ToBytes();
            count     = (long)(Length / 4);
            remainder = (int)(Length % 4);

            if (count < 10000)
            {
                countDigit = 4;
                countFmt   = "{0,4}";
            }
            else if (count < 0x010000)
            {
                countDigit = 4;
                countFmt   = "{0,4:X}";
            }
            else if (count < 0x0100000000)
            {
                countDigit = 8;
                countFmt   = "{0,8:X}";
            }
            else
            {
                countDigit = 16;
                countFmt   = "{0,16:X}";
            }

            spFmt = String.Format("{{0,{0}}}", countDigit);

            topLineFmt = String.Format(@"
 {0} 01234567 89ABCDEF 01234567 89ABCDEF
 {0}+--------+--------+--------+--------+", spFmt);

            Func <string, Action <string, string, string, string> > func = s =>
            {
                long   lineCount = 0;
                string lineFmt   = String.Format(" {0}|{{1,8}} {{2,8}} {{3,8}} {{4,8}}|", s);
                return((arg1, arg2, arg3, arg4) =>
                {
                    Console.WriteLine(lineFmt, ++lineCount, arg1, arg2, arg3, arg4);
                });
            };
            var printLine = func(countFmt);

            bottomLineFmt = String.Format(" {0}+--------+--------+--------+--------+", spFmt);

            Console.WriteLine(headerFmt,
                              Fin, Rsv1, Rsv2, Rsv3, Opcode,
                              Masked, PayloadLen, extPayloadLen,
                              BitConverter.ToString(MaskingKey),
                              payloadData);

            Console.WriteLine(topLineFmt, String.Empty);

            for (i = 0; i <= count; i++)
            {
                j = i * 4;
                if (i < count)
                {
                    printLine(
                        Convert.ToString(buffer[j], 2).PadLeft(8, '0'),
                        Convert.ToString(buffer[j + 1], 2).PadLeft(8, '0'),
                        Convert.ToString(buffer[j + 2], 2).PadLeft(8, '0'),
                        Convert.ToString(buffer[j + 3], 2).PadLeft(8, '0'));
                }
                else if (i == count && remainder > 0)
                {
                    printLine(
                        Convert.ToString(buffer[j], 2).PadLeft(8, '0'),
                        remainder >= 2 ? Convert.ToString(buffer[j + 1], 2).PadLeft(8, '0') : String.Empty,
                        remainder == 3 ? Convert.ToString(buffer[j + 2], 2).PadLeft(8, '0') : String.Empty,
                        String.Empty);
                }
            }

            Console.WriteLine(bottomLineFmt, String.Empty);
        }
Ejemplo n.º 13
0
 public WsFrame(Fin fin, Opcode opcode, PayloadData payloadData)
     : this(fin, opcode, Mask.MASK, payloadData)
 {
 }
Ejemplo n.º 14
0
 internal CloseEventArgs()
 {
     _payloadData = PayloadData.Empty;
 }
Ejemplo n.º 15
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="payloadData"></param>
 /// <param name="mask"></param>
 /// <returns></returns>
 internal static WebSocketFrame CreatePongFrame(PayloadData payloadData, bool mask)
 {
     return(new WebSocketFrame(Fin.Final, Opcode.Pong, payloadData, false, mask));
 }
Ejemplo n.º 16
0
 public WsFrame(Opcode opcode, Mask mask, PayloadData payload) : this(Fin.Final, opcode, mask, payload)
 {
 }
Ejemplo n.º 17
0
 public WsFrame(Fin fin, Opcode opcode, Mask mask, PayloadData payload) : this(fin, opcode, mask, payload, false)
 {
 }
Ejemplo n.º 18
0
 public WsFrame(Opcode opcode, PayloadData payload) : this(opcode, Mask.Mask, payload)
 {
 }
Ejemplo n.º 19
0
 static PayloadData()
 {
     Empty     = new PayloadData();
     MaxLength = Int64.MaxValue;
 }
Ejemplo n.º 20
0
 internal CloseEventArgs(ushort code, string reason)
 {
     _payloadData = new PayloadData(code, reason);
 }
Ejemplo n.º 21
0
        private void pong(string data)
        {
            var payloadData = new PayloadData(data);

            pong(payloadData);
        }
Ejemplo n.º 22
0
 public MessageEventArgs(Opcode type, PayloadData data)
 {
     _type = type;
     _data = data;
 }
Ejemplo n.º 23
0
 static PayloadData()
 {
     PayloadData.Empty     = new PayloadData();
     PayloadData.MaxLength = 9223372036854775807L;
 }
Ejemplo n.º 24
0
 internal static WsFrame CreatePongFrame(Mask mask, PayloadData payload)
 {
     return(new WsFrame(Opcode.Pong, mask, payload));
 }
Ejemplo n.º 25
0
 public WsFrame(Opcode opcode, PayloadData payloadData)
     : this(Fin.FINAL, opcode, payloadData)
 {
 }
Ejemplo n.º 26
0
        /// <summary>
        /// Closes the WebSocket connection and releases all associated resources.
        /// </summary>
        public void Close()
        {
            var data = new PayloadData(new byte[] {});

            close(data);
        }
Ejemplo n.º 27
0
        private static WsFrame parse(byte[] header, Stream stream, bool unmask)
        {
            Fin    fin    = ((header[0] & 128) != 128) ? Fin.More : Fin.Final;
            Rsv    rsv    = ((header[0] & 64) != 64) ? Rsv.Off : Rsv.On;
            Rsv    rsv2   = ((header[0] & 32) != 32) ? Rsv.Off : Rsv.On;
            Rsv    rsv3   = ((header[0] & 16) != 16) ? Rsv.Off : Rsv.On;
            Opcode opcode = (Opcode)(header[0] & 15);
            Mask   mask   = ((header[1] & 128) != 128) ? Mask.Unmask : Mask.Mask;
            byte   b      = header[1] & 127;
            string text   = (!WsFrame.isControl(opcode) || fin != Fin.More) ? ((WsFrame.isData(opcode) || rsv != Rsv.On) ? null : "A non data frame is compressed.") : "A control frame is fragmented.";

            if (text != null)
            {
                throw new WebSocketException(CloseStatusCode.IncorrectData, text);
            }
            if (WsFrame.isControl(opcode) && b > 125)
            {
                throw new WebSocketException(CloseStatusCode.InconsistentData, "The payload data length of a control frame is greater than 125 bytes.");
            }
            WsFrame wsFrame = new WsFrame
            {
                Fin        = fin,
                Rsv1       = rsv,
                Rsv2       = rsv2,
                Rsv3       = rsv3,
                Opcode     = opcode,
                Mask       = mask,
                PayloadLen = b
            };
            int num = (b >= 126) ? ((b != 126) ? 8 : 2) : 0;

            byte[] array = (num <= 0) ? new byte[0] : stream.ReadBytes(num);
            if (num > 0 && array.Length != num)
            {
                throw new WebSocketException("The 'Extended Payload Length' of a frame cannot be read from the data source.");
            }
            wsFrame.ExtPayloadLen = array;
            bool flag = mask == Mask.Mask;

            byte[] array2 = (!flag) ? new byte[0] : stream.ReadBytes(4);
            if (flag && array2.Length != 4)
            {
                throw new WebSocketException("The 'Masking Key' of a frame cannot be read from the data source.");
            }
            wsFrame.MaskingKey = array2;
            ulong num2 = (b >= 126) ? ((b != 126) ? array.ToUInt64(ByteOrder.Big) : ((ulong)array.ToUInt16(ByteOrder.Big))) : ((ulong)b);

            byte[] array3;
            if (num2 > 0UL)
            {
                if (b > 126 && num2 > 9223372036854775807UL)
                {
                    throw new WebSocketException(CloseStatusCode.TooBig, "The 'Payload Data' length is greater than the allowable length.");
                }
                array3 = ((b <= 126) ? stream.ReadBytes((int)num2) : stream.ReadBytes((long)num2, 1024));
                if (array3.LongLength != (long)num2)
                {
                    throw new WebSocketException("The 'Payload Data' of a frame cannot be read from the data source.");
                }
            }
            else
            {
                array3 = new byte[0];
            }
            PayloadData payloadData = new PayloadData(array3, flag);

            if (flag && unmask)
            {
                payloadData.Mask(array2);
                wsFrame.Mask       = Mask.Unmask;
                wsFrame.MaskingKey = new byte[0];
            }
            wsFrame.PayloadData = payloadData;
            return(wsFrame);
        }
Ejemplo n.º 28
0
 public CloseEventArgs()
 {
     _payloadData = PayloadData.Empty;
 }
Ejemplo n.º 29
0
 internal WebSocketFrame(Opcode opcode, PayloadData payloadData, bool mask)
     : this(Fin.Final, opcode, payloadData, false, mask)
 {
 }
Ejemplo n.º 30
0
 internal static WsFrame CreateCloseFrame(Mask mask, PayloadData payload)
 {
     return(new WsFrame(Opcode.Close, mask, payload));
 }