Пример #1
0
        public async Task <TLApi> GetFileSession(int dc)
        {
            logger.debug("Getting file session for dc {0}", dc);
            await             Established;
            ConfigConstructor config = (ConfigConstructor)gateway.Config;

            TelegramDC targetDc;

            if (dcs.ContainsKey(dc))
            {
                targetDc = dcs[dc];
            }
            else
            {
                targetDc = new TelegramDC();
                foreach (var dcOption in config.dc_options)
                {
                    DcOptionConstructor optionConstructor = (DcOptionConstructor)dcOption;
                    if (optionConstructor.id == dc)
                    {
                        TelegramEndpoint endpoint = new TelegramEndpoint(optionConstructor.ip_address, optionConstructor.port);
                        targetDc.Endpoints.Add(endpoint);
                    }
                }

                dcs[dc] = targetDc;
            }

            MTProtoGateway fileGateway = await targetDc.GetFileGateway(gateway.Salt);

            TLApi fileGatewayApi = new TLApi(fileGateway);

            if (targetDc.FileAuthorized || dc == mainDcId)
            {
                return(fileGatewayApi);
            }

            Task <auth_ExportedAuthorization>     exportAuthTask = Api.auth_exportAuthorization(dc);
            Auth_exportedAuthorizationConstructor exportedAuth   = (Auth_exportedAuthorizationConstructor)await exportAuthTask;
            auth_Authorization authorization = await fileGatewayApi.auth_importAuthorization(exportedAuth.id, exportedAuth.bytes);

            targetDc.SaveFileAuthorization(authorization);

            return(fileGatewayApi);
        }
Пример #2
0
        public async Task Migrate(int dc)
        {
            if (gateway == null)
            {
                logger.error("gateway not found, migration impossible");
                return;
            }

            if (gateway.Config == null)
            {
                logger.error("config in gateway not found, migration impossible");
                return;
            }

            ConfigConstructor config = (ConfigConstructor)gateway.Config;

            if (config.this_dc == dc)
            {
                logger.warning("migration to same dc: {0}", dc);
                return;
            }

            TelegramDC newDc = new TelegramDC();

            foreach (var dcOption in config.dc_options)
            {
                DcOptionConstructor optionConstructor = (DcOptionConstructor)dcOption;
                if (optionConstructor.id == dc)
                {
                    TelegramEndpoint endpoint = new TelegramEndpoint(optionConstructor.ip_address, optionConstructor.port);
                    newDc.Endpoints.Add(endpoint);
                }
            }

            dcs[dc]  = newDc;
            mainDcId = dc;

            gateway.Dispose();
            gateway         = null;
            establishedTask = new TaskCompletionSource <object>();
            ConnectAsync();
            await Established;
        }