示例#1
0
        private void HandleInstrumentAdditionRequest()
        {
            using (var ms = new MemoryStream())
            {
                var buffer     = _socket.ReceiveFrameBytes();
                var instrument = MyUtils.ProtoBufDeserialize <Instrument>(buffer, ms);

                _logger.Info($"Instruments Server: Received instrument addition request. Instrument: {instrument}");

                Instrument addedInstrument;

                try
                {
                    addedInstrument = _instrumentManager.AddInstrument(instrument);
                }
                catch (Exception ex)
                {
                    addedInstrument = null;

                    _logger.Error($"Instruments Server: Instrument addition error: {ex.Message}");
                }

                _socket.SendMoreFrame(addedInstrument != null ? MessageType.Success : MessageType.Error);

                _socket.SendFrame(MyUtils.ProtoBufSerialize(addedInstrument, ms));
            }
        }
        private void handleMessage(string msg, NetMQSocket socket)
        {
            JObject    req      = JObject.Parse(msg);
            string     tickerId = (string)req["ticker"];
            TickerInfo info;
            bool       found = false;

            lock (symbols)
            {
                found = symbols.TryGetValue(tickerId, out info);
            }
            if (found)
            {
                JObject resp = new JObject();
                resp["ticker"]     = info.TickerId;
                resp["lot_size"]   = info.LotSize;
                resp["tick_size"]  = info.TickSize;
                resp["tick_price"] = info.TickPrice;
                socket.SendMoreFrame("OK");
                socket.SendFrame(resp.ToString());
            }
            else
            {
                socket.SendMoreFrame("ERROR");
                socket.SendFrame("Unknown ticker");
            }
        }
示例#3
0
        /// <summary>
        ///     Given a historical data request and the data that fill it,
        ///     send the reply to the client who made the request.
        /// </summary>
        private void SendFilledHistoricalRequest(HistoricalDataRequest request, List <OHLCBar> data)
        {
            lock (_socketLock)
            {
                if (_socket != null)
                {
                    using (var ms = new MemoryStream())
                    {
                        // This is a 5 part message
                        // 1st message part: the identity string of the client that we're routing the data to
                        var clientIdentity = request.RequesterIdentity;

                        _socket.SendMoreFrame(clientIdentity ?? string.Empty);
                        // 2nd message part: the type of reply we're sending
                        _socket.SendMoreFrame(MessageType.HistReply);
                        // 3rd message part: the HistoricalDataRequest object that was used to make the request
                        _socket.SendMoreFrame(MyUtils.ProtoBufSerialize(request, ms));
                        // 4th message part: the size of the uncompressed, serialized data. Necessary for decompression on the client end.
                        var uncompressed = MyUtils.ProtoBufSerialize(data, ms);

                        _socket.SendMoreFrame(BitConverter.GetBytes(uncompressed.Length));
                        // 5th message part: the compressed serialized data.
                        var compressed = new byte[LZ4Codec.MaximumOutputSize(uncompressed.Length)];
                        var targetSpan = new Span <byte>(compressed);

                        int compressedLength = LZ4Codec.Encode(new Span <byte>(uncompressed), targetSpan, LZ4Level.L03_HC);
                        //var compressed = LZ4Codec.EncodeHC(uncompressed, 0, uncompressed.Length); // compress

                        _socket.SendFrame(targetSpan.Slice(0, compressedLength).ToArray());
                    }
                }
            }
        }
        // Accept a real time data request
        private void HandleRealTimeDataRequest()
        {
            using (var ms = new MemoryStream())
            {
                bool hasMore;
                var  buffer  = _requestSocket.ReceiveFrameBytes(out hasMore);
                var  request = MyUtils.ProtoBufDeserialize <RealTimeDataRequest>(buffer, ms);
                // Make sure the ID and data sources are set
                if (!request.Instrument.ID.HasValue)
                {
                    SendErrorReply("Instrument had no ID set.", buffer);

                    _logger.Error("Instrument with no ID requested.");

                    return;
                }

                if (request.Instrument.Datasource == null)
                {
                    SendErrorReply("Instrument had no data source set.", buffer);

                    _logger.Error("Instrument with no data source requested.");

                    return;
                }
                // With the current approach we can't handle multiple real time data streams from
                // the same symbol and data source, but at different frequencies

                // Forward the request to the broker
                try
                {
                    if (_broker.RequestRealTimeData(request))
                    {
                        // And report success back to the requesting client
                        _requestSocket.SendMoreFrame(MessageType.Success);
                        // Along with the request
                        _requestSocket.SendFrame(MyUtils.ProtoBufSerialize(request, ms));
                    }
                    else
                    {
                        throw new Exception("Unknown error.");
                    }
                }
                catch (Exception ex)
                {
                    SendErrorReply(ex.Message, buffer);

                    _logger.Error($"RTDS: Error handling RTD request {request.Instrument.Symbol} @ {request.Instrument.Datasource} ({request.Frequency}): {ex.Message}");
                }
            }
        }
        /// <summary>
        ///     When data arrives from an external data source to the broker, this event is fired.
        /// </summary>
        private void BrokerRealTimeDataArrived(object sender, RealTimeDataEventArgs e)
        {
            lock (_publisherSocketLock)
            {
                if (_publisherSocket == null)
                {
                    return;
                }

                using (var ms = new MemoryStream())
                {
                    Serializer.Serialize(ms, e);
                    _publisherSocket.SendMoreFrame(BitConverter.GetBytes(e.InstrumentID)); // Start by sending the ticker before the data
                    _publisherSocket.SendMoreFrame(MessageType.RealTimeBars);
                    _publisherSocket.SendFrame(ms.ToArray());                              // Then send the serialized bar
                }
            }
        }
示例#6
0
        /// <summary>
        ///     When data arrives from an external data source to the broker, this event is fired.
        /// </summary>
        private void BrokerRealTimeDataArrived(object sender, RealTimeDataEventArgs e)
        {
            lock (_publisherSocketLock)
            {
                if (_publisherSocket == null)
                {
                    return;
                }

                using (var ms = new MemoryStream())
                {
                    Serializer.Serialize(ms, e);
                    _publisherSocket.SendMoreFrame(Encoding.UTF8.GetBytes($"{e.InstrumentID}~{(int)e.Frequency}")); // Start by sending the id+freq before the data
                    _publisherSocket.SendMoreFrame(MessageType.RealTimeBars);
                    _publisherSocket.SendFrame(ms.ToArray());                                                       // Then send the serialized bar
                }
            }
        }
        protected override void DoClient(int id, NetMQSocket socket)
        {
            const string value    = "Hello World";
            var          expected = value + " " + id;

            Console.WriteLine("Client: {0} Publishing: {1}", id, expected);
            socket.SendMoreFrame(Topic);
            socket.SendFrame(expected);
        }
        private void sendResponse(List <Bar> data, NetMQSocket sock)
        {
            try
            {
                MemoryStream stream = new MemoryStream();
                foreach (Bar b in data)
                {
                    BarSerializer.WriteToStream(stream, b);
                }

                sock.SendMoreFrame("OK");
                sock.SendFrame(stream.ToArray());
            }
            catch (Exception e)
            {
                sock.SendMoreFrame("ERROR");
                sock.SendFrame(e.ToString());
            }
        }
        private void SendTicks(string ticker, List <Tick> ticks, NetMQSocket sock)
        {
            var stream = new MemoryStream();

            foreach (var tick in ticks)
            {
                TickSerializer.WriteToStream(stream, tick);
            }

            sock.SendMoreFrame(ticker);
            sock.SendFrame(stream.ToArray());
        }
示例#10
0
        protected override void DoWork(NetMQSocket socket)
        {
            var received = socket.ReceiveMultipartStrings();

            for (var i = 0; i < received.Count; i++)
            {
                if (i == received.Count - 1)
                {
                    socket.SendFrame(received[i]);
                }
                else
                {
                    socket.SendMoreFrame(received[i]);
                }
            }
        }
示例#11
0
        public bool Send(byte[] message, bool dontWait, bool more)
        {
            int frameSize         = 2 + 1 + message.Length;
            int payloadStartIndex = 2;
            int payloadLength     = message.Length + 1;

            if (payloadLength > 125)
            {
                frameSize         += 2;
                payloadStartIndex += 2;

                if (payloadLength > ushort.MaxValue)
                {
                    frameSize         += 6;
                    payloadStartIndex += 6;
                }
            }

            byte[] frame = new byte[frameSize];

            frame[0] = (byte)0x81; // Text and Final

            // No mask
            frame[1] = 0x00;

            if (payloadLength <= 125)
            {
                frame[1] |= (byte)(payloadLength & 127);
            }
            else
            {
                // TODO: implement
            }

            // more byte
            frame[payloadStartIndex] = (byte)(more ? '1' : '0');
            payloadStartIndex++;

            // payload
            Buffer.BlockCopy(message, 0, frame, payloadStartIndex, message.Length);

            try
            {
                if (dontWait)
                {
                    return(m_streamSocket.TrySendFrame(Identity, Identity.Length, true) &&
                           m_streamSocket.TrySendFrame(frame, frame.Length));
                }
                else
                {
                    m_streamSocket.SendMoreFrame(Identity, Identity.Length);
                    m_streamSocket.SendFrame(frame, frame.Length);

                    return(true);
                }
            }
            catch (NetMQException exception)
            {
                m_state = WebSocketClientState.Closed;
                throw exception;
            }
        }
示例#12
0
        private void SocketReceiveReady(object sender, NetMQSocketEventArgs e)
        {
            var hasMore = false;
            var request = string.Empty;

            lock (_socketLock) {
                var receiveResult = _socket?.TryReceiveFrameString(out request, out hasMore);

                if (!receiveResult.HasValue || !receiveResult.Value || string.IsNullOrEmpty(request))
                {
                    return;
                }
            }

            var instruments = new List <Instrument>();

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

                    _logger.Info($"Instruments Server: Received search request: {searchInstrument}");

                    try {
                        instruments = _instrumentManager.FindInstruments(null, searchInstrument);
                    }
                    catch (Exception ex) {
                        _logger.Error($"Instruments Server: Instrument search error: {ex.Message}");
                    }
                }
                else if (request == "ALL") // If the request is for all the instruments, we don't need to receive anything else
                {
                    _logger.Info("Instruments Server: received request for list of all instruments.");

                    try {
                        instruments = _instrumentManager.FindInstruments();
                    }
                    catch (Exception ex) {
                        _logger.Error($"Instruments Server: Instrument search error: {ex.Message}");
                    }
                }
                else if (request == "ADD" && hasMore) // Request to add instrument
                {
                    var buffer     = _socket.ReceiveFrameBytes();
                    var instrument = MyUtils.ProtoBufDeserialize <Instrument>(buffer, ms);

                    _logger.Info($"Instruments Server: Received instrument addition request. Instrument: {instrument}");

                    Instrument addedInstrument;

                    try {
                        addedInstrument = _instrumentManager.AddInstrument(instrument);
                    }
                    catch (Exception ex) {
                        addedInstrument = null;

                        _logger.Error($"Instruments Server: Instrument addition error: {ex.Message}");
                    }

                    _socket.SendMoreFrame(addedInstrument != null ? "SUCCESS" : "FAILURE");

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

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

                var uncompressed = MyUtils.ProtoBufSerialize(instruments, ms); // Serialize the list of instruments

                ms.Read(uncompressed, 0, (int)ms.Length);                      // Get the uncompressed data

                var 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.SendMoreFrame(BitConverter.GetBytes(uncompressed.Length));
                // Then finally send the results
                _socket.SendFrame(result);
            }
        }
示例#13
0
        public void SendMessage <T>(string topic, T message)
        {
            var serializedMessage = _serializer.Serialize(message);

            _socket.SendMoreFrame(topic).SendFrame(serializedMessage);
        }
示例#14
0
        private void TransactionDelete()
        {
            byte[] transactionIdBytes = m_serverSocket.ReceiveFrameBytes();

            int transactionId = BitConverter.ToInt32(transactionIdBytes, 0);

            byte[] documentIdBytes = m_serverSocket.ReceiveFrameBytes();

            DocumentId documentId = new DocumentId(documentIdBytes);

            try
            {
                m_db.TransactionDelete(transactionId, documentId);

                // sending success
                m_serverSocket.SendFrame(Protocol.Success);
            }
            catch (TransactionNotExistException ex)
            {
                m_serverSocket.SendMoreFrame(Protocol.Failed).SendFrame("Transaction doesn't exist");
            }
            catch (DocumentLockedException)
            {
                m_serverSocket.SendMoreFrame(Protocol.Failed).SendFrame("Document locked by another transaction");
            }
        }
示例#15
0
        /// <summary>
        ///     Given a historical data request and the data that fill it,
        ///     send the reply to the client who made the request.
        /// </summary>
        private void SendFilledHistoricalRequest(HistoricalDataRequest request, List <OHLCBar> data)
        {
            lock (socketLock)
            {
                if (socket != null)
                {
                    using (var ms = new MemoryStream())
                    {
                        // This is a 5 part message
                        // 1st message part: the identity string of the client that we're routing the data to
                        var clientIdentity = request.RequesterIdentity;

                        socket.SendMoreFrame(clientIdentity ?? string.Empty);
                        // 2nd message part: the type of reply we're sending
                        socket.SendMoreFrame(BitConverter.GetBytes((byte)DataRequestMessageType.HistReply));
                        // 3rd message part: the HistoricalDataRequest object that was used to make the request
                        socket.SendMoreFrame(MyUtils.ProtoBufSerialize(request, ms));
                        // 4th message part: the size of the uncompressed, serialized data. Necessary for decompression on the client end.
                        var uncompressed = MyUtils.ProtoBufSerialize(data, ms);

                        socket.SendMoreFrame(BitConverter.GetBytes(uncompressed.Length));
                        // 5th message part: the compressed serialized data.
                        var compressed = LZ4Codec.EncodeHC(uncompressed, 0, uncompressed.Length); // compress

                        socket.SendFrame(compressed);
                    }
                }
            }
        }