Skip to content

Commit 44aee88

Browse files
committed
Redesign message I/O to use byte streams
This change redesigns the existing MessageReader and MessageWriter classes to read/write messages using Streams instead of TextReader/TextWriter classes. This makes the host process compatible with VS Code's latest debugger client so that messages sent from it can be read correctly. It also sets us up for being able to communicate through other types of streams such as sockets.
1 parent 28e9669 commit 44aee88

31 files changed

+411
-225
lines changed

src/PowerShellEditorServices.Host/MessageLoop.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Microsoft.PowerShell.EditorServices.Transport.Stdio.Response;
1313
using Nito.AsyncEx;
1414
using System;
15+
using System.IO;
1516
using System.Management.Automation;
1617
using System.Reflection;
1718
using System.Text;
@@ -24,6 +25,8 @@ public class MessageLoop : IHost
2425
{
2526
#region Private Fields
2627

28+
private Stream inputStream;
29+
private Stream outputStream;
2730
private IConsoleHost consoleHost;
2831
private EditorSession editorSession;
2932
private MessageReader messageReader;
@@ -75,17 +78,19 @@ async Task ListenForMessages()
7578
MessageTypeResolver messageTypeResolver = new MessageTypeResolver();
7679
messageTypeResolver.ScanForMessageTypes(typeof(StartedEvent).Assembly);
7780

81+
// Open the standard input/output streams
82+
this.inputStream = System.Console.OpenStandardInput();
83+
this.outputStream = System.Console.OpenStandardOutput();
84+
7885
// Set up the reader and writer
7986
this.messageReader =
8087
new MessageReader(
81-
System.Console.In,
82-
MessageFormat.WithContentLength,
88+
this.inputStream,
8389
messageTypeResolver);
8490

8591
this.messageWriter =
8692
new MessageWriter(
87-
System.Console.Out,
88-
MessageFormat.WithContentLength,
93+
this.outputStream,
8994
messageTypeResolver);
9095

9196
// Set up the console host which will send events
@@ -102,7 +107,7 @@ async Task ListenForMessages()
102107
this.editorSession.DebugService.DebuggerStopped += DebugService_DebuggerStopped;
103108

104109
// Send a "started" event
105-
this.messageWriter.WriteMessage(
110+
await this.messageWriter.WriteMessage(
106111
new StartedEvent());
107112

108113
// Run the message loop
@@ -120,7 +125,7 @@ async Task ListenForMessages()
120125
{
121126
// Write an error response
122127
this.messageWriter.WriteMessage(
123-
MessageErrorResponse.CreateParseErrorResponse(e));
128+
MessageErrorResponse.CreateParseErrorResponse(e)).Wait();
124129

125130
// Continue the loop
126131
continue;
@@ -141,7 +146,7 @@ await messageProcessor.ProcessMessage(
141146
if (newMessage != null)
142147
{
143148
// Return an error response to keep the client moving
144-
this.messageWriter.WriteMessage(
149+
await this.messageWriter.WriteMessage(
145150
MessageErrorResponse.CreateUnhandledMessageResponse(
146151
newMessage));
147152
}
@@ -154,9 +159,9 @@ await messageProcessor.ProcessMessage(
154159
}
155160
}
156161

157-
void DebugService_DebuggerStopped(object sender, DebuggerStopEventArgs e)
162+
async void DebugService_DebuggerStopped(object sender, DebuggerStopEventArgs e)
158163
{
159-
this.messageWriter.WriteMessage(
164+
await this.messageWriter.WriteMessage(
160165
new StoppedEvent
161166
{
162167
Body = new StoppedEventBody
@@ -177,11 +182,11 @@ void PowerShellSession_BreakpointUpdated(object sender, BreakpointUpdatedEventAr
177182
{
178183
}
179184

180-
void PowerShellSession_OutputWritten(object sender, OutputWrittenEventArgs e)
185+
async void PowerShellSession_OutputWritten(object sender, OutputWrittenEventArgs e)
181186
{
182187
// TODO: change this to use the OutputEvent!
183188

184-
this.messageWriter.WriteMessage(
189+
await this.messageWriter.WriteMessage(
185190
new ReplWriteOutputEvent
186191
{
187192
Body = new ReplWriteOutputEventBody

src/PowerShellEditorServices.Host/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ static void Main(string[] args)
2727
"/waitForDebugger",
2828
StringComparison.InvariantCultureIgnoreCase));
2929

30+
waitForDebugger = true;
31+
3032
// Should we wait for the debugger before starting?
3133
if (waitForDebugger)
3234
{

src/PowerShellEditorServices.Transport.Stdio/Constants.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace Microsoft.PowerShell.EditorServices.Transport.Stdio
1616
{
1717
public static class Constants
1818
{
19-
public const string ContentLengthString = "Content-Length: ";
19+
public const string ContentLengthFormatString = "Content-Length: {0}\r\n\r\n";
2020
public static readonly JsonSerializerSettings JsonSerializerSettings;
2121

2222
static Constants()

src/PowerShellEditorServices.Transport.Stdio/Message/MessageFormat.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)