Skip to content

Commit 5ff4c3c

Browse files
committed
feat: add CancellationToken to ITonClient params (fix #8)
1 parent 9ce0661 commit 5ff4c3c

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

TonLibDotNet/ITonClient.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public interface ITonClient
2020
/// Will do nothing, if already connected.
2121
/// </remarks>
2222
/// <returns>Information about blockchain params, e.g. <see cref="OptionsConfigInfo.DefaultWalletId">DefaultWalletId</see>.</returns>
23-
Task<OptionsInfo?> InitIfNeeded();
23+
Task<OptionsInfo?> InitIfNeeded(CancellationToken cancellationToken = default);
2424

2525
/// <summary>
2626
/// De-initialize TonClient. Next call to <see cref="InitIfNeeded"/> will choose LiteServer again and connect to it.
@@ -38,9 +38,9 @@ public interface ITonClient
3838
/// InitIfNeeded();
3939
/// </code>
4040
/// </remarks>
41-
Task<OptionsInfo?> Reinit();
41+
Task<OptionsInfo?> Reinit(CancellationToken cancellationToken = default);
4242

43-
Task<TResponse> Execute<TResponse>(RequestBase<TResponse> request)
43+
Task<TResponse> Execute<TResponse>(RequestBase<TResponse> request, CancellationToken cancellationToken = default)
4444
where TResponse : TypeBase;
4545

4646
[Obsolete("Use TonUtils.Coins.FromNano()")]

TonLibDotNet/TonClient.cs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ public static void RegisterAssembly(System.Reflection.Assembly assembly)
6363
}
6464

6565
/// <inheritdoc />
66-
public async Task<OptionsInfo?> InitIfNeeded()
66+
public async Task<OptionsInfo?> InitIfNeeded(CancellationToken cancellationToken = default)
6767
{
6868
if (needReinit)
6969
{
7070
logger.LogDebug("Reinitializing...");
7171

72-
if (!await syncRoot.WaitAsync(tonOptions.ConcurrencyTimeout))
72+
if (!await syncRoot.WaitAsync(tonOptions.ConcurrencyTimeout, cancellationToken))
7373
{
7474
throw new TimeoutException("Failed while waiting for semaphore");
7575
}
@@ -95,14 +95,14 @@ public static void RegisterAssembly(System.Reflection.Assembly assembly)
9595
var localConfigSource = tonOptions.UseMainnet ? tonOptions.ConfigPathLocalMainnet : tonOptions.ConfigPathLocalTestnet;
9696
if (!string.IsNullOrEmpty(localConfigSource))
9797
{
98-
fullConfig = await File.ReadAllTextAsync(localConfigSource);
98+
fullConfig = await File.ReadAllTextAsync(localConfigSource, cancellationToken);
9999
logger.LogDebug("Used local config file: {Name}", localConfigSource);
100100
}
101101
else
102102
{
103103
var remoteConfigSource = tonOptions.UseMainnet ? tonOptions.ConfigPathMainnet : tonOptions.ConfigPathTestnet;
104104
using var httpClient = new HttpClient();
105-
fullConfig = await httpClient.GetStringAsync(remoteConfigSource).ConfigureAwait(false);
105+
fullConfig = await httpClient.GetStringAsync(remoteConfigSource, cancellationToken).ConfigureAwait(false);
106106
logger.LogDebug("Used internet config file: {Url}", remoteConfigSource);
107107
}
108108

@@ -115,7 +115,7 @@ public static void RegisterAssembly(System.Reflection.Assembly assembly)
115115

116116
tonOptions.Options.Config.ConfigJson = jdoc.ToJsonString();
117117

118-
OptionsInfo = await Execute(new Init(tonOptions.Options));
118+
OptionsInfo = await Execute(new Init(tonOptions.Options), cancellationToken);
119119
return OptionsInfo;
120120
}
121121

@@ -127,19 +127,19 @@ public virtual void Deinit()
127127
}
128128

129129
/// <inheritdoc />
130-
public Task<OptionsInfo?> Reinit()
130+
public Task<OptionsInfo?> Reinit(CancellationToken cancellationToken = default)
131131
{
132132
Deinit();
133-
return InitIfNeeded();
133+
return InitIfNeeded(cancellationToken);
134134
}
135135

136136
/// <inheritdoc />
137-
public async Task<TResponse> Execute<TResponse>(RequestBase<TResponse> request)
137+
public async Task<TResponse> Execute<TResponse>(RequestBase<TResponse> request, CancellationToken cancellationToken = default)
138138
where TResponse : TypeBase
139139
{
140140
if (client == null)
141141
{
142-
if (!await syncRoot.WaitAsync(tonOptions.ConcurrencyTimeout))
142+
if (!await syncRoot.WaitAsync(tonOptions.ConcurrencyTimeout, cancellationToken))
143143
{
144144
throw new TimeoutException("Failed while waiting for semaphore");
145145
}
@@ -169,14 +169,14 @@ public async Task<TResponse> Execute<TResponse>(RequestBase<TResponse> request)
169169
throw new InvalidOperationException($"Must call {nameof(InitIfNeeded)}() first");
170170
}
171171

172-
if (!await syncRoot.WaitAsync(tonOptions.ConcurrencyTimeout))
172+
if (!await syncRoot.WaitAsync(tonOptions.ConcurrencyTimeout, cancellationToken))
173173
{
174174
throw new TimeoutException("Failed while waiting for semaphore");
175175
}
176176

177177
try
178178
{
179-
var res = await ExecuteInternalAsync(request);
179+
var res = await ExecuteInternalAsync(request, cancellationToken);
180180

181181
if (request is Init)
182182
{
@@ -223,7 +223,7 @@ public void Dispose()
223223
Dispose(disposing: false);
224224
}
225225

226-
protected async Task<TResponse> ExecuteInternalAsync<TResponse>(RequestBase<TResponse> request)
226+
protected async Task<TResponse> ExecuteInternalAsync<TResponse>(RequestBase<TResponse> request, CancellationToken cancellationToken = default)
227227
where TResponse : TypeBase
228228
{
229229
if (client == null)
@@ -253,9 +253,13 @@ protected async Task<TResponse> ExecuteInternalAsync<TResponse>(RequestBase<TRes
253253

254254
while (true)
255255
{
256+
cancellationToken.ThrowIfCancellationRequested();
257+
256258
var respTextPtr = tonlib_client_json_receive(client.Value, tonOptions.TonLibTimeout.TotalSeconds);
257259
var respText = Marshal.PtrToStringAnsi(respTextPtr);
258260

261+
cancellationToken.ThrowIfCancellationRequested();
262+
259263
if (string.IsNullOrEmpty(respText))
260264
{
261265
throw new TonClientException(0, "Empty response received");
@@ -303,7 +307,7 @@ protected async Task<TResponse> ExecuteInternalAsync<TResponse>(RequestBase<TRes
303307
if (DateTimeOffset.UtcNow < endOfLoop)
304308
{
305309
var delay = (ssip.ToSeqno - ssip.CurrentSeqno) < 1000 ? 50 : 500;
306-
await Task.Delay(delay);
310+
await Task.Delay(delay, cancellationToken);
307311
continue;
308312
}
309313
}

0 commit comments

Comments
 (0)