-
-
Notifications
You must be signed in to change notification settings - Fork 884
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new Roslyn server support for C# #4279
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
param ( | ||
[string]$ServerName = ".", | ||
[Parameter(Mandatory=$true)][string]$PipeName | ||
) | ||
|
||
# Use named pipe as stdin/out | ||
|
||
$Source = @" | ||
using System; | ||
using System.Text; | ||
using System.IO; | ||
using System.IO.Pipes; | ||
using System.Threading.Tasks; | ||
|
||
public static class StdPipe | ||
{ | ||
public static void RouteToPipe(string pipeServer, string pipeName) | ||
{ | ||
var pipeClient = new NamedPipeClientStream(pipeServer, pipeName, PipeDirection.InOut, PipeOptions.Asynchronous); | ||
pipeClient.Connect(); | ||
|
||
var pipeReader = new BufferedStream(pipeClient); | ||
var pipeWriter = new BufferedStream(pipeClient); | ||
|
||
var stdin = new BufferedStream(Console.OpenStandardInput()); | ||
var stdout = new BufferedStream(Console.OpenStandardOutput()); | ||
|
||
var tasks = new Task<byte[]>[2] | ||
{ | ||
ReadHeaderDelimitedAsync(pipeReader), | ||
ReadHeaderDelimitedAsync(stdin) | ||
}; | ||
|
||
while (true) | ||
{ | ||
var doneIdx = Task.WaitAny(tasks); | ||
|
||
var bytesRead = tasks[doneIdx].Result; | ||
if (doneIdx == 0) | ||
{ | ||
// pipe in -> stdout | ||
if (bytesRead.Length == 0) | ||
{ | ||
// pipe was closed | ||
break; | ||
} | ||
|
||
stdout.Write(bytesRead, 0, bytesRead.Length); | ||
stdout.Flush(); | ||
tasks[doneIdx] = ReadHeaderDelimitedAsync(pipeReader); | ||
} | ||
else | ||
{ | ||
// stdin -> pipe out | ||
pipeWriter.Write(bytesRead, 0, bytesRead.Length); | ||
pipeWriter.Flush(); | ||
tasks[doneIdx] = ReadHeaderDelimitedAsync(stdin); | ||
} | ||
} | ||
} | ||
|
||
private static async Task<byte[]> ReadHeaderDelimitedAsync(Stream stream) | ||
{ | ||
// Assigning new tasks with this function blocks the thread | ||
// unless this is awaited first. | ||
await Task.Yield(); | ||
|
||
var idx = 0; | ||
var header = new byte[64]; | ||
int b = 0; | ||
do | ||
{ | ||
var bytesRead = await stream.ReadAsync(header, idx, 1); | ||
if (bytesRead == 0) | ||
continue; | ||
b = header[idx++]; | ||
} while (b != '\r'); | ||
|
||
var colonPos = Array.IndexOf(header, (byte)':'); | ||
if (colonPos == -1) | ||
{ | ||
return new byte[0]; | ||
} | ||
|
||
var headerName = new byte[colonPos]; | ||
Array.Copy(header, headerName, colonPos); | ||
var headerContent = new byte[idx - colonPos - 1]; | ||
Array.Copy(header, colonPos + 2, headerContent, 0, headerContent.Length - 2); | ||
|
||
if (Encoding.ASCII.GetString(headerName) != "Content-Length") | ||
{ | ||
return new byte[0]; | ||
} | ||
if (headerContent.Length > 20) | ||
{ | ||
return new byte[0]; | ||
} | ||
int contentLength; | ||
try | ||
{ | ||
contentLength = int.Parse(Encoding.ASCII.GetString(headerContent)); | ||
} | ||
catch (Exception) | ||
{ | ||
return new byte[0]; | ||
} | ||
|
||
var buffer = new byte[contentLength + idx + 3]; | ||
var c = 0; | ||
for (var i = 0; i < idx; i++) | ||
{ | ||
buffer[c++] = header[i]; | ||
} | ||
|
||
// LF, CRLF | ||
var bytesToRead = contentLength + 3; | ||
while (bytesToRead > 0) | ||
{ | ||
var bytesRead = await stream.ReadAsync(buffer, c, bytesToRead); | ||
bytesToRead -= bytesRead; | ||
c += bytesRead; | ||
} | ||
|
||
return buffer; | ||
} | ||
} | ||
"@ | ||
|
||
Add-Type -TypeDefinition $Source -Language CSharp | ||
|
||
try { | ||
[StdPipe]::RouteToPipe($ServerName, $PipeName) | ||
} catch [System.AggregateException] { | ||
Write-Error $error[0].exception.innerexception | ||
throw $error[0].exception.innerexception | ||
} | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this file for? 🤔 Can you explain it? Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This server only supports connecting via named pipes and has no stdin option. However, Emacs does not natively support connecting via named pipes on Windows, so this script is meant as a shim to convert the named pipe I/O to stdin I/O.