コード例 #1
0
        private static void WorkerTaskB(object portNumber)
        {
            using (NetMQContext context = NetMQContext.Create())
            {
                using (DealerSocket worker = context.CreateDealerSocket())
                {
                    worker.Options.Identity = Encoding.Unicode.GetBytes("B");
                    worker.Connect(string.Format("tcp://localhost:{0}", portNumber));

                    int total = 0;

                    bool end = false;

                    while (!end)
                    {
                        string request = worker.ReceiveString();

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

                    Console.WriteLine("B Received: {0}", total);
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Process replies on the request socket.
        /// Heartbeats, errors, and subscribing to real time data streams.
        /// </summary>
        private void _reqSocket_ReceiveReady(object sender, NetMQSocketEventArgs e)
        {
            using (var ms = new MemoryStream())
            {
                lock (_reqSocketLock)
                {
                    string reply = _reqSocket.ReceiveString();

                    if (reply == null)
                    {
                        return;
                    }

                    reply = _reqSocket.ReceiveString();

                    switch (reply)
                    {
                    case "PONG":     //reply to heartbeat message
                        _lastHeartBeat = DateTime.Now;
                        break;

                    case "ERROR":     //something went wrong
                    {
                        //first the message
                        string error = _reqSocket.ReceiveString();

                        //then the request
                        byte[] buffer  = _reqSocket.Receive();
                        var    request = MyUtils.ProtoBufDeserialize <RealTimeDataRequest>(buffer, ms);

                        //error event
                        RaiseEvent(Error, this, new ErrorArgs(-1, "Real time data request error: " + error, request.RequestID));
                        return;
                    }

                    case "SUCCESS":     //successful request to start a new real time data stream
                    {
                        //receive the request
                        byte[] buffer  = _reqSocket.Receive();
                        var    request = MyUtils.ProtoBufDeserialize <RealTimeDataRequest>(buffer, ms);

                        //Add it to the active streams
                        lock (_realTimeDataStreamsLock)
                        {
                            RealTimeDataStreams.Add(request);
                        }

                        //request worked, so we subscribe to the stream
                        _subSocket.Subscribe(BitConverter.GetBytes(request.Instrument.ID.Value));
                    }
                    break;

                    case "CANCELED":     //successful cancelation of a real time data stream
                    {
                        //also receive the symbol
                        string symbol = _reqSocket.ReceiveString();

                        //nothing to do?
                    }
                    break;
                    }
                }
            }
        }
コード例 #3
0
        /// <summary>
        /// Tries to connect to the QDMS server.
        /// </summary>
        public void Connect()
        {
            Dispose();

            _context      = NetMQContext.Create();
            _reqSocket    = _context.CreateDealerSocket();
            _subSocket    = _context.CreateSubscriberSocket();
            _dealerSocket = _context.CreateSocket(ZmqSocketType.Dealer);

            _reqSocket.Options.Identity    = Encoding.UTF8.GetBytes(_name);
            _subSocket.Options.Identity    = Encoding.UTF8.GetBytes(_name);
            _dealerSocket.Options.Identity = Encoding.UTF8.GetBytes(_name);

            _dealerSocket.ReceiveReady += _dealerSocket_ReceiveReady;
            _reqSocket.ReceiveReady    += _reqSocket_ReceiveReady;
            _subSocket.ReceiveReady    += _subSocket_ReceiveReady;

            _reqSocket.Connect(string.Format("tcp://{0}:{1}", _host, _realTimeRequestPort));

            //start off by sending a ping to make sure everything is regular
            string reply = "";

            try
            {
                _reqSocket.SendMore("");
                _reqSocket.Send("PING");

                _reqSocket.ReceiveString(TimeSpan.FromSeconds(1)); //empty frame starts the REP message //todo receive string?
                reply = _reqSocket.ReceiveString(TimeSpan.FromMilliseconds(50));
            }
            catch
            {
                Dispose();
            }


            if (reply != "PONG") //server didn't reply or replied incorrectly
            {
                _reqSocket.Disconnect(string.Format("tcp://{0}:{1}", _host, _realTimeRequestPort));
                _reqSocket.Close();
                {
                    RaiseEvent(Error, this, new ErrorArgs(-1, "Could not connect to server."));
                    return;
                }
            }

            _lastHeartBeat = DateTime.Now;
            _subSocket.Connect(string.Format("tcp://{0}:{1}", _host, _realTimePublishPort));
            _dealerSocket.Connect(string.Format("tcp://{0}:{1}", _host, _historicalDataPort));

            _running = true;

            //this loop sends out historical data requests and receives the data
            _dealerLoopThread = new Thread(DealerLoop)
            {
                Name = "Client Dealer Loop"
            };
            _dealerLoopThread.Start();

            //this loop takes care of replies to the request socket: heartbeats and data request status messages
            _poller = new Poller();
            _poller.AddSocket(_reqSocket);
            _poller.AddSocket(_subSocket);
            _poller.AddSocket(_dealerSocket);
            Task.Factory.StartNew(_poller.Start, TaskCreationOptions.LongRunning);

            _heartBeatTimer          = new Timer(1000);
            _heartBeatTimer.Elapsed += _timer_Elapsed;
            _heartBeatTimer.Start();
        }