Skip to content

Commit 1eb6cde

Browse files
Changed auto-reconnect behavior, added AutoReconnect property
1 parent d320b86 commit 1eb6cde

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

UnityProject/Assets/LoomSDK/Source/Runtime/DAppChainClient.cs

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ public class DAppChainClient : IDisposable
3737
/// </summary>
3838
public TxMiddleware TxMiddleware { get; set; }
3939

40+
/// <summary>
41+
/// Whether clients will attempt to connect automatically when in Disconnected state
42+
/// before communicating.
43+
/// </summary>
44+
public bool AutoReconnect { get; set; } = true;
45+
4046
/// <summary>
4147
/// Logger to be used for logging, defaults to <see cref="NullLogger"/>.
4248
/// </summary>
@@ -107,10 +113,14 @@ public void Dispose()
107113
/// <returns>The nonce.</returns>
108114
public async Task<ulong> GetNonceAsync(string key)
109115
{
116+
if (this.readClient == null)
117+
throw new InvalidOperationException("Read client is not set");
118+
110119
await EnsureConnected();
111-
return await this.readClient.SendAsync<ulong, NonceParams>(
120+
string nonce = await this.readClient.SendAsync<string, NonceParams>(
112121
"nonce", new NonceParams { Key = key }
113122
);
123+
return UInt64.Parse(nonce);
114124
}
115125

116126
/// <summary>
@@ -120,6 +130,9 @@ public async Task<ulong> GetNonceAsync(string key)
120130
/// <exception cref="Exception">If a contract matching the given name wasn't found</exception>
121131
public async Task<Address> ResolveContractAddressAsync(string contractName)
122132
{
133+
if (this.readClient == null)
134+
throw new InvalidOperationException("Read client is not set");
135+
123136
await EnsureConnected();
124137
var addrStr = await this.readClient.SendAsync<string, ResolveParams>(
125138
"resolve", new ResolveParams { ContractName = contractName }
@@ -189,6 +202,9 @@ internal async Task<BroadcastTxResult> CommitTxAsync(IMessage tx)
189202
/// <returns>Deserialized response.</returns>
190203
internal async Task<T> QueryAsync<T>(Address contract, byte[] query, Address caller = default(Address), VMType vmType = VMType.Plugin)
191204
{
205+
if (this.readClient == null)
206+
throw new InvalidOperationException("Read client is not set");
207+
192208
var queryParams = new QueryParams
193209
{
194210
ContractAddress = contract.LocalAddress,
@@ -211,13 +227,16 @@ internal async Task<BroadcastTxResult> CommitTxAsync(IMessage tx)
211227
/// <exception cref="InvalidTxNonceException">Thrown when transaction is rejected by the DAppChain due to a bad nonce.</exception>
212228
private async Task<BroadcastTxResult> TryCommitTxAsync(IMessage tx)
213229
{
230+
if (this.writeClient == null)
231+
throw new InvalidOperationException("Write client was not set");
232+
233+
await EnsureConnected();
214234
byte[] txBytes = tx.ToByteArray();
215235
if (this.TxMiddleware != null)
216236
{
217237
txBytes = await this.TxMiddleware.Handle(txBytes);
218238
}
219239
string payload = CryptoBytes.ToBase64String(txBytes);
220-
await EnsureConnected();
221240
var result = await this.writeClient.SendAsync<BroadcastTxResult, string[]>("broadcast_tx_commit", new string[] { payload });
222241
if (result != null)
223242
{
@@ -241,19 +260,19 @@ private async void SubReadClient(EventHandler<RawChainEventArgs> handler)
241260
{
242261
try
243262
{
263+
await EnsureConnected();
244264
EventHandler<JsonRpcEventData> wrapper = (sender, e) =>
245265
{
246266
handler(this, new RawChainEventArgs
247267
(
248268
e.ContractAddress,
249269
e.CallerAddress,
250-
e.BlockHeight,
270+
UInt64.Parse(e.BlockHeight),
251271
e.Data,
252272
e.Topics
253273
));
254274
};
255275
this.eventSubs.Add(handler, wrapper);
256-
await EnsureConnected();
257276
await this.readClient.SubscribeAsync(wrapper);
258277
}
259278
catch (Exception e)
@@ -275,9 +294,20 @@ private async void UnsubReadClient(EventHandler<RawChainEventArgs> handler)
275294
}
276295
}
277296

278-
private async Task EnsureConnected() {
279-
await EnsureConnected(this.readClient);
280-
await EnsureConnected(this.writeClient);
297+
private async Task EnsureConnected()
298+
{
299+
if (!this.AutoReconnect)
300+
return;
301+
302+
if (this.readClient != null)
303+
{
304+
await EnsureConnected(this.readClient);
305+
}
306+
307+
if (this.writeClient != null)
308+
{
309+
await EnsureConnected(this.writeClient);
310+
}
281311
}
282312

283313
private async Task EnsureConnected(IRpcClient rpcClient) {

0 commit comments

Comments
 (0)