コード例 #1
0
    // Client thread which does not block Update()
    void NetMQClient()
    {
        AsyncIO.ForceDotNet.Force();
        NetMQContext context = null;

        context = NetMQContext.Create();

        string msg;
        var    timeout = new System.TimeSpan(0, 0, 1); //1sec

        Debug.Log("Connect to the server.");
        var requestSocket = new RequestSocket(">tcp://192.168.11.36:50020");

        requestSocket.SendFrame("SUB_PORT");
        bool is_connected = requestSocket.TryReceiveFrameString(timeout, out msg);

        while (is_connected && stop_thread_ == false)
        {
            Debug.Log("Request a message.");
            requestSocket.SendFrame("msg");
            is_connected = requestSocket.TryReceiveFrameString(timeout, out msg);
            Debug.Log("Sleep");
            Thread.Sleep(1000);
        }

        requestSocket.Close();
        Debug.Log("ContextTerminate.");
        context.Terminate();
    }
コード例 #2
0
 /// <summary>
 /// Dispose managed resources.
 /// </summary>
 /// <param name="disposing">Is desposing.</param>
 protected virtual void Dispose(bool disposing)
 {
     if (!disposed)
     {
         if (disposing)
         {
             CancelPolling();
             if (pubSocket != null)
             {
                 pubSocket.Disconnect(PublisherAddress);
                 pubSocket.Dispose();
                 pubSocket = null;
             }
             if (poller != null)
             {
                 poller.Dispose();
                 poller = null;
             }
             if (context != null)
             {
                 context.Terminate();
                 context.Dispose();
                 context = null;
             }
             if (source != null)
             {
                 source.Dispose();
                 source = null;
             }
         }
         // Shared cleanup logic.
         disposed = true;
     }
 }
コード例 #3
0
 void OnApplicationQuit()
 {
     Debug.Log("Shutting down...");
     subSocket.Close();
     context.Terminate();
     Debug.Log("Done...");
 }
コード例 #4
0
        private void SendEvents(String submissionUri, CancellationToken cancellationToken)
        {
            using (NetMQContext context = NetMQContext.Create())
            {
                using (NetMQSocket pushSocket = context.CreatePushSocket())
                {
                    pushSocket.IgnoreErrors = true;
                    pushSocket.Connect(submissionUri);

                    Logger.Info("ZeroMQCache: Connected to submissionUri \"{0}\".", submissionUri);

                    while (!cancellationToken.IsCancellationRequested)
                    {
                        try
                        {
                            ZeroMQMessage message;

                            if (mQueue.TryTake(out message, TimeSpan.FromSeconds(1)))
                            {
                                Logger.Debug("ZeroMQCache: Sending -> {0}", message.ToString());

                                pushSocket.SendMore(message.Topic);
                                pushSocket.SendMore(message.Client);
                                pushSocket.Send(message.Content);
                            }
                        }
                        catch (OperationCanceledException)
                        {
                            // We have been asked to cancel operation
                            break;
                        }
                        catch (Exception ex)
                        {
                            Logger.Error("ZeroMQCache: Error sending message.", ex);
                        }
                    }

                    // Close socket
                    pushSocket.Close();
                }

                context.Terminate();
            }
        }
コード例 #5
0
        private void ReceiveEvents(String subscriptionUri, String topic, CancellationToken cancellationToken)
        {
            using (NetMQContext context = NetMQContext.Create())
            {
                using (NetMQSocket subscriberSocket = context.CreateSubscriberSocket())
                {
                    subscriberSocket.IgnoreErrors = true;
                    subscriberSocket.Connect(subscriptionUri);
                    subscriberSocket.Subscribe(topic);

                    Logger.Info("ZeroMQCache: Connected to subscriptionUri \"{0}\".", subscriptionUri);

                    // Eventhandler delegate to handle receiving messages
                    subscriberSocket.ReceiveReady += (sender, args) =>
                    {
                        try
                        {
                            if (args.ReceiveReady)
                            {
                                // Recieve and relay
                                NetMQMessage netMQmessage = args.Socket.ReceiveMessage();

                                // Recieve the message
                                ZeroMQMessage message = new ZeroMQMessage()
                                {
                                    Topic   = netMQmessage.Pop().ConvertToString(),
                                    Client  = netMQmessage.Pop().ConvertToString(),
                                    Content = netMQmessage.Pop().ConvertToString()
                                };

                                Logger.Debug("ZeroMQCache: Received -> {0}", message.ToString());

                                XmlCacheEvent cacheEvent = XmlCacheEvent.FromXml(message.Content);

                                if (cacheEvent != null)
                                {
                                    OnCacheEvent(cacheEvent.CacheRegion, cacheEvent.Key, cacheEvent.EventType);
                                }
                            }
                        }
                        catch (OperationCanceledException)
                        {
                            // We have been asked to cancel operation
                            return;
                        }
                        catch (Exception ex)
                        {
                            Logger.Error("ZeroMQCache: Error receiving message.", ex);
                        }
                    };

                    while (!cancellationToken.IsCancellationRequested)
                    {
                        try
                        {
                            subscriberSocket.Poll(TimeSpan.FromSeconds(1));
                        }
                        catch (OperationCanceledException)
                        {
                            // We have been asked to cancel operation
                            break;
                        }
                        catch (Exception ex)
                        {
                            Logger.Error("ZeroMQCache: Error polling for messages.", ex);
                        }
                    }

                    // Close socket
                    subscriberSocket.Close();
                }

                context.Terminate();
            }
        }
コード例 #6
0
        private void ReceiveEvents(String submissionUri, String topic, CancellationToken cancellationToken)
        {
            using (NetMQContext context = NetMQContext.Create())
            {
                using (NetMQSocket pullSocket = context.CreatePullSocket())
                {
                    pullSocket.IgnoreErrors = true;
                    pullSocket.Bind(submissionUri);

                    Logger.Info("ZeroMQBroker: Bound to submissionUri \"{0}\".", submissionUri);

                    // Eventhandler delegate to handle receiving messages
                    pullSocket.ReceiveReady += (sender, args) =>
                    {
                        try
                        {
                            if (args.ReceiveReady)
                            {
                                // Recieve and relay
                                NetMQMessage netMQmessage = args.Socket.ReceiveMessage();

                                // Recieve the message
                                ZeroMQMessage message = new ZeroMQMessage()
                                {
                                    Topic   = netMQmessage.Pop().ConvertToString(),
                                    Client  = netMQmessage.Pop().ConvertToString(),
                                    Content = netMQmessage.Pop().ConvertToString()
                                };

                                Logger.Debug("ZeroMQBroker: Received -> {0}", message.ToString());

                                mQueue.Add(message);

                                // ZeroMQBroker relays multiple topics, verify if the message was meant for the current client
                                if (String.Equals(topic, message.Topic, StringComparison.OrdinalIgnoreCase))
                                {
                                    XmlCacheEvent cacheEvent = XmlCacheEvent.FromXml(message.Content);

                                    if (cacheEvent != null)
                                    {
                                        OnCacheEvent(cacheEvent.CacheRegion, cacheEvent.Key, cacheEvent.EventType);
                                    }
                                }
                            }
                        }
                        catch (OperationCanceledException)
                        {
                            // We have been asked to cancel operation
                            return;
                        }
                        catch (Exception ex)
                        {
                            Logger.Error("ZeroMQBroker: Error receiving message.", ex);
                        }
                    };

                    while (!cancellationToken.IsCancellationRequested)
                    {
                        try
                        {
                            pullSocket.Poll(TimeSpan.FromSeconds(1));
                        }
                        catch (OperationCanceledException)
                        {
                            // We have been asked to cancel operation
                            break;
                        }
                        catch (Exception ex)
                        {
                            Logger.Error("ZeroMQBroker: Error polling for messages.", ex);
                        }
                    }

                    // Close socket
                    pullSocket.Close();
                }

                context.Terminate();
            }
        }