Example #1
0
        public async Task <TgConnection> ReConnect(int dcId)
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException(nameof(TgConnectionPool));
            }

            // it is not perfect but it should work right almost every time
            if (_connTasks.TryGetValue(dcId, out var connTask))
            {
                return(await connTask);
            }
            if (!_conns.TryGetValue(dcId, out var conn))
            {
                throw Helpers.FailedAssertion($"TgConnectionPool.Reconnect: DC {dcId} not found.");
            }

            var connectInfo = ConnectInfo.FromSession(conn.Session.Get());
            var newConnTask = _connTasks[dcId] = TgConnectionEstablisher.EstablishConnection(connectInfo, _callMiddlewareChain, _connHandler);

            try
            {
                return(_conns[dcId] = await newConnTask);
            }
            finally
            {
                _connTasks.TryRemove(dcId, out _);
            }
        }
Example #2
0
 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;
 }
Example #3
0
        public static async Task <TgBellhop> Connect(
            ConnectInfo connectInfo,
            TgCallMiddlewareChain?callMiddlewareChain = null,
            TcpClientConnectionHandler?connHandler    = null
            )
        {
            callMiddlewareChain ??= TgCallMiddlewareChain.Default;
            var conn = await TaskWrapper.Wrap(() =>
                                              TgConnectionEstablisher.EstablishConnection(connectInfo, callMiddlewareChain, connHandler)
                                              ).ConfigureAwait(false);

            var pool = new TgConnectionPool(conn, callMiddlewareChain, connHandler);

            return(new TgBellhop(pool, conn));
        }