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,8 +30,9 @@ namespace TLSharp.Core
private List<TLDcOption> dcOptions;
private TcpClientConnectionHandler _handler;
public TelegramClient(int apiId, string apiHash,
ISessionStore store = null, string sessionUserId = "session", TcpClientConnectionHandler handler = null)
public TelegramClient(int apiId, string apiHash, ISessionStore store = null, string sessionUserId = "session", TcpClientConnectionHandler handler = null)
{
try
{
if (apiId == default(int))
throw new MissingApiConfigurationException("API_ID");
@ -49,8 +50,14 @@ namespace TLSharp.Core
_session = Session.TryLoadOrCreateNew(store, sessionUserId);
_transport = new TcpTransport(_session.ServerAddress, _session.Port, _handler);
}
public async Task ConnectAsync(bool reconnect = false)
catch
{
throw new Exception("Not connected to the internet");
}
}
public async Task<bool> ConnectAsync(bool reconnect = false)
{
try
{
if (_session.AuthKey == null || reconnect)
{
@ -58,7 +65,6 @@ namespace TLSharp.Core
_session.AuthKey = result.AuthKey;
_session.TimeOffset = result.TimeOffset;
}
_sender = new MtProtoSender(_transport, _session);
//set-up layer
@ -75,10 +81,14 @@ namespace TLSharp.Core
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)
{
if (dcOptions == null || !dcOptions.Any())