Skip to content

Commit d843b7f

Browse files
committed
feat: 0.2.0 - Update to process plugin schema version 3.
1 parent 6ce4b7c commit d843b7f

7 files changed

+215
-98
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System.Text;
2+
3+
namespace Dprint.Plugins.Roslyn.Communication
4+
{
5+
public abstract class MessagePart
6+
{
7+
public abstract byte[] GetBytes();
8+
9+
public string IntoString()
10+
{
11+
return Encoding.UTF8.GetString(GetBytes());
12+
}
13+
14+
public static MessagePart FromString(string text)
15+
{
16+
return new VariableMessagePart(Encoding.UTF8.GetBytes(text));
17+
}
18+
19+
public static MessagePart FromInt(int value)
20+
{
21+
return new IntegerMessagePart(value);
22+
}
23+
}
24+
25+
internal class VariableMessagePart : MessagePart
26+
{
27+
public VariableMessagePart(byte[] data)
28+
{
29+
Data = data;
30+
}
31+
32+
public byte[] Data { get; }
33+
34+
public override byte[] GetBytes() => Data;
35+
}
36+
37+
internal class IntegerMessagePart : MessagePart
38+
{
39+
public IntegerMessagePart(int value)
40+
{
41+
Value = value;
42+
}
43+
44+
public int Value { get; }
45+
46+
public override byte[] GetBytes() => BigEndianBitConverter.GetBytes((uint)Value);
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace Dprint.Plugins.Roslyn.Communication
6+
{
7+
public class StdIoMessenger
8+
{
9+
private readonly StdIoReaderWriter _readerWriter;
10+
11+
public StdIoMessenger(StdIoReaderWriter readerWriter)
12+
{
13+
_readerWriter = readerWriter;
14+
}
15+
16+
public int ReadCode()
17+
{
18+
return _readerWriter.ReadInt();
19+
}
20+
21+
public List<MessagePart> ReadMultiPartMessage(int partCount)
22+
{
23+
var parts = new List<MessagePart>(partCount);
24+
for (var i = 0; i < partCount; i++)
25+
parts.Add(new VariableMessagePart(_readerWriter.ReadVariableData()));
26+
_readerWriter.ReadSuccessBytes();
27+
return parts;
28+
}
29+
30+
public MessagePart ReadSinglePartMessage()
31+
{
32+
return ReadMultiPartMessage(1).Single();
33+
}
34+
35+
public void ReadZeroPartMessage()
36+
{
37+
_readerWriter.ReadSuccessBytes();
38+
}
39+
40+
public void SendMessage(int code, params MessagePart[] parts)
41+
{
42+
try
43+
{
44+
_readerWriter.SendInt(code);
45+
foreach (var part in parts)
46+
{
47+
switch (part)
48+
{
49+
case VariableMessagePart variablePart:
50+
_readerWriter.SendVariableWidth(variablePart.Data);
51+
break;
52+
case IntegerMessagePart intPart:
53+
_readerWriter.SendInt(intPart.Value);
54+
break;
55+
default:
56+
throw new NotImplementedException($"Not implemented message part: {part.GetType()}");
57+
}
58+
}
59+
_readerWriter.SendSuccessBytes();
60+
}
61+
catch (Exception ex)
62+
{
63+
Console.Error.Write($"Catastrophic error sending message: {ex.Message}");
64+
Environment.Exit(1); // exit the process... can't send back invalid data at this point
65+
}
66+
}
67+
}
68+
}

DprintPluginRoslyn/Communication/StdInOutReaderWriter.cs DprintPluginRoslyn/Communication/StdIoReaderWriter.cs

+29-20
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
namespace Dprint.Plugins.Roslyn.Communication
66
{
7-
public class StdInOutReaderWriter : IDisposable
7+
public class StdIoReaderWriter : IDisposable
88
{
99
private readonly Stream _stdin;
1010
private readonly Stream _stdout;
1111
private readonly int _bufferSize = 1024;
1212

13-
public StdInOutReaderWriter()
13+
public StdIoReaderWriter()
1414
{
1515
_stdin = Console.OpenStandardInput();
1616
_stdout = Console.OpenStandardOutput();
@@ -22,58 +22,61 @@ public void Dispose()
2222
_stdout.Dispose();
2323
}
2424

25-
public int ReadMessageKind()
26-
{
27-
return ReadInt();
28-
}
29-
30-
private int ReadInt()
25+
public int ReadInt()
3126
{
3227
byte[] buffer = new byte[4];
3328
ReadStdIn(buffer, 0, buffer.Length);
3429
return (int)BigEndianBitConverter.GetUInt(buffer);
3530
}
3631

37-
public string ReadMessagePartAsString()
32+
public void ReadSuccessBytes()
3833
{
39-
return Encoding.UTF8.GetString(ReadMessagePart());
34+
byte[] buffer = new byte[4];
35+
ReadStdIn(buffer, 0, buffer.Length);
36+
for (var i = 0; i < buffer.Length; i++)
37+
{
38+
if (buffer[i] != 255)
39+
{
40+
Console.Error.Write($"Catastrophic error. Did not find success bytes. Instead found: [{string.Join(", ", buffer)}]");
41+
Environment.Exit(1);
42+
}
43+
}
4044
}
4145

42-
public byte[] ReadMessagePart()
46+
public byte[] ReadVariableData()
4347
{
4448
var size = ReadInt();
45-
var messageData = new byte[size];
49+
var variableData = new byte[size];
4650

4751
if (size > 0)
4852
{
4953
// read the first part of the message part
50-
ReadStdIn(messageData, 0, Math.Min(_bufferSize, size));
54+
ReadStdIn(variableData, 0, Math.Min(_bufferSize, size));
5155

5256
var index = _bufferSize;
5357
while (index < size)
5458
{
5559
// send "ready" to the client
5660
SendInt(0);
57-
_stdout.Flush();
5861

5962
// read from buffer
60-
ReadStdIn(messageData, index, Math.Min(size - index, _bufferSize));
63+
ReadStdIn(variableData, index, Math.Min(size - index, _bufferSize));
6164
index += _bufferSize;
6265
}
6366
}
6467

65-
return messageData;
68+
return variableData;
6669
}
6770

68-
public void SendMessageKind(int messageKind)
71+
public void SendInt(int messageKind)
6972
{
70-
SendInt(messageKind);
73+
RawSendInt(messageKind);
7174
_stdout.Flush();
7275
}
7376

7477
public void SendVariableWidth(byte[] data)
7578
{
76-
SendInt(data.Length);
79+
RawSendInt(data.Length);
7780
WriteStdOut(data, 0, Math.Min(data.Length, _bufferSize));
7881
_stdout.Flush();
7982

@@ -90,7 +93,13 @@ public void SendVariableWidth(byte[] data)
9093
}
9194
}
9295

93-
public void SendInt(int value)
96+
public void SendSuccessBytes()
97+
{
98+
var successBytes = new byte[] { 255, 255, 255, 255 };
99+
WriteStdOut(successBytes, 0, 4);
100+
}
101+
102+
private void RawSendInt(int value)
94103
{
95104
var bytes = BigEndianBitConverter.GetBytes((uint)value);
96105
WriteStdOut(bytes, 0, bytes.Length);

DprintPluginRoslyn/DprintPluginRoslyn.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
<RootNamespace>Dprint.Plugins.Roslyn</RootNamespace>
77
<AssemblyName>dprint-plugin-roslyn</AssemblyName>
88
<Nullable>enable</Nullable>
9-
<Version>0.1.0</Version>
9+
<Version>0.2.0</Version>
1010
<Authors>David Sherret</Authors>
1111
<Company>Dprint Code Formatting</Company>
1212
</PropertyGroup>
1313

1414
<ItemGroup>
15-
<None Remove="LICENSE" />
15+
<EmbeddedResource Include="../LICENSE" />
1616
</ItemGroup>
1717

1818
<ItemGroup>

0 commit comments

Comments
 (0)