protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            RequestAction?.Invoke(request);
            var response = await base.SendAsync(request, cancellationToken);

            ResponseAction?.Invoke(response);

            return(response);
        }
Ejemplo n.º 2
0
        public IEnumerator Run <T>(
            UnityAction <AsyncResult <T> > callback,
            [CanBeNull] GameSession gameSession,
            RequestAction <T> requestAction)
        {
            bool isReopenTried         = false;
            bool isAuthenticationTried = false;

            AsyncResult <T> asyncResult = null;

            while (true)
            {
                yield return(requestAction.Invoke(ar => asyncResult = ar));

                if (asyncResult.Error is SessionNotOpenException && !isReopenTried)
                {
                    isReopenTried = true;

                    AsyncResult <OpenResult> asyncOpenResult = null;

                    yield return(_reopener.ReOpen(Gs2Session, Gs2RestSession, aor => asyncOpenResult = aor));

                    _reopener.Callback?.Invoke(asyncOpenResult);

                    if (asyncOpenResult.Error == null)
                    {
                        continue;
                    }
                }

                var authenticator = _authenticator;
                if (gameSession != null && authenticator != null && asyncResult.Error is UnauthorizedException && !isAuthenticationTried)
                {
                    isAuthenticationTried = true;

                    AsyncResult <AccessToken> asyncAuthenticationResult = null;

                    yield return(authenticator.Authentication(aar => asyncAuthenticationResult = aar));

                    if (asyncAuthenticationResult.Error == null)
                    {
                        gameSession.AccessToken = asyncAuthenticationResult.Result;
                    }

                    authenticator.Callback?.Invoke(asyncAuthenticationResult);

                    if (asyncAuthenticationResult.Error == null)
                    {
                        continue;
                    }
                }

                break;
            }

            callback.Invoke(asyncResult);
        }
Ejemplo n.º 3
0
 public Task <byte[]> Request(byte[] request, int timeout, CancellationTokenSource source)
 {
     RequestAction?.Invoke();
     ServerRequest = request;
     ServerTimeout = timeout;
     Source        = source;
     if (ServerError != null)
     {
         throw ServerError;
     }
     return(Task.FromResult(ServerResponse));
 }