protected override void OnActivate()
        {
            base.OnActivate();

            _proxyConfig = _transportService.GetProxyConfig() as TLProxyConfig76;
            if (_proxyConfig != null)
            {
                _isEnabled   = _proxyConfig.IsEnabled.Value;
                _useForCalls = _proxyConfig.UseForCalls.Value;

                var now = TLUtils.DateToUniversalTimeTLInt(MTProtoService.ClientTicksDelta, DateTime.Now);
                Items.Clear();
                for (var i = 0; i < _proxyConfig.Items.Count; i++)
                {
                    var item = _proxyConfig.Items[i];
                    item.IsSelected = i == _proxyConfig.SelectedIndex.Value;
                    Items.Add(item);

                    if (CheckProxy(item))
                    {
                        item.Status = ProxyStatus.Connecting;
                        item.Ping   = null;
                        _proxyChecker.CheckAsync(item, 10.0,
                                                 (proxyItem, ping) => Execute.BeginOnUIThread(() =>
                        {
                            proxyItem.Proxy.CheckTime = now;
                            proxyItem.Proxy.Ping      = ping;
                            proxyItem.Proxy.Status    = item.Ping != null ? ProxyStatus.Available : ProxyStatus.Unavailable;

                            Set(_proxyConfig);
                        }));
                    }
                }
            }

            _transportService.TransportConnected  += OnTransportConnected;
            _transportService.TransportConnecting += OnTransportConnecting;
        }
Example #2
0
        public ITransport GetSpecialTransport(string host, int port, string staticHost, int staticPort, TransportType type, short protocolDCId, byte[] protocolSecret, TLProxyBase proxy, out bool isCreated)
        {
            var random = TLLong.Random();   // Important! To ping multiple connections to one proxy, will be closed after first ping otherwise
            var key    = string.Format("{0} {1} {2} {3} {4} {5}", host, port, protocolDCId, type, random, proxy != null ? string.Format("{0}:{1}", proxy.Server, proxy.Port) : string.Empty);

            if (_specialCache.ContainsKey(key))
            {
                isCreated = false;

#if LOG_REGISTRATION
                TLUtils.WriteLog(string.Format("Old transport {2} {0}:{1}", host, port, _specialCache[key].Id));
#endif
                return(_specialCache[key]);
            }

#if WINDOWS_PHONE
            if (type == TransportType.Http)
            {
                var transport = new HttpTransport(host, MTProtoTransportType.Special, GetProxyConfig());

                _specialCache.Add(key, transport);
                isCreated = true;
                return(transport);
                //transport.SetAddress(host, port, () => callback(transport));
            }
            else
#endif
            {
                var proxyConfig = new TLProxyConfig76
                {
                    CustomFlags   = new TLLong(0),
                    IsEnabled     = TLBool.True,
                    SelectedIndex = new TLInt(0),
                    UseForCalls   = TLBool.False,
                    Items         = new TLVector <TLProxyBase> {
                        proxy
                    }
                };

                var transport =
#if WIN_RT
                    new TcpTransportWinRT(host, port, staticHost, staticPort, MTProtoTransportType.Special, proxyConfig);
#elif NATIVE
                    new NativeTcpTransport(host, port, staticHost, staticPort, MTProtoTransportType.Special, protocolDCId, protocolSecret, proxyConfig);
#else
                    new TcpTransport(host, port, staticHost, staticPort, MTProtoTransportType.Special, proxyConfig);
#endif
                transport.Connecting     += OnConnecting;
                transport.Connected      += OnConnected;
                transport.ConnectionLost += OnConnectionLost;
                transport.CheckConfig    += OnCheckConfig;

#if LOG_REGISTRATION
                TLUtils.WriteLog(string.Format("New transport {2} {0}:{1}", host, port, transport.Id));
#endif
                TLUtils.WritePerformance(string.Format("  TCP: New transport {0}:{1}", host, port));

                _specialCache.Add(key, transport);
                isCreated = true;

                Debug.WriteLine("  TCP: New transport {0}:{1}", host, port);
                return(transport);
                //trasport.SetAddress(host, port, () => callback(trasport));
            }
        }