Exemplo n.º 1
0
        /// <summary>
        /// Handles incoming data "push" requests: the client sends data for us to add to local storage.
        /// </summary>
        private void AcceptDataAdditionRequest(string requesterIdentity, NetMQSocket socket)
        {
            //final message part: receive the DataAdditionRequest object
            bool hasMore;
            var  ms = new MemoryStream();

            byte[] buffer = socket.Receive(out hasMore);

            var request = MyUtils.ProtoBufDeserialize <DataAdditionRequest>(buffer, ms);

            //log the request
            Log(LogLevel.Info, string.Format("Received data push request for {0}.",
                                             request.Instrument.Symbol));

            //start building the reply
            socket.SendMore(requesterIdentity);
            socket.SendMore("PUSHREP");
            try
            {
                _broker.AddData(request);
                socket.Send("OK");
            }
            catch (Exception ex)
            {
                socket.SendMore("ERROR");
                socket.Send(ex.Message);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Handles requests for information on data that is available in local storage
        /// </summary>
        private void AcceptAvailableDataRequest(string requesterIdentity, NetMQSocket socket)
        {
            //get the instrument
            bool hasMore;
            var  ms = new MemoryStream();

            byte[] buffer = socket.Receive(out hasMore);

            var instrument = MyUtils.ProtoBufDeserialize <Instrument>(buffer, ms);

            //log the request
            Log(LogLevel.Info, string.Format("Received local data storage info request for {0}.",
                                             instrument.Symbol));

            //and send the reply

            var storageInfo = _broker.GetAvailableDataInfo(instrument);

            socket.SendMore(requesterIdentity);
            socket.SendMore("AVAILABLEDATAREP");

            socket.SendMore(MyUtils.ProtoBufSerialize(instrument, ms));

            socket.SendMore(BitConverter.GetBytes(storageInfo.Count));
            foreach (StoredDataInfo sdi in storageInfo)
            {
                socket.SendMore(MyUtils.ProtoBufSerialize(sdi, ms));
            }
            socket.Send("END");
        }
Exemplo n.º 3
0
 protected override void DoServer(NetMQSocket socket, int messageSize)
 {
     for (int i = 0; i < Iterations; i++)
     {
         byte[] message = socket.Receive();
         socket.Send(message);
     }
 }
Exemplo n.º 4
0
        // Accept a request to cancel a real time data stream
        // Obviously we only actually cancel it if
        private void HandleRTDataCancelRequest()
        {
            bool hasMore;

            byte[] buffer = _reqSocket.Receive(out hasMore);

            //receive the instrument
            var ms         = new MemoryStream();
            var instrument = MyUtils.ProtoBufDeserialize <Instrument>(buffer, ms);

            if (instrument.ID != null)
            {
                Broker.CancelRTDStream(instrument.ID.Value);
            }

            //two part message:
            //1: "CANCELED"
            //2: the symbol
            _reqSocket.SendMore("CANCELED");
            _reqSocket.Send(instrument.Symbol);
        }
Exemplo n.º 5
0
        public void MultipleLargeMessages()
        {
            byte[] largeMessage = new byte[12000];

            for (int i = 0; i < 12000; i++)
            {
                largeMessage[i] = (byte)(i % 256);
            }

            using (NetMQContext context = NetMQContext.Create())
            {
                using (NetMQSocket pubSocket = context.CreatePublisherSocket())
                {
                    pubSocket.Bind("tcp://127.0.0.1:5558");

                    using (NetMQSocket subSocket = context.CreateSubscriberSocket())
                    {
                        subSocket.Connect("tcp://127.0.0.1:5558");
                        subSocket.Subscribe("");

                        Thread.Sleep(1000);

                        pubSocket.Send("");
                        subSocket.Receive();

                        for (int i = 0; i < 100; i++)
                        {
                            pubSocket.Send(largeMessage);

                            byte[] recvMesage = subSocket.Receive();

                            for (int j = 0; j < 12000; j++)
                            {
                                Assert.AreEqual(largeMessage[j], recvMesage[j]);
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 6
0
        protected override void DoServer(NetMQSocket socket, int messageSize)
        {
            var msg = new Msg();

            msg.InitEmpty();

            for (int i = 0; i < Iterations; i++)
            {
                socket.Receive(ref msg);

                socket.Send(ref msg, more: false);
            }
        }
Exemplo n.º 7
0
        public int ReceiveMessage(NetMQSocket subscriber)
        {
            bool hasMore = true;
            var address = string.Empty;
            int i = 0;
            int retValue = 0;

            var buffer = subscriber.Receive(out hasMore);

            while (hasMore)
            {

                if (i == 0)
                {
                    address = Encoding.Unicode.GetString(buffer);
                }

                if (i == 1)
                {
                    byte[] messageAsBytes = buffer;
                    string message = Encoding.Unicode.GetString(messageAsBytes);
                    if (message == "ADDSUBSCRIBER")
                    {
                        retValue = 1;
                    }
                    else
                    {
                        retValue = -1;
                    }

                }

                i++;
                buffer = subscriber.Receive(out hasMore);
            }

            //zmqMessage = zmqOut;
            return retValue;
        }
Exemplo n.º 8
0
        protected override void DoServer(NetMQSocket socket, int messageSize)
        {
            Msg msg = new Msg();

            msg.InitEmpty();

            for (int i = 0; i < Iterations; i++)
            {
                socket.Receive(ref msg, SendReceiveOptions.None);

                socket.Send(ref msg, SendReceiveOptions.None);
            }
        }
Exemplo n.º 9
0
        protected override long DoClient(NetMQSocket socket, int messageSize)
        {
            var msg = new byte[messageSize];

            var watch = Stopwatch.StartNew();

            for (int i = 0; i < Iterations; i++)
            {
                socket.Send(msg);
                socket.Receive(); // ignore response
            }

            return watch.ElapsedTicks;
        }
Exemplo n.º 10
0
        protected override long DoClient(NetMQSocket socket, int messageSize)
        {
            var msg   = new Msg();
            var watch = Stopwatch.StartNew();

            for (int i = 0; i < Iterations; i++)
            {
                msg.InitGC(new byte[messageSize], messageSize);
                socket.Send(ref msg, more: false);
                socket.Receive(ref msg);
                msg.Close();
            }

            return(watch.ElapsedTicks);
        }
Exemplo n.º 11
0
        private void PullerReceiveReady(object sender, NetMQSocketEventArgs e)
        {
            NetMQSocket socket = e.Socket;
            bool        hasMore;

            try
            {
                string msg = Encoding.UTF8.GetString(socket.Receive(SendReceiveOptions.DontWait, out hasMore));
                if (msg.Length > 0 && this.pullMsgHandler != null)
                {
                    this.pullMsgHandler(msg);
                }
            }
            catch (Exception ce)
            {
                Console.WriteLine("OnPull Error: {0}", ce.Message);
                log.Error("Pulling Message error: ", ce);
            }
        }
Exemplo n.º 12
0
            public ResponseMsg Send(RequestMsg msg)
            {
                if (_disposeCount != 0)
                {
                    throw new ObjectDisposedException(this.GetType().Name);
                }

                try
                {
                    _socket.Send(msg.SerializeMessage());
                    return(_socket.Receive().DeserializeMessage() as ResponseMsg);
                }
                catch (TerminatingException)
                {
                    Debug.WriteLine(string.Format("TerminatingException: auto-disposing {0} ({1:x})...", this.GetType().Name, this.GetHashCode()));
                    ((IDisposable)this).Dispose();
                    throw;
                }
            }
Exemplo n.º 13
0
        private void WaitForMessages(NetMQSocket subscriberSocket)
        {
            while (_running)
            {
                try
                {
                    byte[] bytes = subscriberSocket.Receive();

                    NetMQMessage message = NetMQMessage.FromBytes(bytes);

                    TraceMessages(message.ScaleoutMessage.Messages, "Receiving at " + _configuration.PublisherAddress);
                    OnReceived(0, (ulong)message.MessageId, message.ScaleoutMessage);
                }
                catch (TerminatingException)
                {
                    //Noop
                }
            }
        }
Exemplo n.º 14
0
        private static void RelayMessage(NetMQSocket source, NetMQSocket destination)
        {
            bool hasMore = true;

            while (hasMore)
            {
                // side effect warning!
                // note! that this uses Receive mode that gets a byte[], the router c# implementation
                // doesnt work if you get a string message instead of the byte[] i would prefer the solution thats commented.
                // but the router doesnt seem to be able to handle the response back to the client
                //string message = source.Receive(Encoding.Unicode);
                //hasMore = source.RcvMore;
                //destination.Send(message, Encoding.Unicode, hasMore ? SendRecvOpt.SNDMORE : SendRecvOpt.NONE);

                //int bytesReceived;
                byte[] message = source.Receive(true, out hasMore);
                //hasMore = source.ReceiveMore;
                destination.Send(message, message.Length, hasMore ? SendReceiveOptions.SendMore : SendReceiveOptions.None);
            }
        }
Exemplo n.º 15
0
        private void RepReceiveReady(object sender, NetMQSocketEventArgs e)
        {
            NetMQSocket socket = e.Socket;
            bool        hasMore;

            try
            {
                string msg = Encoding.UTF8.GetString(socket.Receive(SendReceiveOptions.DontWait, out hasMore));
                if (msg.Length > 0)
                {
                    string resp = reqMsgHandler(msg);
                    byte[] data = Encoding.UTF8.GetBytes(resp);
                    socket.Send(data, data.Length);
                }
            }
            catch (Exception ce)
            {
                Console.WriteLine("OnResponse Error: {0}", ce.Message);
                log.Error("Response Message error: ", ce);
            }
        }
Exemplo n.º 16
0
        public void HasInTest()
        {
            using (NetMQContext context = NetMQContext.Create())
            {
                using (NetMQSocket server = context.CreateRouterSocket())
                {
                    server.Bind("tcp://*:5557");

                    // no one sent a message so it should be fasle
                    Assert.IsFalse(server.HasIn);

                    using (NetMQSocket client = context.CreateDealerSocket())
                    {
                        client.Connect("tcp://localhost:5557");

                        // wait for the client to connect
                        Thread.Sleep(100);

                        // now we have one client connected but didn't send a message yet
                        Assert.IsFalse(server.HasIn);

                        client.Send("1");

                        // wait for the message to arrive
                        Thread.Sleep(100);

                        // the has in should indicate a message is ready
                        Assert.IsTrue(server.HasIn);

                        byte[] identity = server.Receive();
                        string message  = server.ReceiveString();

                        Assert.AreEqual(message, "1");

                        // we read the message, it should false again
                        Assert.IsFalse(server.HasIn);
                    }
                }
            }
        }
Exemplo n.º 17
0
        /// <summary>
        /// Add an instrument to QDMS.
        /// </summary>
        /// <param name="instrument"></param>
        /// <returns>The instrument with its ID set if successful, null otherwise.</returns>
        public Instrument AddInstrument(Instrument instrument)
        {
            if (!Connected)
            {
                RaiseEvent(Error, this, new ErrorArgs(-1, "Could not add instrument - not connected."));
                return(null);
            }

            if (instrument == null)
            {
                RaiseEvent(Error, this, new ErrorArgs(-1, "Could not add instrument - instrument is null."));
                return(null);
            }

            using (NetMQSocket s = _context.CreateSocket(ZmqSocketType.Req))
            {
                s.Connect(string.Format("tcp://{0}:{1}", _host, _instrumentServerPort));
                var ms = new MemoryStream();

                s.SendMore("ADD"); //first we send an "ADD" request

                //then we need to serialize and send the instrument
                s.Send(MyUtils.ProtoBufSerialize(instrument, ms));

                //then get the reply
                string result = s.ReceiveString(TimeSpan.FromSeconds(1));

                if (result != "SUCCESS")
                {
                    RaiseEvent(Error, this, new ErrorArgs(-1, "Instrument addition failed: received no reply."));
                    return(null);
                }

                //Addition was successful, receive the instrument and return it
                byte[] serializedInstrument = s.Receive();

                return(MyUtils.ProtoBufDeserialize <Instrument>(serializedInstrument, ms));
            }
        }
Exemplo n.º 18
0
        private static void RepOnReceiveReady(object sender, NetMQSocketEventArgs socket)
        {
            try
            {
                NetMQSocket rep = socket.Socket;

                bool   hasMore;
                byte[] message = rep.Receive(SendReceiveOptions.DontWait, out hasMore);

                //Thread.Sleep(1000); //  Simulate 'work'

                byte[] response =
                    Encoding.Unicode.GetBytes(Encoding.Unicode.GetString(message) + " World from worker " + Encoding.Unicode.GetString(rep.Options.Identity));

                rep.Send(response, response.Length, SendReceiveOptions.DontWait);
            }
            catch (Exception exc)
            {
                Console.WriteLine("Exception on RepOnReceiveReady: {0}", exc.Message);
                throw;
            }
        }
Exemplo n.º 19
0
        /// <summary>
        /// Processes incoming historical data requests.
        /// </summary>
        private void AcceptHistoricalDataRequest(string requesterIdentity, NetMQSocket socket)
        {
            //third: a serialized HistoricalDataRequest object which contains the details of the request
            bool hasMore;

            byte[] buffer = socket.Receive(out hasMore);


            var ms = new MemoryStream();

            ms.Write(buffer, 0, buffer.Length);
            ms.Position = 0;
            HistoricalDataRequest request = Serializer.Deserialize <HistoricalDataRequest>(ms);

            //log the request
            Log(LogLevel.Info, string.Format("Historical Data Request from client {0}: {7} {1} @ {2} from {3} to {4} Location: {5} {6:;;SaveToLocal}",
                                             requesterIdentity,
                                             request.Instrument.Symbol,
                                             Enum.GetName(typeof(BarSize), request.Frequency),
                                             request.StartingDate,
                                             request.EndingDate,
                                             request.DataLocation,
                                             request.SaveDataToStorage ? 0 : 1,
                                             request.Instrument.Datasource.Name));

            request.RequesterIdentity = requesterIdentity;

            try
            {
                _broker.RequestHistoricalData(request);
            }
            catch (Exception ex) //there's some sort of problem with fulfilling the request. Inform the client.
            {
                SendErrorReply(requesterIdentity, request.RequestID, ex.Message);
            }
        }
Exemplo n.º 20
0
        /// <summary>
        /// Called when we get some sort of error reply
        /// </summary>
        private void HandleErrorReply()
        {
            //the request ID
            bool hasMore;

            byte[] buffer = _dealerSocket.Receive(out hasMore);
            if (!hasMore)
            {
                return;
            }
            int requestID = BitConverter.ToInt32(buffer, 0);

            //remove from pending requests
            lock (_pendingHistoricalRequestsLock)
            {
                PendingHistoricalRequests.RemoveAll(x => x.RequestID == requestID);
            }

            //finally the error message
            string message = _dealerSocket.ReceiveString();

            //raise the error event
            RaiseEvent(Error, this, new ErrorArgs(-1, message, requestID));
        }
Exemplo n.º 21
0
        public void OnDataReady()
        {
            switch (m_state)
            {
            case WebSocketClientState.Closed:
                m_state = WebSocketClientState.Handshake;
                string clientHandshake = m_streamSocket.ReceiveString();

                string[] lines = clientHandshake.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

                string key;

                if (ValidateClientHandshake(lines, out key))
                {
                    string acceptKey = GenerateAcceptKey(key);

                    try
                    {
                        m_streamSocket.SendMore(Identity, Identity.Length, true);
                        m_streamSocket.Send("HTTP/1.1 101 Switching Protocols\r\n" +
                                            "Upgrade: websocket\r\n" +
                                            "Connection: Upgrade\r\n" +
                                            "Sec-WebSocket-Accept: " + acceptKey + "\r\n" +
                                            "Sec-WebSocket-Protocol: WSNetMQ\r\n\r\n");

                        m_decoder          = new Decoder();
                        m_decoder.Message += OnMessage;
                        m_state            = WebSocketClientState.Ready;
                    }
                    catch (NetMQException)
                    {
                        m_state = WebSocketClientState.Closed;
                    }
                }
                else
                {
                    m_state = WebSocketClientState.Closed;

                    try
                    {
                        m_streamSocket.SendMore(Identity, Identity.Length, true);
                        m_streamSocket.Send("HTTP/1.1 400 Bad Request\r\nSec-WebSocket-Version: 13\r\n");

                        // invalid request, close the socket and raise closed event
                        m_streamSocket.SendMore(Identity, Identity.Length, true);
                        m_streamSocket.Send("");
                    }
                    catch (NetMQException ex)
                    {
                    }
                }

                break;

            case WebSocketClientState.Ready:
                byte[] message = m_streamSocket.Receive();
                m_decoder.Process(message);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Exemplo n.º 22
0
        protected override void DoServer(NetMQSocket socket, int messageSize)
        {
            var msg  = new Msg();
            msg.InitEmpty();

            for (int i = 0; i < Iterations; i++)
            {
                socket.Receive(ref msg);

                socket.Send(ref msg, more: false);
            }
        }
Exemplo n.º 23
0
		public async Task Start ()
		{
			ThrowIfDisposed ();
			var ct = cancellationTokenSource.Token;

			nmqPoller = new Poller ();
			nmqScheduler = new NetMQScheduler (nmqContext, nmqPoller);
			nmqServer = nmqContext.CreateResponseSocket ();
			nmqServer.Bind (Address.AbsoluteUri.TrimEnd ('/'));

			serverTask = Task.Factory.StartNew (() => {
				ct.ThrowIfCancellationRequested ();

				while (true) {
					if (ct.IsCancellationRequested) {
						// clean up here
						ct.ThrowIfCancellationRequested ();
					}
					var msg = nmqServer.Receive ();
					var request = Request.Deserialize (msg);
					var result = Handle (request);

					byte[] output_buffer = result.Serialize ();
					nmqServer.Send (output_buffer);
				}
			}, ct);

			await serverTask;
		}
Exemplo n.º 24
0
        private void OnMessage(object sender, NetMQSocketEventArgs e)
        {
            byte[] messageTypeBytes = m_serverSocket.Receive();

            MessageType messageType = (MessageType)messageTypeBytes[0];

            switch (messageType)
            {
            case MessageType.Get:
                Get();
                break;

            case MessageType.Update:
                Update();
                break;

            case MessageType.Delete:
                Delete();
                break;

            case MessageType.StartTransaction:
                StartTransaction();
                break;

            case MessageType.Commit:
                Commit();
                break;

            case MessageType.Rollback:
                Rollback();
                break;

            case MessageType.TransactionGet:
                TransactionGet();
                break;

            case MessageType.TransactionUpdate:
                TransactionUpdate();
                break;

            case MessageType.TransactionDelete:
                TransactionDelete();
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Exemplo n.º 25
0
        private static void ProxyBetween(NetMQSocket from, NetMQSocket to, [CanBeNull] NetMQSocket control)
        {
            var msg = new Msg();
            msg.InitEmpty();

            var copy = new Msg();
            copy.InitEmpty();

            while (true)
            {
                from.Receive(ref msg);
                var more = msg.HasMore;

                if (control != null)
                {
                    copy.Copy(ref msg);

                    control.Send(ref copy, more);
                }

                to.Send(ref msg, more);

                if (!more)
                    break;
            }

            copy.Close();
            msg.Close();
        }
Exemplo n.º 26
0
        private static void ProxyBetween(NetMQSocket from, NetMQSocket to, [CanBeNull] NetMQSocket control)
        {
            var msg = new Msg();
            msg.InitEmpty();

            var copy = new Msg();
            copy.InitEmpty();

            while (true)
            {
                from.Receive(ref msg, SendReceiveOptions.None);
                bool more = from.Options.ReceiveMore;

                if (control != null)
                {
                    copy.Copy(ref msg);

                    control.Send(ref copy, more ? SendReceiveOptions.SendMore : SendReceiveOptions.None);
                }

                to.Send(ref msg, more ? SendReceiveOptions.SendMore : SendReceiveOptions.None);

                if (!more)
                {
                    break;
                }
            }

            copy.Close();
            msg.Close();
        }
Exemplo n.º 27
0
        private static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                SysLog.WriteError("参数不正确,请以\r\ncmd demo.exe {本机监听端口号} \r\n启动,例如\r\ndemo.exe 20000");
                Console.WriteLine("参数不正确,请以\r\ncmd demo.exe {本机监听端口号} \r\n启动,例如\r\ndemo.exe 20000");
                return;
            }
            timer.Elapsed += Timer_Tick;
            timer.Interval = 60 * 60 * 1000;
            timer.Enabled  = true;
            //new Thread(isReceivedLink).Start();
            Console.Title  = "Demo" + args[0];
            port           = args[0];
            SysLog.LogName = $"port:{args[0]}.log";
            //进程通信,给服务端通知这是开启的哪个端口
            //Info.ProInfo.GetProcessInfo(args[0]);


            while (true)
            {
                try
                {
                    //创建消息上下文对象
                    using (NetMQContext context = NetMQContext.Create())
                    {
                        //创建Socket对象

                        NetMQSocket serverSocket = context.CreateResponseSocket();

                        //绑定本地端口,监听客户端的请求,端口号与配置一致
                        serverSocket.Bind($"tcp://*:{args[0]}");
                        Console.WriteLine($"开始监听{args[0]}");
                        SysLog.WriteLog($"开始监听{args[0]}");
                        string IP = "";
                        byte[] data;
                        while (true)
                        {
                            //接收消息
                            data = serverSocket.Receive();

                            SysLog.WriteLog($"收到数据,来自:{serverSocket.Options.GetLastEndpoint}");
                            Console.WriteLine($"收到数据,来自:{serverSocket.Options.GetLastEndpoint}数据类型:{data[2]}");
                            receivedCount++;
                            //saveInFileBytes(data);
                            IP = serverSocket.Options.GetLastEndpoint;
                            if (IP.Contains(":"))
                            {
                                IP = IP.Split(':')[0];
                            }
                            try
                            {
                                //解析消息
                                IMsg msg = MsgParser.Parse(data);
                                //出现错误时,msg会为null,继续等待下次接收到的数据。
                                if (msg == null)
                                {
                                    continue;
                                }
                                //创建回复消息
                                ConfirmMsg responseMsg = new ConfirmMsg
                                {
                                    ReceiveMsgType = msg.Type,
                                    ReceiveMsgId   = "0"
                                };

                                //打印消息数据
                                switch (msg.Type)
                                {
                                //车辆消息--可以根据实际情况获取消息内容
                                case MsgType.Vehicle:
                                {
                                    VehicleMsg vMsg = msg as VehicleMsg;
                                    responseMsg.ReceiveMsgId = vMsg.Id;
                                    SysLog.WriteLog(string.Format("车辆信息:({0} {1} {2}t {3})", vMsg.Id, vMsg.EvtTime, vMsg.Weight_T, vMsg.Status));
                                    Console.WriteLine(string.Format("车辆信息:({0} {1} {2} {3}t {4} {5})", vMsg.Id, vMsg.Plate, vMsg.EvtTime, vMsg.Weight_T, vMsg.Speed, vMsg.Status));
                                    Intodb(vMsg, IP, args[0]);
                                    vMsg = null;
                                    break;
                                }

                                //称重消息--可以根据实际情况获取消息内容
                                case MsgType.Wim:
                                {
                                    WIMMsg wMsg = msg as WIMMsg;
                                    SysLog.WriteLog(string.Format("称重信息(时间:{0},车道:{1}, 轴数:{2},总重:{3}t, 车速:{4}k/h,车长: {5}m)",
                                                                  wMsg.EvtTime.ToString("yyyy-MM-dd HH:mm:ss"), wMsg.LaneNo, wMsg.AxlesCount, wMsg.Weight_T, wMsg.Speed, wMsg.Length_M));
                                    responseMsg.ReceiveMsgId = wMsg.Id;
                                    wMsg = null;
                                    break;
                                }

                                //心跳消息
                                case MsgType.Heart:
                                {
                                    SysLog.WriteLog("Heart");
                                    Console.WriteLine("Heart");
                                    break;
                                }
                                }

                                //发送回复消息
                                serverSocket.Send(responseMsg.Encode());
                            }
                            catch (Exception ex)
                            {
                                SysLog.WriteError(ex.Message);
                                Console.WriteLine(ex.Message);
                                Console.WriteLine(ex.InnerException.Message);
                                Console.WriteLine("接收数据出现错误");
                                return;
                            }

                            Console.WriteLine();
                        }
                    }
                }
                catch (SocketException es)
                {
                    SysLog.WriteError(es.Message);
                    Console.WriteLine(es.Message);
                    SysLog.WriteError("服务出错,正在软件内重启");
                    Console.WriteLine("服务出错,正在软件内重启");
                    Thread.Sleep(2000);
                }
                catch (NetMQException e2)
                {
                    SysLog.WriteError("error" + e2.InnerException.Message);
                    Console.WriteLine("error" + e2.InnerException.Message);
                    SysLog.WriteError("服务出错,正在软件内重启");
                    Console.WriteLine("服务出错,正在软件内重启");
                    Thread.Sleep(2000);
                }
                catch (Exception eall)
                {
                    SysLog.WriteError(eall.Message);
                    Console.WriteLine(eall.Message);
                    SysLog.WriteError("服务出错,正在软件内重启");
                    Console.WriteLine("服务出错,正在软件内重启");
                    Thread.Sleep(2000);
                }
            }
        }
Exemplo n.º 28
0
        /// <summary>
        /// Query the server for contracts matching a particular set of features.
        /// </summary>
        /// <param name="instrument">An Instrument object; any features that are not null will be search parameters. If null, all instruments are returned.</param>
        /// <returns>A list of instruments matching these features.</returns>
        public List <Instrument> FindInstruments(Instrument instrument = null)
        {
            if (!Connected)
            {
                RaiseEvent(Error, this, new ErrorArgs(-1, "Could not request instruments - not connected."));
                return(new List <Instrument>());
            }

            using (NetMQSocket s = _context.CreateSocket(ZmqSocketType.Req))
            {
                s.Connect(string.Format("tcp://{0}:{1}", _host, _instrumentServerPort));
                var ms = new MemoryStream();

                if (instrument == null) //all contracts
                {
                    s.Send("ALL");
                }
                else //an actual search
                {
                    s.SendMore("SEARCH"); //first we send a search request

                    //then we need to serialize and send the instrument
                    s.Send(MyUtils.ProtoBufSerialize(instrument, ms));
                }

                //first we receive the size of the final uncompressed byte[] array
                bool   hasMore;
                byte[] sizeBuffer = s.Receive(out hasMore);
                if (sizeBuffer.Length == 0)
                {
                    RaiseEvent(Error, this, new ErrorArgs(-1, "Contract request failed, received no reply."));
                    return(new List <Instrument>());
                }

                int outputSize = BitConverter.ToInt32(sizeBuffer, 0);

                //then the actual data
                byte[] buffer = s.Receive(out hasMore);
                if (buffer.Length == 0)
                {
                    RaiseEvent(Error, this, new ErrorArgs(-1, "Contract request failed, received no data."));
                    return(new List <Instrument>());
                }

                try
                {
                    //then we process it by first decompressing
                    ms.SetLength(0);
                    byte[] decoded = LZ4Codec.Decode(buffer, 0, buffer.Length, outputSize);
                    ms.Write(decoded, 0, decoded.Length);
                    ms.Position = 0;

                    //and finally deserializing
                    return(Serializer.Deserialize <List <Instrument> >(ms));
                }
                catch (Exception ex)
                {
                    RaiseEvent(Error, this, new ErrorArgs(-1, "Error processing instrument data: " + ex.Message));
                    return(new List <Instrument>());
                }
            }
        }
Exemplo n.º 29
0
        /// <summary>
        /// Handles requests for information on data that is available in local storage
        /// </summary>
        private void AcceptAvailableDataRequest(string requesterIdentity, NetMQSocket socket)
        {
            //get the instrument
            bool hasMore;
            var ms = new MemoryStream();
            byte[] buffer = socket.Receive(out hasMore);

            var instrument = MyUtils.ProtoBufDeserialize<Instrument>(buffer, ms);

            //log the request
            Log(LogLevel.Info, string.Format("Received local data storage info request for {0}.",
                instrument.Symbol));

            //and send the reply

            var storageInfo = _broker.GetAvailableDataInfo(instrument);
            socket.SendMore(requesterIdentity);
            socket.SendMore("AVAILABLEDATAREP");

            socket.SendMore(MyUtils.ProtoBufSerialize(instrument, ms));

            socket.SendMore(BitConverter.GetBytes(storageInfo.Count));
            foreach (StoredDataInfo sdi in storageInfo)
            {
                socket.SendMore(MyUtils.ProtoBufSerialize(sdi, ms));
            }
            socket.Send("END");
        }
Exemplo n.º 30
0
        void _socket_ReceiveReady(object sender, NetMQSocketEventArgs e)
        {
            var ms = new MemoryStream();
            List <Instrument> instruments;
            bool   hasMore;
            string request = _socket.ReceiveString(SendReceiveOptions.DontWait, out hasMore);

            if (request == null)
            {
                return;
            }

            //if the request is for a search, receive the instrument w/ the search parameters and pass it to the searcher
            if (request == "SEARCH" && hasMore)
            {
                byte[] buffer           = _socket.Receive();
                var    searchInstrument = MyUtils.ProtoBufDeserialize <Instrument>(buffer, ms);

                Log(LogLevel.Info, string.Format("Instruments Server: Received search request: {0}",
                                                 searchInstrument));

                try
                {
                    instruments = _instrumentManager.FindInstruments(null, searchInstrument);
                }
                catch (Exception ex)
                {
                    Log(LogLevel.Error, string.Format("Instruments Server: Instrument search error: {0}",
                                                      ex.Message));
                    instruments = new List <Instrument>();
                }
            }
            else if (request == "ALL") //if the request is for all the instruments, we don't need to receive anything else
            {
                Log(LogLevel.Info, "Instruments Server: received request for list of all instruments.");
                instruments = _instrumentManager.FindInstruments();
            }
            else if (request == "ADD" && hasMore) //request to add instrument
            {
                byte[] buffer     = _socket.Receive();
                var    instrument = MyUtils.ProtoBufDeserialize <Instrument>(buffer, ms);

                Log(LogLevel.Info, string.Format("Instruments Server: Received instrument addition request. Instrument: {0}",
                                                 instrument));

                Instrument addedInstrument;
                try
                {
                    addedInstrument = _instrumentManager.AddInstrument(instrument);
                }
                catch (Exception ex)
                {
                    addedInstrument = null;
                    Log(LogLevel.Error, string.Format("Instruments Server: Instrument addition error: {0}",
                                                      ex.Message));
                }
                _socket.SendMore(addedInstrument != null ? "SUCCESS" : "FAILURE");

                _socket.Send(MyUtils.ProtoBufSerialize(addedInstrument, ms));

                return;
            }
            else //no request = loop again
            {
                return;
            }

            byte[] uncompressed = MyUtils.ProtoBufSerialize(instruments, ms); //serialize the list of instruments
            ms.Read(uncompressed, 0, (int)ms.Length);                         //get the uncompressed data
            byte[] result = LZ4Codec.Encode(uncompressed, 0, (int)ms.Length); //compress it

            //before we send the result we must send the length of the uncompressed array, because it's needed for decompression
            _socket.SendMore(BitConverter.GetBytes(uncompressed.Length));

            //then finally send the results
            _socket.Send(result);
        }
Exemplo n.º 31
0
        /// <summary>
        /// Handles incoming data "push" requests: the client sends data for us to add to local storage.
        /// </summary>
        private void AcceptDataAdditionRequest(string requesterIdentity, NetMQSocket socket)
        {
            //final message part: receive the DataAdditionRequest object
            bool hasMore;
            var ms = new MemoryStream();
            byte[] buffer = socket.Receive(out hasMore);

            var request = MyUtils.ProtoBufDeserialize<DataAdditionRequest>(buffer, ms);

            //log the request
            Log(LogLevel.Info, string.Format("Received data push request for {0}.",
                request.Instrument.Symbol));

            //start building the reply
            socket.SendMore(requesterIdentity);
            socket.SendMore("PUSHREP");
            try
            {
                _broker.AddData(request);
                socket.Send("OK");
            }
            catch (Exception ex)
            {
                socket.SendMore("ERROR");
                socket.Send(ex.Message);
            }
        }
        private void WaitForMessages(NetMQSocket subscriberSocket)
        {
            while(_running)
            {
                try
                {
                    byte[] bytes = subscriberSocket.Receive();

                    NetMQMessage message = NetMQMessage.FromBytes(bytes);

                    TraceMessages(message.ScaleoutMessage.Messages, "Receiving at " + _configuration.PublisherAddress);
                    OnReceived(0, (ulong) message.MessageId, message.ScaleoutMessage);
                }
                catch(TerminatingException)
                {
                    //Noop
                }
            }
        }
Exemplo n.º 33
0
        protected override long DoClient(NetMQSocket socket, int messageSize)
        {
            var msg = new Msg();
            msg.InitGC(new byte[messageSize], messageSize);

            var watch = Stopwatch.StartNew();

            for (int i = 0; i < Iterations; i++)
            {
                socket.Send(ref msg, SendReceiveOptions.None);
                socket.Receive(ref msg, SendReceiveOptions.None);
            }

            return watch.ElapsedTicks;
        }
Exemplo n.º 34
0
        /// <summary>
        /// Processes incoming historical data requests.
        /// </summary>
        private void AcceptHistoricalDataRequest(string requesterIdentity, NetMQSocket socket)
        {
            //third: a serialized HistoricalDataRequest object which contains the details of the request
            bool hasMore;
            byte[] buffer = socket.Receive(out hasMore);

            var ms = new MemoryStream();
            ms.Write(buffer, 0, buffer.Length);
            ms.Position = 0;
            HistoricalDataRequest request = Serializer.Deserialize<HistoricalDataRequest>(ms);

            //log the request
            Log(LogLevel.Info, string.Format("Historical Data Request from client {0}: {7} {1} @ {2} from {3} to {4} Location: {5} {6:;;SaveToLocal}",
                requesterIdentity,
                request.Instrument.Symbol,
                Enum.GetName(typeof(BarSize), request.Frequency),
                request.StartingDate,
                request.EndingDate,
                request.DataLocation,
                request.SaveDataToStorage ? 0 : 1,
                request.Instrument.Datasource.Name));

            request.RequesterIdentity = requesterIdentity;

            try
            {
                _broker.RequestHistoricalData(request);
            }
            catch (Exception ex) //there's some sort of problem with fulfilling the request. Inform the client.
            {
                SendErrorReply(requesterIdentity, request.RequestID, ex.Message);
            }
        }