示例#1
0
        private void ReadLoop()
        {
            if (!Reading)
            {
                return;
            }

            var _buffer = new byte[1024];
            var _signal = new EventSignal <CallbackDetail <int> >();

            _signal.Finished += (sender, e) =>
            {
                if (e.Result.IsFaulted)
                {
                    Exception exception = e.Result.Exception.GetBaseException();

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

                int _read = e.Result.Result;

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

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

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

                if (_read <= _buffer.Length)
                {
                    // If we read less than we wanted or if we filled the buffer, process it
                    ProcessBuffer();
                }
            };
            StreamExtensions.ReadAsync(_signal, m_stream, _buffer);
        }
示例#2
0
        internal static EventSignal <NegotiationResponse> GetNegotiationResponse(
            IHttpClient httpClient,
            IConnection connection)
        {
            string _negotiateUrl    = connection.Url + "negotiate";
            var    _negotiateSignal = new EventSignal <NegotiationResponse>();
            var    _signal          = httpClient.GetAsync(_negotiateUrl, connection.PrepareRequest);

            _signal.Finished += (sender, e) =>
            {
                string _raw = e.Result.ReadAsString();
                if (_raw == null)
                {
                    throw new InvalidOperationException("Server negotiation failed.");
                }

                _negotiateSignal.OnFinish(JsonConvert.DeserializeObject <NegotiationResponse>(_raw));
            };
            return(_negotiateSignal);
        }