Beispiel #1
0
        /// <summary>
        /// Disconnects the <see cref="FileClient"/> from the <see cref="FileStream"/>.
        /// </summary>
        public override void Disconnect()
        {
            if (CurrentState != ClientState.Disconnected)
            {
                m_fileClient.Reset();
                m_receiveDataTimer.Stop();

                if (m_connectionThread != null)
                {
                    m_connectionThread.Abort();
                }

                OnConnectionTerminated();
            }
        }
Beispiel #2
0
        /// <summary>
        /// Disconnects the <see cref="SerialClient"/> from the <see cref="SerialPort"/>.
        /// </summary>
        public override void Disconnect()
        {
            if (CurrentState != ClientState.Disconnected)
            {
                if (m_serialClient.Provider != null)
                {
                    m_serialClient.Provider.DataReceived  -= SerialPort_DataReceived;
                    m_serialClient.Provider.ErrorReceived -= SerialPort_ErrorReceived;
                }

                m_serialClient.Reset();

                if (m_connectionThread != null)
                {
                    m_connectionThread.Abort();
                }

                OnConnectionTerminated();
            }
        }
Beispiel #3
0
        private void OpenSocket(object state)
        {
            int connectionAttempts = 0;

            while (MaxConnectionAttempts == -1 || connectionAttempts < MaxConnectionAttempts)
            {
                if ((object)m_zeroMQClient.Provider != null)
                {
                    // Disconnect any existing ZeroMQ socket
                    try
                    {
                        m_zeroMQClient.Provider.Disconnect(ServerUri);
                    }
                    catch (Exception ex)
                    {
                        OnDisconnectException(ex);
                    }

                    m_zeroMQClient.Reset();
                }

                try
                {
                    OnConnectionAttempt();

                    // Create ZeroMQ Dealer socket - closest match to IClient implementation
                    m_zeroMQClient.Provider                      = new ZSocket(ZContext.Create(), ZSocketType.DEALER);
                    m_zeroMQClient.Provider.Identity             = m_zeroMQClient.ID.ToByteArray();
                    m_zeroMQClient.Provider.SendHighWatermark    = m_maxSendQueueSize;
                    m_zeroMQClient.Provider.ReceiveHighWatermark = m_maxReceiveQueueSize;
                    m_zeroMQClient.Provider.Immediate            = true;
                    m_zeroMQClient.Provider.SetOption(ZSocketOption.LINGER, 0);
                    m_zeroMQClient.Provider.SetOption(ZSocketOption.SNDTIMEO, 1000);
                    m_zeroMQClient.Provider.SetOption(ZSocketOption.RCVTIMEO, -1);
                    m_zeroMQClient.Provider.SetOption(ZSocketOption.RECONNECT_IVL, -1);
                    m_zeroMQClient.Provider.IPv6 = (Transport.GetDefaultIPStack() == IPStack.IPv6);
                    m_zeroMQClient.Provider.Connect(ServerUri);

                    m_connectionHandle.Set();
                    connectionAttempts = 0;

                    OnConnectionEstablished();
                }
                catch (Exception ex)
                {
                    m_zeroMQClient.Provider = null;

                    // Log exception during connection attempt
                    OnConnectionException(ex);

                    // Keep retrying connection
                    Thread.Sleep(1000);
                    connectionAttempts++;
                    continue;
                }

                try
                {
                    // Start data reception loop
                    ReceiveDataHandler();
                }
                catch (Exception ex)
                {
                    // Notify of the exception.
                    OnReceiveDataException(ex);
                }

                // If client is no longer connected, exit loop, else sleep for a moment before attempting reconnect
                if (Enabled)
                {
                    Thread.Sleep(1000);
                }
                else
                {
                    break;
                }
            }
        }