Replace `?? CancellationToken.None` pattern with the simpler `default`
This commit is contained in:
parent
b173712a15
commit
f2a4d14172
|
@ -14,52 +14,67 @@ namespace BizHawk.Client.Common
|
||||||
/// <summary>calls <see cref="ClientWebSocket.State"/> getter (unless closed/disposed, then <see cref="WebSocketState.Closed"/> is always returned)</summary>
|
/// <summary>calls <see cref="ClientWebSocket.State"/> getter (unless closed/disposed, then <see cref="WebSocketState.Closed"/> is always returned)</summary>
|
||||||
public WebSocketState State => _w?.State ?? WebSocketState.Closed;
|
public WebSocketState State => _w?.State ?? WebSocketState.Closed;
|
||||||
|
|
||||||
public ClientWebSocketWrapper(Uri uri, CancellationToken? cancellationToken = null)
|
public ClientWebSocketWrapper(
|
||||||
|
Uri uri,
|
||||||
|
CancellationToken cancellationToken = default/* == CancellationToken.None */)
|
||||||
{
|
{
|
||||||
cancellationToken ??= CancellationToken.None;
|
|
||||||
_w = new ClientWebSocket();
|
_w = new ClientWebSocket();
|
||||||
_w.ConnectAsync(uri, cancellationToken.Value).Wait(cancellationToken.Value);
|
_w.ConnectAsync(uri, cancellationToken).Wait(cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>calls <see cref="ClientWebSocket.CloseAsync"/></summary>
|
/// <summary>calls <see cref="ClientWebSocket.CloseAsync"/></summary>
|
||||||
/// <remarks>also calls <see cref="ClientWebSocket.Dispose"/> (wrapper property <see cref="State"/> will continue to work, method calls will throw <see cref="ObjectDisposedException"/>)</remarks>
|
/// <remarks>also calls <see cref="ClientWebSocket.Dispose"/> (wrapper property <see cref="State"/> will continue to work, method calls will throw <see cref="ObjectDisposedException"/>)</remarks>
|
||||||
public Task Close(WebSocketCloseStatus closeStatus, string statusDescription, CancellationToken? cancellationToken = null)
|
public Task Close(
|
||||||
|
WebSocketCloseStatus closeStatus,
|
||||||
|
string statusDescription,
|
||||||
|
CancellationToken cancellationToken = default/* == CancellationToken.None */)
|
||||||
{
|
{
|
||||||
if (_w == null) throw new ObjectDisposedException(nameof(_w));
|
if (_w == null) throw new ObjectDisposedException(nameof(_w));
|
||||||
var task = _w.CloseAsync(closeStatus, statusDescription, cancellationToken ?? CancellationToken.None);
|
var task = _w.CloseAsync(closeStatus, statusDescription, cancellationToken);
|
||||||
_w.Dispose();
|
_w.Dispose();
|
||||||
_w = null;
|
_w = null;
|
||||||
return task;
|
return task;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>calls <see cref="ClientWebSocket.ReceiveAsync"/></summary>
|
/// <summary>calls <see cref="ClientWebSocket.ReceiveAsync"/></summary>
|
||||||
public Task<WebSocketReceiveResult> Receive(ArraySegment<byte> buffer, CancellationToken? cancellationToken = null)
|
public Task<WebSocketReceiveResult> Receive(
|
||||||
=> _w?.ReceiveAsync(buffer, cancellationToken ?? CancellationToken.None)
|
ArraySegment<byte> buffer,
|
||||||
?? throw new ObjectDisposedException(nameof(_w));
|
CancellationToken cancellationToken = default/* == CancellationToken.None */)
|
||||||
|
=> _w?.ReceiveAsync(buffer, cancellationToken)
|
||||||
|
?? throw new ObjectDisposedException(nameof(_w));
|
||||||
|
|
||||||
/// <summary>calls <see cref="ClientWebSocket.ReceiveAsync"/></summary>
|
/// <summary>calls <see cref="ClientWebSocket.ReceiveAsync"/></summary>
|
||||||
public string Receive(int bufferCap, CancellationToken? cancellationToken = null)
|
public string Receive(
|
||||||
|
int bufferCap,
|
||||||
|
CancellationToken cancellationToken = default/* == CancellationToken.None */)
|
||||||
{
|
{
|
||||||
if (_w == null) throw new ObjectDisposedException(nameof(_w));
|
if (_w == null) throw new ObjectDisposedException(nameof(_w));
|
||||||
var buffer = new byte[bufferCap];
|
var buffer = new byte[bufferCap];
|
||||||
var result = Receive(new ArraySegment<byte>(buffer), cancellationToken ?? CancellationToken.None).Result;
|
var result = Receive(new ArraySegment<byte>(buffer), cancellationToken).Result;
|
||||||
return Encoding.UTF8.GetString(buffer, 0, result.Count);
|
return Encoding.UTF8.GetString(buffer, 0, result.Count);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>calls <see cref="ClientWebSocket.SendAsync"/></summary>
|
/// <summary>calls <see cref="ClientWebSocket.SendAsync"/></summary>
|
||||||
public Task Send(ArraySegment<byte> buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken? cancellationToken = null)
|
public Task Send(
|
||||||
=> _w?.SendAsync(buffer, messageType, endOfMessage, cancellationToken ?? CancellationToken.None)
|
ArraySegment<byte> buffer,
|
||||||
?? throw new ObjectDisposedException(nameof(_w));
|
WebSocketMessageType messageType,
|
||||||
|
bool endOfMessage,
|
||||||
|
CancellationToken cancellationToken = default/* == CancellationToken.None */)
|
||||||
|
=> _w?.SendAsync(buffer, messageType, endOfMessage, cancellationToken)
|
||||||
|
?? throw new ObjectDisposedException(nameof(_w));
|
||||||
|
|
||||||
/// <summary>calls <see cref="ClientWebSocket.SendAsync"/></summary>
|
/// <summary>calls <see cref="ClientWebSocket.SendAsync"/></summary>
|
||||||
public Task Send(string message, bool endOfMessage, CancellationToken? cancellationToken = null)
|
public Task Send(
|
||||||
|
string message,
|
||||||
|
bool endOfMessage,
|
||||||
|
CancellationToken cancellationToken = default/* == CancellationToken.None */)
|
||||||
{
|
{
|
||||||
if (_w == null) throw new ObjectDisposedException(nameof(_w));
|
if (_w == null) throw new ObjectDisposedException(nameof(_w));
|
||||||
return Send(
|
return Send(
|
||||||
new ArraySegment<byte>(Encoding.UTF8.GetBytes(message)),
|
new ArraySegment<byte>(Encoding.UTF8.GetBytes(message)),
|
||||||
WebSocketMessageType.Text,
|
WebSocketMessageType.Text,
|
||||||
endOfMessage,
|
endOfMessage,
|
||||||
cancellationToken ?? CancellationToken.None
|
cancellationToken
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,9 @@ namespace BizHawk.Client.Common
|
||||||
{
|
{
|
||||||
public sealed class WebSocketServer
|
public sealed class WebSocketServer
|
||||||
{
|
{
|
||||||
public ClientWebSocketWrapper Open(Uri uri, CancellationToken? cancellationToken = null) => new ClientWebSocketWrapper(uri, cancellationToken);
|
public ClientWebSocketWrapper Open(
|
||||||
|
Uri uri,
|
||||||
|
CancellationToken cancellationToken = default/* == CancellationToken.None */)
|
||||||
|
=> new(uri, cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue