Ejemplo n.º 1
0
        public async Task <IConnectionState> Run(Action <ServerSentEvent> msgReceived, CancellationToken cancelToken)
        {
            int i = 0;

            var stream = mResponse.GetResponseStream();
            {
                byte[] buffer   = new byte[1024 * 8];
                var    taskRead = await stream.ReadAsync(buffer, 0, buffer.Length, cancelToken);


                if (!cancelToken.IsCancellationRequested)
                {
                    int bytesRead = taskRead;
                    if (bytesRead > 0) // stream has not reached the end yet
                    {
                        //Console.WriteLine("ReadCallback {0} bytesRead", bytesRead);
                        string text = Encoding.UTF8.GetString(buffer, 0, bytesRead);
                        text = mRemainingText + text;
                        string[] lines = StringSplitter.SplitIntoLines(text, out mRemainingText);
                        foreach (string line in lines)
                        {
                            if (cancelToken.IsCancellationRequested)
                            {
                                break;
                            }

                            // Dispatch message if empty lne
                            if (string.IsNullOrEmpty(line.Trim()) && mSse != null)
                            {
                                msgReceived(mSse);
                                mSse = null;
                            }
                            else if (line.StartsWith(":"))
                            {
                                // This a comment, just log it.
                            }
                            else
                            {
                                string fieldName  = String.Empty;
                                string fieldValue = String.Empty;
                                if (line.Contains(':'))
                                {
                                    int index = line.IndexOf(':');
                                    fieldName  = line.Substring(0, index);
                                    fieldValue = line.Substring(index + 1).TrimStart();
                                }
                                else
                                {
                                    fieldName = line;
                                }

                                if (String.Compare(fieldName, "event", true) == 0)
                                {
                                    mSse           = mSse ?? new ServerSentEvent();
                                    mSse.EventType = fieldValue;
                                }
                                else if (String.Compare(fieldName, "data", true) == 0)
                                {
                                    mSse      = mSse ?? new ServerSentEvent();
                                    mSse.Data = fieldValue + '\n';
                                }
                                else if (String.Compare(fieldName, "id", true) == 0)
                                {
                                    mSse             = mSse ?? new ServerSentEvent();
                                    mSse.LastEventId = fieldValue;
                                }
                                else if (String.Compare(fieldName, "retry", true) == 0)
                                {
                                    int parsedRetry;
                                    if (int.TryParse(fieldValue, out parsedRetry))
                                    {
                                        mSse       = mSse ?? new ServerSentEvent();
                                        mSse.Retry = parsedRetry;
                                    }
                                }
                                else
                                {
                                    // Ignore this, just log it
                                }
                            }
                        }

                        if (!cancelToken.IsCancellationRequested)
                        {
                            return(this);
                        }
                    }
                    else // end of the stream reached
                    {
                        _logger.Trace("No bytes read. End of stream.");
                    }
                }
                return(new DisconnectedState(mResponse.ResponseUri, mWebRequesterFactory, mHeaders));
            }
        }
        public Task <IConnectionState> Run(Action <ServerSentEvent> msgReceived, CancellationToken cancelToken)
        {
            int i = 0;

            Task <IConnectionState> t = new Task <IConnectionState>(() =>
            {
                //using (mResponse)
                {
                    //using (var stream = mResponse.GetResponseStream())
                    var stream = mResponse.GetResponseStream();
                    {
                        byte[] buffer = new byte[1024 * 8];
                        var taskRead  = stream.ReadAsync(buffer, 0, buffer.Length, cancelToken);

                        try
                        {
                            taskRead.Wait(cancelToken);
                        }
                        catch (Exception ex)
                        {
                            _logger.Trace(ex, "ConnectedState.Run");
                        }
                        if (!cancelToken.IsCancellationRequested && !taskRead.IsFaulted)
                        {
                            int bytesRead = taskRead.Result;
                            if (bytesRead > 0) // stream has not reached the end yet
                            {
                                //Console.WriteLine("ReadCallback {0} bytesRead", bytesRead);
                                string text    = Encoding.UTF8.GetString(buffer, 0, bytesRead);
                                text           = mRemainingText + text;
                                string[] lines = StringSplitter.SplitIntoLines(text, out mRemainingText);
                                foreach (string line in lines)
                                {
                                    if (cancelToken.IsCancellationRequested)
                                    {
                                        break;
                                    }

                                    // Dispatch message if empty lne
                                    if (string.IsNullOrEmpty(line.Trim()) && mSse != null)
                                    {
                                        _logger.Trace("Message received");
                                        msgReceived(mSse);
                                        mSse = null;
                                    }
                                    else if (line.StartsWith(":"))
                                    {
                                        // This a comment, just log it.
                                        _logger.Trace("A comment was received: " + line);
                                    }
                                    else
                                    {
                                        string fieldName  = String.Empty;
                                        string fieldValue = String.Empty;
                                        if (line.Contains(':'))
                                        {
                                            int index  = line.IndexOf(':');
                                            fieldName  = line.Substring(0, index);
                                            fieldValue = line.Substring(index + 1).TrimStart();
                                        }
                                        else
                                        {
                                            fieldName = line;
                                        }

                                        if (String.Compare(fieldName, "event", true) == 0)
                                        {
                                            mSse           = mSse ?? new ServerSentEvent();
                                            mSse.EventType = fieldValue;
                                        }
                                        else if (String.Compare(fieldName, "data", true) == 0)
                                        {
                                            mSse      = mSse ?? new ServerSentEvent();
                                            mSse.Data = fieldValue + '\n';
                                        }
                                        else if (String.Compare(fieldName, "id", true) == 0)
                                        {
                                            mSse             = mSse ?? new ServerSentEvent();
                                            mSse.LastEventId = fieldValue;
                                        }
                                        else if (String.Compare(fieldName, "retry", true) == 0)
                                        {
                                            int parsedRetry;
                                            if (int.TryParse(fieldValue, out parsedRetry))
                                            {
                                                mSse       = mSse ?? new ServerSentEvent();
                                                mSse.Retry = parsedRetry;
                                            }
                                        }
                                        else
                                        {
                                            // Ignore this, just log it
                                            _logger.Warn("A unknown line was received: " + line);
                                        }
                                    }
                                }

                                if (!cancelToken.IsCancellationRequested)
                                {
                                    return(this);
                                }
                            }
                            else // end of the stream reached
                            {
                                _logger.Trace("No bytes read. End of stream.");
                            }
                        }
                        else
                        {
                            if (taskRead.IsFaulted)
                            {
                                _logger.Error(taskRead.Exception.StackTrace);
                            }
                        }

                        //stream.Dispose()
                        //stream.Close();
                        //mResponse.Close();
                        //mResponse.Dispose();
                        (mRequester as WebRequester).WebRequest.Abort();
                        return(new DisconnectedState(mResponse.ResponseUri, mWebRequesterFactory));
                    }
                }
            });

            t.Start();
            return(t);
        }