sochix / TLSharp

Telegram client library implemented in C#
1k stars 380 forks source link

Connect error after long work #888

Open alexey-prokopyev opened 4 years ago

alexey-prokopyev commented 4 years ago

Hello. I'm writing simple app that connects to the telegram server and requests members count of channel and do some other stuff. I want the application to work constantly and when the connection is broken, it will reconnect automatically. After 8..24 hours i get an exception in GetUserDialogsAsync function: System.AggregateException: One or more errors occurred. (Not connected!) ---> System.InvalidOperationException: Not connected! at TLSharp.Core.TelegramClient.RequestWithDcMigration(TLMethod request) at TLSharp.Core.TelegramClient.SendRequestAsync[T](TLMethod methodToExecute) at TLSharp.Core.TelegramClient.GetUserDialogsAsync(Int32 offsetDate, Int32 offsetId, TLAbsInputPeer offsetPeer, Int32 limit) --- End of inner exception stack trace --- at System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification) at Coworkings.UserClient.GetChannelInfo(String channelName) in C:\Develop\CoworkingBot\CoworkingBot\Clients\UserClient.cs:line 227`

Ok, in case of errors i try to reconnect:

        public bool Reconnect()
        {
            try
            {
                const int sleepTimeMs = 5000;

                client.Dispose();

                for (int i = 0; i < 3; i++)
                {
                    try
                    {
                       if (!client.IsConnected)
                        {
                            client.ConnectAsync().Wait();
                            logger.Debug("Connect OK");
                        }

                        if (!client.IsConnected)
                        {
                            logger.Error("Ошибка подключения");
                            Thread.Sleep(sleepTimeMs);
                            continue;
                        }

                        if (!client.IsUserAuthorized())
                        {
                            logger.Error("Нет авторизации");
                            Thread.Sleep(sleepTimeMs);
                            continue;
                        }

                        if (client.IsUserAuthorized())
                        {
                            logger.Info("UserClient авторизован");
                        }

                        return IsInitialized();
                    }
                    catch (FloodException ex)
                    {
                        logger.Error("Received FloodException. Sleep to " + ex.TimeToWait + " sec...");
                        logger.Error(ex.ToString());
                        Thread.Sleep(ex.TimeToWait);
                        Thread.Sleep(sleepTimeMs);
                        continue;
                    }
                    catch (InvalidOperationException ex)
                    {
                        logger.Error(ex.ToString());
                        Thread.Sleep(sleepTimeMs);
                        continue;
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex.ToString());
                        Thread.Sleep(sleepTimeMs);
                        continue;
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
            }
            return false;
        }

But it doesn't work. Reconnect() returns true, but client.GetUserDialogsAsync() throw exception again.

dliasaitov commented 3 years ago

Same problem, maybe u found solution?