mirror of
https://github.com/sochix/TLSharp.git
synced 2025-12-06 08:02:00 +01:00
Added connection checks
Added connection checks, in order to see if the user is connected to the internet or not
This commit is contained in:
parent
c1ce5e25ba
commit
e9957bcc1c
|
|
@ -30,55 +30,65 @@ namespace TLSharp.Core
|
||||||
private List<TLDcOption> dcOptions;
|
private List<TLDcOption> dcOptions;
|
||||||
private TcpClientConnectionHandler _handler;
|
private TcpClientConnectionHandler _handler;
|
||||||
|
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
if (apiId == default(int))
|
try
|
||||||
throw new MissingApiConfigurationException("API_ID");
|
|
||||||
if (string.IsNullOrEmpty(apiHash))
|
|
||||||
throw new MissingApiConfigurationException("API_HASH");
|
|
||||||
|
|
||||||
if (store == null)
|
|
||||||
store = new FileSessionStore();
|
|
||||||
|
|
||||||
TLContext.Init();
|
|
||||||
_apiHash = apiHash;
|
|
||||||
_apiId = apiId;
|
|
||||||
_handler = handler;
|
|
||||||
|
|
||||||
_session = Session.TryLoadOrCreateNew(store, sessionUserId);
|
|
||||||
_transport = new TcpTransport(_session.ServerAddress, _session.Port, _handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task ConnectAsync(bool reconnect = false)
|
|
||||||
{
|
|
||||||
if (_session.AuthKey == null || reconnect)
|
|
||||||
{
|
{
|
||||||
var result = await Authenticator.DoAuthentication(_transport);
|
if (apiId == default(int))
|
||||||
_session.AuthKey = result.AuthKey;
|
throw new MissingApiConfigurationException("API_ID");
|
||||||
_session.TimeOffset = result.TimeOffset;
|
if (string.IsNullOrEmpty(apiHash))
|
||||||
|
throw new MissingApiConfigurationException("API_HASH");
|
||||||
|
|
||||||
|
if (store == null)
|
||||||
|
store = new FileSessionStore();
|
||||||
|
|
||||||
|
TLContext.Init();
|
||||||
|
_apiHash = apiHash;
|
||||||
|
_apiId = apiId;
|
||||||
|
_handler = handler;
|
||||||
|
|
||||||
|
_session = Session.TryLoadOrCreateNew(store, sessionUserId);
|
||||||
|
_transport = new TcpTransport(_session.ServerAddress, _session.Port, _handler);
|
||||||
}
|
}
|
||||||
|
catch
|
||||||
_sender = new MtProtoSender(_transport, _session);
|
|
||||||
|
|
||||||
//set-up layer
|
|
||||||
var config = new TLRequestGetConfig();
|
|
||||||
var request = new TLRequestInitConnection()
|
|
||||||
{
|
{
|
||||||
ApiId = _apiId,
|
throw new Exception("Not connected to the internet");
|
||||||
AppVersion = "1.0.0",
|
}
|
||||||
DeviceModel = "PC",
|
|
||||||
LangCode = "en",
|
|
||||||
Query = config,
|
|
||||||
SystemVersion = "Win 10.0"
|
|
||||||
};
|
|
||||||
var invokewithLayer = new TLRequestInvokeWithLayer() { Layer = 66, Query = request };
|
|
||||||
await _sender.Send(invokewithLayer);
|
|
||||||
await _sender.Receive(invokewithLayer);
|
|
||||||
|
|
||||||
dcOptions = ((TLConfig)invokewithLayer.Response).DcOptions.ToList();
|
|
||||||
}
|
}
|
||||||
|
public async Task<bool> ConnectAsync(bool reconnect = false)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (_session.AuthKey == null || reconnect)
|
||||||
|
{
|
||||||
|
var result = await Authenticator.DoAuthentication(_transport);
|
||||||
|
_session.AuthKey = result.AuthKey;
|
||||||
|
_session.TimeOffset = result.TimeOffset;
|
||||||
|
}
|
||||||
|
_sender = new MtProtoSender(_transport, _session);
|
||||||
|
|
||||||
|
//set-up layer
|
||||||
|
var config = new TLRequestGetConfig();
|
||||||
|
var request = new TLRequestInitConnection()
|
||||||
|
{
|
||||||
|
ApiId = _apiId,
|
||||||
|
AppVersion = "1.0.0",
|
||||||
|
DeviceModel = "PC",
|
||||||
|
LangCode = "en",
|
||||||
|
Query = config,
|
||||||
|
SystemVersion = "Win 10.0"
|
||||||
|
};
|
||||||
|
var invokewithLayer = new TLRequestInvokeWithLayer() { Layer = 66, Query = request };
|
||||||
|
await _sender.Send(invokewithLayer);
|
||||||
|
await _sender.Receive(invokewithLayer);
|
||||||
|
dcOptions = ((TLConfig)invokewithLayer.Response).DcOptions.ToList();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
private async Task ReconnectToDcAsync(int dcId)
|
private async Task ReconnectToDcAsync(int dcId)
|
||||||
{
|
{
|
||||||
if (dcOptions == null || !dcOptions.Any())
|
if (dcOptions == null || !dcOptions.Any())
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue