Exemplo n.º 1
0
 /// <summary>
 /// Parse 完成一个 处理一个
 /// </summary>
 private void ParseReciveWapper()
 {
     lock (_reciveWapperQueue)
     {
         while (_reciveWapperQueue.Count > 0)
         {
             WebSocketReciveDataParse.ReciveData reciveData = _parser.GetReciveData();
             ReciveWapper reciveWapper = _reciveWapperQueue.Dequeue();
             if (!(reciveWapper.CancellationToken != CancellationToken.None) || !reciveWapper.CancellationToken.IsCancellationRequested)
             {
                 if (reciveData == null && _IsEof)
                 {
                     //读完了但没有数据
                     reciveWapper.TaskCompletionSource.SetException(new SocketException(10061));
                 }
                 else if (reciveData == null && !_IsEof)
                 {
                     //没有数据 但还没有读完 正在处理中
                     if (_reciveWapperQueue.Count < 1)
                     {
                         _reciveWapperQueue.Enqueue(reciveWapper);//放回去
                         break;
                     }
                     ReciveWapper[] array = _reciveWapperQueue.ToArray();
                     _reciveWapperQueue.Clear();
                     _reciveWapperQueue.Enqueue(reciveWapper);// 让它先出
                     for (int i = 0; i < array.Length; i++)
                     {
                         ReciveWapper item = array[i];
                         _reciveWapperQueue.Enqueue(item);
                     }
                     break;
                 }
                 else if (reciveData != null && reciveWapper.Buffer.Count < reciveData.BytesLength)
                 {
                     reciveWapper.TaskCompletionSource.SetException(new Exception("buffer buffer too small."));
                 }
                 else
                 {
                     if (reciveData != null)
                     {
                         Buffer.BlockCopy(reciveData.Data, 0, reciveWapper.Buffer.Array, reciveWapper.Buffer.Offset, reciveData.BytesLength);
                         reciveWapper.TaskCompletionSource.SetResult(new Tuple <int, bool, int>(reciveData.Type, reciveData.IsEof, reciveData.BytesLength));
                     }
                 }
             }
         }
     }
 }
Exemplo n.º 2
0
        private Task <Tuple <int, bool, int> > ReceiveAsync(ArraySegment <byte> buffer, CancellationToken cancellationToken)
        {
            TaskCompletionSource <Tuple <int, bool, int> > taskCompletionSource = new TaskCompletionSource <Tuple <int, bool, int> >();

            if (_IsEof)
            {
                taskCompletionSource.SetException(new Exception());
                return(taskCompletionSource.Task);
            }
            ReciveWapper item = new ReciveWapper
            {
                Buffer               = buffer,
                CancellationToken    = cancellationToken,
                TaskCompletionSource = taskCompletionSource
            };

            lock (_reciveWapperQueue)
            {
                _reciveWapperQueue.Enqueue(item);
            }
            ParseReciveWapper();//返回给Adapte 类型 长度 和 byte数组
            return(taskCompletionSource.Task);
        }