private void Connect()
            {
                // getting the snapshot
                using (RequestSocket requestSocket = context.CreateRequestSocket())
                {
                    requestSocket.Connect(string.Format("tcp://{0}:{1}", address, SnapshotProtocol.Port));

                    requestSocket.Send(SnapshotProtocol.GetTradessCommand);

                    string json;

                    requestSocket.Options.ReceiveTimeout = SnapshotProtocol.RequestTimeout;

                    try
                    {
                        json = requestSocket.ReceiveString();
                    }
                    catch (AgainException ex)
                    {
                        // Fail to receive trades, we call on error and don't try to do anything with subscriber
                        // calling on error from poller thread block the application
                        Task.Run(() => subject.OnError(new Exception("No response from server")));
                        return;
                    }

                    while (json != SnapshotProtocol.EndOfTickers)
                    {
                        PublishTicker(json);

                        json = requestSocket.ReceiveString();
                    }
                }

                subscriberSocket = context.CreateSubscriberSocket();
                subscriberSocket.Subscribe(StreamingProtocol.TradesTopic);
                subscriberSocket.Subscribe(StreamingProtocol.HeartbeatTopic);
                subscriberSocket.Connect(string.Format("tcp://{0}:{1}", address, StreamingProtocol.Port));
                subscriberSocket.ReceiveReady += OnSubscriberReady;

                poller.AddSocket(subscriberSocket);

                // reset timeout timer
                timeoutTimer.Enable = false;
                timeoutTimer.Enable = true;
            }
Beispiel #2
0
        public static void WorkerTask(object portNumber)
        {
            var randomizer = new Random(DateTime.Now.Millisecond);

            using (NetMQContext context = NetMQContext.Create())
            {
                using (RequestSocket worker = context.CreateRequestSocket())
                {
                    //  We use a string identity for ease here
                    string id  = ZHelpers.SetID(worker, Encoding.Unicode);
                    string cnn = string.Format("tcp://localhost:{0}", portNumber);
                    worker.Connect(cnn);
                    Console.WriteLine("[W] ID {0} connect to {1}", id, cnn);

                    int total = 0;

                    bool end = false;
                    while (!end)
                    {
                        //  Tell the router we're ready for work
                        string msg = "Ready";
                        worker.Send(msg);
                        //Console.WriteLine("[W] Message sent: {0}", msg);

                        //  Get workload from router, until finished
                        string workload = worker.ReceiveString();
                        //Console.WriteLine("[W] Workload received: {0}", workload);

                        if (workload.Equals("END"))
                        {
                            end = true;
                        }
                        else
                        {
                            total++;

                            Thread.Sleep(randomizer.Next(1, 1000)); //  Simulate 'work'
                        }
                    }

                    Console.WriteLine("ID ({0}) processed: {1} tasks", Encoding.Unicode.GetString(worker.Options.Identity), total);
                }
            }
        }