Cleanup some dead code (#1434)

- Rename SocketBuilder to ConnectionBuilder
- Removed ChannelReaderExtensions
This commit is contained in:
David Fowler 2018-02-10 22:16:30 -08:00 committed by GitHub
parent 9589775f3d
commit 3934ffd5af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 60 deletions

View File

@ -1,47 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
namespace Microsoft.AspNetCore.SignalR.Internal
{
public static class ChannelReaderExtensions
{
/// <summary>Asynchronously reads an item from the channel.</summary>
/// <param name="channel">The channel</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> used to cancel the read operation.</param>
/// <returns>A <see cref="ValueTask{TResult}"/> that represents the asynchronous read operation.</returns>
public static ValueTask<T> ReadAsync<T>(this ChannelReader<T> channel, CancellationToken cancellationToken = default)
{
try
{
return
cancellationToken.IsCancellationRequested
? new ValueTask<T>(Task.FromCanceled<T>(cancellationToken))
: channel.TryRead(out T item)
? new ValueTask<T>(item)
: ReadAsyncCore(cancellationToken);
}
catch (Exception e)
{
return new ValueTask<T>(Task.FromException<T>(e));
}
async ValueTask<T> ReadAsyncCore(CancellationToken ct)
{
while (await channel.WaitToReadAsync(ct).ConfigureAwait(false))
{
if (channel.TryRead(out T item))
{
return item;
}
}
throw new ChannelClosedException();
}
}
}
}

View File

@ -7,11 +7,11 @@ using Microsoft.AspNetCore.Protocols;
namespace Microsoft.AspNetCore.Sockets
{
public static class SocketBuilderExtensions
public static class ConnectionBuilderExtensions
{
public static IConnectionBuilder Use(this IConnectionBuilder socketBuilder, Func<ConnectionContext, Func<Task>, Task> middleware)
public static IConnectionBuilder Use(this IConnectionBuilder connectionBuilder, Func<ConnectionContext, Func<Task>, Task> middleware)
{
return socketBuilder.Use(next =>
return connectionBuilder.Use(next =>
{
return context =>
{
@ -21,9 +21,9 @@ namespace Microsoft.AspNetCore.Sockets
});
}
public static IConnectionBuilder Run(this IConnectionBuilder socketBuilder, Func<ConnectionContext, Task> middleware)
public static IConnectionBuilder Run(this IConnectionBuilder connectionBuilder, Func<ConnectionContext, Task> middleware)
{
return socketBuilder.Use(next =>
return connectionBuilder.Use(next =>
{
return context =>
{

View File

@ -27,11 +27,11 @@ namespace Microsoft.AspNetCore.Sockets
public void MapSocket(PathString path, Action<IConnectionBuilder> socketConfig) =>
MapSocket(path, new HttpSocketOptions(), socketConfig);
public void MapSocket(PathString path, HttpSocketOptions options, Action<IConnectionBuilder> socketConfig)
public void MapSocket(PathString path, HttpSocketOptions options, Action<IConnectionBuilder> connectionConfig)
{
var socketBuilder = new ConnectionBuilder(_routes.ServiceProvider);
socketConfig(socketBuilder);
var socket = socketBuilder.Build();
var connectionBuilder = new ConnectionBuilder(_routes.ServiceProvider);
connectionConfig(connectionBuilder);
var socket = connectionBuilder.Build();
_routes.MapRoute(path, c => _dispatcher.ExecuteAsync(c, options, socket));
_routes.MapRoute(path + "/negotiate", c => _dispatcher.ExecuteNegotiateAsync(c, options));
}

View File

@ -6,13 +6,13 @@ using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.AspNetCore.Sockets
{
public static class SocketBuilderExtensions
public static class ConnectionBuilderExtensions
{
public static IConnectionBuilder UseEndPoint<TEndPoint>(this IConnectionBuilder socketBuilder) where TEndPoint : EndPoint
public static IConnectionBuilder UseEndPoint<TEndPoint>(this IConnectionBuilder connectionBuilder) where TEndPoint : EndPoint
{
var endpoint = socketBuilder.ApplicationServices.GetRequiredService<TEndPoint>();
var endpoint = connectionBuilder.ApplicationServices.GetRequiredService<TEndPoint>();
// This is a terminal middleware, so there's no need to use the 'next' parameter
return socketBuilder.Run(connection => endpoint.OnConnectedAsync(connection));
return connectionBuilder.Run(connection => endpoint.OnConnectedAsync(connection));
}
}
}