Пример #1
0
    static void PublishLoop(SWIGTYPE_p_void pubSocket)
    {
        int    count = 0;
        string last  = String.Empty;                   //, title = Console.Title;

        var throttle = TimeSpan.FromMilliseconds(100); //~10 msgs/s

        while (true)
        {
            String msg = "MSG #" + (++count);
            using (var buffer = PinnedBuffer.ASCII(msg.PadRight(16, ' ')))
            {
                if (-1 != ZeroMQ.zmq_send(pubSocket, buffer, buffer.Length, 0))
                {
                    last = String.Format("Success: Message [{0}] sent", msg);
                }
                else
                {
                    last = String.Format("Warning: Message [{0}] not sent", msg);
                }
                //Console.Title = String.Format("{0}[{1}]", title, last);
                Console.WriteLine(last);
                Thread.Sleep(throttle);
            }
        }
    }
Пример #2
0
    static void Main(String[] args)
    {
        Console.WriteLine("ZMQ version = {0}", Utils.GetZmqVersion());

        var ctx = ZeroMQ.zmq_ctx_new();

        if (ctx != null)
        {
            var requestSocket = ZeroMQ.zmq_socket(ctx, ZeroMQ.ZMQ_REQ);
            if (requestSocket != null)
            {
                if (-1 != ZeroMQ.zmq_connect(requestSocket, "tcp://localhost:60000"))
                {
                    String msg = 0 == args.Length ?
                                 "Hello World" :
                                 args[0].Substring(0, Math.Min(args[0].Length, 64));
                    using (var buffer = PinnedBuffer.ASCII(msg))
                    {
                        if (-1 != ZeroMQ.zmq_send(requestSocket, buffer, buffer.Length, 0))
                        {
                            Console.WriteLine(msg + " sent");
                            if (-1 != ZeroMQ.zmq_recv(requestSocket, buffer, buffer.Length, 0))
                            {
                                Console.WriteLine(PinnedBuffer.ASCII(buffer) + " received back");
                            }
                            else
                            {
                                Console.WriteLine("receive back failed");
                            }
                        }
                        else
                        {
                            Console.WriteLine("send failed");
                        }
                    }
                }
                else
                {
                    Console.WriteLine("connect failed");
                }

                ZeroMQ.zmq_close(requestSocket);
            }
            else
            {
                Console.WriteLine("socket failed");
            }

            ZeroMQ.zmq_ctx_term(ctx);
        }
        else
        {
            Console.WriteLine("context failed");
        }
    }
Пример #3
0
    static void Main(String[] args)
    {
        Console.WriteLine("ZMQ version = {0}", Utils.GetZmqVersion());

        var ctx = ZeroMQ.zmq_ctx_new();

        if (ctx != null)
        {
            var replySocket = ZeroMQ.zmq_socket(ctx, ZeroMQ.ZMQ_REP);
            if (replySocket != null)
            {
                if (0 == ZeroMQ.zmq_bind(replySocket, "tcp://*:60000"))
                {
                    using (var buffer = new PinnedBuffer(new byte[64]))
                    {
                        if (-1 != ZeroMQ.zmq_recv(replySocket, buffer, buffer.Length, 0))
                        {
                            String msg = PinnedBuffer.ASCII(buffer).Replace('\0', ' ').TrimEnd();
                            Console.WriteLine(msg + " received");

                            if (-1 != ZeroMQ.zmq_send(replySocket, buffer, buffer.Length, 0))
                            {
                                Console.WriteLine(msg + " sent back");
                            }
                            else
                            {
                                Console.WriteLine("send failed");
                            }
                        }
                        else
                        {
                            Console.WriteLine("receive failed");
                        }
                    }
                }
                else
                {
                    Console.WriteLine("bind failed");
                }

                ZeroMQ.zmq_close(replySocket);
            }
            else
            {
                Console.WriteLine("socket failed");
            }

            ZeroMQ.zmq_ctx_term(ctx);
        }
        else
        {
            Console.WriteLine("context failed");
        }
    }
Пример #4
0
    static void Main(String[] args)
    {
        Console.WriteLine("ZMQ version = {0}", Utils.GetZmqVersion());

        var ctx = ZeroMQ.zmq_ctx_new();

        if (ctx != null)
        {
            var subSocket = ZeroMQ.zmq_socket(ctx, ZeroMQ.ZMQ_SUB);
            if (subSocket != null)
            {
                if (0 == ZeroMQ.zmq_connect(subSocket, "tcp://localhost:60001"))
                {
                    int numberOfSubscriptions = 0;
                    for (int i = 0; i < args.Length; ++i)
                    {
                        using (var filter = PinnedBuffer.ASCII(args[i]))
                        {
                            if (-1 != ZeroMQ.zmq_setsockopt_2(subSocket, ZeroMQ.ZMQ_SUBSCRIBE, filter.Pointer, filter.Length))
                            {
                                ++numberOfSubscriptions;
                            }
                            else
                            {
                                Console.WriteLine("subscription failed");
                                break;
                            }
                        }
                    }

                    if (numberOfSubscriptions > 0 && numberOfSubscriptions == args.Length)
                    {
                        Console.WriteLine("Listening...");
                        Console.WriteLine("[CTRL + C] to finish...\n");
                        ListenLoop(subSocket, Subscriptions());
                    }
                }
                else
                {
                    Console.WriteLine("connection failed");
                }
                ZeroMQ.zmq_close(subSocket);
            }
            else
            {
                Console.WriteLine("socket failed");
            }
            ZeroMQ.zmq_ctx_term(ctx);
        }
        else
        {
            Console.WriteLine("context failed");
        }
    }
Пример #5
0
    static void ListenLoop(SWIGTYPE_p_void subSocket)
    {
        string last = String.Empty; //, title = Console.Title;

        using (var buffer = new PinnedBuffer(new byte[16]))
        {
            while (true)
            {
                if (-1 != ZeroMQ.zmq_recv(subSocket, buffer.Pointer, buffer.Length, 0))
                {
                    String msg = PinnedBuffer.ASCII(buffer).Replace('\0', ' ').TrimEnd();
                    last = String.Format("Success: [{0}] received", msg);
                }
                else
                {
                    Console.WriteLine("Warning: zmq_recv failed");
                }
                //Console.Title = String.Format("{0}[{1}]", title, last);
                Console.WriteLine(last);
            }
        }
    }