mirror of
https://github.com/sochix/TLSharp.git
synced 2025-12-06 08:02:00 +01:00
Fix dc loop #719
This commit is contained in:
parent
f2a394ea15
commit
558d58a950
|
|
@ -66,7 +66,7 @@ namespace TLSharp.Core.Network
|
|||
|
||||
byte[] msgKey;
|
||||
byte[] ciphertext;
|
||||
using (MemoryStream plaintextPacket = makeMemory(8 + 8 + 8 + 4 + 4 + packet.Length))
|
||||
using (MemoryStream plaintextPacket = MakeMemory(8 + 8 + 8 + 4 + 4 + packet.Length))
|
||||
{
|
||||
using (BinaryWriter plaintextWriter = new BinaryWriter(plaintextPacket))
|
||||
{
|
||||
|
|
@ -82,7 +82,7 @@ namespace TLSharp.Core.Network
|
|||
}
|
||||
}
|
||||
|
||||
using (MemoryStream ciphertextPacket = makeMemory(8 + 16 + ciphertext.Length))
|
||||
using (MemoryStream ciphertextPacket = MakeMemory(8 + 16 + ciphertext.Length))
|
||||
{
|
||||
using (BinaryWriter writer = new BinaryWriter(ciphertextPacket))
|
||||
{
|
||||
|
|
@ -136,7 +136,7 @@ namespace TLSharp.Core.Network
|
|||
using (var messageStream = new MemoryStream(result.Item1, false))
|
||||
using (var messageReader = new BinaryReader(messageStream))
|
||||
{
|
||||
processMessage(result.Item2, result.Item3, messageReader, request);
|
||||
ProcessMessage(result.Item2, result.Item3, messageReader, request);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -156,7 +156,7 @@ namespace TLSharp.Core.Network
|
|||
await Receive(pingRequest);
|
||||
}
|
||||
|
||||
private bool processMessage(ulong messageId, int sequence, BinaryReader messageReader, TeleSharp.TL.TLMethod request)
|
||||
private bool ProcessMessage(ulong messageId, int sequence, BinaryReader messageReader, TeleSharp.TL.TLMethod request)
|
||||
{
|
||||
// TODO: check salt
|
||||
// TODO: check sessionid
|
||||
|
|
@ -240,7 +240,7 @@ namespace TLSharp.Core.Network
|
|||
using (MemoryStream packedStream = new MemoryStream(packedData, false))
|
||||
using (BinaryReader compressedReader = new BinaryReader(packedStream))
|
||||
{
|
||||
processMessage(messageId, sequence, compressedReader, request);
|
||||
ProcessMessage(messageId, sequence, compressedReader, request);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
|
@ -503,7 +503,7 @@ namespace TLSharp.Core.Network
|
|||
long beginPosition = messageReader.BaseStream.Position;
|
||||
try
|
||||
{
|
||||
if (!processMessage(innerMessageId, sequence, messageReader, request))
|
||||
if (!ProcessMessage(innerMessageId, sequence, messageReader, request))
|
||||
{
|
||||
messageReader.BaseStream.Position = beginPosition + innerLength;
|
||||
}
|
||||
|
|
@ -518,7 +518,7 @@ namespace TLSharp.Core.Network
|
|||
return false;
|
||||
}
|
||||
|
||||
private MemoryStream makeMemory(int len)
|
||||
private MemoryStream MakeMemory(int len)
|
||||
{
|
||||
return new MemoryStream(new byte[len], 0, len, true, true);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,8 +9,9 @@
|
|||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>TLSharp.Core</RootNamespace>
|
||||
<AssemblyName>TLSharp.Core</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<TargetFrameworkProfile />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
|
|
|
|||
|
|
@ -22,13 +22,13 @@ namespace TLSharp.Core
|
|||
public class TelegramClient : IDisposable
|
||||
{
|
||||
private MtProtoSender _sender;
|
||||
private AuthKey _key;
|
||||
private readonly AuthKey _key;
|
||||
private TcpTransport _transport;
|
||||
private string _apiHash = "";
|
||||
private int _apiId = 0;
|
||||
private Session _session;
|
||||
private List<TLDcOption> dcOptions;
|
||||
private TcpClientConnectionHandler _handler;
|
||||
private readonly TcpClientConnectionHandler _handler;
|
||||
|
||||
public TelegramClient(int apiId, string apiHash,
|
||||
ISessionStore store = null, string sessionUserId = "session", TcpClientConnectionHandler handler = null)
|
||||
|
|
@ -78,7 +78,7 @@ namespace TLSharp.Core
|
|||
dcOptions = ((TLConfig)invokewithLayer.Response).DcOptions.ToList();
|
||||
}
|
||||
|
||||
private async Task ReconnectToDcAsync(int dcId)
|
||||
private async Task ReconnectToDcAsync(int dcId, int times)
|
||||
{
|
||||
if (dcOptions == null || !dcOptions.Any())
|
||||
throw new InvalidOperationException($"Can't reconnect. Establish initial connection first.");
|
||||
|
|
@ -87,7 +87,7 @@ namespace TLSharp.Core
|
|||
if (_session.TLUser != null)
|
||||
{
|
||||
TLRequestExportAuthorization exportAuthorization = new TLRequestExportAuthorization() { DcId = dcId };
|
||||
exported = await SendRequestAsync<TLExportedAuthorization>(exportAuthorization);
|
||||
exported = await SendRequestAsync<TLExportedAuthorization>(exportAuthorization,times);
|
||||
}
|
||||
|
||||
var dc = dcOptions.First(d => d.Id == dcId);
|
||||
|
|
@ -101,12 +101,14 @@ namespace TLSharp.Core
|
|||
if (_session.TLUser != null)
|
||||
{
|
||||
TLRequestImportAuthorization importAuthorization = new TLRequestImportAuthorization() { Id = exported.Id, Bytes = exported.Bytes };
|
||||
var imported = await SendRequestAsync<TLAuthorization>(importAuthorization);
|
||||
var imported = await SendRequestAsync<TLAuthorization>(importAuthorization,times);
|
||||
OnUserAuthenticated(((TLUser)imported.User));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private async Task RequestWithDcMigration(TLMethod request)
|
||||
private async Task RequestWithDcMigration(TLMethod request, int times)
|
||||
{
|
||||
if (_sender == null)
|
||||
throw new InvalidOperationException("Not connected!");
|
||||
|
|
@ -122,10 +124,21 @@ namespace TLSharp.Core
|
|||
}
|
||||
catch(DataCenterMigrationException e)
|
||||
{
|
||||
await ReconnectToDcAsync(e.DC);
|
||||
if (times <= 1)
|
||||
{
|
||||
await ReconnectToDcAsync(e.DC, times + 1);
|
||||
// prepare the request for another try
|
||||
request.ConfirmReceived = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
catch (TLSharp.Core.Network.FloodException e)
|
||||
{
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -141,7 +154,7 @@ namespace TLSharp.Core
|
|||
|
||||
var authCheckPhoneRequest = new TLRequestCheckPhone() { PhoneNumber = phoneNumber };
|
||||
|
||||
await RequestWithDcMigration(authCheckPhoneRequest);
|
||||
await RequestWithDcMigration(authCheckPhoneRequest,0);
|
||||
|
||||
return authCheckPhoneRequest.Response.PhoneRegistered;
|
||||
}
|
||||
|
|
@ -153,7 +166,7 @@ namespace TLSharp.Core
|
|||
|
||||
var request = new TLRequestSendCode() { PhoneNumber = phoneNumber, ApiId = _apiId, ApiHash = _apiHash };
|
||||
|
||||
await RequestWithDcMigration(request);
|
||||
await RequestWithDcMigration(request,0);
|
||||
|
||||
return request.Response.PhoneCodeHash;
|
||||
}
|
||||
|
|
@ -171,7 +184,7 @@ namespace TLSharp.Core
|
|||
|
||||
var request = new TLRequestSignIn() { PhoneNumber = phoneNumber, PhoneCodeHash = phoneCodeHash, PhoneCode = code };
|
||||
|
||||
await RequestWithDcMigration(request);
|
||||
await RequestWithDcMigration(request,0);
|
||||
|
||||
OnUserAuthenticated(((TLUser)request.Response.User));
|
||||
|
||||
|
|
@ -182,7 +195,7 @@ namespace TLSharp.Core
|
|||
{
|
||||
var request = new TLRequestGetPassword();
|
||||
|
||||
await RequestWithDcMigration(request);
|
||||
await RequestWithDcMigration(request,0);
|
||||
|
||||
return ((TLPassword)request.Response);
|
||||
}
|
||||
|
|
@ -198,7 +211,7 @@ namespace TLSharp.Core
|
|||
|
||||
var request = new TLRequestCheckPassword() { PasswordHash = password_hash };
|
||||
|
||||
await RequestWithDcMigration(request);
|
||||
await RequestWithDcMigration(request,0);
|
||||
|
||||
OnUserAuthenticated(((TLUser)request.Response.User));
|
||||
|
||||
|
|
@ -209,15 +222,15 @@ namespace TLSharp.Core
|
|||
{
|
||||
var request = new TLRequestSignUp() { PhoneNumber = phoneNumber, PhoneCode = code, PhoneCodeHash = phoneCodeHash, FirstName = firstName, LastName = lastName };
|
||||
|
||||
await RequestWithDcMigration(request);
|
||||
await RequestWithDcMigration(request,0);
|
||||
|
||||
OnUserAuthenticated(((TLUser)request.Response.User));
|
||||
|
||||
return ((TLUser)request.Response.User);
|
||||
}
|
||||
public async Task<T> SendRequestAsync<T>(TLMethod methodToExecute)
|
||||
public async Task<T> SendRequestAsync<T>(TLMethod methodToExecute, int times)
|
||||
{
|
||||
await RequestWithDcMigration(methodToExecute);
|
||||
await RequestWithDcMigration(methodToExecute, times);
|
||||
|
||||
var result = methodToExecute.GetType().GetProperty("Response").GetValue(methodToExecute);
|
||||
|
||||
|
|
@ -231,7 +244,7 @@ namespace TLSharp.Core
|
|||
|
||||
var req = new TLRequestGetContacts() { Hash = "" };
|
||||
|
||||
return await SendRequestAsync<TLContacts>(req);
|
||||
return await SendRequestAsync<TLContacts>(req,0);
|
||||
}
|
||||
|
||||
public async Task<TLAbsUpdates> SendMessageAsync(TLAbsInputPeer peer, string message)
|
||||
|
|
@ -239,13 +252,13 @@ namespace TLSharp.Core
|
|||
if (!IsUserAuthorized())
|
||||
throw new InvalidOperationException("Authorize user first!");
|
||||
|
||||
return await SendRequestAsync<TLAbsUpdates>(
|
||||
new TLRequestSendMessage()
|
||||
var x = new TLRequestSendMessage()
|
||||
{
|
||||
Peer = peer,
|
||||
Message = message,
|
||||
RandomId = Helpers.GenerateRandomLong()
|
||||
});
|
||||
};
|
||||
return await SendRequestAsync<TLAbsUpdates>(x, 0);
|
||||
}
|
||||
|
||||
public async Task<Boolean> SendTypingAsync(TLAbsInputPeer peer)
|
||||
|
|
@ -255,7 +268,7 @@ namespace TLSharp.Core
|
|||
Action = new TLSendMessageTypingAction(),
|
||||
Peer = peer
|
||||
};
|
||||
return await SendRequestAsync<Boolean>(req);
|
||||
return await SendRequestAsync<Boolean>(req,0);
|
||||
}
|
||||
|
||||
public async Task<TLAbsDialogs> GetUserDialogsAsync(int offsetDate = 0, int offsetId = 0, TLAbsInputPeer offsetPeer = null, int limit = 100)
|
||||
|
|
@ -273,7 +286,7 @@ namespace TLSharp.Core
|
|||
OffsetPeer = offsetPeer,
|
||||
Limit = limit
|
||||
};
|
||||
return await SendRequestAsync<TLAbsDialogs>(req);
|
||||
return await SendRequestAsync<TLAbsDialogs>(req,0);
|
||||
}
|
||||
|
||||
public async Task<TLAbsUpdates> SendUploadedPhoto(TLAbsInputPeer peer, TLAbsInputFile file, string caption)
|
||||
|
|
@ -285,7 +298,7 @@ namespace TLSharp.Core
|
|||
ClearDraft = false,
|
||||
Media = new TLInputMediaUploadedPhoto() { File = file, Caption = caption },
|
||||
Peer = peer
|
||||
});
|
||||
},0);
|
||||
}
|
||||
|
||||
public async Task<TLAbsUpdates> SendUploadedDocument(
|
||||
|
|
@ -304,7 +317,7 @@ namespace TLSharp.Core
|
|||
Attributes = attributes
|
||||
},
|
||||
Peer = peer
|
||||
});
|
||||
},0);
|
||||
}
|
||||
|
||||
public async Task<TLFile> GetFile(TLAbsInputFileLocation location, int filePartSize, int offset = 0)
|
||||
|
|
@ -315,7 +328,7 @@ namespace TLSharp.Core
|
|||
Location = location,
|
||||
Limit = filePartSize,
|
||||
Offset = offset
|
||||
});
|
||||
},0);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
@ -339,7 +352,7 @@ namespace TLSharp.Core
|
|||
MaxId = maxId,
|
||||
MinId = minId
|
||||
};
|
||||
return await SendRequestAsync<TLAbsMessages>(req);
|
||||
return await SendRequestAsync<TLAbsMessages>(req,0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -356,7 +369,7 @@ namespace TLSharp.Core
|
|||
Limit = limit
|
||||
};
|
||||
|
||||
return await SendRequestAsync<TLFound>(r);
|
||||
return await SendRequestAsync<TLFound>(r,0);
|
||||
}
|
||||
|
||||
private void OnUserAuthenticated(TLUser TLUser)
|
||||
|
|
|
|||
|
|
@ -97,7 +97,7 @@ namespace TLSharp.Core.Utils
|
|||
FilePart = partNumber,
|
||||
Bytes = part,
|
||||
FileTotalParts = partsCount
|
||||
});
|
||||
},0);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -106,7 +106,7 @@ namespace TLSharp.Core.Utils
|
|||
FileId = file_id,
|
||||
FilePart = partNumber,
|
||||
Bytes = part
|
||||
});
|
||||
},0);
|
||||
}
|
||||
partNumber++;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="DotNetZip" version="1.9.3" targetFramework="net451" />
|
||||
<package id="DotNetZip" version="1.11.0" targetFramework="net451" />
|
||||
<package id="MarkerMetro.Unity.Ionic.Zlib" version="2.0.0.14" targetFramework="net452" />
|
||||
</packages>
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
|
|
@ -7,7 +7,8 @@
|
|||
<OutputType>Library</OutputType>
|
||||
<RootNamespace>TLSharp.Tests.NUnit</RootNamespace>
|
||||
<AssemblyName>TLSharp.Tests.NUnit</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
|
||||
<TargetFrameworkProfile />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>TLSharp.Tests.VS</RootNamespace>
|
||||
<AssemblyName>TLSharp.Tests.VS</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
|
||||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
|
||||
|
|
@ -16,6 +16,7 @@
|
|||
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
|
||||
<IsCodedUITest>False</IsCodedUITest>
|
||||
<TestProjectType>UnitTest</TestProjectType>
|
||||
<TargetFrameworkProfile />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
|
|
|
|||
|
|
@ -9,8 +9,9 @@
|
|||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>TLSharp.Tests</RootNamespace>
|
||||
<AssemblyName>TLSharp.Tests</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<TargetFrameworkProfile />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<appSettings>
|
||||
<add key="ApiHash" value="" />
|
||||
<add key="ApiId" value="" />
|
||||
<add key="NumberToAuthenticate" value="" />
|
||||
<add key="CodeToAuthenticate" value="" />
|
||||
<add key="ApiHash" value=""/>
|
||||
<add key="ApiId" value=""/>
|
||||
<add key="NumberToAuthenticate" value=""/>
|
||||
<add key="CodeToAuthenticate" value=""/>
|
||||
<add key="PasswordToAuthenticate" value=""/>
|
||||
<add key="NotRegisteredNumberToSignUp" value=""/>
|
||||
<add key="NumberToSendMessage" value=""/>
|
||||
|
|
@ -12,4 +12,4 @@
|
|||
<add key="NumberToGetUserFull" value=""/>
|
||||
<add key="NumberToAddToChat" value=""/>
|
||||
</appSettings>
|
||||
</configuration>
|
||||
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/></startup></configuration>
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<startup>
|
||||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
|
||||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
|
||||
</startup>
|
||||
</configuration>
|
||||
|
|
@ -9,9 +9,10 @@
|
|||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>TeleSharp.Generator</RootNamespace>
|
||||
<AssemblyName>TeleSharp.Generator</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
|
||||
<TargetFrameworkProfile />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
|
|
|
|||
|
|
@ -9,8 +9,9 @@
|
|||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>TeleSharp.TL</RootNamespace>
|
||||
<AssemblyName>TeleSharp.TL</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<TargetFrameworkProfile />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
|
|
|
|||
Loading…
Reference in a new issue