Exemplo n.º 1
0
        /// <summary>
        /// Construct a <see cref="SessionController"/>
        /// </summary>
        /// <param name="reattachInformation">The value of <see cref="reattachInformation"/></param>
        /// <param name="process">The value of <see cref="process"/></param>
        /// <param name="byondLock">The value of <see cref="byondLock"/></param>
        /// <param name="byondTopicSender">The value of <see cref="byondTopicSender"/></param>
        /// <param name="interopContext">The value of <see cref="interopContext"/></param>
        /// <param name="chat">The value of <see cref="chat"/></param>
        /// <param name="chatJsonTrackingContext">The value of <see cref="chatJsonTrackingContext"/></param>
        /// <param name="logger">The value of <see cref="logger"/></param>
        /// <param name="launchSecurityLevel">The value of <see cref="launchSecurityLevel"/></param>
        /// <param name="startupTimeout">The optional time to wait before failing the <see cref="LaunchResult"/></param>
        public SessionController(
            ReattachInformation reattachInformation,
            IProcess process,
            IByondExecutableLock byondLock,
            IByondTopicSender byondTopicSender,
            IJsonTrackingContext chatJsonTrackingContext,
            ICommContext interopContext,
            IChat chat,
            ILogger <SessionController> logger,
            DreamDaemonSecurity?launchSecurityLevel,
            uint?startupTimeout)
        {
            this.chatJsonTrackingContext = chatJsonTrackingContext;             // null valid
            this.reattachInformation     = reattachInformation ?? throw new ArgumentNullException(nameof(reattachInformation));
            this.byondTopicSender        = byondTopicSender ?? throw new ArgumentNullException(nameof(byondTopicSender));
            this.process        = process ?? throw new ArgumentNullException(nameof(process));
            this.byondLock      = byondLock ?? throw new ArgumentNullException(nameof(byondLock));
            this.interopContext = interopContext ?? throw new ArgumentNullException(nameof(interopContext));
            this.chat           = chat ?? throw new ArgumentNullException(nameof(chat));
            this.logger         = logger ?? throw new ArgumentNullException(nameof(logger));

            this.launchSecurityLevel = launchSecurityLevel;

            interopContext.RegisterHandler(this);

            portClosedForReboot = false;
            disposed            = false;
            apiValidationStatus = ApiValidationStatus.NeverValidated;
            released            = false;

            rebootTcs = new TaskCompletionSource <object>();

            process.Lifetime.ContinueWith(x => chatJsonTrackingContext.Active = false, TaskScheduler.Current);

            async Task <LaunchResult> GetLaunchResult()
            {
                var  startTime = DateTimeOffset.Now;
                Task toAwait   = process.Startup;

                if (startupTimeout.HasValue)
                {
                    toAwait = Task.WhenAny(process.Startup, Task.Delay(startTime.AddSeconds(startupTimeout.Value) - startTime));
                }

                await toAwait.ConfigureAwait(false);

                var result = new LaunchResult
                {
                    ExitCode    = process.Lifetime.IsCompleted ? (int?)await process.Lifetime.ConfigureAwait(false) : null,
                    StartupTime = process.Startup.IsCompleted ? (TimeSpan?)(DateTimeOffset.Now - startTime) : null
                };

                return(result);
            }

            LaunchResult = GetLaunchResult();

            logger.LogDebug("Created session controller. Primary: {0}, CommsKey: {1}, Port: {2}", IsPrimary, reattachInformation.AccessIdentifier, Port);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Construct a <see cref="SessionController"/>
        /// </summary>
        /// <param name="reattachInformation">The value of <see cref="reattachInformation"/></param>
        /// <param name="process">The value of <see cref="process"/></param>
        /// <param name="byondLock">The value of <see cref="byondLock"/></param>
        /// <param name="byondTopicSender">The value of <see cref="byondTopicSender"/></param>
        /// <param name="interopContext">The value of <see cref="interopContext"/></param>
        /// <param name="chat">The value of <see cref="chat"/></param>
        /// <param name="chatJsonTrackingContext">The value of <see cref="chatJsonTrackingContext"/></param>
        /// <param name="logger">The value of <see cref="logger"/></param>
        /// <param name="startupTimeout">The optional time to wait before failing the <see cref="LaunchResult"/></param>
        public SessionController(ReattachInformation reattachInformation, IProcess process, IByondExecutableLock byondLock, IByondTopicSender byondTopicSender, IJsonTrackingContext chatJsonTrackingContext, ICommContext interopContext, IChat chat, ILogger <SessionController> logger, uint?startupTimeout)
        {
            this.chatJsonTrackingContext = chatJsonTrackingContext;             //null valid
            this.reattachInformation     = reattachInformation ?? throw new ArgumentNullException(nameof(reattachInformation));
            this.byondTopicSender        = byondTopicSender ?? throw new ArgumentNullException(nameof(byondTopicSender));
            this.process        = process ?? throw new ArgumentNullException(nameof(process));
            this.byondLock      = byondLock ?? throw new ArgumentNullException(nameof(byondLock));
            this.interopContext = interopContext ?? throw new ArgumentNullException(nameof(interopContext));
            this.chat           = chat ?? throw new ArgumentNullException(nameof(chat));
            this.logger         = logger ?? throw new ArgumentNullException(nameof(logger));

            interopContext.RegisterHandler(this);

            portClosedForReboot = false;
            disposed            = false;
            apiValidated        = false;
            released            = false;

            rebootTcs = new TaskCompletionSource <object>();

            async Task <LaunchResult> GetLaunchResult()
            {
                var  startTime = DateTimeOffset.Now;
                Task toAwait   = process.Startup;

                if (startupTimeout.HasValue)
                {
                    toAwait = Task.WhenAny(process.Startup, Task.Delay(startTime.AddSeconds(startupTimeout.Value) - startTime));
                }

                await toAwait.ConfigureAwait(false);

                var result = new LaunchResult
                {
                    ExitCode    = process.Lifetime.IsCompleted ? (int?)await process.Lifetime.ConfigureAwait(false) : null,
                    StartupTime = process.Startup.IsCompleted ? (TimeSpan?)(DateTimeOffset.Now - startTime) : null
                };

                return(result);
            };
            LaunchResult = GetLaunchResult();
        }