Skip to content

Commit 834ff89

Browse files
committed
Move TimeOut and BufferSize to new LiveQueryServerConnectionData and many improvements
1 parent 84f7060 commit 834ff89

16 files changed

+146
-73
lines changed

Parse.sln

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1717
EndProject
1818
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Parse.Tests", "Parse.Tests\Parse.Tests.csproj", "{FEB46D0F-384C-4F27-9E0E-F4A636768C90}"
1919
EndProject
20-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ParseApp", "ParseApp\ParseApp.csproj", "{71EF1783-2BAA-4119-A666-B7DCA3FD3085}"
21-
EndProject
2220
Global
2321
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2422
Debug|Any CPU = Debug|Any CPU
@@ -33,10 +31,6 @@ Global
3331
{FEB46D0F-384C-4F27-9E0E-F4A636768C90}.Debug|Any CPU.Build.0 = Debug|Any CPU
3432
{FEB46D0F-384C-4F27-9E0E-F4A636768C90}.Release|Any CPU.ActiveCfg = Release|Any CPU
3533
{FEB46D0F-384C-4F27-9E0E-F4A636768C90}.Release|Any CPU.Build.0 = Release|Any CPU
36-
{71EF1783-2BAA-4119-A666-B7DCA3FD3085}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
37-
{71EF1783-2BAA-4119-A666-B7DCA3FD3085}.Debug|Any CPU.Build.0 = Debug|Any CPU
38-
{71EF1783-2BAA-4119-A666-B7DCA3FD3085}.Release|Any CPU.ActiveCfg = Release|Any CPU
39-
{71EF1783-2BAA-4119-A666-B7DCA3FD3085}.Release|Any CPU.Build.0 = Release|Any CPU
4034
EndGlobalSection
4135
GlobalSection(SolutionProperties) = preSolution
4236
HideSolutionNode = FALSE

Parse/Abstractions/Infrastructure/CustomServiceHub.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public abstract class CustomServiceHub : ICustomServiceHub
6464

6565
public virtual IServerConnectionData ServerConnectionData => Services.ServerConnectionData;
6666

67-
public virtual IServerConnectionData LiveQueryServerConnectionData => Services.LiveQueryServerConnectionData;
67+
public virtual ILiveQueryServerConnectionData LiveQueryServerConnectionData => Services.LiveQueryServerConnectionData;
6868

6969
public virtual IParseDataDecoder Decoder => Services.Decoder;
7070

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace Parse.Abstractions.Infrastructure;
2+
3+
public interface ILiveQueryServerConnectionData : IServerConnectionData
4+
{
5+
/// <summary>
6+
/// Represents the default timeout duration, in milliseconds.
7+
/// </summary>
8+
public const int DefaultTimeOut = 5000; // 5 seconds
9+
10+
/// <summary>
11+
/// The timeout duration, in milliseconds, used for various operations, such as
12+
/// establishing a connection or completing a subscription.
13+
/// </summary>
14+
int TimeOut { get; set; }
15+
16+
/// <summary>
17+
/// The default buffer size, in bytes.
18+
/// </summary>
19+
public const int DefaultBufferSize = 4096; // 4MB
20+
21+
/// <summary>
22+
/// The buffer size, in bytes, used for the WebSocket operations to handle incoming messages.
23+
/// </summary>
24+
int MessageBufferSize { get; set; }
25+
}

Parse/Abstractions/Infrastructure/IMutableServiceHub.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace Parse.Abstractions.Infrastructure;
1919
public interface IMutableServiceHub : IServiceHub
2020
{
2121
IServerConnectionData ServerConnectionData { set; }
22-
IServerConnectionData LiveQueryServerConnectionData { set; }
22+
ILiveQueryServerConnectionData LiveQueryServerConnectionData { set; }
2323
IMetadataController MetadataController { set; }
2424

2525
IServiceHubCloner Cloner { set; }

Parse/Abstractions/Infrastructure/IServiceHub.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public interface IServiceHub
2727
/// The current server connection data that the Parse SDK has been initialized with.
2828
/// </summary>
2929
IServerConnectionData ServerConnectionData { get; }
30-
IServerConnectionData LiveQueryServerConnectionData { get; }
30+
ILiveQueryServerConnectionData LiveQueryServerConnectionData { get; }
3131
IMetadataController MetadataController { get; }
3232

3333
IServiceHubCloner Cloner { get; }

Parse/Abstractions/Platform/LiveQueries/IParseLiveQueryController.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,6 @@ namespace Parse.Abstractions.Platform.LiveQueries;
1111
/// </summary>
1212
public interface IParseLiveQueryController
1313
{
14-
/// <summary>
15-
/// Gets or sets the timeout duration, in milliseconds, for operations performed by the LiveQuery controller.
16-
/// </summary>
17-
/// <remarks>
18-
/// This property determines the maximum time the system will wait for an operation to complete
19-
/// before timing out. It is particularly relevant to tasks such as establishing a connection
20-
/// to the LiveQuery server or awaiting responses for subscription or query update requests.
21-
/// Developers can use this property to configure timeouts based on the expected network
22-
/// and performance conditions of the application environment.
23-
/// </remarks>
24-
public int TimeOut { get; set; }
25-
2614
/// <summary>
2715
/// Event triggered when an error occurs during the operation of the ParseLiveQueryController.
2816
/// </summary>

Parse/Abstractions/Platform/LiveQueries/IParseLiveQuerySubscription.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@
66

77
namespace Parse.Abstractions.Platform.LiveQueries;
88

9-
/// <summary>
10-
/// Represents a live query subscription that is used with Parse's Live Query service.
11-
/// It allows real-time monitoring and event handling for object changes that match
12-
/// a specified query.
13-
/// </summary>
149
public interface IParseLiveQuerySubscription
1510
{
1611
/// <summary>

Parse/Infrastructure/Execution/TextWebSocketClient.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Parse.Infrastructure.Execution;
1212
/// Represents a WebSocket client that allows connecting to a WebSocket server, sending messages, and receiving messages.
1313
/// Implements the <c>IWebSocketClient</c> interface for WebSocket operations.
1414
/// </summary>
15-
class TextWebSocketClient : IWebSocketClient
15+
class TextWebSocketClient(int bufferSize) : IWebSocketClient
1616
{
1717
/// <summary>
1818
/// A private instance of the ClientWebSocket class used to manage the WebSocket connection.
@@ -43,6 +43,8 @@ class TextWebSocketClient : IWebSocketClient
4343

4444
private readonly object connectionLock = new object();
4545

46+
private int BufferSize { get; } = bufferSize;
47+
4648
/// <summary>
4749
/// Opens a WebSocket connection to the specified server URI and starts listening for messages.
4850
/// If the connection is already open or in a connecting state, this method does nothing.
@@ -54,14 +56,19 @@ class TextWebSocketClient : IWebSocketClient
5456
/// </returns>
5557
public async Task OpenAsync(string serverUri, CancellationToken cancellationToken = default)
5658
{
59+
ClientWebSocket webSocketToConnect = null;
5760
lock (connectionLock)
5861
{
5962
webSocket ??= new ClientWebSocket();
63+
if (webSocket.State != WebSocketState.Open && webSocket.State != WebSocketState.Connecting)
64+
{
65+
webSocketToConnect = webSocket;
66+
}
6067
}
6168

62-
if (webSocket.State != WebSocketState.Open && webSocket.State != WebSocketState.Connecting)
69+
if (webSocketToConnect is not null)
6370
{
64-
await webSocket.ConnectAsync(new Uri(serverUri), cancellationToken);
71+
await webSocketToConnect.ConnectAsync(new Uri(serverUri), cancellationToken);
6572
StartListening(cancellationToken);
6673
}
6774
}
@@ -74,12 +81,17 @@ public async Task OpenAsync(string serverUri, CancellationToken cancellationToke
7481
/// <returns>
7582
/// A task representing the asynchronous operation of closing the WebSocket connection.
7683
/// </returns>
77-
public async Task CloseAsync(CancellationToken cancellationToken = default) =>
78-
await webSocket?.CloseAsync(WebSocketCloseStatus.NormalClosure, String.Empty, cancellationToken)!;
84+
public async Task CloseAsync(CancellationToken cancellationToken = default)
85+
{
86+
if (webSocket is not null)
87+
{
88+
await webSocket?.CloseAsync(WebSocketCloseStatus.NormalClosure, String.Empty, cancellationToken)!;
89+
}
90+
}
7991

8092
private async Task ListenForMessages(CancellationToken cancellationToken)
8193
{
82-
byte[] buffer = new byte[1024 * 4];
94+
byte[] buffer = new byte[BufferSize];
8395

8496
try
8597
{
@@ -183,6 +195,10 @@ private void StartListening(CancellationToken cancellationToken)
183195
public async Task SendAsync(string message, CancellationToken cancellationToken = default)
184196
{
185197
ArgumentNullException.ThrowIfNull(webSocket);
198+
if (webSocket.State != WebSocketState.Open)
199+
{
200+
throw new InvalidOperationException($"WebSocket is not in Open state. Current state: {webSocket.State}");
201+
}
186202
await webSocket.SendAsync(Encoding.UTF8.GetBytes(message), WebSocketMessageType.Text, true, cancellationToken);
187203
}
188204
}

Parse/Infrastructure/LateInitializedMutableServiceHub.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,6 @@ public IParseCommandRunner CommandRunner
7272
set => LateInitializer.SetValue(value);
7373
}
7474

75-
public IWebSocketClient WebSocketClient
76-
{
77-
get => LateInitializer.GetValue<IWebSocketClient>(() => new TextWebSocketClient { });
78-
set => LateInitializer.SetValue(value);
79-
}
80-
8175
public IParseCloudCodeController CloudCodeController
8276
{
8377
get => LateInitializer.GetValue<IParseCloudCodeController>(() => new ParseCloudCodeController(CommandRunner, Decoder));
@@ -108,12 +102,6 @@ public IParseQueryController QueryController
108102
set => LateInitializer.SetValue(value);
109103
}
110104

111-
public IParseLiveQueryController LiveQueryController
112-
{
113-
get => LateInitializer.GetValue<IParseLiveQueryController>(() => new ParseLiveQueryController(WebSocketClient, Decoder));
114-
set => LateInitializer.SetValue(value);
115-
}
116-
117105
public IParseSessionController SessionController
118106
{
119107
get => LateInitializer.GetValue<IParseSessionController>(() => new ParseSessionController(CommandRunner, Decoder));
@@ -174,6 +162,19 @@ public IParseInstallationDataFinalizer InstallationDataFinalizer
174162
set => LateInitializer.SetValue(value);
175163
}
176164

165+
166+
public IWebSocketClient WebSocketClient
167+
{
168+
get => LateInitializer.GetValue<IWebSocketClient>(() => LiveQueryServerConnectionData is null ? null : new TextWebSocketClient(LiveQueryServerConnectionData.MessageBufferSize));
169+
set => LateInitializer.SetValue(value);
170+
}
171+
172+
public IParseLiveQueryController LiveQueryController
173+
{
174+
get => LateInitializer.GetValue<IParseLiveQueryController>(() => LiveQueryServerConnectionData is null ? null : new ParseLiveQueryController(LiveQueryServerConnectionData.TimeOut, WebSocketClient, Decoder));
175+
set => LateInitializer.SetValue(value);
176+
}
177+
177178
public IServerConnectionData ServerConnectionData { get; set; }
178-
public IServerConnectionData LiveQueryServerConnectionData { get; set; }
179+
public ILiveQueryServerConnectionData LiveQueryServerConnectionData { get; set; }
179180
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System.Collections.Generic;
2+
using Parse.Abstractions.Infrastructure;
3+
4+
namespace Parse.Infrastructure;
5+
6+
/// <summary>
7+
/// Represents the configuration of the Parse Live Query server.
8+
/// </summary>
9+
public struct LiveQueryServerConnectionData : ILiveQueryServerConnectionData
10+
{
11+
public LiveQueryServerConnectionData() { }
12+
13+
internal bool Test { get; set; }
14+
15+
/// <summary>
16+
/// The timeout duration, in milliseconds, used for various operations, such as
17+
/// establishing a connection or completing a subscription.
18+
/// </summary>
19+
public int TimeOut { get; set; } = ILiveQueryServerConnectionData.DefaultTimeOut;
20+
21+
/// <summary>
22+
/// The buffer size, in bytes, used by the WebSocket client for communication operations.
23+
/// </summary>
24+
public int MessageBufferSize { get; set; } = ILiveQueryServerConnectionData.DefaultBufferSize;
25+
26+
/// <summary>
27+
/// The App ID of your app.
28+
/// </summary>
29+
public string ApplicationID { get; set; }
30+
31+
/// <summary>
32+
/// A URI pointing to the target Parse Server instance hosting the app targeted by <see cref="ApplicationID"/>.
33+
/// </summary>
34+
public string ServerURI { get; set; }
35+
36+
/// <summary>
37+
/// The .NET Key for the Parse app targeted by <see cref="ServerURI"/>.
38+
/// </summary>
39+
public string Key { get; set; }
40+
41+
/// <summary>
42+
/// The Master Key for the Parse app targeted by <see cref="Key"/>.
43+
/// </summary>
44+
public string MasterKey { get; set; }
45+
46+
/// <summary>
47+
/// Additional HTTP headers to be sent with network requests from the SDK.
48+
/// </summary>
49+
public IDictionary<string, string> Headers { get; set; }
50+
}

0 commit comments

Comments
 (0)