protected override int RunServerCore(string pipeName, IClientConnectionHost connectionHost, IDiagnosticListener listener, TimeSpan?keepAlive, CancellationToken cancellationToken)
        {
            // Grab the server mutex to prevent multiple servers from starting with the same
            // pipename and consuming excess resources. If someone else holds the mutex
            // exit immediately with a non-zero exit code
            var  mutexName = GenerationServerConnection.GetServerMutexName(pipeName);
            bool holdsMutex;

            using (var serverMutex = new Mutex(initiallyOwned: true,
                                               name: mutexName,
                                               createdNew: out holdsMutex))
            {
                if (!holdsMutex)
                {
                    return(CommonGenerator.Failed);
                }

                try
                {
                    return(base.RunServerCore(pipeName, connectionHost, listener, keepAlive, cancellationToken));
                }
                finally
                {
                    serverMutex.ReleaseMutex();
                }
            }
        }
コード例 #2
0
        internal int RunServer(
            string pipeName,
            string tempPath,
            IClientConnectionHost clientConnectionHost = null,
            IDiagnosticListener listener        = null,
            TimeSpan?keepAlive                  = null,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            if (GenerationServerConnection.IsPipePathTooLong(pipeName, tempPath))
            {
                return(CommonGenerator.Failed);
            }

            keepAlive            = keepAlive ?? GetKeepAliveTimeout();
            listener             = listener ?? new EmptyDiagnosticListener();
            clientConnectionHost = clientConnectionHost ?? CreateClientConnectionHost(pipeName);
            return(RunServerCore(pipeName, clientConnectionHost, listener, keepAlive, cancellationToken));
        }
コード例 #3
0
        /// <summary>
        /// Does the client of "pipeStream" have the same identity and elevation as we do?
        /// </summary>
        private bool ClientAndOurIdentitiesMatch(NamedPipeServerStream pipeStream)
        {
            if (PlatformInformation.IsWindows)
            {
                var serverIdentity = GetIdentity(impersonating: false);

                (string name, bool admin)clientIdentity = default;
                pipeStream.RunAsClient(() => { clientIdentity = GetIdentity(impersonating: true); });

                this.Log().Debug($"Server identity = '{serverIdentity.name}', server elevation='{serverIdentity.admin}'.");
                this.Log().Debug($"Client identity = '{clientIdentity.name}', client elevation='{serverIdentity.admin}'.");

                return
                    (StringComparer.OrdinalIgnoreCase.Equals(serverIdentity.name, clientIdentity.name) &&
                     serverIdentity.admin == clientIdentity.admin);
            }
            else
            {
                return(GenerationServerConnection.CheckIdentityUnix(pipeStream));
            }
        }
        protected override bool?WasServerRunning(string pipeName)
        {
            string mutexName = GenerationServerConnection.GetServerMutexName(pipeName);

            return(GenerationServerConnection.WasServerMutexOpen(mutexName));
        }
        protected override string GetDefaultPipeName()
        {
            var clientDirectory = AppDomain.CurrentDomain.BaseDirectory;

            return(GenerationServerConnection.GetPipeNameForPathOpt(clientDirectory));
        }
 protected override async Task <Stream> ConnectForShutdownAsync(string pipeName, int timeout)
 {
     return(await GenerationServerConnection.TryConnectToServerAsync(pipeName, timeout, cancellationToken : default).ConfigureAwait(false));
 }
コード例 #7
0
 /// <summary>
 /// The IsConnected property on named pipes does not detect when the client has disconnected
 /// if we don't attempt any new I/O after the client disconnects. We start an async I/O here
 /// which serves to check the pipe for disconnection.
 ///
 /// This will return true if the pipe was disconnected.
 /// </summary>
 protected override Task CreateMonitorDisconnectTask(CancellationToken cancellationToken)
 {
     return(GenerationServerConnection.CreateMonitorDisconnectTask(_pipeStream, LoggingIdentifier, cancellationToken));
 }