Smpp Dissconnect and re connect

zaikay
Posts: 9
Joined: Mon May 04, 2020 1:51 pm

Re: Smpp Dissconnect and re connect

Post by zaikay » Sat May 16, 2020 9:18 am

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.
alt
Site Admin
Posts: 985
Joined: Tue Apr 25, 2006 9:45 am

Re: Smpp Dissconnect and re connect

Post by alt » 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.

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

    }
zaikay
Posts: 9
Joined: Mon May 04, 2020 1:51 pm

Re: Smpp Dissconnect and re connect

Post by zaikay » Fri May 29, 2020 2:57 pm

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.
zaikay
Posts: 9
Joined: Mon May 04, 2020 1:51 pm

Re: Smpp Dissconnect and re connect

Post by zaikay » Fri May 29, 2020 3:00 pm

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.
Post Reply