Exemple #1
0
    static void Main(String[] args)
    {
        Console.WriteLine("ZMQ version = {0}", Utils.GetZmqVersion());

        var ctx = ZeroMQ.zmq_ctx_new();

        if (ctx != null)
        {
            var pubSocket = ZeroMQ.zmq_socket(ctx, ZeroMQ.ZMQ_PUB);
            if (pubSocket != null)
            {
                if (0 == ZeroMQ.zmq_bind(pubSocket, "tcp://*:60001"))
                {
                    Console.WriteLine("Publishing...");
                    Console.WriteLine("[CTRL + C] to finish...\n");
                    PublishLoop(pubSocket);
                }
                else
                {
                    Console.WriteLine("bind failed");
                }
                ZeroMQ.zmq_close(pubSocket);
            }
            else
            {
                Console.WriteLine("socket failed");
            }
            ZeroMQ.zmq_ctx_term(ctx);
        }
        else
        {
            Console.WriteLine("context failed");
        }
    }
Exemple #2
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);
            }
        }
    }
Exemple #3
0
    static void PublishLoop(SWIGTYPE_p_void pubSocket)
    {
        String last         = String.Empty;
        int    seed         = (int)DateTime.Now.Ticks; //narrowing (discard MSBs)
        var    rnd          = new Random(seed);
        var    symbols      = new String[] { "MSFT", "GOOG", "TSLA", "AMZN", "NVDA", "FB", "PS" };
        var    lastBidPrice = new double[] { 96.71, 1065.13, 277.76, 1571.05, 240.28, 182.50, 19.94 };
        var    throttle     = TimeSpan.FromMilliseconds(100); //~10 msgs/s

        while (true)
        {
            int i      = rnd.Uniform(0, symbols.Length);
            var symbol = symbols[i];
            //var bidPrice = lastBidPrice[i] + (lastBidPrice[i] * rnd.Uniform(-0.0125, 0.0125));
            var bidPrice = lastBidPrice[i] + (lastBidPrice[i] * (rnd.Gaussian() / 100.0));
            lastBidPrice[i] = bidPrice;
            var quantity  = rnd.Uniform(50, 1000 + 1);
            var tradeType = (MarketOrder.TypeOfTrade)rnd.Uniform(1, 5 + 1);
            var timestamp = DateTime.Now;
            var order     = new MarketOrder(symbol, bidPrice, quantity, tradeType, timestamp);
            using (var buffer = new PinnedBuffer(MarketOrder.ToBytes(order)))
            {
                if (-1 != ZeroMQ.zmq_send(pubSocket, buffer, buffer.Length, 0))
                {
                    last = String.Format("Success: Message [{0}] sent", order);
                }
                else
                {
                    last = String.Format("Warning: Message [{0}] not sent", order);
                }
                Console.WriteLine(last);
                Thread.Sleep(throttle);
            }
        }
    }
        public ZmqPollBroker(ZeroMQ.ZmqContext zmqContext,
                         string frontendAddress,
                         string backendAddress)
        {
            this.zmqContext = zmqContext;

            this.frontendAddress = frontendAddress;
            this.backendAddress = backendAddress;
        }
Exemple #5
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");
        }
    }
Exemple #6
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");
        }
    }
Exemple #7
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");
        }
    }
        public ZmqPollServer(ZeroMQ.ZmqContext zmqContext,
                         string brokerBackendAddress,
                         IServiceFactory serviceFactory)
        {
            this.zmqContext = zmqContext;

            this.brokerBackendAddress = brokerBackendAddress;

            this.responsesQueue = new BlockingCollection<Tuple<byte[], byte[]>>(new ConcurrentQueue<Tuple<byte[], byte[]>>(), int.MaxValue);

            this.serviceFactory = serviceFactory;
        }
        public ZmqPollClient(ZeroMQ.ZmqContext zmqContext,
                         string brokerFrontendAddress)
        {
            this.zmqContext = zmqContext;

            this.brokerFrontendAddress = brokerFrontendAddress;

            this.nextId = 0;
            this.requestCallbacks = new ConcurrentDictionary<string, TaskCompletionSource<ResponseData>>();

            this.requestsQueue = new BlockingCollection<byte[]>(new ConcurrentQueue<byte[]>(), int.MaxValue);
        }
Exemple #10
0
        public ZmqBroker(ZeroMQ.ZmqContext zmqContext,
                         string clientInboundAddress,
                         string clientOutboundAddress,
                         string serverInboundAddress,
                         string serverOutboundAddress)
        {
            this.zmqContext = zmqContext;

            this.clientInboundAddress = clientInboundAddress;
            this.clientOutboundAddress = clientOutboundAddress;
            this.serverInboundAddress = serverInboundAddress;
            this.serverOutboundAddress = serverOutboundAddress;
        }
Exemple #11
0
        public ZmqClient(ZeroMQ.ZmqContext zmqContext,
                         string inboundAddress,
                         string outboundAddress)
        {
            this.zmqContext = zmqContext;
            
            this.inboundAddress = inboundAddress;
            this.outboundAddress = outboundAddress;

            this.identity = zmqContext.NewIdentity();

            this.nextId = 0;
            this.requestCallbacks = new ConcurrentDictionary<string, TaskCompletionSource<ResponseData>>();

            this.requestsQueue = new BlockingCollection<byte[]>(new ConcurrentQueue<byte[]>(), int.MaxValue);
        }
Exemple #12
0
        public ZmqServer(ZeroMQ.ZmqContext zmqContext,
                         string inboundAddress,
                         string outboundAddress,
                         IServiceFactory serviceFactory)
        {
            this.zmqContext = zmqContext;

            this.inboundAddress = inboundAddress;
            this.outboundAddress = outboundAddress;

            this.identity = zmqContext.NewIdentity();

            this.responsesQueue = new BlockingCollection<Tuple<byte[], byte[]>>(new ConcurrentQueue<Tuple<byte[], byte[]>>(), int.MaxValue);

            this.serviceFactory = serviceFactory;
        }
Exemple #13
0
 static void ListenLoop(SWIGTYPE_p_void subSocket)
 {
     using (var buffer = new PinnedBuffer(new byte[64]))
     {
         while (true)
         {
             if (-1 != ZeroMQ.zmq_recv(subSocket, buffer.Pointer, buffer.Length, 0))
             {
                 Console.WriteLine("Success: [{0}] received", MarketOrder.FromBytes(buffer));
             }
             else
             {
                 Console.WriteLine("Warning: zmq_recv failed");
             }
         }
     }
 }
Exemple #14
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);
            }
        }
    }
Exemple #15
0
 public static unsafe String GetZmqVersion()
 {
     int[] major = new int[1], minor = new int[1], patch = new int[1];
     ZeroMQ.zmq_version(major, minor, patch);
     return(String.Format("{0}.{1}.{2}", major[0], minor[0], patch[0]));
 }