Пример #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));
            }
        }
Пример #2
0
        /// <summary>
        ///     Handles incoming data "push" requests: the client sends data for us to add to local storage.
        /// </summary>
        private void AcceptDataAdditionRequest(string requesterIdentity)
        {
            lock (_socketLock)
            {
                if (_socket != null)
                {
                    using (var ms = new MemoryStream())
                    {
                        // Final message part: receive the DataAdditionRequest object
                        bool hasMore;
                        var  buffer  = _socket.ReceiveFrameBytes(out hasMore);
                        var  request = MyUtils.ProtoBufDeserialize <DataAdditionRequest>(buffer, ms);

                        _logger.Info($"Received data push request for {request.Instrument.Symbol}.");
                        // Start building the reply
                        _socket.SendMoreFrame(requesterIdentity);
                        _socket.SendMoreFrame(MessageType.HistPushReply);

                        try
                        {
                            _broker.AddData(request);

                            _socket.SendFrame(MessageType.Success);
                        }
                        catch (Exception ex)
                        {
                            _socket.SendMoreFrame(MessageType.Error);
                            _socket.SendFrame(ex.Message);
                        }
                    }
                }
            }
        }
Пример #3
0
        /// <summary>
        ///     Handles requests for information on data that is available in local storage
        /// </summary>
        private void AcceptAvailableDataRequest(string requesterIdentity)
        {
            lock (_socketLock) {
                if (_socket != null)
                {
                    using (var ms = new MemoryStream()) {
                        // Get the instrument
                        bool hasMore;
                        var  buffer     = _socket.ReceiveFrameBytes(out hasMore);
                        var  instrument = MyUtils.ProtoBufDeserialize <Instrument>(buffer, ms);

                        _logger.Info($"Received local data storage info request for {instrument.Symbol}.");
                        // And send the reply
                        var storageInfo = _broker.GetAvailableDataInfo(instrument);

                        _socket.SendMoreFrame(requesterIdentity);
                        _socket.SendMoreFrame("AVAILABLEDATAREP");
                        _socket.SendMoreFrame(MyUtils.ProtoBufSerialize(instrument, ms));
                        _socket.SendMoreFrame(BitConverter.GetBytes(storageInfo.Count));

                        foreach (var sdi in storageInfo)
                        {
                            _socket.SendMoreFrame(MyUtils.ProtoBufSerialize(sdi, ms));
                        }

                        _socket.SendFrame("END");
                    }
                }
            }
        }
        // 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}");
                }
            }
        }
Пример #5
0
        public Server(ISerializer serializer, string serverUri)
        {
            _serializer   = serializer;
            _rsa          = Rsa.Create();
            _sessionCache = new SessionCache();
            _socket       = new ResponseSocket();
            _socket.Bind(serverUri);

            while (true)
            {
                var messageBytes = _socket.ReceiveFrameBytes();
                var message      = _serializer.Deserialize <SecurityLayerMessage>(messageBytes);
                switch (message.Type)
                {
                case SecurityMessageType.ClientHello:
                    HandleClientHello(message.Data);
                    break;

                case SecurityMessageType.ClientKeyExchange:
                    HandleClientKeyExchange(message.Data);
                    break;

                case SecurityMessageType.ApplicationData:
                    HandleApplicationData(message.Data);
                    break;
                }
            }
        }
Пример #6
0
 protected override void DoServer(NetMQSocket socket, int messageSize)
 {
     for (int i = 0; i < Iterations; i++)
     {
         byte[] message = socket.ReceiveFrameBytes();
         socket.SendFrame(message);
     }
 }
Пример #7
0
        /// <summary>
        ///     Handles requests for information on data that is available in local storage
        /// </summary>
        private void AcceptAvailableDataRequest(string requesterIdentity)
        {
            lock (socketLock)
            {
                if (socket != null)
                {
                    // Get the instrument
                    byte[]     buffer = socket.ReceiveFrameBytes();
                    Instrument instrument;
                    using (var ms = new MemoryStream(buffer))
                    {
                        instrument = MyUtils.ProtoBufDeserialize <Instrument>(ms);
                    }

                    logger.Info($"Received local data storage info request for {instrument.Symbol}.");
                    // And send the reply
                    List <StoredDataInfo> storageInfo = broker.GetAvailableDataInfo(instrument);

                    socket.SendMoreFrame(requesterIdentity);
                    socket.SendMoreFrame(BitConverter.GetBytes((byte)DataRequestMessageType.AvailableDataReply));
                    using (var ms = new MemoryStream(buffer))
                    {
                        socket.SendMoreFrame(MyUtils.ProtoBufSerialize(instrument, ms));
                        socket.SendMoreFrame(BitConverter.GetBytes(storageInfo.Count));

                        for (var i = 0; i < storageInfo.Count; i++)
                        {
                            var sdi = storageInfo[i];
                            if (i < storageInfo.Count - 1)
                            {
                                socket.SendMoreFrame(MyUtils.ProtoBufSerialize(sdi, ms));
                            }
                            else
                            {
                                socket.SendFrame(MyUtils.ProtoBufSerialize(sdi, ms));
                            }
                        }
                    }
                }
            }
        }
Пример #8
0
        private static List <byte[]> ReceiveFrameIdentifier(this NetMQSocket socket)
        {
            // There may be additional ZMQ identities attached; read until the delimiter <IDS|MSG>"
            // and store them in message.identifiers
            // http://ipython.org/ipython-doc/dev/development/messaging.html#the-wire-protocol
            byte[] delimiterBytes = Encoding.ASCII.GetBytes(Constants.DELIMITER);
            byte[] delimiter;
            var    identifier = new List <byte[]>();

            do
            {
                delimiter = socket.ReceiveFrameBytes();
                identifier.Add(delimiter);
            } while (!delimiter.SequenceEqual(delimiterBytes));
            // strip delimiter
            identifier.RemoveAt(identifier.Count - 1);
            return(identifier);
        }
Пример #9
0
        public static Message GetMessage(this NetMQSocket socket)
        {
            var message = new Message();

            // There may be additional ZMQ identities attached; read until the delimiter <IDS|MSG>"
            // and store them in message.identifiers
            // http://ipython.org/ipython-doc/dev/development/messaging.html#the-wire-protocol
            var delimiterAsBytes = Encoding.ASCII.GetBytes(Constants.DELIMITER);

            while (true)
            {
                var delimiter = socket.ReceiveFrameBytes();
                if (delimiter.SequenceEqual(delimiterAsBytes))
                {
                    break;
                }
                message.Identifiers.Add(delimiter);
            }

            // Getting Hmac
            message.Signature = socket.ReceiveFrameString();

            // Getting Header
            var header = socket.ReceiveFrameString();

            message.Header = JsonConvert.DeserializeObject <Header>(header);

            // Getting parent header
            var parentHeader = socket.ReceiveFrameString();

            message.ParentHeader = JsonConvert.DeserializeObject <Header>(parentHeader);

            // Getting metadata
            var metadata = socket.ReceiveFrameString();

            message.MetaData = JObject.Parse(metadata);

            // Getting content
            var content = socket.ReceiveFrameString();

            message.Content = JObject.Parse(content);

            return(message);
        }
Пример #10
0
        public static void Server(NetMQContext context)
        {
            using (NetMQSocket serverSocket = context.CreateResponseSocket())
            {
                JavaScriptSerializer seriallizer = null;
                serverSocket.Bind("tcp://127.0.0.1:5555");
                while (true)
                {
                    seriallizer = new JavaScriptSerializer();
                    var msg = serverSocket.ReceiveFrameBytes();


                    Console.WriteLine("Receive message {0}", Common.ByteConvertHelp.Bytes2Object(msg));
                    serverSocket.Send("Send Hello world");
                    if (Common.ByteConvertHelp.Bytes2Object(msg) == "exit")
                    {
                        break;
                    }
                }
            }
        }
Пример #11
0
        private static void RepOnReceiveReady(object sender, NetMQSocketEventArgs args)
        {
            try
            {
                NetMQSocket rep = args.Socket;

                byte[] message = rep.ReceiveFrameBytes();

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

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

                rep.TrySendFrame(response, response.Length);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception on RepOnReceiveReady: {0}", ex.Message);
                throw;
            }
        }
Пример #12
0
        public static Message GetMessage(this NetMQSocket socket)
        {
            // There may be additional ZMQ identities attached; read until the delimiter <IDS|MSG>"
            // and store them in message.identifiers
            // http://ipython.org/ipython-doc/dev/development/messaging.html#the-wire-protocol
            var delimiterAsBytes = Encoding.ASCII.GetBytes(Constants.DELIMITER);

            var identifiers = new List <byte[]>();

            while (true)
            {
                var delimiter = socket.ReceiveFrameBytes();
                if (delimiter.SequenceEqual(delimiterAsBytes))
                {
                    break;
                }
                identifiers.Add(delimiter);
            }

            // Getting Hmac
            var signature = socket.ReceiveFrameString();

            // Getting Header
            var headerJson = socket.ReceiveFrameString();

            // Getting parent header
            var parentHeaderJson = socket.ReceiveFrameString();

            // Getting metadata
            var metadataJson = socket.ReceiveFrameString();

            // Getting content
            var contentJson = socket.ReceiveFrameString();

            var message = DeserializeMessage(signature, headerJson, parentHeaderJson, metadataJson, contentJson, identifiers);

            return(message);
        }
Пример #13
0
        private void ThreadProcessRequest(NetMQSocket socket)
        {
            byte[] response = null;

            try
            {
                var msg = socket.ReceiveFrameBytes();

                var sw = new Stopwatch();
                sw.Start();
                if (Logger.IsTraceEnabled)
                {
                    Logger.Trace($"ProcessRequest, Thread[{Thread.CurrentThread.ManagedThreadId}] IsThreadPoolThread = {Thread.CurrentThread.IsThreadPoolThread}");
                }

                var din = msg.FromProtobuf <In>();

                if (ProfilerLog.ProfilerLogger.IsInfoEnabled)
                {
                    din.Profile.Add("_itin_", $"{DateTime.UtcNow.Ticks}");
                }

                var context = new ActorContext()
                {
                    Version = din.Version, //版本号
                    Action  = (byte)din.Action,
                    Request = new PirateXRequestInfo(
                        din.HeaderBytes, //信息头
                        din.QueryBytes)  //信息体
                    ,
                    RemoteIp    = din.Ip,
                    LastNo      = din.LastNo,
                    SessionId   = din.SessionId,
                    Profile     = din.Profile,
                    ServerName  = din.ServerName,
                    FrontendID  = din.FrontendID,
                    ServerItmes = din.Items
                };

                response = _actorService.OnReceive(context);

                Logger.Warn($"============={sw.ElapsedMilliseconds}=============");
            }
            catch (Exception exception)
            {
                //TODO 处理,,,
                Logger.Error(exception);
            }
            finally
            {
                //socket.SendMoreFrame(address);
                //socket.SendMoreFrame("");
                if (response != null)
                {
                    socket.SendFrame(response);
                }
                else
                {
                    socket.SendFrame("");
                }
            }
            //TODO 需要处理 finnaly的异常,感知socket 然后重启
        }
Пример #14
0
 protected override void DoServer(NetMQSocket socket, int messageSize)
 {
     for (int i = 0; i < Iterations; i++)
     {
         byte[] message = socket.ReceiveFrameBytes();
         socket.SendFrame(message);
     }
 }
Пример #15
0
        public void OnDataReady()
        {
            switch (m_state)
            {
            case WebSocketClientState.Closed:
                m_state = WebSocketClientState.Handshake;
                string clientHandshake = m_streamSocket.ReceiveFrameString();

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

                string key;

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

                    try
                    {
                        if (m_streamSocket.TrySendFrame(Identity, Identity.Length, true) &&
                            m_streamSocket.TrySendFrame("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;
                        }
                        else
                        {
                            m_state = WebSocketClientState.Closed;
                        }
                    }
                    catch (NetMQException)
                    {
                        m_state = WebSocketClientState.Closed;
                    }
                }
                else
                {
                    m_state = WebSocketClientState.Closed;

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

                    // invalid request, close the socket and raise closed event
                    if (m_streamSocket.TrySendFrame(Identity, Identity.Length, true))
                    {
                        m_streamSocket.TrySendFrame("");
                    }
                }

                break;

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

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Пример #16
0
 private byte[] Receive()
 {
     return(_socket.ReceiveFrameBytes());
 }
Пример #17
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);
            }
        }
Пример #18
0
        private void OnMessage(object sender, NetMQSocketEventArgs e)
        {
            byte[] messageTypeBytes = m_serverSocket.ReceiveFrameBytes();

            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();
            }
        }