sochix / TLSharp

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

disconnection without possibility to reconnect #930

Closed solarin closed 4 years ago

solarin commented 4 years ago

the production code i am running is conceptually as simple as this:

while(stayLive)
{ 
    if(!client.IsConnected)
       await client.ConnectAsync();

   jf(!client.IsUserAuthenticated())
   { ...} // auth process

  // do the dirty job

}

the thing is that there are two issues:

  1. client.IsConnected always returns true, unless it's fixed with my previous fix by adding a check on null on sender in IsConnected

  2. if for any reason the tcpClient in TcpTransport becomes disconnected, there is no way to reconnect it and the Send method will always throw an exception (below)

I propose to remove the tcpClient initialization in TcpTransport from the constructor and automatically reconnect the tcpClient if it's disconnected rather than throwing the exception

System.InvalidOperationException: Client not connected to server. at TgSharp.Core.Network.TcpTransport.d4.MoveNext() in \TgSharp\src\TgSharp.Core\Network\TcpTransport.cs:line 39 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult() at TgSharp.Core.Network.MtProtoSender.d6.MoveNext() in \TgSharp\src\TgSharp.Core\Network\MtProtoSender.cs:line 100 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult() at TgSharp.Core.Network.MtProtoSender.d5.MoveNext() in \TgSharp\src\TgSharp.Core\Network\MtProtoSender.cs:line 62 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult() at TgSharp.Core.TelegramClient.d12.MoveNext() in \TgSharp\src\TgSharp.Core\TelegramClient.cs:line 106 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at GroupCasher.GroupManager.d__17.MoveNext() in C:\Users\mengh\source\repos\GroupCasher\GroupCasher\GroupManager.cs:line 125

knocte commented 4 years ago

I propose to remove the tcpClient initialization in TcpTransport from the constructor

I think I agree with this. PR?

and automatically reconnect the tcpClient if it's disconnected rather than throwing the exception

But I don't agree with this.

solarin commented 4 years ago

why not?

try at least to reconnect a couple of times and then throw the exception.

is it too dirty?

knocte commented 4 years ago

I'd prefer if the user of TLSharp handles the reconnection.