Hi thanks!alt wrote: Thu May 14, 2020 2:11 pm Could you test the version 2.8.1-beta-2021 as well? There are some improvements for connection recovery.
I will try next week.
Hi thanks!alt wrote: Thu May 14, 2020 2:11 pm Could you test the version 2.8.1-beta-2021 as well? There are some improvements for connection recovery.
Code: Select all
public static class SmppClientExtension
{
private static ConcurrentDictionary<SmppClient, BoundWaiter> _boundWaiters = new ConcurrentDictionary<SmppClient, BoundWaiter>();
class BoundWaiter
{
public TaskCompletionSource<bool> TaskSource;
public CancellationTokenRegistration CancelRegistration;
}
public static Task WaitForBound(this SmppClient client)
{
return WaitForBound(client, CancellationToken.None);
}
public static Task WaitForBound(this SmppClient client, CancellationToken cancellationToken)
{
if (client.Status == ConnectionStatus.Bound) return Task.FromResult(true);
if (client.ConnectionRecovery)
{
lock (_boundWaiters)
{
return _boundWaiters.GetOrAdd(client, c =>
{
BoundWaiter waiter = new BoundWaiter();
waiter.TaskSource =
new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
waiter.CancelRegistration = cancellationToken.Register(c1 =>
{
var cancelClient = (SmppClient) c1;
BoundWaiter w;
if (_boundWaiters.TryRemove(cancelClient, out w))
{
cancelClient.evRecoverySucceeded -= OnRecoverySucceeded;
w.TaskSource.TrySetCanceled(cancellationToken);
}
}, c);
c.evRecoverySucceeded += OnRecoverySucceeded;
return waiter;
}).TaskSource.Task;
}
}
return Task.FromCanceled(cancellationToken);
}
static void OnRecoverySucceeded(object sender, BindResp data)
{
SmppClient recoveryClient = (SmppClient) sender;
BoundWaiter w;
if (_boundWaiters.TryRemove(recoveryClient, out w))
{
recoveryClient.evRecoverySucceeded -= OnRecoverySucceeded;
w.CancelRegistration.Dispose();
w.TaskSource.TrySetResult(true);
}
}
}
Thank you. Now it works.alt wrote: Thu May 14, 2020 2:11 pm Could you test the version 2.8.1-beta-2021 as well? There are some improvements for connection recovery.
Thank you. My code was only for the test.alt wrote: Thu May 21, 2020 2:53 pm Please note that Thread.Sleep method blocks all worker threads in the ThreadPool. Moreover the InternalSendMessageAsync method overloads the ThreadPool queue with unnecessary Tasks. As the result your application and library cannot do real work.
It would be better if your loop waits from connection recovery.
You can try to use WaitForBound extension method in the loop.