Added timeout to RequestWithDcMigration

Fix #869
This commit is contained in:
Arthur Grand 2019-09-12 14:26:47 +07:00 committed by GitHub
parent 0dd3992997
commit 218544b1b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -28,9 +28,10 @@ namespace TLSharp.Core
private Session _session;
private List<TLDcOption> dcOptions;
private TcpClientConnectionHandler _handler;
private int _requestTimeout;
public TelegramClient(int apiId, string apiHash,
ISessionStore store = null, string sessionUserId = "session", TcpClientConnectionHandler handler = null)
ISessionStore store = null, string sessionUserId = "session", TcpClientConnectionHandler handler = null, int requestTimeout = 5)
{
if (apiId == default(int))
throw new MissingApiConfigurationException("API_ID");
@ -43,6 +44,7 @@ namespace TLSharp.Core
_apiHash = apiHash;
_apiId = apiId;
_handler = handler;
_requestTimeout = requestTimeout;
_session = Session.TryLoadOrCreateNew(store, sessionUserId);
_transport = new TcpTransport(_session.DataCenter.Address, _session.DataCenter.Port, _handler);
@ -111,8 +113,14 @@ namespace TLSharp.Core
throw new InvalidOperationException("Not connected!");
var completed = false;
var startedAt = DateTime.Now;
while(!completed)
{
if ((DateTime.Now - startedAt).TotalSeconds > _requestTimeout)
{
throw new Exception("Request Timeout");
}
try
{
await _sender.Send(request);