Пример #1
0
        private async Task ProcessSendQueue()
        {
            string item = "";

            lock ((_sendQueue as ICollection).SyncRoot)
            {
                if (_sendQueue.Count == 0)
                {
                    return;
                }
                item = _sendQueue[0];
                _sendQueue.RemoveAt(0);
            }

            var bytes  = new List <byte>();
            var data   = Encoding.UTF8.GetBytes(item);
            var header = WebSocketHeader.Create(true, OpCode.Text, data.Length);

            bytes.AddRange(header.ToBinary());
            bytes.AddRange(data);
            var arr = bytes.ToArray();

            try
            {
                await _stream.WriteAsync(arr, 0, arr.Length).ConfigureAwait(false);
            }
            catch (IOException e) when(e.InnerException is SocketException)
            {
                Debug.WriteLine(e.Message);
            }
        }
Пример #2
0
        public static WebSocketHeader Create(bool fin, OpCode opCode, int payloadLength)
        {
            var header = new WebSocketHeader();

            header.Fin           = fin;
            header.Rcv1          = 0;
            header.Rcv2          = 0;
            header.Rcv3          = 0;
            header.OpCode        = opCode;
            header.Mask          = false;
            header.PayloadLength = payloadLength;
            return(header);
        }
Пример #3
0
 private void OnTextFrameReceived(ReadOnlySpan <byte> span, WebSocketHeader header)
 {
     if (header.PayloadLength <= 125)
     {
         if (header.Mask)
         {
             string text = DecodeText(span);
             TextDataReceived?.Invoke(text);
         }
     }
     else if (header.PayloadLength == 126)
     {
         // TODO:
     }
     else if (header.PayloadLength == 127)
     {
         // TODO:
     }
 }
Пример #4
0
        public static WebSocketHeader Parse(ReadOnlySpan <byte> span)
        {
            var header = new WebSocketHeader();

            if (span.Length < 2)
            {
                return(header);
            }

            byte[] bytes = span.Slice(0, 2).ToArray();

            header.Fin           = (bytes[0] >> 7) == 1;
            header.Rcv1          = (byte)(bytes[0] >> 6 & 1);
            header.Rcv2          = (byte)(bytes[0] >> 5 & 1);
            header.Rcv3          = (byte)(bytes[0] >> 4 & 1);
            header.OpCode        = (OpCode)(bytes[0] & 0x0f);
            header.Mask          = (bytes[1] >> 7) == 1;
            header.PayloadLength = bytes[1] & 0x7f;
            return(header);
        }
Пример #5
0
        private void OnDataFrameReceived(ReadOnlySpan <byte> span)
        {
            // Console.WriteLine("--------------------------------------------------");
            // Console.WriteLine(string.Join(" ", span.ToArray().Select(x => x.ToString("X2"))));

            var header = WebSocketHeader.Parse(span);

            Console.WriteLine(header);

            switch (header.OpCode)
            {
            case OpCode.Text:
                OnTextFrameReceived(span, header);
                break;

            case OpCode.Close:
                OnCloseReceived();
                break;
            }
        }