Exemplo n.º 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ZeroMQClient"/> class.
 /// </summary>
 /// <param name="connectString">Connect string of the <see cref="ZeroMQClient"/>. See <see cref="DefaultConnectionString"/> for format.</param>
 public ZeroMQClient(string connectString) : base(TransportProtocol.Tcp, connectString)
 {
     m_zeroMQClient            = new();
     m_zeroMQTransportProtocol = ZeroMQTransportProtocol.Tcp;
     m_completedHandle         = new(true);
     MaxSendQueueSize          = DefaultMaxSendQueueSize;
     MaxReceiveQueueSize       = DefaultMaxReceiveQueueSize;
     m_sendLock = new();
 }
Exemplo n.º 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ZeroMQClient"/> class.
 /// </summary>
 /// <param name="connectString">Connect string of the <see cref="ZeroMQClient"/>. See <see cref="DefaultConnectionString"/> for format.</param>
 public ZeroMQClient(string connectString) : base(TransportProtocol.Tcp, connectString)
 {
     m_zeroMQClient            = new TransportProvider <ZSocket>();
     m_zeroMQTransportProtocol = ZeroMQTransportProtocol.Tcp;
     m_completedHandle         = new ManualResetEventSlim(true);
     MaxSendQueueSize          = DefaultMaxSendQueueSize;
     MaxReceiveQueueSize       = DefaultMaxReceiveQueueSize;
     m_sendLock = new object();
 }
Exemplo n.º 3
0
        private void ReceiveDataHandler()
        {
            while (Enabled)
            {
                Guid clientID = Guid.Empty;
                TransportProvider <DateTime> clientInfo = null;

                try
                {
                    // Receive data from the socket
                    using (ZMessage message = m_zeroMQServer.ReceiveMessage())
                    {
                        // Router socket should provide identity, delimiter and data payload frames
                        if (message.Count == 3)
                        {
                            // Extract client identity
                            clientID = new Guid(message[0].ReadStream());

                            // Lookup client info, adding it if it doesn't exist
                            clientInfo = GetClient(clientID);

                            // Get data payload frame
                            ZFrame frame = message[2];

                            clientInfo.BytesReceived = (int)frame.Length;

                            if (clientInfo.ReceiveBufferSize < clientInfo.BytesReceived)
                            {
                                clientInfo.SetReceiveBuffer(clientInfo.BytesReceived);
                            }

                            frame.Read(clientInfo.ReceiveBuffer, 0, clientInfo.BytesReceived);

                            clientInfo.Statistics.UpdateBytesReceived(clientInfo.BytesReceived);

                            // Update last client activity time
                            clientInfo.Provider = DateTime.UtcNow;
                        }
                    }

                    // Notify consumer of received data
                    if ((object)clientInfo != null)
                    {
                        OnReceiveClientDataComplete(clientID, clientInfo.ReceiveBuffer, clientInfo.BytesReceived);
                    }
                }
                catch (Exception ex)
                {
                    OnReceiveClientDataException(clientID, ex);
                }
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FileClient"/> class.
 /// </summary>
 /// <param name="connectString">Connect string of the <see cref="FileClient"/>. See <see cref="DefaultConnectionString"/> for format.</param>
 public FileClient(string connectString) : base(TransportProtocol.File, connectString)
 {
     m_autoRepeat                = DefaultAutoRepeat;
     m_receiveOnDemand           = DefaultReceiveOnDemand;
     m_receiveInterval           = DefaultReceiveInterval;
     m_startingOffset            = DefaultStartingOffset;
     FileOpenMode                = DefaultFileOpenMode;
     FileShareMode               = DefaultFileShareMode;
     m_fileAccessMode            = DefaultFileAccessMode;
     m_fileClient                = new TransportProvider <FileStream>();
     m_receiveDataTimer          = s_timerScheduler.CreateTimer();
     m_receiveDataTimer.Elapsed += m_receiveDataTimer_Elapsed;
 }
Exemplo n.º 5
0
        /// <summary>
        /// Sends data to the specified client asynchronously.
        /// </summary>
        /// <param name="clientID">ID of the client to which the data is to be sent.</param>
        /// <param name="data">The buffer that contains the binary data to be sent.</param>
        /// <param name="offset">The zero-based position in the <paramref name="data"/> at which to begin sending data.</param>
        /// <param name="length">The number of bytes to be sent from <paramref name="data"/> starting at the <paramref name="offset"/>.</param>
        /// <returns><see cref="WaitHandle"/> for the asynchronous operation.</returns>
        protected override WaitHandle SendDataToAsync(Guid clientID, byte[] data, int offset, int length)
        {
            if (CurrentState != ServerState.Running)
            {
                throw new SocketException((int)SocketError.NotConnected);
            }

            try
            {
                if ((object)m_zeroMQServer != null)
                {
                    // Lookup client info, adding it if it doesn't exist
                    TransportProvider <DateTime> clientInfo = GetClient(clientID);

                    // Router socket should provide identity, delimiter and data payload frames
                    using (ZMessage message = new ZMessage())
                    {
                        // Add identity, delimiter and data payload frames
                        message.Add(new ZFrame(clientID.ToByteArray()));
                        message.Add(new ZFrame());
                        message.Add(new ZFrame(data, offset, length));

                        // ZeroMQ send is asynchronous, but API call is not thread-safe
                        lock (m_sendLock)
                            m_zeroMQServer.Send(message);
                    }

                    clientInfo.Statistics.UpdateBytesSent(length);

                    // Update last client activity time
                    clientInfo.Provider = DateTime.UtcNow;
                }
            }
            catch (Exception ex)
            {
                // Log exception during send operation
                OnSendClientDataException(clientID, ex);
            }

            return(m_completedHandle.WaitHandle);
        }
Exemplo n.º 6
0
 /// <summary>
 /// Gets the <see cref="TransportProvider{Socket}"/> object associated with the specified client ID.
 /// </summary>
 /// <param name="clientID">ID of the client.</param>
 /// <param name="clientInfo">Client information.</param>
 /// <returns><c>true</c> if client exists; otherwise, <c>false</c>.</returns>
 public bool TryGetClient(Guid clientID, out TransportProvider <DateTime> clientInfo)
 {
     return(m_clientInfoLookup.TryGetValue(clientID, out clientInfo));
 }
Exemplo n.º 7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SerialClient"/> class.
 /// </summary>
 /// <param name="connectString">Connect string of the <see cref="SerialClient"/>. See <see cref="DefaultConnectionString"/> for format.</param>
 public SerialClient(string connectString)
     : base(TransportProtocol.Serial, connectString)
 {
     m_serialClient           = new TransportProvider <SerialPort>();
     m_receivedBytesThreshold = 1;
 }