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:
ferferga 2018-03-02 16:34:56 +01:00 committed by GitHub
parent c1ce5e25ba
commit e9957bcc1c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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())