Convert to netcoreapp2.0 (#338)

* Converted samples and test projects to run on netcoreapp2.0
This commit is contained in:
BrennanConroy 2017-03-25 09:37:39 -07:00 committed by David Fowler
parent 430b8a3686
commit 6130003193
22 changed files with 91 additions and 58 deletions

View File

@ -9,6 +9,7 @@
<MoqVersion>4.7.1</MoqVersion>
<NetStandardImplicitPackageVersion>1.6.1</NetStandardImplicitPackageVersion>
<RedisVersion>1.1.605</RedisVersion>
<RuntimeFrameworkVersion>2.0.0-*</RuntimeFrameworkVersion>
<TestSdkVersion>15.0.0</TestSdkVersion>
<XunitVersion>2.2.0</XunitVersion>
</PropertyGroup>

View File

@ -3,7 +3,7 @@
<Import Project="..\..\build\common.props" />
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>

View File

@ -3,7 +3,7 @@
<Import Project="..\..\build\dependencies.props" />
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<TargetFramework>netcoreapp2.0</TargetFramework>
<UserSecretsId>aspnet-ChatSample-f11cf018-e0a8-49fa-b749-4c0eb5c9150b</UserSecretsId>
<PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8</PackageTargetFallback>
<!-- Don't create a NuGet package -->

View File

@ -3,7 +3,7 @@
<Import Project="..\..\build\dependencies.props" />
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<TargetFramework>netcoreapp2.0</TargetFramework>
<!-- Don't create a NuGet package -->
<IsPackable>false</IsPackable>
</PropertyGroup>

View File

@ -3,7 +3,7 @@
<Import Project="..\..\build\dependencies.props" />
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<TargetFramework>netcoreapp2.0</TargetFramework>
<!-- Don't create a NuGet package -->
<IsPackable>false</IsPackable>
</PropertyGroup>

View File

@ -3,8 +3,8 @@
<Import Project="..\..\build\dependencies.props" />
<PropertyGroup>
<TargetFrameworks>netcoreapp1.1;net46</TargetFrameworks>
<PackageTargetFallback Condition=" '$(TargetFramework)' == 'netcoreapp1.1' ">$(PackageTargetFallback);portable-net45+win8+wp8+wpa81</PackageTargetFallback>
<TargetFrameworks>netcoreapp2.0;net46</TargetFrameworks>
<PackageTargetFallback Condition=" '$(TargetFramework)' == 'netcoreapp2.0' ">$(PackageTargetFallback);portable-net45+win8+wp8+wpa81</PackageTargetFallback>
<!-- Don't create a NuGet package -->
<IsPackable>false</IsPackable>
</PropertyGroup>

View File

@ -3,7 +3,7 @@
<Import Project="..\..\build\dependencies.props" />
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<TargetFramework>netcoreapp2.0</TargetFramework>
<OutputType>Exe</OutputType>
<!-- Don't create a NuGet package -->
<IsPackable>false</IsPackable>

View File

@ -20,19 +20,28 @@ namespace Microsoft.AspNetCore.Sockets.Internal.Formatters
{
if (_state.Length == null)
{
var length = buffer.TryReadBytes(sizeof(long))?.ToSingleSpan();
if (length == null || length.Value.Length < sizeof(long))
var lengthBuffer = buffer.TryReadBytes(sizeof(long));
if (lengthBuffer == null)
{
message = default(Message);
return false;
}
var longLength = length.Value.ReadBigEndian<long>();
var length = lengthBuffer.Value.ToSingleSpan();
if (length.Length < sizeof(long))
{
message = default(Message);
return false;
}
var longLength = length.ReadBigEndian<long>();
if (longLength > Int32.MaxValue)
{
throw new FormatException("Messages over 2GB in size are not supported");
}
buffer.Advance(length.Value.Length);
buffer.Advance(length.Length);
_state.Length = (int)longLength;
}

View File

@ -11,8 +11,8 @@ namespace Microsoft.AspNetCore.Sockets.Internal.Formatters
{
public static class ServerSentEventsMessageFormatter
{
private static readonly Span<byte> DataPrefix = new byte[] { (byte)'d', (byte)'a', (byte)'t', (byte)'a', (byte)':', (byte)' ' };
private static readonly Span<byte> Newline = new byte[] { (byte)'\r', (byte)'\n' };
private static readonly byte[] DataPrefix = new byte[] { (byte)'d', (byte)'a', (byte)'t', (byte)'a', (byte)':', (byte)' ' };
private static readonly byte[] Newline = new byte[] { (byte)'\r', (byte)'\n' };
private const byte LineFeed = (byte)'\n';
private const char TextTypeFlag = 'T';

View File

@ -86,17 +86,20 @@ namespace Microsoft.AspNetCore.Sockets.Internal.Formatters
private bool TryReadLength(ref BytesReader buffer)
{
// Read until the first ':' to find the length
var lengthSpan = buffer.ReadBytesUntil((byte)TextMessageFormatter.FieldDelimiter)?.ToSingleSpan();
if (lengthSpan == null)
var lengthBuffer = buffer.ReadBytesUntil((byte)TextMessageFormatter.FieldDelimiter);
if (lengthBuffer == null)
{
// Insufficient data
return false;
}
var lengthSpan = lengthBuffer.Value.ToSingleSpan();
// Parse the length
if (!PrimitiveParser.TryParseInt32(lengthSpan.Value, out var length, out var consumedByLength, encoder: TextEncoder.Utf8) || consumedByLength < lengthSpan.Value.Length)
if (!PrimitiveParser.TryParseInt32(lengthSpan, out var length, out var consumedByLength, encoder: TextEncoder.Utf8) || consumedByLength < lengthSpan.Length)
{
if (TextEncoder.Utf8.TryDecode(lengthSpan.Value, out var lengthString, out _))
if (TextEncoder.Utf8.TryDecode(lengthSpan, out var lengthString, out _))
{
throw new FormatException($"Invalid length: '{lengthString}'");
}

View File

@ -583,8 +583,10 @@ namespace Microsoft.Extensions.WebSockets.Internal
return closeResult;
}
private static void PingPayloadWriter(WritableBuffer output, Span<byte> maskingKey, int payloadLength, DateTime timestamp)
private static unsafe void PingPayloadWriter(WritableBuffer output, uint maskingKeyValue, int payloadLength, DateTime timestamp)
{
var maskingKey = new Span<byte>(&maskingKeyValue, sizeof(uint));
var payload = output.Buffer.Slice(0, payloadLength);
// TODO: Don't put this string on the heap? Is there a way to do that without re-implementing ToString?
@ -612,8 +614,10 @@ namespace Microsoft.Extensions.WebSockets.Internal
output.Advance(payloadLength);
}
private static void CloseResultPayloadWriter(WritableBuffer output, Span<byte> maskingKey, int payloadLength, WebSocketCloseResult result)
private static unsafe void CloseResultPayloadWriter(WritableBuffer output, uint maskingKeyValue, int payloadLength, WebSocketCloseResult result)
{
var maskingKey = new Span<byte>(&maskingKeyValue, sizeof(uint));
// Write the close payload out
var payload = output.Buffer.Slice(0, payloadLength).Span;
result.WriteTo(ref output);
@ -624,8 +628,10 @@ namespace Microsoft.Extensions.WebSockets.Internal
}
}
private static void AppendPayloadWriter(WritableBuffer output, Span<byte> maskingKey, int payloadLength, ReadableBuffer payload)
private static unsafe void AppendPayloadWriter(WritableBuffer output, uint maskingKeyValue, int payloadLength, ReadableBuffer payload)
{
var maskingKey = new Span<byte>(&maskingKeyValue, sizeof(uint));
if (maskingKey.Length > 0)
{
// Mask the payload in it's own buffer
@ -635,7 +641,7 @@ namespace Microsoft.Extensions.WebSockets.Internal
output.Append(payload);
}
private Task SendCoreAsync<T>(bool fin, WebSocketOpcode opcode, int payloadAllocLength, int payloadLength, Action<WritableBuffer, Span<byte>, int, T> payloadWriter, T payload, CancellationToken cancellationToken)
private Task SendCoreAsync<T>(bool fin, WebSocketOpcode opcode, int payloadAllocLength, int payloadLength, Action<WritableBuffer, uint, int, T> payloadWriter, T payload, CancellationToken cancellationToken)
{
if (_sendLock.Wait(0))
{
@ -647,13 +653,13 @@ namespace Microsoft.Extensions.WebSockets.Internal
}
}
private async Task SendCoreWaitForLockAsync<T>(bool fin, WebSocketOpcode opcode, int payloadAllocLength, int payloadLength, Action<WritableBuffer, Span<byte>, int, T> payloadWriter, T payload, CancellationToken cancellationToken)
private async Task SendCoreWaitForLockAsync<T>(bool fin, WebSocketOpcode opcode, int payloadAllocLength, int payloadLength, Action<WritableBuffer, uint, int, T> payloadWriter, T payload, CancellationToken cancellationToken)
{
await _sendLock.WaitAsync(cancellationToken);
await SendCoreLockAcquiredAsync(fin, opcode, payloadAllocLength, payloadLength, payloadWriter, payload, cancellationToken);
}
private async Task SendCoreLockAcquiredAsync<T>(bool fin, WebSocketOpcode opcode, int payloadAllocLength, int payloadLength, Action<WritableBuffer, Span<byte>, int, T> payloadWriter, T payload, CancellationToken cancellationToken)
private async Task SendCoreLockAcquiredAsync<T>(bool fin, WebSocketOpcode opcode, int payloadAllocLength, int payloadLength, Action<WritableBuffer, uint, int, T> payloadWriter, T payload, CancellationToken cancellationToken)
{
try
{
@ -679,17 +685,7 @@ namespace Microsoft.Extensions.WebSockets.Internal
// Write the length and mask flag
WritePayloadLength(payloadLength, buffer);
var maskingKey = Span<byte>.Empty;
if (_maskingKeyBuffer != null)
{
// Get a span of the output buffer for the masking key, write it there, then advance the write head.
maskingKey = buffer.Buffer.Slice(0, 4).Span;
WriteMaskingKey(maskingKey);
buffer.Advance(4);
}
// Write the payload
payloadWriter(buffer, maskingKey, payloadLength, payload);
WritePayload(ref buffer, payloadLength, payloadWriter, payload);
// Flush.
await buffer.FlushAsync();
@ -701,6 +697,28 @@ namespace Microsoft.Extensions.WebSockets.Internal
}
}
private void WritePayload<T>(ref WritableBuffer buffer, int payloadLength, Action<WritableBuffer, uint, int, T> payloadWriter, T payload)
{
var maskingKey = Span<byte>.Empty;
var keySize = sizeof(uint);
if (_maskingKeyBuffer != null)
{
// Get a span of the output buffer for the masking key, write it there, then advance the write head.
maskingKey = buffer.Buffer.Slice(0, keySize).Span;
WriteMaskingKey(maskingKey);
buffer.Advance(keySize);
// Write the payload
payloadWriter(buffer, maskingKey.Read<uint>(), payloadLength, payload);
}
else
{
// Write the payload un-masked
payloadWriter(buffer, 0, payloadLength, payload);
}
}
private int CalculateAllocSize(int payloadAllocLength, int payloadLength)
{
var allocSize = 2;

View File

@ -3,8 +3,8 @@
<Import Project="..\..\build\common.props" />
<PropertyGroup>
<TargetFrameworks>netcoreapp1.1;net46</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">netcoreapp1.1</TargetFrameworks>
<TargetFrameworks>netcoreapp2.0;net46</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">netcoreapp2.0</TargetFrameworks>
<!-- TODO remove when https://github.com/Microsoft/vstest/issues/428 is resolved -->
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>

View File

@ -3,8 +3,8 @@
<Import Project="..\..\build\common.props" />
<PropertyGroup>
<TargetFrameworks>netcoreapp1.1;net46</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">netcoreapp1.1</TargetFrameworks>
<TargetFrameworks>netcoreapp2.0;net46</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">netcoreapp2.0</TargetFrameworks>
<!-- TODO remove when https://github.com/Microsoft/vstest/issues/428 is resolved -->
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>

View File

@ -4,7 +4,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp1.1</TargetFrameworks>
<TargetFrameworks>netcoreapp2.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>

View File

@ -3,8 +3,8 @@
<Import Project="..\..\build\common.props" />
<PropertyGroup>
<TargetFrameworks>netcoreapp1.1;net46</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">netcoreapp1.1</TargetFrameworks>
<TargetFrameworks>netcoreapp2.0;net46</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">netcoreapp2.0</TargetFrameworks>
<!-- TODO remove when https://github.com/Microsoft/vstest/issues/428 is resolved -->
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
@ -37,7 +37,7 @@
<PackageReference Include="xunit" Version="$(XunitVersion)" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp1.1' ">
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.0' ">
<PackageReference Include="System.Net.WebSockets.Client" Version="$(CoreFxVersion)" />
</ItemGroup>

View File

@ -3,8 +3,8 @@
<Import Project="..\..\build\common.props" />
<PropertyGroup>
<TargetFrameworks>netcoreapp1.1;net46</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">netcoreapp1.1</TargetFrameworks>
<TargetFrameworks>netcoreapp2.0;net46</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">netcoreapp2.0</TargetFrameworks>
<!-- TODO remove when https://github.com/Microsoft/vstest/issues/428 is resolved -->
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>

View File

@ -3,8 +3,8 @@
<Import Project="..\..\build\common.props" />
<PropertyGroup>
<TargetFrameworks>netcoreapp1.1;net46</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">netcoreapp1.1</TargetFrameworks>
<TargetFrameworks>netcoreapp2.0;net46</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">netcoreapp2.0</TargetFrameworks>
<!-- TODO remove when https://github.com/Microsoft/vstest/issues/428 is resolved -->
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>

View File

@ -100,7 +100,7 @@ namespace Microsoft.AspNetCore.WebSockets.Internal.ConformanceTest.Autobahn
{
ApplicationBaseUriHint = baseUrl,
ApplicationType = ApplicationType.Portable,
TargetFramework = "netcoreapp1.1",
TargetFramework = "netcoreapp2.0",
EnvironmentName = "Development"
};
@ -111,7 +111,7 @@ namespace Microsoft.AspNetCore.WebSockets.Internal.ConformanceTest.Autobahn
#if NET46
System.Net.ServicePointManager.ServerCertificateValidationCallback = (_, __, ___, ____) => true;
var client = new HttpClient();
#else
#elif NETCOREAPP2_0
var handler = new HttpClientHandler();
if (ssl)
{
@ -121,6 +121,8 @@ namespace Microsoft.AspNetCore.WebSockets.Internal.ConformanceTest.Autobahn
handler.ServerCertificateCustomValidationCallback = (_, __, ___, ____) => true;
}
var client = new HttpClient(handler);
#else
#error Target framework needs to be updated
#endif
// Make sure the server works

View File

@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\build\common.props" />
<PropertyGroup>
<TargetFrameworks>netcoreapp1.1;net46</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">netcoreapp1.1</TargetFrameworks>
<TargetFrameworks>netcoreapp2.0;net46</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">netcoreapp2.0</TargetFrameworks>
<!-- TODO remove when https://github.com/Microsoft/vstest/issues/428 is resolved -->
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
@ -21,7 +21,7 @@
<PackageReference Include="xunit.runner.visualstudio" Version="$(XunitVersion)" />
<PackageReference Include="xunit" Version="$(XunitVersion)" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp1.1' ">
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.0' ">
<PackageReference Include="System.Diagnostics.FileVersionInfo" Version="$(CoreFxVersion)" />
</ItemGroup>
<ItemGroup>

View File

@ -3,8 +3,8 @@
<Import Project="..\..\build\common.props" />
<PropertyGroup>
<TargetFrameworks>netcoreapp1.1;net46</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">netcoreapp1.1</TargetFrameworks>
<TargetFrameworks>netcoreapp2.0;net46</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">netcoreapp2.0</TargetFrameworks>
<!-- TODO remove when https://github.com/Microsoft/vstest/issues/428 is resolved -->
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>

View File

@ -143,15 +143,15 @@ namespace Microsoft.Extensions.WebSockets.Internal.Tests
Assert.Equal(0x85, data[1]);
// We don't know the mask, so we have to read it in order to verify this frame
var mask = data.Slice(2, 4);
var actualPayload = data.Slice(6);
var mask = new byte[] { data[2], data[3], data[4], data[5] };
var actualPayload = new byte[data.Length - 6];
// Unmask the payload
for (int i = 0; i < actualPayload.Length; i++)
{
actualPayload[i] = (byte)(mask[i % 4] ^ actualPayload[i]);
actualPayload[i] = (byte)(mask[i % 4] ^ data[i + 6]);
}
Assert.Equal(new byte[] { 0x0A, 0x0B, 0x0C, 0x0D, 0x0E }, actualPayload.ToArray());
Assert.Equal(new byte[] { 0x0A, 0x0B, 0x0C, 0x0D, 0x0E }, actualPayload);
}
[Theory]

View File

@ -3,7 +3,7 @@
<Import Project="..\..\build\common.props" />
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>