-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Protocol: support tunneling the connection over a Stream. (#301)
- Loading branch information
Showing
8 changed files
with
238 additions
and
10 deletions.
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
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
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,86 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#if NETSTANDARD2_0 | ||
|
||
namespace System.IO | ||
{ | ||
internal static class StreamExtensions | ||
{ | ||
public static ValueTask<int> ReadAsync(this Stream stream, Memory<byte> buffer, CancellationToken cancellationToken = default) | ||
{ | ||
if (MemoryMarshal.TryGetArray(buffer, out ArraySegment<byte> array)) | ||
{ | ||
return new ValueTask<int>(stream.ReadAsync(array.Array, array.Offset, array.Count, cancellationToken)); | ||
} | ||
else | ||
{ | ||
byte[] sharedBuffer = ArrayPool<byte>.Shared.Rent(buffer.Length); | ||
return FinishReadAsync(stream.ReadAsync(sharedBuffer, 0, buffer.Length, cancellationToken), sharedBuffer, buffer); | ||
|
||
static async ValueTask<int> FinishReadAsync(Task<int> readTask, byte[] localBuffer, Memory<byte> localDestination) | ||
{ | ||
try | ||
{ | ||
int result = await readTask.ConfigureAwait(false); | ||
new Span<byte>(localBuffer, 0, result).CopyTo(localDestination.Span); | ||
return result; | ||
} | ||
finally | ||
{ | ||
ArrayPool<byte>.Shared.Return(localBuffer); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public static void Write(this Stream stream, ReadOnlyMemory<byte> buffer) | ||
{ | ||
if (MemoryMarshal.TryGetArray(buffer, out ArraySegment<byte> array)) | ||
{ | ||
stream.Write(array.Array, array.Offset, array.Count); | ||
} | ||
else | ||
{ | ||
byte[] sharedBuffer = ArrayPool<byte>.Shared.Rent(buffer.Length); | ||
try | ||
{ | ||
buffer.Span.CopyTo(sharedBuffer); | ||
stream.Write(sharedBuffer, 0, buffer.Length); | ||
} | ||
finally | ||
{ | ||
ArrayPool<byte>.Shared.Return(sharedBuffer); | ||
} | ||
} | ||
} | ||
|
||
public static ValueTask WriteAsync(this Stream stream, ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default) | ||
{ | ||
if (MemoryMarshal.TryGetArray(buffer, out ArraySegment<byte> array)) | ||
{ | ||
return new ValueTask(stream.WriteAsync(array.Array, array.Offset, array.Count, cancellationToken)); | ||
} | ||
else | ||
{ | ||
byte[] sharedBuffer = ArrayPool<byte>.Shared.Rent(buffer.Length); | ||
buffer.Span.CopyTo(sharedBuffer); | ||
return new ValueTask(FinishWriteAsync(stream.WriteAsync(sharedBuffer, 0, buffer.Length, cancellationToken), sharedBuffer)); | ||
} | ||
} | ||
|
||
private static async Task FinishWriteAsync(Task writeTask, byte[] localBuffer) | ||
{ | ||
try | ||
{ | ||
await writeTask.ConfigureAwait(false); | ||
} | ||
finally | ||
{ | ||
ArrayPool<byte>.Shared.Return(localBuffer); | ||
} | ||
} | ||
} | ||
} | ||
|
||
#endif |
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