Пример #1
0
        /// <summary>
        /// Runs a single task as part of the overall job.
        /// </summary>
        /// <param name="options">The options that dictate the type of task to run.</param>
        /// <param name="taskNumber">The number assigned to this task.</param>
        /// <returns>A boolean indicating whether this single task succeeded or not.</returns>
        private static async Task <bool> RunTask(Options options, int taskNumber)
        {
            SIPTransport sipTransport = new SIPTransport();

            sipTransport.PreferIPv6NameResolution = options.PreferIPv6;

            if (options.Verbose)
            {
                EnableTraceLogs(sipTransport);
            }

            try
            {
                DateTime startTime = DateTime.Now;

                var dstUri = ParseDestination(options.Destination);

                logger.LogDebug($"Destination SIP URI {dstUri}");

                if (options.SourcePort != 0)
                {
                    var sipChannel = sipTransport.CreateChannel(dstUri.Protocol,
                                                                options.PreferIPv6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork,
                                                                options.SourcePort);
                    sipTransport.AddSIPChannel(sipChannel);
                }

                Task <bool> task = null;

                switch (options.Scenario)
                {
                case Scenarios.reg:
                    task = InitiateRegisterTaskAsync(sipTransport, dstUri);
                    break;

                case Scenarios.uac:
                case Scenarios.uacw:
                    task = InitiateCallTaskAsync(sipTransport, dstUri, options.Scenario);
                    break;

                case Scenarios.opt:
                default:
                    task = SendOptionsTaskAsync(sipTransport, dstUri);
                    break;
                }

                var result = await Task.WhenAny(task, Task.Delay(options.Timeout * 1000));

                TimeSpan duration = DateTime.Now.Subtract(startTime);
                bool     failed   = false;

                if (!task.IsCompleted)
                {
                    logger.LogWarning($"=> Request to {dstUri} did not get a response on task {taskNumber} after {duration.TotalMilliseconds.ToString("0")}ms.");
                    failed = true;
                }
                else if (!task.Result)
                {
                    logger.LogWarning($"=> Request to {dstUri} did not get the expected response on task {taskNumber} after {duration.TotalMilliseconds.ToString("0")}ms.");
                    failed = true;
                }
                else
                {
                    logger.LogInformation($"=> Got correct response on send {taskNumber} in {duration.TotalMilliseconds.ToString("0")}ms.");
                }

                return(!failed);
            }
            finally
            {
                logger.LogDebug("Shutting down the SIP transport...");
                sipTransport.Shutdown();
            }
        }