mirror of
https://github.com/sochix/TLSharp.git
synced 2025-12-06 08:02:00 +01:00
Revert field refactoring
This commit is contained in:
parent
0c86a0674b
commit
2e4792c607
|
|
@ -7,15 +7,15 @@ namespace TLSharp.Core.Network
|
||||||
{
|
{
|
||||||
public class MtProtoPlainSender
|
public class MtProtoPlainSender
|
||||||
{
|
{
|
||||||
private int _timeOffset;
|
private int timeOffset;
|
||||||
private long _lastMessageId;
|
private long lastMessageId;
|
||||||
private readonly Random _random;
|
private Random random;
|
||||||
private readonly TcpTransport _transport;
|
private TcpTransport _transport;
|
||||||
|
|
||||||
public MtProtoPlainSender(TcpTransport transport)
|
public MtProtoPlainSender(TcpTransport transport)
|
||||||
{
|
{
|
||||||
_transport = transport;
|
_transport = transport;
|
||||||
_random = new Random();
|
random = new Random();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Send(byte[] data, CancellationToken token)
|
public async Task Send(byte[] data, CancellationToken token)
|
||||||
|
|
@ -62,17 +62,17 @@ namespace TLSharp.Core.Network
|
||||||
private long GetNewMessageId()
|
private long GetNewMessageId()
|
||||||
{
|
{
|
||||||
long time = Convert.ToInt64((DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds);
|
long time = Convert.ToInt64((DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds);
|
||||||
long newMessageId = ((time / 1000 + _timeOffset) << 32) |
|
long newMessageId = ((time / 1000 + timeOffset) << 32) |
|
||||||
((time % 1000) << 22) |
|
((time % 1000) << 22) |
|
||||||
(_random.Next(524288) << 2); // 2^19
|
(random.Next(524288) << 2); // 2^19
|
||||||
// [ unix timestamp : 32 bit] [ milliseconds : 10 bit ] [ buffer space : 1 bit ] [ random : 19 bit ] [ msg_id type : 2 bit ] = [ msg_id : 64 bit ]
|
// [ unix timestamp : 32 bit] [ milliseconds : 10 bit ] [ buffer space : 1 bit ] [ random : 19 bit ] [ msg_id type : 2 bit ] = [ msg_id : 64 bit ]
|
||||||
|
|
||||||
if (_lastMessageId >= newMessageId)
|
if (lastMessageId >= newMessageId)
|
||||||
{
|
{
|
||||||
newMessageId = _lastMessageId + 4;
|
newMessageId = lastMessageId + 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
_lastMessageId = newMessageId;
|
lastMessageId = newMessageId;
|
||||||
return newMessageId;
|
return newMessageId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,12 +23,16 @@ namespace TLSharp.Core
|
||||||
{
|
{
|
||||||
private MtProtoSender _sender;
|
private MtProtoSender _sender;
|
||||||
private TcpTransport _transport;
|
private TcpTransport _transport;
|
||||||
private readonly string _apiHash = "";
|
private string _apiHash = "";
|
||||||
private readonly int _apiId;
|
private int _apiId = 0;
|
||||||
private List<TLDcOption> _dcOptions;
|
private Session _session;
|
||||||
private readonly TcpClientConnectionHandler _handler;
|
private List<TLDcOption> dcOptions;
|
||||||
|
private TcpClientConnectionHandler _handler;
|
||||||
|
|
||||||
public Session Session { get; }
|
public Session Session
|
||||||
|
{
|
||||||
|
get { return _session; }
|
||||||
|
}
|
||||||
|
|
||||||
public TelegramClient(int apiId, string apiHash,
|
public TelegramClient(int apiId, string apiHash,
|
||||||
ISessionStore store = null, string sessionUserId = "session", TcpClientConnectionHandler handler = null)
|
ISessionStore store = null, string sessionUserId = "session", TcpClientConnectionHandler handler = null)
|
||||||
|
|
@ -45,8 +49,8 @@ namespace TLSharp.Core
|
||||||
_apiId = apiId;
|
_apiId = apiId;
|
||||||
_handler = handler;
|
_handler = handler;
|
||||||
|
|
||||||
Session = Session.TryLoadOrCreateNew(store, sessionUserId);
|
_session = Session.TryLoadOrCreateNew(store, sessionUserId);
|
||||||
_transport = new TcpTransport(Session.DataCenter.Address, Session.DataCenter.Port, _handler);
|
_transport = new TcpTransport(_session.DataCenter.Address, _session.DataCenter.Port, _handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -54,14 +58,14 @@ namespace TLSharp.Core
|
||||||
{
|
{
|
||||||
token.ThrowIfCancellationRequested();
|
token.ThrowIfCancellationRequested();
|
||||||
|
|
||||||
if (Session.AuthKey == null || reconnect)
|
if (_session.AuthKey == null || reconnect)
|
||||||
{
|
{
|
||||||
var result = await Authenticator.DoAuthentication(_transport, token).ConfigureAwait(false);
|
var result = await Authenticator.DoAuthentication(_transport, token).ConfigureAwait(false);
|
||||||
Session.AuthKey = result.AuthKey;
|
_session.AuthKey = result.AuthKey;
|
||||||
Session.TimeOffset = result.TimeOffset;
|
_session.TimeOffset = result.TimeOffset;
|
||||||
}
|
}
|
||||||
|
|
||||||
_sender = new MtProtoSender(_transport, Session);
|
_sender = new MtProtoSender(_transport, _session);
|
||||||
|
|
||||||
//set-up layer
|
//set-up layer
|
||||||
var config = new TLRequestGetConfig();
|
var config = new TLRequestGetConfig();
|
||||||
|
|
@ -78,32 +82,32 @@ namespace TLSharp.Core
|
||||||
await _sender.Send(invokewithLayer, token).ConfigureAwait(false);
|
await _sender.Send(invokewithLayer, token).ConfigureAwait(false);
|
||||||
await _sender.Receive(invokewithLayer, token).ConfigureAwait(false);
|
await _sender.Receive(invokewithLayer, token).ConfigureAwait(false);
|
||||||
|
|
||||||
_dcOptions = ((TLConfig)invokewithLayer.Response).DcOptions.ToList();
|
dcOptions = ((TLConfig)invokewithLayer.Response).DcOptions.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task ReconnectToDcAsync(int dcId, CancellationToken token)
|
private async Task ReconnectToDcAsync(int dcId, CancellationToken token)
|
||||||
{
|
{
|
||||||
token.ThrowIfCancellationRequested();
|
token.ThrowIfCancellationRequested();
|
||||||
|
|
||||||
if (_dcOptions == null || !_dcOptions.Any())
|
if (dcOptions == null || !dcOptions.Any())
|
||||||
throw new InvalidOperationException($"Can't reconnect. Establish initial connection first.");
|
throw new InvalidOperationException($"Can't reconnect. Establish initial connection first.");
|
||||||
|
|
||||||
TLExportedAuthorization exported = null;
|
TLExportedAuthorization exported = null;
|
||||||
if (Session.TLUser != null)
|
if (_session.TLUser != null)
|
||||||
{
|
{
|
||||||
TLRequestExportAuthorization exportAuthorization = new TLRequestExportAuthorization() { DcId = dcId };
|
TLRequestExportAuthorization exportAuthorization = new TLRequestExportAuthorization() { DcId = dcId };
|
||||||
exported = await SendRequestAsync<TLExportedAuthorization>(exportAuthorization, token).ConfigureAwait(false);
|
exported = await SendRequestAsync<TLExportedAuthorization>(exportAuthorization, token).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
var dc = _dcOptions.First(d => d.Id == dcId);
|
var dc = dcOptions.First(d => d.Id == dcId);
|
||||||
var dataCenter = new DataCenter (dcId, dc.IpAddress, dc.Port);
|
var dataCenter = new DataCenter (dcId, dc.IpAddress, dc.Port);
|
||||||
|
|
||||||
_transport = new TcpTransport(dc.IpAddress, dc.Port, _handler);
|
_transport = new TcpTransport(dc.IpAddress, dc.Port, _handler);
|
||||||
Session.DataCenter = dataCenter;
|
_session.DataCenter = dataCenter;
|
||||||
|
|
||||||
await ConnectAsync(true, token).ConfigureAwait(false);
|
await ConnectAsync(true, token).ConfigureAwait(false);
|
||||||
|
|
||||||
if (Session.TLUser != null)
|
if (_session.TLUser != null)
|
||||||
{
|
{
|
||||||
TLRequestImportAuthorization importAuthorization = new TLRequestImportAuthorization() { Id = exported.Id, Bytes = exported.Bytes };
|
TLRequestImportAuthorization importAuthorization = new TLRequestImportAuthorization() { Id = exported.Id, Bytes = exported.Bytes };
|
||||||
var imported = await SendRequestAsync<TLAuthorization>(importAuthorization, token).ConfigureAwait(false);
|
var imported = await SendRequestAsync<TLAuthorization>(importAuthorization, token).ConfigureAwait(false);
|
||||||
|
|
@ -127,8 +131,8 @@ namespace TLSharp.Core
|
||||||
}
|
}
|
||||||
catch(DataCenterMigrationException e)
|
catch(DataCenterMigrationException e)
|
||||||
{
|
{
|
||||||
if (Session.DataCenter.DataCenterId.HasValue &&
|
if (_session.DataCenter.DataCenterId.HasValue &&
|
||||||
Session.DataCenter.DataCenterId.Value == e.DC)
|
_session.DataCenter.DataCenterId.Value == e.DC)
|
||||||
{
|
{
|
||||||
throw new Exception($"Telegram server replied requesting a migration to DataCenter {e.DC} when this connection was already using this DataCenter", e);
|
throw new Exception($"Telegram server replied requesting a migration to DataCenter {e.DC} when this connection was already using this DataCenter", e);
|
||||||
}
|
}
|
||||||
|
|
@ -142,7 +146,7 @@ namespace TLSharp.Core
|
||||||
|
|
||||||
public bool IsUserAuthorized()
|
public bool IsUserAuthorized()
|
||||||
{
|
{
|
||||||
return Session.TLUser != null;
|
return _session.TLUser != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<bool> IsPhoneRegisteredAsync(string phoneNumber, CancellationToken token = default(CancellationToken))
|
public async Task<bool> IsPhoneRegisteredAsync(string phoneNumber, CancellationToken token = default(CancellationToken))
|
||||||
|
|
@ -376,10 +380,10 @@ namespace TLSharp.Core
|
||||||
|
|
||||||
private void OnUserAuthenticated(TLUser TLUser)
|
private void OnUserAuthenticated(TLUser TLUser)
|
||||||
{
|
{
|
||||||
Session.TLUser = TLUser;
|
_session.TLUser = TLUser;
|
||||||
Session.SessionExpires = int.MaxValue;
|
_session.SessionExpires = int.MaxValue;
|
||||||
|
|
||||||
Session.Save();
|
_session.Save();
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool IsConnected
|
public bool IsConnected
|
||||||
|
|
|
||||||
|
|
@ -122,14 +122,16 @@ namespace TLSharp.Core.Utils
|
||||||
Parts = partsCount
|
Parts = partsCount
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
else
|
||||||
return new TLInputFile
|
|
||||||
{
|
{
|
||||||
Id = file_id,
|
return new TLInputFile
|
||||||
Name = name,
|
{
|
||||||
Parts = partsCount,
|
Id = file_id,
|
||||||
Md5Checksum = GetFileHash(file)
|
Name = name,
|
||||||
};
|
Parts = partsCount,
|
||||||
|
Md5Checksum = GetFileHash(file)
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue