// TODO: refactor the common part of Connect & ReConnect public async Task <TgConnection> Connect(TgConnection src, int dstDcId) { if (_isDisposed) { throw new ObjectDisposedException(nameof(TgConnectionPool)); } // it is not perfect but it should work right almost every time if (_connTasks.TryGetValue(dstDcId, out var connTask)) { return(await connTask); } if (_conns.TryGetValue(dstDcId, out var conn)) { return(conn); } var task = _connTasks[dstDcId] = EstablishForkConnection(src, dstDcId); try { return(_conns[dstDcId] = await task); } finally { _connTasks.TryRemove(dstDcId, out _); } }
void MirrorUpdates(TgConnection conn) { conn.Transport.Transport.Updates.Subscribe( onNext: Updates.OnNext, onError: Updates.OnError, onCompleted: Updates.OnCompleted ); }
async Task<TgConnection> EstablishForkConnection(TgConnection srcConn, int dcId) { var ep = DcInfoKeeper.FindEndpoint(dcId); var connectInfo = ConnectInfo.FromInfo(srcConn.Session.Get().ApiId, ep); var dstConn = await TgConnectionEstablisher.EstablishConnection(connectInfo, _callMiddlewareChain, _connHandler); await TryExportAuth(srcConn, dstConn); return dstConn; }
public TgConnectionPool( TgConnection mainConn, TgCallMiddlewareChain callMiddlewareChain, TcpClientConnectionHandler? connHandler = null ) { _conns[mainConn.Config.ThisDc] = mainConn; _callMiddlewareChain = callMiddlewareChain; _connHandler = connHandler; }
static async Task TryExportAuth(TgConnection src, TgConnection dst) { if (!src.Session.Get().IsAuthorized) return; try { var auth = await src.Transport.Call(new ExportAuthorization(dst.Config.ThisDc)); await dst.Transport.Call(new ImportAuthorization(auth.Id, auth.Bytes)); } catch (TgNotAuthenticatedException) { } }
static async Task ExportAuth(TgConnection src, TgConnection dst) { var auth = await src.Transport.Call(new ExportAuthorization(dst.Config.ThisDc)); await dst.Transport.Call(new ImportAuthorization(auth.Id, auth.Bytes)); }