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); }
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); }