Example #1
0
            private ServerData CreateServer(string pipeName, ICompilerServerHost compilerServerHost = null)
            {
                var serverData = ServerUtil.CreateServer(pipeName, compilerServerHost).GetAwaiter().GetResult();

                _serverDataList.Add(serverData);
                return(serverData);
            }
Example #2
0
 internal ServerDispatcher(ICompilerServerHost compilerServerHost, IClientConnectionHost clientConnectionHost, IDiagnosticListener?diagnosticListener = null)
 {
     _compilerServerHost   = compilerServerHost;
     _logger               = compilerServerHost.Logger;
     _clientConnectionHost = clientConnectionHost;
     _diagnosticListener   = diagnosticListener ?? new EmptyDiagnosticListener();
 }
Example #3
0
        internal static ServerData CreateServer(
            string pipeName  = null,
            TimeSpan?timeout = null,
            ICompilerServerHost compilerServerHost = null)
        {
            pipeName           = pipeName ?? Guid.NewGuid().ToString();
            compilerServerHost = compilerServerHost ?? new DesktopCompilerServerHost(DefaultClientDirectory, DefaultSdkDirectory);

            var taskSource = new TaskCompletionSource <ServerStats>();
            var cts        = new CancellationTokenSource();
            var thread     = new Thread(_ =>
            {
                var listener = new TestableDiagnosticListener();
                try
                {
                    var clientConnectionHost = new NamedPipeClientConnectionHost(compilerServerHost, pipeName);
                    var mutexName            = BuildProtocolConstants.GetServerMutexName(pipeName);
                    VBCSCompiler.Run(
                        mutexName,
                        clientConnectionHost,
                        listener,
                        timeout ?? TimeSpan.FromMilliseconds(-1),
                        cts.Token);
                }
                finally
                {
                    var serverStats = new ServerStats(connections: listener.ConnectionCount, completedConnections: listener.CompletedCount);
                    taskSource.SetResult(serverStats);
                }
            });

            thread.Start();

            return(new ServerData(cts, taskSource.Task, pipeName));
        }
Example #4
0
        internal int RunServer(
            string pipeName,
            ICompilerServerHost compilerServerHost     = null,
            IClientConnectionHost clientConnectionHost = null,
            IDiagnosticListener listener        = null,
            TimeSpan?keepAlive                  = null,
            CancellationToken cancellationToken = default)
        {
            keepAlive ??= GetKeepAliveTimeout();
            listener ??= new EmptyDiagnosticListener();
            clientConnectionHost ??= CreateClientConnectionHost(pipeName);
            compilerServerHost ??= CreateCompilerServerHost();

            // 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 = BuildServerConnection.GetServerMutexName(pipeName);
            bool createdNew;

            using (var serverMutex = BuildServerConnection.OpenOrCreateMutex(name: mutexName,
                                                                             createdNew: out createdNew))
            {
                if (!createdNew)
                {
                    return(CommonCompiler.Failed);
                }

                CompilerServerLogger.Log("Keep alive timeout is: {0} milliseconds.", keepAlive?.TotalMilliseconds ?? 0);
                FatalError.Handler = FailFast.OnFatalException;

                var dispatcher = new ServerDispatcher(compilerServerHost, clientConnectionHost, listener);
                dispatcher.ListenAndDispatchConnections(keepAlive, cancellationToken);
                return(CommonCompiler.Succeeded);
            }
        }
Example #5
0
        internal static ServerData Create(
            ICompilerServerLogger logger,
            string pipeName = null,
            ICompilerServerHost compilerServerHost     = null,
            IClientConnectionHost clientConnectionHost = null,
            TimeSpan?keepAlive = null)
        {
            // The total pipe path must be < 92 characters on Unix, so trim this down to 10 chars
            pipeName ??= ServerUtil.GetPipeName();
            compilerServerHost ??= BuildServerController.CreateCompilerServerHost(logger);
            clientConnectionHost ??= BuildServerController.CreateClientConnectionHost(pipeName, logger);
            keepAlive ??= TimeSpan.FromMilliseconds(-1);

            var listener           = new TestableDiagnosticListener();
            var serverListenSource = new TaskCompletionSource <bool>();
            var cts       = new CancellationTokenSource();
            var mutexName = BuildServerConnection.GetServerMutexName(pipeName);
            var task      = Task.Run(() =>
            {
                BuildServerController.CreateAndRunServer(
                    pipeName,
                    compilerServerHost,
                    clientConnectionHost,
                    listener,
                    keepAlive: keepAlive,
                    cancellationToken: cts.Token);
                return(listener);
            });

            return(new ServerData(cts, pipeName, logger, task));
        }
        internal static ServerData CreateServer(
            string pipeName = null,
            TimeSpan? timeout = null,
            ICompilerServerHost compilerServerHost = null)
        {
            pipeName = pipeName ?? Guid.NewGuid().ToString();
            compilerServerHost = compilerServerHost ?? new DesktopCompilerServerHost(DefaultClientDirectory, DefaultSdkDirectory);

            var taskSource = new TaskCompletionSource<ServerStats>();
            var cts = new CancellationTokenSource();
            var thread = new Thread(_ =>
            {
                var listener = new TestableDiagnosticListener();
                try
                {
                    var clientConnectionHost = new NamedPipeClientConnectionHost(compilerServerHost, pipeName);
                    var mutexName = BuildProtocolConstants.GetServerMutexName(pipeName);
                    VBCSCompiler.Run(
                        mutexName,
                        clientConnectionHost,
                        listener,
                        timeout ?? TimeSpan.FromMilliseconds(-1),
                        cts.Token);
                }
                finally
                {
                    var serverStats = new ServerStats(connections: listener.ConnectionCount, completedConnections: listener.CompletedCount);
                    taskSource.SetResult(serverStats);
                }
            });

            thread.Start();

            return new ServerData(cts, taskSource.Task, pipeName);
        }
Example #7
0
        internal static async Task <ServerData> CreateServer(
            string pipeName = null,
            ICompilerServerHost compilerServerHost = null,
            bool failingServer = false,
            string tempPath    = null)
        {
            // The total pipe path must be < 92 characters on Unix, so trim this down to 10 chars
            pipeName           = pipeName ?? Guid.NewGuid().ToString().Substring(0, 10);
            compilerServerHost = compilerServerHost ?? BuildServerController.CreateCompilerServerHost();
            tempPath           = tempPath ?? Path.GetTempPath();
            var clientConnectionHost = BuildServerController.CreateClientConnectionHostForServerHost(compilerServerHost, pipeName);

            if (failingServer)
            {
                clientConnectionHost = new FailingClientConnectionHost(clientConnectionHost);
            }

            var serverStatsSource  = new TaskCompletionSource <ServerStats>();
            var serverListenSource = new TaskCompletionSource <bool>();
            var cts       = new CancellationTokenSource();
            var mutexName = BuildServerConnection.GetServerMutexName(pipeName);
            var listener  = new TestableDiagnosticListener();
            var task      = Task.Run(() =>
            {
                listener.Listening += (sender, e) => { serverListenSource.TrySetResult(true); };
                try
                {
                    BuildServerController.CreateAndRunServer(
                        pipeName,
                        tempPath,
                        clientConnectionHost,
                        listener,
                        keepAlive: TimeSpan.FromMilliseconds(-1),
                        cancellationToken: cts.Token);
                }
                finally
                {
                    var serverStats = new ServerStats(connections: listener.ConnectionCount, completedConnections: listener.CompletedCount);
                    serverStatsSource.SetResult(serverStats);
                }
            });

            // The contract of this function is that it will return once the server has started.  Spin here until
            // we can verify the server has started or simply failed to start.
            while (BuildServerConnection.WasServerMutexOpen(mutexName) != true && !task.IsCompleted)
            {
                await Task.Yield();
            }

            if (task.IsFaulted)
            {
                throw task.Exception;
            }

            return(new ServerData(cts, pipeName, serverStatsSource.Task, serverListenSource.Task, listener.ConnectionCompletedCollection));
        }
Example #8
0
        internal static async Task <CompletionData> ProcessClientConnectionAsync(
            ICompilerServerHost compilerServerHost,
            Task <IClientConnection> clientStreamTask,
            bool allowCompilationRequests,
            CancellationToken cancellationToken)
        {
            var clientHandler = new ClientConnectionHandler(compilerServerHost);

            return(await clientHandler.ProcessAsync(clientStreamTask, allowCompilationRequests, cancellationToken).ConfigureAwait(false));
        }
Example #9
0
        internal static async Task <ServerData> CreateServer(
            ICompilerServerLogger logger,
            string pipeName = null,
            ICompilerServerHost compilerServerHost     = null,
            IClientConnectionHost clientConnectionHost = null,
            TimeSpan?keepAlive = null)
        {
            var serverData = ServerData.Create(logger, pipeName, compilerServerHost, clientConnectionHost, keepAlive);
            await serverData.WaitForServerAsync();

            return(serverData);
        }
Example #10
0
            static Task <BuildResponse> ProcessCompilationRequestCore(ICompilerServerHost compilerServerHost, BuildRequest buildRequest, CancellationToken cancellationToken)
            {
                Func <BuildResponse> func = () =>
                {
                    var request  = BuildProtocolUtil.GetRunRequest(buildRequest);
                    var response = compilerServerHost.RunCompilation(request, cancellationToken);
                    return(response);
                };

                var task = new Task <BuildResponse>(func, cancellationToken, TaskCreationOptions.LongRunning);

                task.Start();
                return(task);
            }
Example #11
0
        internal static int CreateAndRunServer(
            string pipeName,
            ICompilerServerHost compilerServerHost     = null,
            IClientConnectionHost clientConnectionHost = null,
            IDiagnosticListener listener        = null,
            TimeSpan?keepAlive                  = null,
            NameValueCollection appSettings     = null,
            CancellationToken cancellationToken = default)
        {
            appSettings ??= new NameValueCollection();
            var controller = new BuildServerController(appSettings);

            return(controller.RunServer(pipeName, compilerServerHost, clientConnectionHost, listener, keepAlive, cancellationToken));
        }
        internal static ServerData CreateServer(
            string pipeName = null,
            ICompilerServerHost compilerServerHost = null,
            bool failingServer = false)
        {
            pipeName           = pipeName ?? Guid.NewGuid().ToString();
            compilerServerHost = compilerServerHost ?? DesktopBuildServerController.CreateCompilerServerHost();
            var clientConnectionHost = DesktopBuildServerController.CreateClientConnectionHostForServerHost(compilerServerHost, pipeName);

            if (failingServer)
            {
                clientConnectionHost = new FailingClientConnectionHost(clientConnectionHost);
            }

            var serverStatsSource  = new TaskCompletionSource <ServerStats>();
            var serverListenSource = new TaskCompletionSource <bool>();
            var cts       = new CancellationTokenSource();
            var mutexName = BuildServerConnection.GetServerMutexName(pipeName);
            var thread    = new Thread(_ =>
            {
                var listener        = new TestableDiagnosticListener();
                listener.Listening += (sender, e) => { serverListenSource.TrySetResult(true); };
                try
                {
                    DesktopBuildServerController.RunServer(
                        pipeName,
                        clientConnectionHost,
                        listener,
                        keepAlive: TimeSpan.FromMilliseconds(-1),
                        cancellationToken: cts.Token);
                }
                finally
                {
                    var serverStats = new ServerStats(connections: listener.ConnectionCount, completedConnections: listener.CompletedCount);
                    serverStatsSource.SetResult(serverStats);
                }
            });

            thread.Start();

            // The contract of this function is that it will return once the server has started.  Spin here until
            // we can verify the server has started or simply failed to start.
            while (BuildServerConnection.WasServerMutexOpen(mutexName) != true && thread.IsAlive)
            {
                Thread.Yield();
            }

            return(new ServerData(cts, pipeName, serverStatsSource.Task, serverListenSource.Task));
        }
Example #13
0
        internal static async Task <ServerData> CreateServer(
            string pipeName = null,
            ICompilerServerHost compilerServerHost     = null,
            IClientConnectionHost clientConnectionHost = null,
            TimeSpan?keepAlive = null)
        {
            // The total pipe path must be < 92 characters on Unix, so trim this down to 10 chars
            pipeName ??= Guid.NewGuid().ToString().Substring(0, 10);
            compilerServerHost ??= BuildServerController.CreateCompilerServerHost();
            clientConnectionHost ??= BuildServerController.CreateClientConnectionHost(pipeName);
            keepAlive ??= TimeSpan.FromMilliseconds(-1);

            var listener = new TestableDiagnosticListener();
            var listenerTaskCompletionSource = new TaskCompletionSource <TestableDiagnosticListener>();
            var serverListenSource           = new TaskCompletionSource <bool>();
            var cts       = new CancellationTokenSource();
            var mutexName = BuildServerConnection.GetServerMutexName(pipeName);
            var task      = Task.Run(() =>
            {
                try
                {
                    BuildServerController.CreateAndRunServer(
                        pipeName,
                        compilerServerHost,
                        clientConnectionHost,
                        listener,
                        keepAlive: keepAlive,
                        cancellationToken: cts.Token);
                }
                finally
                {
                    listenerTaskCompletionSource.SetResult(listener);
                }
            });

            // The contract of this function is that it will return once the server has started.  Spin here until
            // we can verify the server has started or simply failed to start.
            while (BuildServerConnection.WasServerMutexOpen(mutexName) != true && !task.IsCompleted)
            {
                await Task.Yield();
            }

            if (task.IsFaulted)
            {
                throw task.Exception;
            }

            return(new ServerData(cts, pipeName, listenerTaskCompletionSource.Task));
        }
 internal NamedPipeClientConnection(ICompilerServerHost compilerServerHost, string loggingIdentifier, NamedPipeServerStream pipeStream)
     : base(compilerServerHost, loggingIdentifier, pipeStream)
 {
     _pipeStream = pipeStream;
 }
 internal NamedPipeClientConnectionHost(ICompilerServerHost compilerServerHost, string pipeName)
 {
     _compilerServerHost = compilerServerHost;
     _pipeName           = pipeName;
 }
Example #16
0
 private static TestableClientConnection CreateConnection(Stream stream, ICompilerServerHost compilerServerHost = null)
 {
     compilerServerHost = compilerServerHost ?? new Mock<ICompilerServerHost>().Object;
     return new TestableClientConnection(compilerServerHost, stream);
 }
Example #17
0
 internal TcpClientConnection(ICompilerServerHost compilerServerHost, TcpClient client, string loggingIdentifier) : base(compilerServerHost, loggingIdentifier, client.GetStream())
 {
     _client = client;
 }
Example #18
0
 internal TestableClientConnection(ICompilerServerHost compilerServerHost, Stream stream)
     : base(compilerServerHost, "identifier", stream)
 {
     Stream = stream;
     CreateMonitorDisconnectTaskFunc = ct => Task.Delay(-1, ct);
 }
Example #19
0
 internal static IClientConnectionHost CreateClientConnectionHostForServerHost(
     ICompilerServerHost compilerServerHost,
     string pipeName)
 {
     return(new NamedPipeClientConnectionHost(compilerServerHost, pipeName));
 }
Example #20
0
 internal ClientConnectionHandler(ICompilerServerHost compilerServerHost)
 {
     CompilerServerHost = compilerServerHost;
 }
Example #21
0
 public ClientConnection(ICompilerServerHost compilerServerHost, string loggingIdentifier, Stream stream)
 {
     _compilerServerHost = compilerServerHost;
     _loggingIdentifier = loggingIdentifier;
     _stream = stream;
 }
 public NamedPipeClientConnection(ICompilerServerHost compilerServerHost, string loggingIdentifier, NamedPipeServerStream stream)
 {
     _compilerServerHost = compilerServerHost;
     _loggingIdentifier  = loggingIdentifier;
     _stream             = stream;
 }
 internal CSharpCompilerServer(ICompilerServerHost compilerServerHost, string[] args, string clientDirectory, string baseDirectory, string sdkDirectory, string libDirectory, IAnalyzerAssemblyLoader analyzerLoader)
     : base(CSharpCommandLineParser.Default, clientDirectory != null ? Path.Combine(clientDirectory, ResponseFileName) : null, args, clientDirectory, baseDirectory, sdkDirectory, libDirectory, analyzerLoader)
 {
     _compilerServerHost = compilerServerHost;
 }
 internal TcpClientConnectionHost(ICompilerServerHost compilerServerHost, IPEndPoint endPoint)
 {
     _compilerServerHost = compilerServerHost;
     _listener = new TcpListener(endPoint);
     _listener.Start();
 }
 internal NamedPipeClientConnection(ICompilerServerHost compilerServerHost, string loggingIdentifier, NamedPipeServerStream pipeStream)
     : base(compilerServerHost, loggingIdentifier, pipeStream)
 {
     _pipeStream = pipeStream;
 }
Example #26
0
        internal static ServerData CreateServer(
            string pipeName = null,
            TimeSpan? timeout = null,
            ICompilerServerHost compilerServerHost = null,
            IClientConnectionHost clientConnectionHost = null)
        {
            pipeName = pipeName ?? Guid.NewGuid().ToString();
            compilerServerHost = compilerServerHost ?? new DesktopCompilerServerHost(DefaultClientDirectory, DefaultSdkDirectory);

            var serverStatsSource = new TaskCompletionSource<ServerStats>();
            var serverListenSource = new TaskCompletionSource<bool>();
            var cts = new CancellationTokenSource();
            var mutexName = DesktopBuildClient.GetServerMutexName(pipeName);
            var thread = new Thread(_ =>
            {
                var listener = new TestableDiagnosticListener();
                listener.Listening += (sender, e) => { serverListenSource.TrySetResult(true); };
                try
                {
                    clientConnectionHost = clientConnectionHost ?? new NamedPipeClientConnectionHost(compilerServerHost, pipeName);

                    DesktopBuildServerController.RunServer(
                        pipeName,
                        clientConnectionHost,
                        listener,
                        timeout ?? TimeSpan.FromMilliseconds(-1),
                        cts.Token);
                }
                finally
                {
                    var serverStats = new ServerStats(connections: listener.ConnectionCount, completedConnections: listener.CompletedCount);
                    serverStatsSource.SetResult(serverStats);
                }
            });

            thread.Start();

            // The contract of this function is that it will return once the server has started.  Spin here until
            // we can verify the server has started or simply failed to start.
            while (DesktopBuildClient.WasServerMutexOpen(mutexName) != true && thread.IsAlive)
            {
                Thread.Yield();
            }

            return new ServerData(cts, pipeName, serverStatsSource.Task, serverListenSource.Task);
        }
Example #27
0
 internal TcpClientConnectionHost(ICompilerServerHost compilerServerHost, IPEndPoint endPoint)
 {
     _compilerServerHost = compilerServerHost;
     _listener           = new TcpListener(endPoint);
     _listener.Start();
 }
 internal CompilerRequestHandler(ICompilerServerHost compilerServerHost, string clientDirectory)
 {
     _compilerServerHost = compilerServerHost;
     _clientDirectory = clientDirectory;
 }
 internal TcpClientConnection(ICompilerServerHost compilerServerHost, TcpClient client, string loggingIdentifier) : base(compilerServerHost, loggingIdentifier, client.GetStream())
 {
     _client = client;
 }
Example #30
0
 internal VisualBasicCompilerServer(ICompilerServerHost compilerServerHost, string[] args, string clientDirectory, string baseDirectory, string sdkDirectory, string libDirectory, IAnalyzerAssemblyLoader analyzerLoader)
     : base(VisualBasicCommandLineParser.Default, clientDirectory != null ? Path.Combine(clientDirectory, ResponseFileName) : null, args, clientDirectory, baseDirectory, sdkDirectory, libDirectory, analyzerLoader)
 {
     _compilerServerHost = compilerServerHost;
 }
Example #31
0
 private static TestableClientConnection CreateConnection(Stream stream, ICompilerServerHost compilerServerHost = null)
 {
     compilerServerHost = compilerServerHost ?? new Mock <ICompilerServerHost>().Object;
     return(new TestableClientConnection(compilerServerHost, stream));
 }
Example #32
0
 internal TestableClientConnection(ICompilerServerHost compilerServerHost, Stream stream)
     :base(compilerServerHost, "identifier", stream)
 {
     Stream = stream;
     CreateMonitorDisconnectTaskFunc = ct => Task.Delay(-1, ct);
 }
 internal NamedPipeClientConnectionHost(ICompilerServerHost compilerServerHost, string pipeName)
 {
     _compilerServerHost = compilerServerHost;
     _pipeName = pipeName;
 }
Example #34
0
 internal ServerDispatcher(ICompilerServerHost compilerServerHost, IRequestHandler handler, IDiagnosticListener diagnosticListener)
 {
     _compilerServerHost = compilerServerHost;
     _handler = handler;
     _diagnosticListener = diagnosticListener;
 }