Page 2 of 2

Re: Smpp Dissconnect and re connect

Posted: Sat May 16, 2020 9:18 am
by zaikay
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.
Hi thanks!
I will try next week.

Re: Smpp Dissconnect and re connect

Posted: Thu May 21, 2020 2:53 pm
by alt
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.

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);
            }
        }

    }

Re: Smpp Dissconnect and re connect

Posted: Fri May 29, 2020 2:57 pm
by zaikay
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. Now it works.

Re: Smpp Dissconnect and re connect

Posted: Fri May 29, 2020 3:00 pm
by zaikay
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.
Thank you. My code was only for the test.