Esempio n. 1
0
        private async Task ReconnectToDcAsync(int dcId)
        {
            if (dcOptions == null || !dcOptions.Any())
            {
                throw new InvalidOperationException($"Can't reconnect. Establish initial connection first.");
            }

            TLExportedAuthorization exported = null;

            if (_session.TLUser != null)
            {
                TLRequestExportAuthorization exportAuthorization = new TLRequestExportAuthorization()
                {
                    DcId = dcId
                };
                exported = await SendRequestAsync <TLExportedAuthorization>(exportAuthorization);
            }

            var dc = dcOptions.First(d => d.Id == dcId && d.Ipv6 == false);

            //if(dcId == 5)
            //{
            //    dc.IpAddress = "91.108.56.165";
            //}
            _transport             = new TcpTransport(dc.IpAddress, dc.Port, _handler);
            _session.ServerAddress = dc.IpAddress;
            _session.Port          = dc.Port;

            await ConnectAsync(true);

            if (_session.TLUser != null)
            {
                TLRequestImportAuthorization importAuthorization = new TLRequestImportAuthorization()
                {
                    Id = exported.Id, Bytes = exported.Bytes
                };
                var imported = await SendRequestAsync <TLAuthorization>(importAuthorization);

                OnUserAuthenticated(((TLUser)imported.User));
            }
        }
Esempio n. 2
0
        private async Task ReconnectToDcAsync(int dcId, CancellationToken token = default(CancellationToken))
        {
            token.ThrowIfCancellationRequested();

            if (dcOptions == null || !dcOptions.Any())
            {
                throw new InvalidOperationException($"Can't reconnect. Establish initial connection first.");
            }

            TLExportedAuthorization exported = null;

            if (session.TLUser != null)
            {
                TLRequestExportAuthorization exportAuthorization = new TLRequestExportAuthorization()
                {
                    DcId = dcId
                };
                exported = await SendRequestAsync <TLExportedAuthorization>(exportAuthorization, token).ConfigureAwait(false);
            }

            var dc         = dcOptions.First(d => d.Id == dcId);
            var dataCenter = new DataCenter(dcId, dc.IpAddress, dc.Port);

            transport          = new TcpTransport(dc.IpAddress, dc.Port, handler);
            session.DataCenter = dataCenter;

            await ConnectAsync(true, token).ConfigureAwait(false);

            if (session.TLUser != null)
            {
                TLRequestImportAuthorization importAuthorization = new TLRequestImportAuthorization()
                {
                    Id = exported.Id, Bytes = exported.Bytes
                };
                var imported = await SendRequestAsync <TLAuthorization>(importAuthorization, token).ConfigureAwait(false);

                OnUserAuthenticated((TLUser)imported.User);
            }
        }
Esempio n. 3
0
        private async Task ReconnectToDcAsync(int dcId)
        {
            if (dcOptions == null || !dcOptions.Any())
            {
                throw new InvalidOperationException($"Can't reconnect. Establish initial connection first.");
            }

            TLExportedAuthorization exported = null;

            if (IsUserAuthorized())
            {
                TLRequestExportAuthorization exportAuthorization = new TLRequestExportAuthorization()
                {
                    DcId = dcId
                };
                exported = await SendRequestAsync <TLExportedAuthorization>(exportAuthorization);
            }

            var dc         = dcOptions.First(d => d.Id == dcId && (_useIpV6DataCenters || !d.Ipv6));
            var dataCenter = new DataCenter(dcId, dc.IpAddress, dc.Port);

            _transport          = new TcpTransport(dc.IpAddress, dc.Port, _handler);
            _session.DataCenter = dataCenter;

            await ConnectAsync(true);

            if (IsUserAuthorized())
            {
                TLRequestImportAuthorization importAuthorization = new TLRequestImportAuthorization()
                {
                    Id = exported.Id, Bytes = exported.Bytes
                };
                var imported = await SendRequestAsync <TLAuthorization>(importAuthorization);

                OnUserAuthenticated(((TLUser)imported.User));
            }
        }
        private async Task ReconnectToDcAsync(int dcId, CancellationToken token = default(CancellationToken))
        {
            token.ThrowIfCancellationRequested();

            if (dcOptions == null || !dcOptions.Any())
            {
                throw new InvalidOperationException($"Can't reconnect. Establish initial connection first.");
            }

            TLExportedAuthorization exported = null;

            if (session.TLUser != null)
            {
                TLRequestExportAuthorization exportAuthorization = new TLRequestExportAuthorization()
                {
                    DcId = dcId
                };
                exported = await SendRequestAsync <TLExportedAuthorization>(exportAuthorization, token).ConfigureAwait(false);
            }

            IEnumerable <TLDcOption> dcs;

            if (dcIpVersion == DataCenterIPVersion.OnlyIPv6)
            {
                dcs = dcOptions.Where(d => d.Id == dcId && d.Ipv6); // selects only ipv6 addresses
            }
            else if (dcIpVersion == DataCenterIPVersion.OnlyIPv4)
            {
                dcs = dcOptions.Where(d => d.Id == dcId && !d.Ipv6); // selects only ipv4 addresses
            }
            else
            {
                dcs = dcOptions.Where(d => d.Id == dcId); // any
            }
            dcs = dcs.Where(d => !d.MediaOnly);

            TLDcOption dc;

            if (dcIpVersion != DataCenterIPVersion.Default)
            {
                if (!dcs.Any())
                {
                    throw new Exception($"Telegram server didn't provide us with any IPAddress that matches your preferences. If you chose OnlyIPvX, try switch to PreferIPvX instead.");
                }
                dcs = dcs.OrderBy(d => d.Ipv6);
                dc  = dcIpVersion == DataCenterIPVersion.PreferIPv4 ? dcs.First() : dcs.Last(); // ipv4 addresses are at the beginning of the list because it was ordered
            }
            else
            {
                dc = dcs.First();
            }

            var dataCenter = new DataCenter(dcId, dc.IpAddress, dc.Port);

            transport          = new TcpTransport(dc.IpAddress, dc.Port, handler);
            session.DataCenter = dataCenter;

            await ConnectAsync(true, token).ConfigureAwait(false);

            if (session.TLUser != null)
            {
                TLRequestImportAuthorization importAuthorization = new TLRequestImportAuthorization()
                {
                    Id = exported.Id, Bytes = exported.Bytes
                };
                var imported = await SendRequestAsync <TLAuthorization>(importAuthorization, token).ConfigureAwait(false);

                OnUserAuthenticated((TLUser)imported.User);
            }
        }