Skip to content

Commit 69388a3

Browse files
committed
🚧 HubContext
1 parent 92fc5a5 commit 69388a3

File tree

2 files changed

+63
-19
lines changed

2 files changed

+63
-19
lines changed

src/BD.Common8.Bcl/IO/Pipelines/DuplexPipeStream.cs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -169,32 +169,41 @@ private async ValueTask<int> ReadAsyncInternal(Memory<byte> destination, Cancell
169169
{
170170
while (true)
171171
{
172-
var result = await input.ReadAsync(cancellationToken);
173-
var readableBuffer = result.Buffer;
174172
try
175173
{
176-
if (throwOnCancelled && result.IsCanceled && cancelCalled)
174+
var result = await input.ReadAsync(cancellationToken);
175+
var readableBuffer = result.Buffer;
176+
try
177177
{
178-
// Reset the bool
179-
cancelCalled = false;
180-
throw new OperationCanceledException();
178+
if (throwOnCancelled && result.IsCanceled && cancelCalled)
179+
{
180+
// Reset the bool
181+
cancelCalled = false;
182+
throw new OperationCanceledException();
183+
}
184+
185+
if (!readableBuffer.IsEmpty)
186+
{
187+
// buffer.Count is int
188+
var count = (int)Math.Min(readableBuffer.Length, destination.Length);
189+
readableBuffer = readableBuffer.Slice(0, count);
190+
readableBuffer.CopyTo(destination.Span);
191+
return count;
192+
}
193+
194+
if (result.IsCompleted)
195+
{
196+
return 0;
197+
}
181198
}
182-
183-
if (!readableBuffer.IsEmpty)
199+
finally
184200
{
185-
// buffer.Count is int
186-
var count = (int)Math.Min(readableBuffer.Length, destination.Length);
187-
readableBuffer = readableBuffer.Slice(0, count);
188-
readableBuffer.CopyTo(destination.Span);
189-
return count;
201+
input.AdvanceTo(readableBuffer.End, readableBuffer.End);
190202
}
191-
192-
if (result.IsCompleted)
193-
return 0;
194203
}
195-
finally
204+
catch (OperationCanceledException)
196205
{
197-
input.AdvanceTo(readableBuffer.End, readableBuffer.End);
206+
return -1;
198207
}
199208
}
200209
}

src/BD.Common8.Ipc.Server/Services/Implementation/IpcServerService.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,11 @@ static bool IsUsePort(IPAddress address, int port)
186186
builder.Services.AddRoutingCore();
187187
builder.Services.AddLogging(ConfigureLogging);
188188
builder.Services.ConfigureHttpJsonOptions(ConfigureHttpJsonOptions);
189-
ConfigureSignalRProtocol(builder.Services.AddSignalR(ConfigureSignalR));
189+
builder.Services.AddSingleton(
190+
typeof(HubConnectionHandler<>),
191+
typeof(FixHubConnectionHandler<>));
192+
var signalRServerBuilder = builder.Services.AddSignalR(ConfigureSignalR);
193+
ConfigureSignalRProtocol(signalRServerBuilder);
190194
builder.Services.AddHttpContextAccessor();
191195
ConfigureAuthentication(builder.Services.AddAuthentication(DefaultAuthenticationScheme));
192196

@@ -421,6 +425,9 @@ protected virtual void ConfigureHub(HttpConnectionDispatcherOptions options)
421425

422426
public IServiceProvider Services => app.ThrowIsNull().Services;
423427

428+
public abstract bool TryGetHubContext([NotNullWhen(true)] out IHubContext? hubContext);
429+
430+
[Obsolete("use TryGetHubContext", true)]
424431
public abstract IHubContext HubContext { get; }
425432

426433
readonly Dictionary<string, Type> hubTypes = [];
@@ -636,4 +643,32 @@ protected override Task<AuthenticateResult> HandleAuthenticateAsync()
636643
var result = HandleAuthenticate();
637644
return Task.FromResult(result);
638645
}
646+
}
647+
648+
file sealed class FixHubConnectionHandler<
649+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] THub>(
650+
HubLifetimeManager<THub> lifetimeManager,
651+
IHubProtocolResolver protocolResolver,
652+
IOptions<HubOptions> globalHubOptions,
653+
IOptions<HubOptions<THub>> hubOptions,
654+
ILoggerFactory loggerFactory,
655+
IUserIdProvider userIdProvider,
656+
IServiceScopeFactory serviceScopeFactory) :
657+
HubConnectionHandler<THub>(lifetimeManager, protocolResolver, globalHubOptions,
658+
hubOptions, loggerFactory, userIdProvider,
659+
serviceScopeFactory)
660+
where THub : Hub
661+
{
662+
/// <inheritdoc/>
663+
public sealed override async Task OnConnectedAsync(ConnectionContext connection)
664+
{
665+
try
666+
{
667+
await base.OnConnectedAsync(connection);
668+
}
669+
catch (ObjectDisposedException)
670+
{
671+
// 应用程序退出时引发 Ioc 被释放异常忽略
672+
}
673+
}
639674
}

0 commit comments

Comments
 (0)