Client Web Socket implementation (#46)

This commit is contained in:
2023-05-14 22:46:38 +02:00
committed by GitHub
parent a25f2e5acb
commit de08c0e3b0
13 changed files with 234 additions and 107 deletions
@@ -4,7 +4,7 @@ namespace System.Extensions;
public static class ObjectExtensions
{
public static T Deserialize<T>(this string serialized)
public static T? Deserialize<T>(this string serialized)
where T : class
{
if (serialized.IsNullOrWhiteSpace())
@@ -31,7 +31,7 @@ public static class ObjectExtensions
return (T)obj;
}
public static T As<T>(this object obj) where T : class
public static T? As<T>(this object obj) where T : class
{
return obj as T;
}
@@ -7,7 +7,7 @@ public static class Optional
return new Optional<T>.None();
}
public static Optional<T> FromValue<T>(T value)
public static Optional<T> FromValue<T>(T? value)
{
if (value == null)
{
@@ -27,7 +27,7 @@ public abstract class Optional<T> : IEquatable<Optional<T>>
private T Value { get; }
public T ExtractValue()
public T? ExtractValue()
{
if (this is None)
{
@@ -38,7 +38,7 @@ public abstract class Optional<T> : IEquatable<Optional<T>>
return this.Value;
}
}
public Optional<T> Do(Action<T> onSome, Action onNone)
public Optional<T> Do(Action<T>? onSome, Action? onNone)
{
if (onSome is null)
{
@@ -61,7 +61,7 @@ public abstract class Optional<T> : IEquatable<Optional<T>>
return this;
}
public Optional<T> DoAny(Action<T> onSome = null, Action onNone = null)
public Optional<T> DoAny(Action<T>? onSome = default, Action? onNone = default)
{
if (this is Some)
{
@@ -74,7 +74,7 @@ public abstract class Optional<T> : IEquatable<Optional<T>>
return this;
}
public Optional<V> Switch<V>(Func<T, V> onSome, Func<V> onNone)
public Optional<V> Switch<V>(Func<T, V>? onSome, Func<V>? onNone)
{
if (onSome is null)
{
@@ -95,7 +95,7 @@ public abstract class Optional<T> : IEquatable<Optional<T>>
return Optional.FromValue(onNone.Invoke());
}
}
public Optional<V> SwitchAny<V>(Func<T, V> onSome = null, Func<V> onNone = null)
public Optional<V> SwitchAny<V>(Func<T, V>? onSome = null, Func<V>? onNone = null)
{
if (this is Some)
{
@@ -107,7 +107,7 @@ public abstract class Optional<T> : IEquatable<Optional<T>>
}
}
public override bool Equals(object obj)
public override bool Equals(object? obj)
{
if (obj is Optional<T>)
{
@@ -117,17 +117,17 @@ public abstract class Optional<T> : IEquatable<Optional<T>>
return base.Equals(obj);
}
public bool Equals(Optional<T> other)
public bool Equals(Optional<T>? other)
{
if (this is Some && other is Some)
{
return this.As<Some>().Equals(other.As<Some>());
return this.As<Some>() is Some some ? some.Equals(other.As<Some>()) : false;
}
return false;
}
public static implicit operator Optional<T>(T value)
public static implicit operator Optional<T>(T? value)
{
return Optional.FromValue(value);
}
@@ -143,7 +143,7 @@ public abstract class Optional<T> : IEquatable<Optional<T>>
{
}
public override bool Equals(object obj)
public override bool Equals(object? obj)
{
if (obj is Some)
{
@@ -153,40 +153,40 @@ public abstract class Optional<T> : IEquatable<Optional<T>>
return base.Equals(obj);
}
public bool Equals(Some other)
public bool Equals(Some? other)
{
if (other is None)
{
return false;
}
if (other is Some)
if (other is not null)
{
return this.Value.Equals(other.Value);
return this.Value is not null && this.Value.Equals(other.Value);
}
return false;
}
public static bool operator ==(Some left, Some right)
public static bool operator ==(Some? left, Some right)
{
return left?.Equals(right) == true;
}
public static bool operator !=(Some left, Some right)
public static bool operator !=(Some? left, Some right)
{
return left?.Equals(right) != true;
}
public override int GetHashCode()
{
return this.Value is object ? this.Value.GetHashCode() : this.As<object>().GetHashCode();
return this.Value is object ? this.Value.GetHashCode() : this.As<object>() is object obj ? obj.GetHashCode() : 0;
}
}
internal class None : Optional<T>
{
public None() : base(default)
public None() : base(default!)
{
}
}
@@ -2,7 +2,7 @@
public class Result<TSuccess, TFailure>
{
private readonly object value;
private readonly object? value;
public Result(TSuccess successValue)
{
@@ -13,7 +13,7 @@ public class Result<TSuccess, TFailure>
this.value = failureValue;
}
public bool TryExtractSuccess(out TSuccess successValue)
public bool TryExtractSuccess(out TSuccess? successValue)
{
if (this.value is TSuccess success)
@@ -27,7 +27,7 @@ public class Result<TSuccess, TFailure>
return false;
}
}
public bool TryExtractFailure(out TFailure failureValue)
public bool TryExtractFailure(out TFailure? failureValue)
{
if (this.value is TFailure failure)
@@ -64,7 +64,7 @@ public class Result<TSuccess, TFailure>
return this;
}
public Result<TSuccess, TFailure> DoAny(Action onSuccess = null, Action onFailure = null)
public Result<TSuccess, TFailure> DoAny(Action? onSuccess = null, Action? onFailure = null)
{
if (this.value is TSuccess)
{
@@ -100,7 +100,7 @@ public class Result<TSuccess, TFailure>
return this;
}
public Result<TSuccess, TFailure> DoAny(Action<TSuccess> onSuccess = null, Action<TFailure> onFailure = null)
public Result<TSuccess, TFailure> DoAny(Action<TSuccess>? onSuccess = null, Action<TFailure>? onFailure = null)
{
if (this.value is TSuccess success)
{
@@ -136,7 +136,7 @@ public class Result<TSuccess, TFailure>
throw new InvalidOperationException($"{nameof(this.value)} must be of type {typeof(TSuccess)} or {typeof(TFailure)} in order to switch to {typeof(T)}");
}
public T SwitchAny<T>(Func<TSuccess, T> onSuccess = null, Func<TFailure, T> onFailure = null)
public T? SwitchAny<T>(Func<TSuccess, T>? onSuccess = null, Func<TFailure, T>? onFailure = null)
{
if (this.value is TSuccess success)
{
@@ -176,11 +176,11 @@ public class Result<TSuccess, TFailure>
{
if (this.value is TSuccess success)
{
return Result<V, K>.Success(onSuccess is null ? default : onSuccess.Invoke(success));
return Result<V, K>.Success(onSuccess is not null ? onSuccess.Invoke(success) : default!);
}
else if (this.value is TFailure failure)
{
return Result<V, K>.Failure(onFailure is null ? default : onFailure.Invoke(failure));
return Result<V, K>.Failure(onFailure is not null ? onFailure.Invoke(failure) : default!);
}
throw new InvalidOperationException($"{nameof(this.value)} must be of type {typeof(TSuccess)} or {typeof(TFailure)} in order to switch to Result of type {typeof(V)} or {typeof(K)}");
@@ -82,7 +82,7 @@ public static class TaskExtensions
{
synch.EndMessageLoop();
}
}, null);
}, default!);
synch.BeginMessageLoop();
SynchronizationContext.SetSynchronizationContext(oldContext);
@@ -115,16 +115,16 @@ public static class TaskExtensions
{
synch.EndMessageLoop();
}
}, null);
}, default!);
synch.BeginMessageLoop();
SynchronizationContext.SetSynchronizationContext(oldContext);
return ret;
return ret!;
}
private class ExclusiveSynchronizationContext : SynchronizationContext
{
private bool done;
public Exception InnerException { get; set; }
public Exception? InnerException { get; set; }
readonly AutoResetEvent workItemsWaiting = new AutoResetEvent(false);
readonly Queue<Tuple<SendOrPostCallback, object>> items =
new Queue<Tuple<SendOrPostCallback, object>>();
@@ -146,14 +146,14 @@ public static class TaskExtensions
public void EndMessageLoop()
{
this.Post(_ => this.done = true, null);
this.Post(_ => this.done = true, default!);
}
public void BeginMessageLoop()
{
while (!this.done)
{
Tuple<SendOrPostCallback, object> task = null;
Tuple<SendOrPostCallback, object> task = default!;
lock (this.items)
{
if (this.items.Count > 0)