Example #1
0
 public void checkTargetListener(Targetable target)
 {
     if (targetListener != null)
     {
         bool targetChosen = targetListener.checkTarget(target);
         if (targetChosen)
         {
             targetListener = null;
         }
     }
 }
Example #2
0
    protected void startListening()
    {
        listeningStarted = true;
        GameObject go = Instantiate(PrefabDictionary.Instance().targetListener, details.cardBase.transform);

        targetListener = go.GetComponent <TargetListener>();
        targetListener.targetCatgeory = targetCategory;
        targetListener.targetSource   = details.cardBase;
        targetListener.sourceAbility  = this;
        GameAlertUI.Instance().setText(getAlertPromptText());
    }
Example #3
0
 public ListenerOperation RegisterOperation(TestContext ctx, HttpOperation operation, Handler handler, string path)
 {
     lock (this) {
         if (TargetListener != null)
         {
             var targetOperation = TargetListener.RegisterOperation(ctx, operation, handler, path);
             registry.Add(targetOperation.Uri.LocalPath, targetOperation);
             return(targetOperation.CreateProxy(this));
         }
         if (path == null)
         {
             var id = Interlocked.Increment(ref nextRequestID);
             path = $"/id/{operation.ID}/{handler.GetType ().Name}/";
         }
         var me = $"{nameof (RegisterOperation)}({handler.Value})";
         Debug($"{me} {path}");
         var uri = new Uri(Server.TargetUri, path);
         var listenerOperation = new ListenerOperation(this, operation, handler, uri);
         registry.Add(path, listenerOperation);
         return(listenerOperation);
     }
 }
Example #4
0
        public async Task <Response> RunWithContext(TestContext ctx, ListenerOperation operation, Request request,
                                                    ClientFunc clientFunc, CancellationToken cancellationToken)
        {
            var me = $"{ME}({operation.Operation.ME}) RUN WITH CONTEXT";

            ListenerContext context        = null;
            ListenerContext targetContext  = null;
            var             reusing        = !operation.Operation.HasAnyFlags(HttpOperationFlags.DontReuseConnection);
            var             delayedContext = operation.Operation.HasAnyFlags(HttpOperationFlags.DelayedListenerContext);

            /*
             * When using HttpOperationFlags.DelayedListenerContext, then we call the `clientFunc` before we
             * actually start listening.
             */

            if (UsingInstrumentation && !delayedContext)
            {
                context = await FindContext(ctx, operation, reusing);

                reusing = context.ReusingConnection;

                ctx.LogDebug(2, $"{me} - CREATE CONTEXT: {reusing} {context.ME}");

                await context.ServerStartTask.ConfigureAwait(false);

                ctx.LogDebug(2, $"{me} - CREATE CONTEXT #1: {reusing} {context.ME}");
            }

            if (TargetListener?.UsingInstrumentation ?? false)
            {
                targetContext = await TargetListener.FindContext(ctx, operation.TargetOperation, false);

                ctx.LogDebug(2, $"{me} - CREATE TARGET CONTEXT: {reusing} {targetContext.ME}");
                try {
                    await targetContext.ServerStartTask.ConfigureAwait(false);
                } catch {
                    context?.Dispose();
                    throw;
                }
            }

            var clientTask         = clientFunc(ctx, request, cancellationToken);
            var serverInitTask     = operation.ServerInitTask;
            var serverFinishedTask = operation.ServerFinishedTask;

            ExceptionDispatchInfo throwMe = null;
            bool initDone = false, serverDone = false, clientDone = false;

            while (!initDone || !serverDone || !clientDone)
            {
                ctx.LogDebug(2, $"{me} LOOP: init={initDone} server={serverDone} client={clientDone}");

                if (clientDone)
                {
                    if (operation.Operation.HasAnyFlags(
                            HttpOperationFlags.AbortAfterClientExits, HttpOperationFlags.ServerAbortsHandshake,
                            HttpOperationFlags.ClientAbortsHandshake))
                    {
                        ctx.LogDebug(2, $"{me} - ABORTING");
                        break;
                    }
                    if (!initDone)
                    {
                        ctx.LogDebug(2, $"{me} - ERROR: {clientTask.Result}");
                        throwMe = ExceptionDispatchInfo.Capture(new ConnectionException(
                                                                    $"{me} client exited before server accepted connection."));
                        break;
                    }
                }

                var tasks = new List <Task> ();
                if (!initDone)
                {
                    tasks.Add(serverInitTask);
                }
                if (!serverDone)
                {
                    tasks.Add(serverFinishedTask);
                }
                if (!clientDone)
                {
                    tasks.Add(clientTask);
                }
                var finished = await Task.WhenAny(tasks).ConfigureAwait(false);

                string which;
                if (finished == serverInitTask)
                {
                    which    = "init";
                    initDone = true;
                }
                else if (finished == serverFinishedTask)
                {
                    which      = "server";
                    serverDone = true;
                }
                else if (finished == clientTask)
                {
                    which      = "client";
                    clientDone = true;
                }
                else
                {
                    throwMe = ExceptionDispatchInfo.Capture(new InvalidOperationException());
                    break;
                }

                ctx.LogDebug(2, $"{me} #4: {which} exited - {finished.Status}");
                if (finished.Status == TaskStatus.Faulted || finished.Status == TaskStatus.Canceled)
                {
                    if (operation.Operation.HasAnyFlags(HttpOperationFlags.ExpectServerException) &&
                        (finished == serverFinishedTask || finished == serverInitTask))
                    {
                        ctx.LogDebug(2, $"{me} EXPECTED EXCEPTION {finished.Exception.GetType ()}");
                    }
                    else if (finished.Status == TaskStatus.Canceled)
                    {
                        ctx.LogDebug(2, $"{me} CANCELED");
                        throwMe = ExceptionDispatchInfo.Capture(new OperationCanceledException());
                        break;
                    }
                    else
                    {
                        ctx.LogDebug(2, $"{me} FAILED: {finished.Exception.Message}");
                        throwMe = ExceptionDispatchInfo.Capture(finished.Exception);
                        break;
                    }
                }
            }

            if (throwMe != null)
            {
                ctx.LogDebug(2, $"{me} THROWING {throwMe.SourceException.Message}");
                lock (this) {
                    operation.OnError(throwMe.SourceException);
                    if (context != null)
                    {
                        context.Dispose();
                    }
                    if (targetContext != null)
                    {
                        targetContext.Dispose();
                    }
                    mainLoopEvent.Set();
                }
                throwMe.Throw();
            }

            return(clientTask.Result);
        }