private void OnNegotiationRequestFinished(HTTPRequest req, HTTPResponse resp)
        {
            if (this.State == ConnectionStates.CloseInitiated)
            {
                SetState(ConnectionStates.Closed);
                return;
            }

            string errorReason = null;

            switch (req.State)
            {
            // The request finished without any problem.
            case HTTPRequestStates.Finished:
                if (resp.IsSuccess)
                {
                    HTTPManager.Logger.Information("HubConnection", "Negotiation Request Finished Successfully! Response: " + resp.DataAsText);

                    // Parse negotiation
                    this.NegotiationResult = NegotiationResult.Parse(resp, out errorReason, this);

                    // Room for improvement: check validity of the negotiation result:
                    //  If url and accessToken is present, the other two must be null.
                    //  https://github.com/aspnet/SignalR/blob/dev/specs/TransportProtocols.md#post-endpoint-basenegotiate-request

                    if (string.IsNullOrEmpty(errorReason))
                    {
                        if (this.NegotiationResult.Url != null)
                        {
                            this.SetState(ConnectionStates.Redirected);

                            if (++this.RedirectCount >= this.Options.MaxRedirects)
                            {
                                errorReason = string.Format("MaxRedirects ({0:N0}) reached!", this.Options.MaxRedirects);
                            }
                            else
                            {
                                var oldUri = this.Uri;
                                this.Uri = this.NegotiationResult.Url;

                                if (this.OnRedirected != null)
                                {
                                    try
                                    {
                                        this.OnRedirected(this, oldUri, Uri);
                                    }
                                    catch (Exception ex)
                                    {
                                        HTTPManager.Logger.Exception("HubConnection", "OnNegotiationRequestFinished - OnRedirected", ex);
                                    }
                                }

                                StartConnect();
                            }
                        }
                        else
                        {
                            ConnectImpl(this.Options.PreferedTransport);
                        }
                    }
                }
                else     // Internal server error?
                {
                    errorReason = string.Format("Negotiation Request Finished Successfully, but the server sent an error. Status Code: {0}-{1} Message: {2}",
                                                resp.StatusCode,
                                                resp.Message,
                                                resp.DataAsText);
                }
                break;

            // The request finished with an unexpected error. The request's Exception property may contain more info about the error.
            case HTTPRequestStates.Error:
                errorReason = "Negotiation Request Finished with Error! " + (req.Exception != null ? (req.Exception.Message + "\n" + req.Exception.StackTrace) : "No Exception");
                break;

            // The request aborted, initiated by the user.
            case HTTPRequestStates.Aborted:
                errorReason = "Negotiation Request Aborted!";
                break;

            // Connecting to the server is timed out.
            case HTTPRequestStates.ConnectionTimedOut:
                errorReason = "Negotiation Request - Connection Timed Out!";
                break;

            // The request didn't finished in the given time.
            case HTTPRequestStates.TimedOut:
                errorReason = "Negotiation Request - Processing the request Timed Out!";
                break;
            }

            if (errorReason != null)
            {
                this.NegotiationResult = new NegotiationResult();
                this.NegotiationResult.NegotiationResponse = resp;

                SetState(ConnectionStates.Closed, errorReason);
            }
        }
Exemplo n.º 2
0
 public ResultElement(byte[] buffer, int offset)
 {
     Result         = (NegotiationResult)LittleEndianConverter.ToUInt16(buffer, offset + 0);
     Reason         = (RejectionReason)LittleEndianConverter.ToUInt16(buffer, offset + 2);
     TransferSyntax = new SyntaxID(buffer, offset + 4);
 }