Skip to content

Commit

Permalink
Version 0.0.19
Browse files Browse the repository at this point in the history
  • Loading branch information
LucHeart committed Apr 26, 2024
1 parent 9cfc059 commit 33d4532
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 34 deletions.
6 changes: 3 additions & 3 deletions SDK.CSharp.Hub/SDK.CSharp.Hub.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<AssemblyName>OpenShock.SDK.CSharp.Hub</AssemblyName>
<RootNamespace>OpenShock.SDK.CSharp.Hub</RootNamespace>
<Company>OpenShock</Company>
<AssemblyVersion>0.0.16</AssemblyVersion>
<Version>0.0.16</Version>
<AssemblyVersion>0.0.19</AssemblyVersion>
<Version>0.0.19</Version>
<Title>SDK.DotNet.Hub</Title>
<Authors>OpenShock</Authors>
<Description>Extension for OpenShock.SDK.CSharp</Description>
Expand All @@ -35,7 +35,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Condition=" '$(Configuration)' == 'Release' " Include="OpenShock.SDK.CSharp" Version="0.0.15" />
<PackageReference Condition=" '$(Configuration)' == 'Release' " Include="OpenShock.SDK.CSharp" Version="0.0.19" />
<PackageReference Include="Semver" Version="2.3.0"/>
</ItemGroup>

Expand Down
10 changes: 4 additions & 6 deletions SDK.CSharp.Live/IOpenShockLiveControlClient.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
using OneOf;
using OneOf.Types;
using OpenShock.SDK.CSharp.Live.LiveControlModels;
using OpenShock.SDK.CSharp.Live.LiveControlModels;
using OpenShock.SDK.CSharp.Updatables;

namespace OpenShock.SDK.CSharp.Live;

public interface IOpenShockLiveControlClient
{
public ulong Latency { get; }
public WebsocketConnectionState State { get; }
public IAsyncUpdatable<ulong> Latency { get; }
public IAsyncUpdatable<WebsocketConnectionState> State { get; }

# region Events

public event Func<Task>? OnDispose;
public event Func<WebsocketConnectionState, Task>? OnStateUpdate;
public event Func<Task>? OnDeviceNotConnected;
public event Func<Task>? OnDeviceConnected;

Expand Down
31 changes: 11 additions & 20 deletions SDK.CSharp.Live/OpenShockLiveControlClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using OpenShock.SDK.CSharp.Live.LiveControlModels;
using OpenShock.SDK.CSharp.Live.Utils;
using OpenShock.SDK.CSharp.Serialization;
using OpenShock.SDK.CSharp.Updatables;
using OpenShock.SDK.CSharp.Utils;

namespace OpenShock.SDK.CSharp.Live;
Expand All @@ -27,12 +28,9 @@ public sealed class OpenShockLiveControlClient : IOpenShockLiveControlClient, IA
private readonly string _authToken;
private readonly ILogger<OpenShockLiveControlClient> _logger;
private ClientWebSocket? _clientWebSocket = null;

public event Func<WebsocketConnectionState, Task>? OnStateUpdate;

public event Func<Task>? OnDeviceNotConnected;
public event Func<Task>? OnDeviceConnected;
private WebsocketConnectionState _state = WebsocketConnectionState.Disconnected;

public event Func<Task>? OnDispose;

private readonly CancellationTokenSource _dispose;
Expand All @@ -59,16 +57,8 @@ public OpenShockLiveControlClient(string gateway, Guid deviceId, string authToke
private ValueTask QueueMessage(BaseRequest<LiveRequestType> data) =>
_channel.Writer.WriteAsync(data, _dispose.Token);

public WebsocketConnectionState State
{
get => _state;
private set
{
_state = value;
OnStateUpdate?.Raise(value);
}
}

private readonly AsyncUpdatableVariable<WebsocketConnectionState> _state = new(WebsocketConnectionState.Disconnected);
public IAsyncUpdatable<WebsocketConnectionState> State => _state;

private async Task MessageLoop()
{
Expand Down Expand Up @@ -98,7 +88,7 @@ private async Task<OneOf<Success, NotFound, Shutdown, Reconnecting>> ConnectAsyn
return new Shutdown();
}

State = WebsocketConnectionState.Connecting;
_state.Value = WebsocketConnectionState.Connecting;
#if NETSTANDARD2_1
_currentConnectionClose?.Cancel();
#else
Expand All @@ -121,7 +111,7 @@ private async Task<OneOf<Success, NotFound, Shutdown, Reconnecting>> ConnectAsyn
await _clientWebSocket.ConnectAsync(new Uri($"wss://{_gateway}/1/ws/live/{_deviceId}"), _linked.Token);

_logger.LogInformation("Connected to websocket");
State = WebsocketConnectionState.Connected;
_state.Value = WebsocketConnectionState.Connected;

Run(ReceiveLoop, _linked.Token);
Run(MessageLoop, _linked.Token);
Expand All @@ -145,7 +135,7 @@ private async Task<OneOf<Success, NotFound, Shutdown, Reconnecting>> ConnectAsyn
_logger.LogError(e, "Error while connecting, retrying in 3 seconds");
}

State = WebsocketConnectionState.Reconnecting;
_state.Value = WebsocketConnectionState.Reconnecting;
_clientWebSocket.Abort();
_clientWebSocket.Dispose();
await Task.Delay(3000, _dispose.Token);
Expand Down Expand Up @@ -248,7 +238,7 @@ await _clientWebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Normal cl
}

_logger.LogWarning("Lost websocket connection, trying to reconnect in 3 seconds");
State = WebsocketConnectionState.Reconnecting;
_state.Value = WebsocketConnectionState.Reconnecting;

_clientWebSocket?.Abort();
_clientWebSocket?.Dispose();
Expand Down Expand Up @@ -298,7 +288,7 @@ await QueueMessage(new BaseRequest<LiveRequestType>
return;
}

Latency = latencyAnnounceResponse.OwnLatency;
_latency.Value = latencyAnnounceResponse.OwnLatency;
break;

case LiveResponseType.DeviceNotConnected:
Expand Down Expand Up @@ -353,7 +343,8 @@ public Task Run(Task? function, CancellationToken cancellationToken = default, [
file.Substring(index + 1, file.Length - index - 1), member, line, t.Exception?.StackTrace);
}, TaskContinuationOptions.OnlyOnFaulted);

public ulong Latency { get; private set; } = 0;
private readonly AsyncUpdatableVariable<ulong> _latency = new(0);
public IAsyncUpdatable<ulong> Latency => _latency;

public async Task SendFrame(ClientLiveFrame frame)
{
Expand Down
6 changes: 3 additions & 3 deletions SDK.CSharp.Live/SDK.CSharp.Live.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<AssemblyName>OpenShock.SDK.CSharp.Live</AssemblyName>
<RootNamespace>OpenShock.SDK.CSharp.Live</RootNamespace>
<Company>OpenShock</Company>
<AssemblyVersion>0.0.17</AssemblyVersion>
<Version>0.0.17</Version>
<AssemblyVersion>0.0.19</AssemblyVersion>
<Version>0.0.19</Version>
<Title>SDK.DotNet.Live</Title>
<Authors>OpenShock</Authors>
<Description>Extension for OpenShock.SDK.CSharp</Description>
Expand All @@ -31,7 +31,7 @@
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="3.0.0" />
<PackageReference Include="OneOf" Version="3.0.263" />
<PackageReference Condition=" '$(Configuration)' == 'Release' " Include="OpenShock.SDK.CSharp" Version="0.0.15" />
<PackageReference Condition=" '$(Configuration)' == 'Release' " Include="OpenShock.SDK.CSharp" Version="0.0.19" />
<PackageReference Include="PolySharp" Version="1.14.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
4 changes: 2 additions & 2 deletions SDK.CSharp/SDK.CSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<AssemblyName>OpenShock.SDK.CSharp</AssemblyName>
<RootNamespace>OpenShock.SDK.CSharp</RootNamespace>
<Company>OpenShock</Company>
<AssemblyVersion>0.0.16</AssemblyVersion>
<Version>0.0.16</Version>
<AssemblyVersion>0.0.19</AssemblyVersion>
<Version>0.0.19</Version>
<Title>SDK.DotNet</Title>
<Authors>OpenShock</Authors>
<Description>.NET / C# SDK for developing OpenShock applications. Used to interact with a OpenShock backend.</Description>
Expand Down
24 changes: 24 additions & 0 deletions SDK.CSharp/Updatables/AsyncUpdatableVariable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using OpenShock.SDK.CSharp.Utils;

namespace OpenShock.SDK.CSharp.Updatables;

public sealed class AsyncUpdatableVariable<T>(T internalValue) : IAsyncUpdatable<T>
{
public T Value
{
get => internalValue;
set
{
if (internalValue!.Equals(value)) return;
internalValue = value;
Task.Run(() => OnValueChanged?.Raise(value));
}
}

public event Func<T, Task>? OnValueChanged;

public void UpdateWithoutNotify(T newValue)
{
internalValue = newValue;
}
}
6 changes: 6 additions & 0 deletions SDK.CSharp/Updatables/IAsyncUpdatable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace OpenShock.SDK.CSharp.Updatables;

public interface IAsyncUpdatable<out T> : IUpdatableBase<T>
{
public event Func<T, Task>? OnValueChanged;
}
6 changes: 6 additions & 0 deletions SDK.CSharp/Updatables/IUpdatable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace OpenShock.SDK.CSharp.Updatables;

public interface IUpdatable<out T> : IUpdatableBase<T>
{
public event Action<T>? OnValueChanged;
}
6 changes: 6 additions & 0 deletions SDK.CSharp/Updatables/IUpdatableBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace OpenShock.SDK.CSharp.Updatables;

public interface IUpdatableBase<out T>
{
public T Value { get; }
}
22 changes: 22 additions & 0 deletions SDK.CSharp/Updatables/UpdatableVariable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace OpenShock.SDK.CSharp.Updatables;

public sealed class UpdatableVariable<T>(T internalValue) : IUpdatable<T>
{
public T Value
{
get => internalValue;
set
{
if (internalValue!.Equals(value)) return;
internalValue = value;
OnValueChanged?.Invoke(value);
}
}

public event Action<T>? OnValueChanged;

public void UpdateWithoutNotify(T newValue)
{
internalValue = newValue;
}
}

0 comments on commit 33d4532

Please sign in to comment.