StartReading() public method

public StartReading ( ) : void
return void
        private void OpenConnection(IConnection connection,
            string data,
            Action initializeCallback,
            Action<Exception> errorCallback)
        {
            // If we're reconnecting add /connect to the url
            bool _reconnecting = initializeCallback == null;
            string _url = (_reconnecting ? connection.Url : connection.Url + "connect");
            Action<IRequest> _prepareRequest = PrepareRequest(connection);
            EventSignal<IResponse> _signal;

            if (shouldUsePost(connection))
            {
                _url += GetReceiveQueryString(connection, data);
                //Debug.WriteLine("ServerSentEventsTransport: POST {0}", _url);

                _signal = m_httpClient.PostAsync(
                    _url,
                    request =>
                    {
                        _prepareRequest(request);
                        request.Accept = "text/event-stream";
                    },
                    new Dictionary<string, string> {
                    {
                        "groups", GetSerializedGroups(connection) }
                    });
            }
            else
            {
                _url += GetReceiveQueryStringWithGroups(connection, data);
                //Debug.WriteLine("ServerSentEventsTransport: GET {0}", _url);

                _signal = m_httpClient.GetAsync(
                    _url,
                    request =>
                    {
                        _prepareRequest(request);
                        request.Accept = "text/event-stream";
                    });
            }

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

                    if (!HttpBasedTransport.IsRequestAborted(_exception))
                    {
                        if (errorCallback != null &&
                            Interlocked.Exchange(ref m_initializedCalled, 1) == 0)
                            errorCallback(_exception);
                        else if (_reconnecting)
                            // Only raise the error event if we failed to reconnect
                            connection.OnError(_exception);
                    }

                    if (_reconnecting)
                    {
                        // Retry
                        Reconnect(connection, data);
                        return;
                    }
                }
                else
                {
                    // Get the reseponse stream and read it for messages
                    IResponse _response = e.Result;
                    Stream _stream = _response.GetResponseStream();
                    AsyncStreamReader _reader = new AsyncStreamReader(
                        _stream,
                        connection,
                        () =>
                        {
                            if (Interlocked.CompareExchange(ref m_initializedCalled, 1, 0) == 0)
                                initializeCallback();
                        },
                        () =>
                        {
                            _response.Close();
                            Reconnect(connection, data);
                        });

                    if (_reconnecting)
                        // Raise the reconnect event if the connection comes back up
                        connection.OnReconnected();

                    _reader.StartReading();

                    // Set the reader for this connection
                    connection.Items[m_readerKey] = _reader;
                }
            };

            if (initializeCallback != null)
            {
                int _tryed = 0;
                while (true)
                {
                    _tryed++;
                    //Debug.WriteLine("Checking if connection initialized for the {0} time: ", _tryed.ToString());

                    Thread.Sleep(m_connectionTimeout);
                    if (Interlocked.CompareExchange(ref m_initializedCalled, 1, 0) == 0)
                    {
                        if (_tryed < m_connectionRetry)
                        {
                            //Debug.WriteLine("Connection initialized failed after {0} times.", _tryed.ToString());
                            continue;
                        }

                        //Debug.WriteLine("Giving up on connection initializing.");
                        Debug.Log("stopping");
                        // Stop the connection
                        Stop(connection);

                        // Connection timeout occurred
                        errorCallback(new TimeoutException());

                        break;
                    }
                    else
                    {
                        //Debug.WriteLine("Connection initialized succeed.");
                        break;
                    }
                }
            }
        }
Example #2
0
        private void OpenConnection(IConnection connection,
                                    string data,
                                    Action initializeCallback,
                                    Action <Exception> errorCallback)
        {
            // If we're reconnecting add /connect to the url
            bool                    _reconnecting   = initializeCallback == null;
            string                  _url            = (_reconnecting ? connection.Url : connection.Url + "connect");
            Action <IRequest>       _prepareRequest = PrepareRequest(connection);
            EventSignal <IResponse> _signal;

            if (shouldUsePost(connection))
            {
                _url += GetReceiveQueryString(connection, data);
                Debug.WriteLine(string.Format("SSE: POST {0}", _url));

                _signal = m_httpClient.PostAsync(
                    _url,
                    request =>
                {
                    _prepareRequest(request);
                    request.Accept = "text/event-stream";
                },
                    new Dictionary <string, string> {
                    {
                        "groups", GetSerializedGroups(connection)
                    }
                });
            }
            else
            {
                _url += GetReceiveQueryStringWithGroups(connection, data);
                Debug.WriteLine(string.Format("SSE: GET {0}", _url));

                _signal = m_httpClient.GetAsync(
                    _url,
                    request =>
                {
                    _prepareRequest(request);
                    request.Accept = "text/event-stream";
                });
            }

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

                    if (!HttpBasedTransport.IsRequestAborted(_exception))
                    {
                        if (errorCallback != null &&
                            Interlocked.Exchange(ref m_initializedCalled, 1) == 0)
                        {
                            errorCallback(_exception);
                        }
                        else if (_reconnecting)
                        {
                            // Only raise the error event if we failed to reconnect
                            connection.OnError(_exception);
                        }
                    }

                    if (_reconnecting)
                    {
                        // Retry
                        Reconnect(connection, data);
                        return;
                    }
                }
                else
                {
                    // Get the reseponse stream and read it for messages
                    IResponse         _response = e.Result;
                    Stream            _stream   = _response.GetResponseStream();
                    AsyncStreamReader _reader   = new AsyncStreamReader(
                        _stream,
                        connection,
                        () =>
                    {
                        if (Interlocked.CompareExchange(ref m_initializedCalled, 1, 0) == 0)
                        {
                            initializeCallback();
                        }
                    },
                        () =>
                    {
                        _response.Close();
                        Reconnect(connection, data);
                    });

                    if (_reconnecting)
                    {
                        // Raise the reconnect event if the connection comes back up
                        connection.OnReconnected();
                    }

                    _reader.StartReading();

                    // Set the reader for this connection
                    connection.Items[m_readerKey] = _reader;
                }
            };

            if (initializeCallback != null)
            {
                int _tryed = 0;
                while (true)
                {
                    _tryed++;
                    Debug.Write("Checking if connection initialized for the '" + _tryed.ToString() + "' time: ");

                    Thread.Sleep(m_connectionTimeout);
                    if (Interlocked.CompareExchange(ref m_initializedCalled, 1, 0) == 0)
                    {
                        if (_tryed < m_connectionRetry)
                        {
                            Debug.WriteLine("failed.");
                            continue;
                        }

                        Debug.WriteLine("giving up.");

                        // Stop the connection
                        Stop(connection);

                        // Connection timeout occurred
                        errorCallback(new TimeoutException());

                        break;
                    }
                    else
                    {
                        Debug.WriteLine("success.");
                        break;
                    }
                }
            }
        }
        private void OpenConnection(IConnection connection, string data, dotnet2::System.Action initializeCallback, Action<Exception> errorCallback)
        {
            // If we're reconnecting add /connect to the url
            bool reconnecting = initializeCallback == null;

            var url = (reconnecting ? connection.Url : connection.Url + "connect");

            Action<IRequest> prepareRequest = PrepareRequest(connection);

            EventSignal<IResponse> signal;

            if (shouldUsePost(connection))
            {
                url += GetReceiveQueryString(connection, data);

                Debug.WriteLine(string.Format("SSE: POST {0}", url));

                signal = _httpClient.PostAsync(url, request =>
                                                        {
                                                            prepareRequest(request);
                                                            request.Accept = "text/event-stream";
                                                        }, new Dictionary<string, string> {{"groups", GetSerializedGroups(connection)}});
            }
            else
            {
                url += GetReceiveQueryStringWithGroups(connection, data);

                Debug.WriteLine(string.Format("SSE: GET {0}", url));

                signal = _httpClient.GetAsync(url, request =>
                {
                    prepareRequest(request);

                    request.Accept = "text/event-stream";
                });
            }

            signal.Finished += (sender,e)=> {
                if (e.Result.IsFaulted)
                {
                    var exception = e.Result.Exception.GetBaseException();
                    if (!IsRequestAborted(exception))
                    {
                        if (errorCallback != null &&
                            Interlocked.Exchange(ref _initializedCalled, 1) == 0)
                        {
                            errorCallback(exception);
                        }
                        else if (reconnecting)
                        {
                            // Only raise the error event if we failed to reconnect
                            connection.OnError(exception);
                        }
                    }

                    if (reconnecting)
                    {
                        // Retry
                        Reconnect(connection, data);
                        return;
                    }
                }
                else
                {
                    // Get the reseponse stream and read it for messages
                    var response = e.Result;
                    var stream = response.GetResponseStream();
                    var reader = new AsyncStreamReader(stream,
                                                       connection,
                                                       () =>
                                                       {
                                                           if (Interlocked.CompareExchange(ref _initializedCalled, 1, 0) == 0)
                                                           {
                                                               initializeCallback();
                                                           }
                                                       },
                                                       () =>
                                                       {
                                                           response.Close();

                                                           Reconnect(connection, data);
                                                       });

                    if (reconnecting)
                    {
                        // Raise the reconnect event if the connection comes back up
                        connection.OnReconnected();
                    }

                    reader.StartReading();

                    // Set the reader for this connection
                    connection.Items[ReaderKey] = reader;
                }
            };

            if (initializeCallback != null)
            {
                Thread.Sleep(ConnectionTimeout);
                if (Interlocked.CompareExchange(ref _initializedCalled, 1, 0) == 0)
                {
                    // Stop the connection
                    Stop(connection);

                    // Connection timeout occured
                    errorCallback(new TimeoutException());
                }
            }
        }