コード例 #1
0
ファイル: WebSocket.cs プロジェクト: yilin101/jellyfin
        private async Task <bool> ConcatenateFragmentsIntoAsync(Stream dest)
        {
            while (true)
            {
                var frame = await WebSocketFrame.ReadAsync(_stream, true).ConfigureAwait(false);

                if (frame.IsFinal)
                {
                    /* FINAL */

                    // CONT
                    if (frame.IsContinuation)
                    {
                        dest.WriteBytes(frame.PayloadData.ApplicationData);
                        break;
                    }

                    // PING
                    if (frame.IsPing)
                    {
                        processPingFrame(frame);
                        continue;
                    }

                    // PONG
                    if (frame.IsPong)
                    {
                        processPongFrame(frame);
                        continue;
                    }

                    // CLOSE
                    if (frame.IsClose)
                    {
                        return(await ProcessCloseFrameAsync(frame).ConfigureAwait(false));
                    }
                }
                else
                {
                    /* MORE */

                    // CONT
                    if (frame.IsContinuation)
                    {
                        dest.WriteBytes(frame.PayloadData.ApplicationData);
                        continue;
                    }
                }

                // ?
                return(await ProcessUnsupportedFrameAsync(
                           frame,
                           CloseStatusCode.IncorrectData,
                           "An incorrect data has been received while receiving fragmented data.").ConfigureAwait(false));
            }

            return(true);
        }
コード例 #2
0
ファイル: WebSocket.cs プロジェクト: yilin101/jellyfin
        private void startReceiving()
        {
            if (_messageEventQueue.Count > 0)
            {
                _messageEventQueue.Clear();
            }

            _exitReceiving = new AutoResetEvent(false);
            _receivePong   = new AutoResetEvent(false);

            Action receive = null;

            receive = async() => await WebSocketFrame.ReadAsync(
                _stream,
                true,
                async frame =>
            {
                if (await ProcessWebSocketFrameAsync(frame).ConfigureAwait(false) && _readyState != WebSocketState.Closed)
                {
                    receive();

                    if (!frame.IsData)
                    {
                        return;
                    }

                    await _forEvent.WaitAsync().ConfigureAwait(false);

                    try
                    {
                        var e = dequeueFromMessageEventQueue();
                        if (e != null && _readyState == WebSocketState.Open)
                        {
                            OnMessage.Emit(this, e);
                        }
                    }
                    catch (Exception ex)
                    {
                        await ProcessExceptionAsync(ex, "An exception has occurred while OnMessage.").ConfigureAwait(false);
                    }
                    finally
                    {
                        _forEvent.Release();
                    }
                }
                else if (_exitReceiving != null)
                {
                    _exitReceiving.Set();
                }
            },
                async ex => await ProcessExceptionAsync(ex, "An exception has occurred while receiving a message.")).ConfigureAwait(false);

            receive();
        }
コード例 #3
0
        private void startReceiving()
        {
            if (_messageEventQueue.Count > 0)
            {
                _messageEventQueue.Clear();
            }

            _exitReceiving = new AutoResetEvent(false);
            _receivePong   = new AutoResetEvent(false);

            Action receive = null;

            receive = () => WebSocketFrame.ReadAsync(
                _stream,
                true,
                frame =>
            {
                if (processWebSocketFrame(frame) && _readyState != WebSocketState.Closed)
                {
                    receive();

                    if (!frame.IsData)
                    {
                        return;
                    }

                    lock (_forEvent)
                    {
                        try
                        {
                            var e = dequeueFromMessageEventQueue();
                            if (e != null && _readyState == WebSocketState.Open)
                            {
                                OnMessage.Emit(this, e);
                            }
                        }
                        catch (Exception ex)
                        {
                            processException(ex, "An exception has occurred while OnMessage.");
                        }
                    }
                }
                else if (_exitReceiving != null)
                {
                    _exitReceiving.Set();
                }
            },
                ex => processException(ex, "An exception has occurred while receiving a message."));

            receive();
        }