Example #1
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;
        }
Example #2
0
        // save timer end

        public async Task ConnectAsync() {
            try {
                if (gateway == null) {
                    logger.info("creating new mtproto gateway...");
                    gateway = new MTProtoGateway(MainDc, this, true, cachedSalt);
                    gateway.UpdatesEvent += updates.ProcessUpdates;
                    gateway.BrokenSessionEvent += GatewayOnBrokenSessionEvent;

                    while (true) {
                        try {
                            await gateway.ConnectAsync();
                            break;
                        } catch (MTProtoBrokenSessionException e) {
                            logger.info("broken session exception");
                        }
                    }
                    api = new TLApi(gateway);
                    logger.info("connection established, notifying");
                    //establishedTask.SetResult(null);

                    updates.RequestDifference();

                    gateway.ReconnectEvent += () => {
                        GoToOnline();
                        updates.RequestDifference();
                    };

                    GoToOnline();

                    if(!saveSessionTimerInitialized) {
                        SaveSessionTimer();
                        saveSessionTimerInitialized = true;
                    }
                }
            }
            catch (Exception ex) {
                logger.error("session exception: {0}", ex);
                throw ex;
            }
        }
Example #3
0
 private void GatewayOnBrokenSessionEvent() {
     logger.info("creating new session... TODO: destroy old session");
     // creating new session
     id = Helpers.GenerateRandomUlong();
     sequence = 0;
     gateway.Dispose();
     gateway = new MTProtoGateway(MainDc, this, true, cachedSalt);
     gateway.UpdatesEvent += updates.ProcessUpdates;
     gateway.BrokenSessionEvent += GatewayOnBrokenSessionEvent;
 }
Example #4
0
        public async Task<MTProtoGateway> GetFileGateway(ulong salt) {
            await _lock.WaitAsync();
//            fileGateway = new MTProtoGateway(this, fileSession);
            try {
                if (fileGateway != null) {
                    return fileGateway;
                }

                if (fileSession == null) {
                    fileSession = new TelegramFileSession(Helpers.GenerateRandomUlong(), 0);
                }

                fileGateway = new MTProtoGateway(this, fileSession, false, salt);
                await fileGateway.ConnectAsync();

                return fileGateway;
            }
            finally {
                _lock.Release();
            }
        }