Exemple #1
0
        public void Start()
        {
            if (SubscribePort == 0)
            {
                this.SubscribePort = ZeroMQConstants.ServerReceivePort;
            }

            if (PublishPort == 0)
            {
                this.PublishPort = ZeroMQConstants.ServerPublishPort;
            }


            context = new ZeroMQContext();

            publisher  = context.CreateServerSocket(ZSocketType.PUB);
            subscriber = context.CreateServerSocket(ZSocketType.SUB);

            var publisherString  = "tcp://*:" + PublishPort;
            var subscriberString = "tcp://*:" + SubscribePort;

            Console.WriteLine("Publisher  -> " + publisherString);
            Console.WriteLine("Subscriber -> " + subscriberString);

            publisher.Bind(publisherString, out var connectError);
            if (connectError != null)
            {
                Console.WriteLine($"Connection error: {connectError.Name} - {connectError.Number} - {connectError.Text}");
            }

            subscriber.Bind(subscriberString, out var connect2Error);
            if (connect2Error != null)
            {
                Console.WriteLine($"Connection error: {connect2Error.Name} - {connect2Error.Number} - {connect2Error.Text}");
            }

            subscriber.SubscribeAll();
            runningThread = new Thread(new ThreadStart(() =>
            {
                Running = true;
                while (Active)
                {
                    try
                    {
                        using (ZMessage message = subscriber.ReceiveMessage(out var error))
                        {
                            if (error == null && message != null && message.Count > 0)
                            {
                                publisher.Send(message);
                            }
                            else
                            {
                                Thread.Sleep(1);
                            }
                        }
                    }
                    catch
                    {
                    }
                }
                Running = false;
            }));
            runningThread.Start();
        }
Exemple #2
0
        public string GetResponse(string Host, int Port, string Message, int TimeOutSeconds = 10)
        {
            var Context = new ZeroMQContext();
            var Client  = Context.CreateClientSocket(ZSocketType.REQ);

            Client.ReceiveTimeout = TimeSpan.FromSeconds(TimeOutSeconds);
            Client.Connect("tcp://" + Host + ":" + Port, out var connectError);

            string responseString = "";

            if (connectError != null)
            {
                Console.WriteLine($"Connection error: {connectError.Name} - {connectError.Number} - {connectError.Text}");
                return(null);
            }
            else
            {
                var requestMessage = new ZMessage();
                requestMessage.Add(new ZFrame(Message));
                Client.Send(requestMessage, out var sendError);
                if (sendError == null)
                {
                    try
                    {
                        ZMessage response = null;
                        bool     timeout  = false;
                        DateTime sendTime = DateTime.Now;
                        while (response == null && !timeout)
                        {
                            response = Client.ReceiveMessage(out var error);
                            if (error != null)
                            {
                                timeout = true;
                                Console.WriteLine($"ZeroMQ P2P Client ReadResponse error: {error.Name} - {error.Number} - {error.Text}");
                            }
                            else
                            {
                                timeout = (DateTime.Now - sendTime).TotalSeconds > TimeOutSeconds;
                            }
                        }

                        if (response != null)
                        {
                            responseString = response[0].ReadString();
                        }
                    }
                    catch (Exception err)
                    {
                        Console.WriteLine("Failed -> " + err.ToString());
                    }
                }
                else
                {
                    Console.WriteLine($"Send error: {sendError.Name} - {sendError.Number} - {sendError.Text}");
                }
                requestMessage.Dispose();
            }

            Context.Dispose();

            return(responseString);
        }
Exemple #3
0
 public ZeroMQP2PListener()
 {
     Context  = new ZeroMQContext();
     Listener = Context.CreateServerSocket(ZSocketType.REP);
 }
Exemple #4
0
 public ZeroMQClient()
 {
     context    = new ZeroMQContext();
     subscriber = context.CreateClientSocket(ZSocketType.SUB);
     publisher  = context.CreateClientSocket(ZSocketType.PUB);
 }