Skip to content
This repository was archived by the owner on Apr 8, 2020. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ internal class SocketNodeInstance : OutOfProcessNodeInstance
TypeNameHandling = TypeNameHandling.None
};

private readonly static int streamBufferSize = 16 * 1024;
private readonly static UTF8Encoding utf8EncodingWithoutBom = new UTF8Encoding(false);

private readonly SemaphoreSlim _connectionCreationSemaphore = new SemaphoreSlim(1);
private bool _connectionHasFailed;
private StreamConnection _physicalConnection;
Expand Down Expand Up @@ -89,7 +92,7 @@ protected override async Task<T> InvokeExportAsync<T>(NodeInvocationInfo invocat
virtualConnection = _virtualConnectionClient.OpenVirtualConnection();

// Send request
await WriteJsonLineAsync(virtualConnection, invocationInfo, cancellationToken);
WriteJsonLine(virtualConnection, invocationInfo);

// Determine what kind of response format is expected
if (typeof(T) == typeof(Stream))
Expand Down Expand Up @@ -169,11 +172,20 @@ protected override void Dispose(bool disposing)
base.Dispose(disposing);
}

private static async Task WriteJsonLineAsync(Stream stream, object serializableObject, CancellationToken cancellationToken)
private static void WriteJsonLine(Stream stream, object serializableObject)
{
var json = JsonConvert.SerializeObject(serializableObject, jsonSerializerSettings);
var bytes = Encoding.UTF8.GetBytes(json + '\n');
await stream.WriteAsync(bytes, 0, bytes.Length, cancellationToken);
using (var streamWriter = new StreamWriter(stream, utf8EncodingWithoutBom, streamBufferSize, true))
using (var jsonWriter = new JsonTextWriter(streamWriter))
{
jsonWriter.CloseOutput = false;

var serializer = JsonSerializer.Create(jsonSerializerSettings);
serializer.Serialize(jsonWriter, serializableObject);
jsonWriter.Flush();

streamWriter.WriteLine();
streamWriter.Flush();
}
}

private static async Task<T> ReadJsonAsync<T>(Stream stream, CancellationToken cancellationToken)
Expand All @@ -184,7 +196,7 @@ private static async Task<T> ReadJsonAsync<T>(Stream stream, CancellationToken c

private static async Task<byte[]> ReadAllBytesAsync(Stream input, CancellationToken cancellationToken)
{
byte[] buffer = new byte[16 * 1024];
byte[] buffer = new byte[streamBufferSize];

using (var ms = new MemoryStream())
{
Expand Down