Esempio n. 1
0
        public async Task Accept(TestContext ctx, CancellationToken cancellationToken)
        {
            var me = $"{ME}({connection.ME}:{ReusingConnection}) ACCEPT";

            ctx.LogDebug(2, $"{me}");

            try {
                if (ReusingConnection)
                {
                    serverStartTask.TrySetResult(null);
                    assignedOperation?.Finish();
                }
                else
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    var acceptTask = connection.AcceptAsync(ctx, cancellationToken);

                    serverStartTask.TrySetResult(null);

                    await acceptTask.ConfigureAwait(false);

                    ctx.LogDebug(2, $"{me} DONE: {connection.RemoteEndPoint} {assignedOperation?.ME}");

                    assignedOperation?.Finish();
                }
            } catch (OperationCanceledException) {
                // OnCanceled ();
                throw;
            } catch (Exception ex) {
                ctx.LogDebug(2, $"{me} FAILED: {ex.Message}");
                // OnError (ex);
                throw;
            }
        }
Esempio n. 2
0
        async void MainLoop()
        {
            while (!closed)
            {
                Debug($"MAIN LOOP");

                var taskList    = new List <Task> ();
                var contextList = new List <ListenerTask> ();
                lock (this) {
                    Debug($"MAIN LOOP - SCHEDULER: {connections.Count}");
                    RunScheduler();

                    taskList.Add(mainLoopEvent.WaitAsync());
                    PopulateTaskList(contextList, taskList);

                    Debug($"MAIN LOOP - SCHEDULER DONE: {connections.Count} {taskList.Count}");
                }

                var finished = await Task.WhenAny(taskList).ConfigureAwait(false);

                Debug($"MAIN LOOP TASK: {finished.Status} {finished == taskList[0]} {taskList[0].Status}");

                ListenerOperation operation = null;
                ListenerContext   context;
                bool success;

                lock (this) {
                    if (closed)
                    {
                        break;
                    }
                    if (finished == taskList[0])
                    {
                        mainLoopEvent.Reset();
                        continue;
                    }

                    int idx = -1;
                    for (int i = 0; i < contextList.Count; i++)
                    {
                        if (finished == taskList[i + 1])
                        {
                            idx = i;
                            break;
                        }
                    }

                    var task = contextList[idx];
                    context = task.Context;
                    listenerTasks.Remove(task);

                    var me = $"MAIN LOOP TASK #1 ({idx}, {context.State})";
                    Debug($"{me}");

                    if (context.State == ConnectionState.Listening && currentOperation?.AssignedContext == context)
                    {
                        operation = Interlocked.Exchange(ref currentOperation, null);
                        Debug($"{me} GOT OPERATION {operation.ID}");
                    }

                    try {
                        success = context.MainLoopListenerTaskDone(TestContext, cts.Token);
                    } catch (Exception ex) {
                        Debug($"{me} EX: {ex.Message}");
                        connections.Remove(context);
                        context.Dispose();
                        success = false;
                    }

                    Debug($"{me} OP={operation?.ID}");
                }

                if (operation != null)
                {
                    operation.Finish();
                }
            }

            Debug($"MAIN LOOP COMPLETE");

            lock (this) {
                var iter = connections.First;
                while (iter != null)
                {
                    var node = iter.Value;
                    iter = iter.Next;

                    node.Dispose();
                    connections.Remove(node);
                }

                cts.Dispose();
                exited = true;
            }

            Debug($"MAIN LOOP COMPLETE #1");

            finishedEvent.SetResult(null);

            void PopulateTaskList(List <ListenerTask> contextList, List <Task> taskList)
            {
                var iter = listenerTasks.First;

                while (iter != null)
                {
                    var node = iter;
                    iter = iter.Next;

                    contextList.Add(node.Value);
                    taskList.Add(node.Value.Task);
                }
            }
        }