コード例 #1
0
        public Authenticator(MTProtoPlainSender sender)
        {
            // Make sure a connection is provided
            if (sender == null || sender.Connection == null || !sender.Connection.IsConnected)
            {
                throw new ArgumentException("The connection to secure is invalid", nameof(sender));
            }

            // Save the connection
            MTSender = sender;

            // Subscribe to the event... maybe?
            MTSender.TLObjectReceivedEvent += Sender_TLObjectReceivedEvent;
            Logger.Log(Logger.Level.Debug, $"Subscribed to {nameof(MTSender.TLObjectReceivedEvent)} event");

            Logger.Log(Logger.Level.Info, $"Authenticator created");
        }
コード例 #2
0
ファイル: MTProtoSender.cs プロジェクト: studasd/GlassTL
        /// <summary>
        /// Initializes an authorized connection to Telegram
        /// </summary>
        private async Task InitConnect()
        {
            Logger.Log(Logger.Level.Info, "Attempting connection");

            // Attempt to connect X number of times
            for (int i = 0; i < RetryCount; i++)
            {
                try
                {
                    if (i > 0)
                    {
                        Logger.Log(Logger.Level.Info, "Reattempting connection");
                    }
                    Connection.Connect(ConnectionTimeout);
                    Logger.Log(Logger.Level.Info, $"Connection created after {i + 1} attempt(s)");
                    break;
                }
                catch (Exception ex)
                {
                    Logger.Log(new ConnectionFailedException($"Connection attempt {i + 1} has failed.\n\n\t{ex.Message}", ex));
                    Logger.Log(Logger.Level.Info, $"Sleeping for {RetryDelay}ms");
                    await Task.Delay(RetryDelay);
                }
            }

            if (!Connection.IsConnected)
            {
                var sad = new ConnectionFailedException($"Failed to connect to Telegram {RetryCount} times in a row.");
                Logger.Log(sad);
                throw sad;
            }

            // Determine if we need to secure the connection
            if (State.AuthInfo.AuthKey == null)
            {
                Logger.Log(Logger.Level.Info, "Attempting to secure the connection");

                // Initialize a new instance of the plan sender based on the same connection
                using (var plain = new MTProtoPlainSender(Connection))
                {
                    // Attempt to authenticate X number of times
                    for (int i = 0; i < RetryCount; i++)
                    {
                        try
                        {
                            if (i > 0)
                            {
                                Logger.Log(Logger.Level.Info, "Reattempting to secure the connection");
                            }
                            State.AuthInfo = await new Authenticator(plain).DoAuthentication().TimeoutAfter(ConnectionTimeout);
                            Logger.Log(Logger.Level.Info, $"Obtained a secure connection after {i + 1} attempt(s)");
                            break;
                        }
                        catch (Exception ex)
                        {
                            Logger.Log(Logger.Level.Error, $"Securing attempt {i + 1} has failed.\n\n\t{ex.Message}");
                            Logger.Log(Logger.Level.Info, $"Sleeping for {RetryDelay}ms");
                            await Task.Delay(RetryDelay);
                        }
                    }
                }

                // Determine if the authorization was successful
                if (State.AuthInfo.AuthKey == null)
                {
                    var sad = new ConnectionFailedException($"Failed to secure the connection {RetryCount} times in a row.");
                    Logger.Log(sad);
                    Disconnect();
                    throw sad;
                }
            }

            // Get ourselves ready to handle requests
            CommunicationEstablished = true;

            Logger.Log <MTProtoSender>(Logger.Level.Debug, $"Attaching event handlers");
            Connection.DataReceivedEvent += Connection_DataReceivedEvent;

            Logger.Log <MTProtoSender>(Logger.Level.Debug, $"Starting Ack Handler");
            AckCancellation = new CancellationTokenSource();
            AckHandler      = Task.Run(() => AckHandlerMethod(AckCancellation.Token));
        }