Exemple #1
0
        void OnChannelFaulted(object sender, EventArgs e)
        {
            if (_client == null)
            {
                return;
            }
            if (_client.State != CommunicationState.Faulted)
            {
                return;
            }

            Logger?.LogTextMessage($"OnChannelFaulted() called on channel: {((IContextChannel)sender).RemoteAddress} State: {_client.State}");

            //TEST
            //Console.WriteLine($"OnChannelFaulted called.Channel {((IContextChannel)sender).RemoteAddress}state is:" + _client.State);
            //Console.WriteLine();

            try
            {
                /*if (_client.ClientCredentials != null)
                 * {
                 *  _client.ClientCredentials.UserName.UserName = "******";
                 *  _client.ClientCredentials.UserName.Password = "******".Reverse().ToString();
                 * }*/
                _client.Abort();
            }
            catch (Exception)
            {
            }
            _client = null;
            if (!Config.TaskCancellationToken.Token.IsCancellationRequested)
            {
                _reconnectionProcessor?.Invoke(this);
            }
        }
Exemple #2
0
 public void Close()
 {
     if (_client == null)
     {
         return;
     }
     try
     {
         /*if (_client.ClientCredentials != null)
          * {
          *  _client.ClientCredentials.UserName.UserName = "******";
          *  _client.ClientCredentials.UserName.Password = "******".Reverse().ToString();
          * }*/
         _client.UnsubscribeFromPasswordChange();
     }
     catch (Exception exception)
     {
         _client?.Abort();
         _client = null;
         Logger?.LogTextMessage($"UnsubscribeFromPasswordChange() - error: {exception.Message}");
         return;
     }
     try
     {
         _client?.Close();
     }
     catch (Exception)
     {
     }
     _client = null;
 }
Exemple #3
0
        void OnChannelFaulted(object sender, EventArgs e)
        {
            if (_client == null)
            {
                return;
            }
            if (_client.State != CommunicationState.Faulted)
            {
                return;
            }

            Logger?.LogTextMessage($"OnChannelFaulted() called on channel: {((IContextChannel)sender).RemoteAddress} State: {_client.State}");

            try
            {
                _client.Abort();
            }
            catch (Exception)
            {
            }
            _client = null;
            if (!Config.TaskCancellationToken.Token.IsCancellationRequested)
            {
                _reconnectionProcessor?.Invoke(this);
            }
        }
Exemple #4
0
 public void Close()
 {
     if (_client == null)
     {
         return;
     }
     try
     {
         _client.UnsubscribeFromPasswordChange();
     }
     catch (Exception exception)
     {
         _client.Abort();
         _client = null;
         Logger?.LogTextMessage($"UnsubscribeFromPasswordChange() - error: {exception.Message}");
         return;
     }
     try
     {
         _client.Close();
     }
     catch (Exception)
     {
     }
     _client = null;
 }
Exemple #5
0
 void ProcessCancellationPending()
 {
     if (_client == null)
     {
         return;
     }
     if (Config.TaskCancellationToken.Token.IsCancellationRequested)
     {
         if (_client.State == CommunicationState.Faulted)
         {
             _client.Abort();
         }
         else
         {
             _client.Close();
         }
         _client = null;
     }
 }
Exemple #6
0
        public void Initialize()
        {
            Logger?.LogTextMessage("Initialize()...");
            _needToAddFaultHandlers = true;
            bool needSaveToLog = true;

            while (!Config.TaskCancellationToken.Token.IsCancellationRequested)
            {
                if (_client == null)
                {
                    try
                    {
                        _callbackInstanceContext = new InstanceContext(this);
                        var clientTcpBinding = new NetTcpBinding();
                        clientTcpBinding.Security.Mode = SecurityMode.TransportWithMessageCredential;
                        clientTcpBinding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;

                        clientTcpBinding.OpenTimeout                       = TimeSpan.FromSeconds(10);
                        clientTcpBinding.ReceiveTimeout                    = TimeSpan.FromSeconds(10);
                        clientTcpBinding.ReliableSession.Enabled           = true;
                        clientTcpBinding.ReliableSession.InactivityTimeout = TimeSpan.FromSeconds(10);

                        var clientEndpointAddress = new EndpointAddress(new Uri("net.tcp://" + Config.Ip + ":" + Config.Port + "/D2SecService"),
                                                                        new DnsEndpointIdentity("OICD2 Server"));

                        _client = new D2UserClient(_callbackInstanceContext, clientTcpBinding, clientEndpointAddress);

                        if (_client.ClientCredentials != null)
                        {
                            _client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
                            _client.ClientCredentials.UserName.UserName = "******";
                            _client.ClientCredentials.UserName.Password = new string("D2_User".ToCharArray().Reverse().ToArray());
                        }
                    }
                    catch (Exception exception)
                    {
                        if (needSaveToLog)
                        {
                            Logger?.LogTextMessage($"CreateClient() error: {exception.Message}\nStack:\n{exception.StackTrace}");
                            needSaveToLog = false;
                        }
                        Thread.Sleep(100);
                        continue;
                    }
                }
                try
                {
                    //var sw = Stopwatch.StartNew();
                    _client.SubscribeOnPasswordChange();
                    //sw.Stop();
                    //Debug.Print(sw.Elapsed.ToString());
                    foreach (var chan in _callbackInstanceContext.OutgoingChannels)
                    {
                        chan.Faulted += OnChannelFaulted;
                        //chan.Closed += OnChannelFaulted;
                    }
                }
                catch (Exception exception)
                {
                    if (_client.State == CommunicationState.Faulted)
                    {
                        _client.Abort();
                        if (needSaveToLog)
                        {
                            Logger?.LogTextMessage($"SubscribeOnPasswordChange() error: {exception.Message}");
                            needSaveToLog = false;
                        }
                        _client = null;
                        Thread.Sleep(100);
                        continue;
                    }
                }
                Logger?.LogTextMessage("Initialize() - OK");
                break;
            }
            ProcessCancellationPending();
        }