예제 #1
0
        public void Connect()
        {
            try
            {
                if (_connected)
                {
                    return;
                }
                do
                {
                    try
                    {
                        //at server clientPipeRead->_pipeClientWrite
                        var ps = new PipeSecurity();
                        ps.AddAccessRule(new PipeAccessRule("Everyone", PipeAccessRights.FullControl, System.Security.AccessControl.AccessControlType.Allow));

                        if (_pipeClientWrite != null)
                        {
                            _pipeClientWrite.Close();
                        }
                        //_pipeClientWrite.Dispose();
                        _pipeClientWrite = new NamedPipeServerStream("clientPipeRead", PipeDirection.InOut, 10, PipeTransmissionMode.Message, PipeOptions.WriteThrough, 1024, 1024, ps);
                        //if (_pipeServerWrite == null)
                        //    _pipeServerWrite = new NamedPipeServerStream("opcPipeRead", PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.WriteThrough, 1024, 1024, ps);

                        _pipeClientWrite.WaitForConnection();
                        _ssWrite = new StreamString(_pipeClientWrite);

                        if (_pipeClientRead != null)
                        {
                            _pipeClientRead.Close();
                        }
                        //_pipeClientRead.Dispose();
                        _pipeClientRead = new NamedPipeServerStream("clientPipeWrite", PipeDirection.InOut, 10, PipeTransmissionMode.Message, PipeOptions.WriteThrough, 1024, 1024, ps);
                        _pipeClientRead.WaitForConnection();
                        _ssRead = new StreamString(_pipeClientRead);

                        _connected = true;
                        return;
                    }
                    catch (Exception ex)
                    {
                        if (ex.Message.Contains("The network path was not found"))
                        {
                            Thread.Sleep(TimeSleep); //wait for new connection
                        }
                        else
                        {
                            throw new Exception(ex.Message);
                        }
                    }
                } while (!_connected);
            }
            catch (Exception ex)
            {
                File.AppendAllText(_pathLog, "\r\n" + DateTime.Now.ToString() + ", ClientWinsService: " + ex.ToString());
            }
        }
예제 #2
0
        private void Woker_DoWork(object sender, DoWorkEventArgs e)
        {
            bool serverStop = false;
            var  worker     = sender as BackgroundWorker;

            do
            {
                try
                {
                    serverStop = false;
                    if (!_connected)
                    {
                        _pipeClientRead = new NamedPipeClientStream(WinsServerComputer, "clientPipeRead", PipeDirection.InOut,
                                                                    PipeOptions.None, TokenImpersonationLevel.Impersonation);
                        _pipeClientWrite = new NamedPipeClientStream(WinsServerComputer, "clientPipeWrite",
                                                                     PipeDirection.InOut, PipeOptions.None, TokenImpersonationLevel.Impersonation);

                        _pipeClientRead.Connect();
                        _pipeClientWrite.Connect();
                        _ssRead  = new StreamString(_pipeClientRead);
                        _ssWrite = new StreamString(_pipeClientWrite);
                    }
                    e.Result = true;
                    return;
                }
                catch (Exception ex)
                {
                    if (ex.Message.Contains("The network path was not found"))
                    {
                        serverStop = true;
                        Thread.Sleep(TimeSleep); //wait for new connection
                    }
                    else
                    {
                        e.Result = false;
                    }
                    //e.Result = false;
                    //e.Cancel = true;
                }
            } while (serverStop);
        }
예제 #3
0
        public void Connect()
        {
            try
            {
                if (_connected)
                {
                    return;
                }
                do
                {
                    //try
                    //{
                    _pipeWClientRead  = new NamedPipeClientStream(OpcServerComputer, "opcPipeRead", PipeDirection.InOut);
                    _pipeWClientWrite = new NamedPipeClientStream(OpcServerComputer, "opcPipeWrite", PipeDirection.InOut);

                    _pipeWClientRead.Connect();
                    _pipeWClientWrite.Connect();
                    _ssRead  = new StreamString(_pipeWClientRead);
                    _ssWrite = new StreamString(_pipeWClientWrite);

                    _connected = true;
                    return;
                    //}
                    //catch (Exception ex)
                    //{
                    //    if (ex.Message.Contains("The network path was not found"))
                    //        Thread.Sleep(TimeSleep); //wait for new connection
                    //    else
                    //        throw new Exception(ex.Message);
                    //}
                } while (!_connected);
            }
            catch (Exception ex)
            {
                File.AppendAllText(_pathLog, "\r\n" + DateTime.Now.ToString() + ", ClientOpcServerService: " + ex.ToString());
            }
        }