Skip to content

Commit 9cfc059

Browse files
committed
Add debug logging for failed messages and add tps to response types
1 parent a4d2c2c commit 9cfc059

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

SDK.CSharp.Live/LiveControlModels/LiveResponseType.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ public enum LiveResponseType
77
{
88
Frame = 0,
99

10+
TPS = 50,
11+
1012
DeviceNotConnected = 100,
1113
DeviceConnected = 101,
1214
ShockerNotFound = 150,

SDK.CSharp.Live/OpenShockLiveControlClient.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private async Task<OneOf<Success, NotFound, Shutdown, Reconnecting>> ConnectAsyn
100100

101101
State = WebsocketConnectionState.Connecting;
102102
#if NETSTANDARD2_1
103-
_currentConnectionClose?.Cancel();
103+
_currentConnectionClose?.Cancel();
104104
#else
105105
if (_currentConnectionClose != null) await _currentConnectionClose.CancelAsync();
106106
#endif
@@ -213,7 +213,8 @@ await _clientWebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Normal cl
213213
message.Switch(wsRequest => { Run(HandleMessage(wsRequest)); },
214214
failed =>
215215
{
216-
_logger.LogWarning("Deserialization failed for websocket message {Exception}",
216+
_logger.LogWarning("Deserialization failed for websocket message [{Message}] \n\n {Exception}",
217+
failed.Message,
217218
failed.Exception);
218219
},
219220
_ => { });
@@ -299,7 +300,7 @@ await QueueMessage(new BaseRequest<LiveRequestType>
299300

300301
Latency = latencyAnnounceResponse.OwnLatency;
301302
break;
302-
303+
303304
case LiveResponseType.DeviceNotConnected:
304305
await OnDeviceNotConnected.Raise();
305306
break;

SDK.CSharp.Live/SDK.CSharp.Live.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
<AssemblyName>OpenShock.SDK.CSharp.Live</AssemblyName>
99
<RootNamespace>OpenShock.SDK.CSharp.Live</RootNamespace>
1010
<Company>OpenShock</Company>
11-
<AssemblyVersion>0.0.16</AssemblyVersion>
12-
<Version>0.0.16</Version>
11+
<AssemblyVersion>0.0.17</AssemblyVersion>
12+
<Version>0.0.17</Version>
1313
<Title>SDK.DotNet.Live</Title>
1414
<Authors>OpenShock</Authors>
1515
<Description>Extension for OpenShock.SDK.CSharp</Description>

SDK.CSharp.Live/Utils/JsonWebSocketUtils.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Buffers;
22
using System.Net.WebSockets;
3+
using System.Text;
34
using System.Text.Json;
45
using Microsoft.IO;
56

@@ -8,7 +9,7 @@ namespace OpenShock.SDK.CSharp.Live.Utils;
89
public static class JsonWebSocketUtils
910
{
1011
private const uint MaxMessageSize = 512_000; // 512 000 bytes
11-
12+
1213
public static readonly RecyclableMemoryStreamManager RecyclableMemory = new();
1314

1415
public static async Task<OneOf.OneOf<T?, DeserializeFailed, WebsocketClosure>> ReceiveFullMessageAsyncNonAlloc<T>(
@@ -32,7 +33,7 @@ await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closure during mess
3233
}
3334

3435
if (buffer.Length + result.Count > MaxMessageSize) throw new MessageTooLongException();
35-
36+
3637
message.Write(buffer, 0, result.Count);
3738
} while (!result.EndOfMessage);
3839

@@ -42,7 +43,11 @@ await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closure during mess
4243
}
4344
catch (Exception e)
4445
{
45-
return new DeserializeFailed { Exception = e };
46+
return new DeserializeFailed
47+
{
48+
Message = Encoding.UTF8.GetString(message.GetBuffer().AsSpan(0, bytes)),
49+
Exception = e
50+
};
4651
}
4752
}
4853
finally
@@ -51,10 +56,12 @@ await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closure during mess
5156
}
5257
}
5358

54-
public static Task SendFullMessage<T>(T obj, WebSocket socket, CancellationToken cancelToken, JsonSerializerOptions jsonSerializerOptions, int maxChunkSize = 256) =>
59+
public static Task SendFullMessage<T>(T obj, WebSocket socket, CancellationToken cancelToken,
60+
JsonSerializerOptions jsonSerializerOptions, int maxChunkSize = 256) =>
5561
SendFullMessageBytes(JsonSerializer.SerializeToUtf8Bytes(obj, jsonSerializerOptions), socket, cancelToken);
5662

57-
public static async Task SendFullMessageBytes(byte[] msg, WebSocket socket, CancellationToken cancelToken, int maxChunkSize = 256)
63+
public static async Task SendFullMessageBytes(byte[] msg, WebSocket socket, CancellationToken cancelToken,
64+
int maxChunkSize = 256)
5865
{
5966
var doneBytes = 0;
6067

@@ -74,6 +81,7 @@ public static async Task SendFullMessageBytes(byte[] msg, WebSocket socket, Canc
7481
/// </summary>
7582
public readonly struct DeserializeFailed
7683
{
84+
public required string Message { get; init; }
7785
public required Exception Exception { get; init; }
7886
}
7987

0 commit comments

Comments
 (0)