PR review changes

This commit is contained in:
mark 2020-04-26 17:29:26 +01:00
parent 3e0e53bb09
commit 43d9d12167
8 changed files with 159 additions and 231 deletions

View file

@ -162,8 +162,7 @@ Full code you can see at [DownloadFileFromContactTest](https://github.com/sochix
private async Task MainAsync(string[] args)
{
TelegramClient client = null;
try
{
// -- if necessary, IP can be changed so the client can connect to the test network.
Session session = null;
// new Session(new FileSessionStore(), "session")
@ -300,11 +299,6 @@ Full code you can see at [DownloadFileFromContactTest](https://github.com/sochix
await client.WaitEventAsync();
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
private void Client_Updates(TelegramClient client, TLAbsUpdates updates)
{
@ -323,7 +317,11 @@ Full code you can see at [DownloadFileFromContactTest](https://github.com/sochix
{
var peer = new TLInputPeerUser() { UserId = status.UserId };
client.SendMessageAsync(peer, "Você está online.").Wait();
} catch {}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
}

View file

@ -1,69 +0,0 @@
using System;
namespace TLSharp.Core.Network
{
public class FloodException : Exception
{
public TimeSpan TimeToWait { get; private set; }
internal FloodException(TimeSpan timeToWait)
: base($"Flood prevention. Telegram now requires your program to do requests again only after {timeToWait.TotalSeconds} seconds have passed ({nameof(TimeToWait)} property)." +
" If you think the culprit of this problem may lie in TLSharp's implementation, open a Github issue please.")
{
TimeToWait = timeToWait;
}
}
public class BadMessageException : Exception
{
internal BadMessageException(string description) : base(description)
{
}
}
internal abstract class DataCenterMigrationException : Exception
{
internal int DC { get; private set; }
private const string REPORT_MESSAGE =
" See: https://github.com/sochix/TLSharp#i-get-a-xxxmigrationexception-or-a-migrate_x-error";
protected DataCenterMigrationException(string msg, int dc) : base(msg + REPORT_MESSAGE)
{
DC = dc;
}
}
internal class PhoneMigrationException : DataCenterMigrationException
{
internal PhoneMigrationException(int dc)
: base($"Phone number registered to a different DC: {dc}.", dc)
{
}
}
internal class FileMigrationException : DataCenterMigrationException
{
internal FileMigrationException(int dc)
: base($"File located on a different DC: {dc}.", dc)
{
}
}
internal class UserMigrationException : DataCenterMigrationException
{
internal UserMigrationException(int dc)
: base($"User located on a different DC: {dc}.", dc)
{
}
}
internal class NetworkMigrationException : DataCenterMigrationException
{
internal NetworkMigrationException(int dc)
: base($"Network located on a different DC: {dc}.", dc)
{
}
}
}

View file

@ -10,6 +10,7 @@ using TeleSharp.TL;
using TLSharp.Core.Exceptions;
using TLSharp.Core.MTProto;
using TLSharp.Core.MTProto.Crypto;
using TLSharp.Core.Network.Exceptions;
using TLSharp.Core.Network.Requests;
using TLSharp.Core.Utils;
@ -164,7 +165,7 @@ namespace TLSharp.Core.Network
using (var messageStream = new MemoryStream(result.Item1, false))
using (var messageReader = new BinaryReader(messageStream))
{
await processMessageAsync(result.Item2, result.Item3, messageReader, request, token);
await ProcessMessageAsync(result.Item2, result.Item3, messageReader, request, token);
}
token.ThrowIfCancellationRequested();
@ -173,14 +174,14 @@ namespace TLSharp.Core.Network
return null;
}
public async Task<byte[]> Receive(int timeoutms, CancellationToken token = default(CancellationToken))
public async Task<byte[]> Receive(TimeSpan timeToWait, CancellationToken token = default(CancellationToken))
{
var result = DecodeMessage((await transport.Receieve(timeoutms)).Body);
var result = DecodeMessage((await transport.Receive(timeToWait)).Body);
using (var messageStream = new MemoryStream(result.Item1, false))
using (var messageReader = new BinaryReader(messageStream))
{
await processMessageAsync(result.Item2, result.Item3, messageReader, null);
await ProcessMessageAsync(result.Item2, result.Item3, messageReader, null);
}
token.ThrowIfCancellationRequested();
@ -203,7 +204,7 @@ namespace TLSharp.Core.Network
await Receive(pingRequest, token).ConfigureAwait(false);
}
private async Task<bool> processMessageAsync(ulong messageId, int sequence, BinaryReader messageReader, TeleSharp.TL.TLMethod request, CancellationToken token = default(CancellationToken))
private async Task<bool> ProcessMessageAsync(ulong messageId, int sequence, BinaryReader messageReader, TeleSharp.TL.TLMethod request, CancellationToken token = default(CancellationToken))
{
token.ThrowIfCancellationRequested();
@ -330,7 +331,7 @@ namespace TLSharp.Core.Network
}
using (BinaryReader compressedReader = new BinaryReader(ms))
{
await processMessageAsync(messageId, sequence, compressedReader, request, token);
await ProcessMessageAsync(messageId, sequence, compressedReader, request, token);
}
}
@ -592,7 +593,7 @@ namespace TLSharp.Core.Network
long beginPosition = messageReader.BaseStream.Position;
try
{
var processedMessage = await processMessageAsync(innerMessageId, sequence, messageReader, request, token);
var processedMessage = await ProcessMessageAsync(innerMessageId, sequence, messageReader, request, token);
if (!processedMessage)
{
messageReader.BaseStream.Position = beginPosition + innerLength;
@ -600,7 +601,7 @@ namespace TLSharp.Core.Network
}
catch (InvalidOperationException e)
{
throw e;
throw;
}
catch (Exception e)
{

View file

@ -102,13 +102,13 @@ namespace TLSharp.Core.Network
return new TcpMessage(seq, body);
}
public async Task<TcpMessage> Receieve(int timeoutms)
public async Task<TcpMessage> Receive(TimeSpan timeToWait)
{
var stream = tcpClient.GetStream();
var packetLengthBytes = new byte[4];
var token = tokenSource.Token;
stream.ReadTimeout = timeoutms;
stream.ReadTimeout = (int)timeToWait.TotalMilliseconds;
int bytes = 0;
try
{
@ -118,7 +118,7 @@ namespace TLSharp.Core.Network
var socketError = io.InnerException as SocketException;
if (socketError != null && socketError.SocketErrorCode == SocketError.TimedOut)
throw new OperationCanceledException();
throw io;
throw;
}
if (bytes != 4)
throw new InvalidOperationException("Couldn't read the packet length");

View file

@ -76,7 +76,6 @@
<Compile Include="TelegramClient.cs" />
<Compile Include="Utils\Helpers.cs" />
<Compile Include="Network\Sniffer.cs" />
<Compile Include="Network\Exceptions.cs" />
<Compile Include="DataCenter.cs" />
</ItemGroup>
<ItemGroup>

View file

@ -16,6 +16,7 @@ using TLSharp.Core.Auth;
using TLSharp.Core.Exceptions;
using TLSharp.Core.MTProto.Crypto;
using TLSharp.Core.Network;
using TLSharp.Core.Network.Exceptions;
using TLSharp.Core.Utils;
using TLAuthorization = TeleSharp.TL.Auth.TLAuthorization;
@ -164,7 +165,7 @@ namespace TLSharp.Core
_looping = false;
}
public async Task MainLoopAsync(int timeslicems, CancellationToken token = default(CancellationToken))
public async Task MainLoopAsync(TimeSpan timeToWait, CancellationToken token = default(CancellationToken))
{
var lastPing = DateTime.UtcNow;
await SendPingAsync();
@ -172,7 +173,7 @@ namespace TLSharp.Core
{
try
{
await WaitEventAsync(timeslicems, token);
await WaitEventAsync(timeToWait, token);
}
catch (OperationCanceledException)
{
@ -232,9 +233,9 @@ namespace TLSharp.Core
}
}
public async Task WaitEventAsync(int timeoutms, CancellationToken token = default(CancellationToken))
public async Task WaitEventAsync(TimeSpan timeToWait, CancellationToken token = default(CancellationToken))
{
await sender.Receive (timeoutms, token);
await sender.Receive (timeToWait, token);
}
public bool IsUserAuthorized()

View file

@ -1,5 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="MarkerMetro.Unity.Ionic.Zlib" version="2.0.0.14" targetFramework="net452" />
<package id="NLog" version="4.4.12" targetFramework="net45" />
</packages>

View file

@ -128,7 +128,7 @@ namespace TLSharp.Tests
var hash = await client.SendCodeRequestAsync(NumberToAuthenticate);
var code = CodeToAuthenticate; // you can change code in debugger too
if (string.IsNullOrWhiteSpace(code))
if (String.IsNullOrWhiteSpace(code))
{
throw new Exception("CodeToAuthenticate is empty in the app.config file, fill it with the code you just got now by SMS/Telegram");
}
@ -177,7 +177,7 @@ namespace TLSharp.Tests
if (user == null)
{
throw new Exception("Number was not found in Contacts List of user: " + NumberToSendMessage);
throw new System.Exception("Number was not found in Contacts List of user: " + NumberToSendMessage);
}
await client.SendTypingAsync(new TLInputPeerUser() { UserId = user.Id });
@ -431,7 +431,7 @@ namespace TLSharp.Tests
}
};
await client.MainLoopAsync(1000);
await client.MainLoopAsync(new TimeSpan(0, 0, 1));
// At this point you would send yourself a UPDATE_1 message to trigger update