private void ReadLoop()
        {
            if (!Reading)
            {
                return;
            }

            var buffer = new byte[1024];

            _stream.ReadAsync(buffer).ContinueWith(task =>
            {
                if (task.Exception != null)
                {
                    var exception = task.Exception.GetBaseException();

                    if (!HttpBasedTransport.IsRequestAborted(exception))
                    {
                        if (!(exception is IOException))
                        {
                            _connection.OnError(exception);
                        }
                        StopReading(true);
                    }
                    return;
                }

                var read = task.Result;

                // Put chunks in the buffer
                if (read > 0)
                {
                    _buffer.Add(buffer, read);
                }

                // Stop any reading we're doing
                if (read == 0)
                {
                    StopReading(true);
                    return;
                }

                // Keep reading the next set of data
                ReadLoop();

                // If we read less than we wanted or if we filled the buffer, process it
                if (read <= buffer.Length)
                {
                    ProcessBuffer();
                }
            });
        }
        private void ProcessChunks()
        {
            Debug.WriteLine("ProcessChunks");
            while (Reading && _buffer.HasChunks)
            {
                var line = _buffer.ReadLine();

                // No new lines in the buffer so stop processing
                if (line == null)
                {
                    break;
                }

                if (!Reading)
                {
                    return;
                }

                // Try parsing the sseEvent
                SseEvent sseEvent;
                if (!SseEvent.TryParse(line, out sseEvent))
                {
                    continue;
                }

                if (!Reading)
                {
                    return;
                }

                Debug.WriteLine("SSE READ: " + sseEvent);

                switch (sseEvent.Type)
                {
                case EventType.Id:
                    _connection.MessageId = sseEvent.Data;
                    break;

                case EventType.Data:
                    if (!sseEvent.Data.Equals("initialized", StringComparison.OrdinalIgnoreCase))
                    {
                        if (Reading)
                        {
                            // We don't care about timeout messages here since it will just reconnect
                            // as part of being a long running request
                            bool timedOutReceived;
                            bool disconnectReceived;

                            HttpBasedTransport.ProcessResponse(
                                _connection,
                                sseEvent.Data,
                                out timedOutReceived,
                                out disconnectReceived);

                            if (disconnectReceived)
                            {
                                _connection.Stop();
                            }

                            if (timedOutReceived)
                            {
                                return;
                            }
                        }
                    }
                    break;
                }
            }
        }
示例#3
0
 public Task <NegotiationResponse> Negotiate(IConnection connection)
 {
     return(HttpBasedTransport.GetNegotiationResponse(_httpClient, connection));
 }