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
|
||||
{
|
||||
private int _timeOffset;
|
||||
private long _lastMessageId;
|
||||
private readonly Random _random;
|
||||
private readonly TcpTransport _transport;
|
||||
private int timeOffset;
|
||||
private long lastMessageId;
|
||||
private Random random;
|
||||
private TcpTransport _transport;
|
||||
|
||||
public MtProtoPlainSender(TcpTransport transport)
|
||||
{
|
||||
_transport = transport;
|
||||
_random = new Random();
|
||||
random = new Random();
|
||||
}
|
||||
|
||||
public async Task Send(byte[] data, CancellationToken token)
|
||||
|
|
@ -62,17 +62,17 @@ namespace TLSharp.Core.Network
|
|||
private long GetNewMessageId()
|
||||
{
|
||||
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) |
|
||||
(_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 ]
|
||||
|
||||
if (_lastMessageId >= newMessageId)
|
||||
if (lastMessageId >= newMessageId)
|
||||
{
|
||||
newMessageId = _lastMessageId + 4;
|
||||
newMessageId = lastMessageId + 4;
|
||||
}
|
||||
|
||||
_lastMessageId = newMessageId;
|
||||
lastMessageId = newMessageId;
|
||||
return newMessageId;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -23,12 +23,16 @@ namespace TLSharp.Core
|
|||
{
|
||||
private MtProtoSender _sender;
|
||||
private TcpTransport _transport;
|
||||
private readonly string _apiHash = "";
|
||||
private readonly int _apiId;
|
||||
private List<TLDcOption> _dcOptions;
|
||||
private readonly TcpClientConnectionHandler _handler;
|
||||
private string _apiHash = "";
|
||||
private int _apiId = 0;
|
||||
private Session _session;
|
||||
private List<TLDcOption> dcOptions;
|
||||
private TcpClientConnectionHandler _handler;
|
||||
|
||||
public Session Session { get; }
|
||||
public Session Session
|
||||
{
|
||||
get { return _session; }
|
||||
}
|
||||
|
||||
public TelegramClient(int apiId, string apiHash,
|
||||
ISessionStore store = null, string sessionUserId = "session", TcpClientConnectionHandler handler = null)
|
||||
|
|
@ -45,8 +49,8 @@ namespace TLSharp.Core
|
|||
_apiId = apiId;
|
||||
_handler = handler;
|
||||
|
||||
Session = Session.TryLoadOrCreateNew(store, sessionUserId);
|
||||
_transport = new TcpTransport(Session.DataCenter.Address, Session.DataCenter.Port, _handler);
|
||||
_session = Session.TryLoadOrCreateNew(store, sessionUserId);
|
||||
_transport = new TcpTransport(_session.DataCenter.Address, _session.DataCenter.Port, _handler);
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -54,14 +58,14 @@ namespace TLSharp.Core
|
|||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
if (Session.AuthKey == null || reconnect)
|
||||
if (_session.AuthKey == null || reconnect)
|
||||
{
|
||||
var result = await Authenticator.DoAuthentication(_transport, token).ConfigureAwait(false);
|
||||
Session.AuthKey = result.AuthKey;
|
||||
Session.TimeOffset = result.TimeOffset;
|
||||
_session.AuthKey = result.AuthKey;
|
||||
_session.TimeOffset = result.TimeOffset;
|
||||
}
|
||||
|
||||
_sender = new MtProtoSender(_transport, Session);
|
||||
_sender = new MtProtoSender(_transport, _session);
|
||||
|
||||
//set-up layer
|
||||
var config = new TLRequestGetConfig();
|
||||
|
|
@ -78,32 +82,32 @@ namespace TLSharp.Core
|
|||
await _sender.Send(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)
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
if (_dcOptions == null || !_dcOptions.Any())
|
||||
if (dcOptions == null || !dcOptions.Any())
|
||||
throw new InvalidOperationException($"Can't reconnect. Establish initial connection first.");
|
||||
|
||||
TLExportedAuthorization exported = null;
|
||||
if (Session.TLUser != null)
|
||||
if (_session.TLUser != null)
|
||||
{
|
||||
TLRequestExportAuthorization exportAuthorization = new TLRequestExportAuthorization() { DcId = dcId };
|
||||
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);
|
||||
|
||||
_transport = new TcpTransport(dc.IpAddress, dc.Port, _handler);
|
||||
Session.DataCenter = dataCenter;
|
||||
_session.DataCenter = dataCenter;
|
||||
|
||||
await ConnectAsync(true, token).ConfigureAwait(false);
|
||||
|
||||
if (Session.TLUser != null)
|
||||
if (_session.TLUser != null)
|
||||
{
|
||||
TLRequestImportAuthorization importAuthorization = new TLRequestImportAuthorization() { Id = exported.Id, Bytes = exported.Bytes };
|
||||
var imported = await SendRequestAsync<TLAuthorization>(importAuthorization, token).ConfigureAwait(false);
|
||||
|
|
@ -127,8 +131,8 @@ namespace TLSharp.Core
|
|||
}
|
||||
catch(DataCenterMigrationException e)
|
||||
{
|
||||
if (Session.DataCenter.DataCenterId.HasValue &&
|
||||
Session.DataCenter.DataCenterId.Value == e.DC)
|
||||
if (_session.DataCenter.DataCenterId.HasValue &&
|
||||
_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);
|
||||
}
|
||||
|
|
@ -142,7 +146,7 @@ namespace TLSharp.Core
|
|||
|
||||
public bool IsUserAuthorized()
|
||||
{
|
||||
return Session.TLUser != null;
|
||||
return _session.TLUser != null;
|
||||
}
|
||||
|
||||
public async Task<bool> IsPhoneRegisteredAsync(string phoneNumber, CancellationToken token = default(CancellationToken))
|
||||
|
|
@ -376,10 +380,10 @@ namespace TLSharp.Core
|
|||
|
||||
private void OnUserAuthenticated(TLUser TLUser)
|
||||
{
|
||||
Session.TLUser = TLUser;
|
||||
Session.SessionExpires = int.MaxValue;
|
||||
_session.TLUser = TLUser;
|
||||
_session.SessionExpires = int.MaxValue;
|
||||
|
||||
Session.Save();
|
||||
_session.Save();
|
||||
}
|
||||
|
||||
public bool IsConnected
|
||||
|
|
|
|||
|
|
@ -122,14 +122,16 @@ namespace TLSharp.Core.Utils
|
|||
Parts = partsCount
|
||||
};
|
||||
}
|
||||
|
||||
return new TLInputFile
|
||||
else
|
||||
{
|
||||
Id = file_id,
|
||||
Name = name,
|
||||
Parts = partsCount,
|
||||
Md5Checksum = GetFileHash(file)
|
||||
};
|
||||
return new TLInputFile
|
||||
{
|
||||
Id = file_id,
|
||||
Name = name,
|
||||
Parts = partsCount,
|
||||
Md5Checksum = GetFileHash(file)
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue