diff options
author | Karen Martikyan <karen.martikyan@digitain.com> | 2020-11-07 13:30:09 +0300 |
---|---|---|
committer | Karen Martikyan <karen.martikyan@digitain.com> | 2020-11-07 13:30:09 +0300 |
commit | b4286bda105c55c6a526db8712875fe76ecca329 (patch) | |
tree | 1e8d4e4cd441a7647db4582978b04d0ee64e6649 /Duplicati/Library | |
parent | 1f55b1854e8e28e2e965faa24f4a7f4440bf314f (diff) |
Working state
Diffstat (limited to 'Duplicati/Library')
-rw-r--r-- | Duplicati/Library/Backend/Telegram/Extensions/TelegramClientExtensions.cs | 38 | ||||
-rw-r--r-- | Duplicati/Library/Backend/Telegram/TelegramBackend.cs | 69 |
2 files changed, 53 insertions, 54 deletions
diff --git a/Duplicati/Library/Backend/Telegram/Extensions/TelegramClientExtensions.cs b/Duplicati/Library/Backend/Telegram/Extensions/TelegramClientExtensions.cs index 370298c45..7f03af2bd 100644 --- a/Duplicati/Library/Backend/Telegram/Extensions/TelegramClientExtensions.cs +++ b/Duplicati/Library/Backend/Telegram/Extensions/TelegramClientExtensions.cs @@ -1,4 +1,6 @@ -using System.Net.Sockets; +using System; +using System.IO; +using System.Net.Sockets; using System.Reflection; using TLSharp.Core; using TLSharp.Core.Network; @@ -42,5 +44,39 @@ namespace Duplicati.Library.Backend.Extensions return true; } + + public static bool FlushStreams(this TelegramClient client) + { + if (client.IsConnected == false) + { + return false; + } + + var transportFieldInfo = typeof(TelegramClient).GetField("transport", _bindingFlags); + var transportField = (TcpTransport)transportFieldInfo.GetValue(client); + + if (transportField == null || transportField.IsConnected == false) + { + return false; + } + + var tcpClientFieldInfo = typeof(TcpTransport).GetField("tcpClient", _bindingFlags); + var tcpClient = (TcpClient)tcpClientFieldInfo.GetValue(transportField); + + if (tcpClient == null || tcpClient.Connected == false) + { + return false; + } + + var availableDataLength = tcpClient.Available; + //tcpClient.LingerState. + Console.WriteLine($"Available data is {availableDataLength}"); + if (availableDataLength != 0) + { + tcpClient.GetStream().Read(new byte[availableDataLength], 0, availableDataLength); + } + + return true; + } } }
\ No newline at end of file diff --git a/Duplicati/Library/Backend/Telegram/TelegramBackend.cs b/Duplicati/Library/Backend/Telegram/TelegramBackend.cs index 8eec58154..d61e231e6 100644 --- a/Duplicati/Library/Backend/Telegram/TelegramBackend.cs +++ b/Duplicati/Library/Backend/Telegram/TelegramBackend.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; -using System.Reflection; using System.Threading; using System.Threading.Tasks; using Duplicati.Library.Backend.Extensions; @@ -14,7 +13,6 @@ using TeleSharp.TL.Channels; using TeleSharp.TL.Messages; using TLSharp.Core; using TLSharp.Core.Exceptions; -using TLSharp.Core.Network; using TLSharp.Core.Network.Exceptions; using TLSharp.Core.Utils; using TLRequestDeleteMessages = TeleSharp.TL.Channels.TLRequestDeleteMessages; @@ -23,7 +21,7 @@ namespace Duplicati.Library.Backend { public class Telegram : IStreamingBackend, IBackend { - private TelegramClient m_telegramClient; + private static TelegramClient m_telegramClient; private readonly EncryptedFileSessionStore m_encSessionStore; private static readonly object m_lockObj = new object(); @@ -108,7 +106,9 @@ namespace Duplicati.Library.Backend public void Dispose() { - m_telegramClient?.Dispose(); + // Do not dispose m_telegramClient. + // There are bugs connected with reusing + // the old sockets } public string DisplayName { get; } = Strings.DisplayName; @@ -118,19 +118,11 @@ namespace Duplicati.Library.Backend { return SafeExecute<IEnumerable<IFileEntry>>(() => { - try - { - Authenticate(); - EnsureChannelCreated(); - var fileInfos = ListChannelFileInfos(); - var result = fileInfos.Select(fi => fi.ToFileEntry()); - return result; - } - catch (Exception nrf) - { - Console.WriteLine(nrf); - throw; - } + Authenticate(); + EnsureChannelCreated(); + var fileInfos = ListChannelFileInfos(); + var result = fileInfos.Select(fi => fi.ToFileEntry()); + return result; }, nameof(List)); } @@ -138,6 +130,7 @@ namespace Duplicati.Library.Backend public Task PutAsync(string remotename, Stream stream, CancellationToken cancelToken) { Test(); + cancelToken.ThrowIfCancellationRequested(); SafeExecute(() => { cancelToken.ThrowIfCancellationRequested(); @@ -150,7 +143,7 @@ namespace Duplicati.Library.Backend cancelToken.ThrowIfCancellationRequested(); var file = m_telegramClient.UploadFile(remotename, sr, cancelToken).GetAwaiter().GetResult(); - + cancelToken.ThrowIfCancellationRequested(); var inputPeerChannel = new TLInputPeerChannel {ChannelId = channel.Id, AccessHash = (long)channel.AccessHash}; var fileNameAttribute = new TLDocumentAttributeFilename @@ -306,11 +299,7 @@ namespace Duplicati.Library.Backend public void Test() { - // No need to use SafeExecute methods - lock (m_lockObj) - { - Authenticate(); - } + SafeExecute(Authenticate, nameof(Authenticate)); } public void CreateFolder() @@ -438,45 +427,19 @@ namespace Duplicati.Library.Backend } - private void EnsureConnected(CancellationToken? cancelToken = null) + private void EnsureConnected(CancellationToken cancelToken = default) { if (m_telegramClient.IsReallyConnected()) { return; } - - var cts = new CancellationTokenSource(); - var lastException = (Exception)null; - for (int i = 0; i < 3; i++) - { - cancelToken?.ThrowIfCancellationRequested(); - try - { - m_telegramClient.ConnectAsync(false, cts.Token).Wait(TimeSpan.FromSeconds(5)); - if (m_telegramClient.IsConnected) - { - break; - } - } - catch (Exception e) - { - lastException = e; - } - } + + m_telegramClient.ConnectAsync(false, cancelToken).GetAwaiter().GetResult(); if (m_telegramClient.IsReallyConnected() == false) { - if (lastException != null) - { - throw lastException; - } - else - { - throw new WebException("Unable to connect to telegram"); - } + throw new WebException("Unable to connect to telegram"); } - - cts.Cancel(); } private bool IsAuthenticated() |