Beispiel #1
0
        private async Task ReconnectImpl()
        {
            await CloseProto();

            Debug.WriteLine("Creating new transport..");
            if (session.authKey == null)
            {
                var result = await Authenticator.Authenticate(session.serverAddress, session.port);

                session.authKey    = result.authKey;
                session.timeOffset = result.timeOffset;
                session.salt       = result.serverSalt;
            }

            protoSender = new MtProtoSender(session);

            Subscribe();
            protoSender.Start();

            var request = new InitConnectionAndGetConfigRequest(apiLayer, apiId, deviceInfo);

            await SendRpcRequest(request);

            configuration = request.config;
            dcOptions     = new DcOptionsCollection(request.config.dcOptions);

            OnConnectionStateChanged(ConnectionStateEventArgs.Connected());
        }
        private async Task ReconnectImpl()
        {
            await CloseProto();

            Debug.WriteLine("Creating new transport..");
            if (ClientSettings.Session.AuthKey == null)
            {
                Step3_Response result = await Authenticator.Authenticate(ClientSettings.Session.ServerAddress, ClientSettings.Session.Port);

                ClientSettings.Session.AuthKey    = result.authKey;
                ClientSettings.Session.TimeOffset = result.timeOffset;
                ClientSettings.Session.Salt       = result.serverSalt;
            }

            _protoSender = new MtProtoSender(ClientSettings.Session);

            Subscribe();
            _protoSender.Start();

            var request = new InitConnectionAndGetConfigRequest(s_apiLayer, ClientSettings.AppId, _deviceInfo);

            await SendRpcRequest(request);

            _configuration = request.config;
            _dcOptions     = new DcOptionsCollection(request.config.dcOptions);

            OnConnectionStateChanged(ConnectionStateEventArgs.Connected());
        }
Beispiel #3
0
        private async Task StartReconnecting()
        {
            while (!isClosed)
            {
                try
                {
                    await ReconnectImpl();

                    break;
                }
                catch (Exception ex)
                {
                    protoSender?.Dispose();

                    Debug.WriteLine($"Failed to initialize connection: {ex.Message}");
                    Debug.WriteLine($"Retrying in {connectionReinitializationTimeoutSeconds} seconds..");

                    OnConnectionStateChanged(ConnectionStateEventArgs.Disconnected(connectionReinitializationTimeoutSeconds));
                    await Task.Delay(TimeSpan.FromSeconds(connectionReinitializationTimeoutSeconds));
                }
            }
        }
Beispiel #4
0
 private void OnConnectionStateChanged(ConnectionStateEventArgs e)
 {
     Debug.WriteLine($"Connection status: {(e.isConnected ? "connected" : "disconnected")}");
     ConnectionStateChanged?.Invoke(this, e);
 }