コード例 #1
0
        void ensureClientConnectedThreadStart()
        {
            while (!this.ensureClientConnectedThreadNeedQuit)
            {
                try
                {
                    lock (this.clientSyncRoot)
                    {
                        if (this.serviceClient == null)
                        {
                            Uri serviceUrl = new Uri(string.Format(CultureInfo.InvariantCulture, "net.tcp://{0}:{1}/KEPServerExRelay/", this.serverIP, this.servicePort));

                            this.serviceCallback             = new KEPServerExRelayServiceCallback();
                            this.serviceCallback.DataChange += this.serviceCallback_DataChange;

                            this.serviceClient = new KEPServerExRelayServiceClient(new InstanceContext(this.serviceCallback));
                            this.serviceClient.Endpoint.Address = new EndpointAddress(serviceUrl, this.serviceClient.Endpoint.Address.Identity);
                            this.serviceClient.Open();

                            this.serviceClient.InnerDuplexChannel.Faulted += this.clientInnerChannel_Faulted;

                            Subscriber subscriber = new Subscriber();
                            subscriber.Type = this.GetType().FullName;
                            subscriber.Name = string.Empty;
                            this.serviceClient.Subscribe(subscriber);

                            if (this.needRestart)
                            {
                                if (this.serviceClient.GetIsRunning())
                                {
                                    this.serviceClient.Stop();
                                }
                            }

                            if (!this.serviceClient.GetIsRunning())
                            {
                                this.serviceClient.Start(this.project);
                            }

                            this.needRestart = false;
                        }
                    }

                    this.IsConnected = true;
                }
                catch
                {
                    this.ReleaseClient();

                    DateTime sleepBegin = DateTime.Now;
                    while (!this.ensureClientConnectedThreadNeedQuit && (DateTime.Now - sleepBegin) < this.ensureClientPeriod)
                    {
                        Thread.Sleep(1);
                    }
                }

                Thread.Sleep(1);
            }
        }
コード例 #2
0
        void ReleaseClient()
        {
            // ClientBase.Close() 可能会引发 ICommunicationObject.Faulted,继而再次调用本方法,所以 ClientBase.Abort() 前需要判断非空
            lock (this.clientSyncRoot)
            {
                if (this.serviceCallback != null)
                {
                    this.serviceCallback.DataChange -= this.serviceCallback_DataChange;

                    this.serviceCallback = null;
                }

                if (this.serviceClient != null)
                {
                    try
                    {
                        if (this.serviceClient.State == CommunicationState.Opened)
                        {
                            this.serviceClient.Unsubscribe();
                        }

                        this.serviceClient.Close();
                    }
                    catch
                    {
                        if (this.serviceClient != null)
                        {
                            this.serviceClient.Abort();
                        }
                    }

                    this.serviceClient = null;
                }
            }

            this.IsConnected = false;
        }