Files
sqltoolsservice/src/Microsoft.SqlTools.Hosting/Hosting/Protocol/Channel/StdioClientChannel.cs
Karl Burtram 0af7bef66d Create MS.SqlTools.Credentials project (#249)
* Initial credential service files

* Clean-up hostloader

* Update build scripts to build credentials archive

* Move hosting files to new assembly

* Add credentials files to MS.SqlTools.Credentials

* Remove duplicate files

* Update namespace in program.cs

* Fix test build breaks

* Update extensions visibility.

* Remove unused resource strings

* Add xproj files to SLN for appveyor builds

* Fix appveyor build break in test project

* Fix extensibility tests

* Fix various typos in latest iteration

* Add settings for Integration build

* Fix codecoverage.bat to use full pdb for new projects

* Fix bug when packing in folder with native images

* Fix typos in xproj

* Reset XLF to fix build.cmd
2017-02-23 16:09:58 -08:00

127 lines
4.0 KiB
C#

//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol.Serializers;
namespace Microsoft.SqlTools.ServiceLayer.Hosting.Protocol.Channel
{
/// <summary>
/// Provides a client implementation for the standard I/O channel.
/// Launches the server process and then attaches to its console
/// streams.
/// </summary>
public class StdioClientChannel : ChannelBase
{
private string serviceProcessPath;
private string serviceProcessArguments;
private Stream inputStream;
private Stream outputStream;
private Process serviceProcess;
/// <summary>
/// Gets the process ID of the server process.
/// </summary>
public int ProcessId { get; private set; }
/// <summary>
/// Initializes an instance of the StdioClient.
/// </summary>
/// <param name="serverProcessPath">The full path to the server process executable.</param>
/// <param name="serverProcessArguments">Optional arguments to pass to the service process executable.</param>
public StdioClientChannel(
string serverProcessPath,
params string[] serverProcessArguments)
{
this.serviceProcessPath = serverProcessPath;
if (serverProcessArguments != null)
{
this.serviceProcessArguments =
string.Join(
" ",
serverProcessArguments);
}
}
protected override void Initialize(IMessageSerializer messageSerializer)
{
this.serviceProcess = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = this.serviceProcessPath,
Arguments = this.serviceProcessArguments,
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
StandardOutputEncoding = Encoding.UTF8,
},
EnableRaisingEvents = true,
};
// Start the process
this.serviceProcess.Start();
this.ProcessId = this.serviceProcess.Id;
// Open the standard input/output streams
this.inputStream = this.serviceProcess.StandardOutput.BaseStream;
this.outputStream = this.serviceProcess.StandardInput.BaseStream;
// Set up the message reader and writer
this.MessageReader =
new MessageReader(
this.inputStream,
messageSerializer);
this.MessageWriter =
new MessageWriter(
this.outputStream,
messageSerializer);
this.IsConnected = true;
}
public override Task WaitForConnection()
{
// We're always connected immediately in the stdio channel
return Task.FromResult(true);
}
protected override void Shutdown()
{
if (this.inputStream != null)
{
this.inputStream.Dispose();
this.inputStream = null;
}
if (this.outputStream != null)
{
this.outputStream.Dispose();
this.outputStream = null;
}
if (this.MessageReader != null)
{
this.MessageReader = null;
}
if (this.MessageWriter != null)
{
this.MessageWriter = null;
}
this.serviceProcess.Kill();
}
}
}