-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1b8eb03
commit da4600c
Showing
7 changed files
with
109 additions
and
56 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
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,46 @@ | ||
using System.Net.WebSockets; | ||
using Stl.Internal; | ||
|
||
namespace Stl.Rpc.WebSockets; | ||
|
||
public class WebSocketOwner( | ||
string name, | ||
WebSocket webSocket, | ||
IServiceProvider services) | ||
: SafeAsyncDisposableBase | ||
{ | ||
private ILogger? _log; | ||
|
||
public IServiceProvider Services { get; } = services; | ||
public string Name { get; } = name; | ||
public WebSocket WebSocket { get; } = webSocket; | ||
public object? Handler { get; init; } | ||
public LogLevel LogLevel { get; init; } = LogLevel.Information; | ||
|
||
protected ILogger Log => _log ??= Services.LogFor(GetType()); | ||
|
||
public virtual Task ConnectAsync(Uri uri, CancellationToken cancellationToken = default) | ||
{ | ||
if (WebSocket is not ClientWebSocket webSocket) | ||
throw Errors.MustBeAssignableTo<ClientWebSocket>(WebSocket.GetType()); | ||
|
||
Log.IfEnabled(LogLevel)?.Log(LogLevel, "'{Name}': connecting to {Uri}", Name, uri); | ||
#if NET7_0_OR_GREATER | ||
if (Handler is HttpMessageHandler handler) | ||
return webSocket.ConnectAsync(uri, new HttpMessageInvoker(handler), cancellationToken); | ||
#endif | ||
return webSocket.ConnectAsync(uri, cancellationToken); | ||
} | ||
|
||
protected override async Task DisposeAsync(bool disposing) | ||
{ | ||
if (!disposing) | ||
return; | ||
|
||
WebSocket.Dispose(); | ||
if (Handler is IAsyncDisposable ad) | ||
await ad.DisposeAsync().ConfigureAwait(false); | ||
else if (Handler is IDisposable d) | ||
d.Dispose(); | ||
} | ||
} |