/// <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"); }
private void SendReply(byte[] id, byte[] msgId, TReply reply) { byte[] data = _replyMarshaller(reply); _replySocket.SendMore(id); _replySocket.SendMore(msgId); _replySocket.Send(data); }
/// <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 OnRequest(IRequest <TRequest, TReply> obj) { byte[] msgId = GetId(); _requests[new Guid(msgId)] = obj; _requestSocket.SendMore(_id); _requestSocket.SendMore(msgId); byte[] requestData = _requestMarshaller(obj.Request); _requestSocket.Send(requestData); }
private static void WorkerOnReceiveReady(object sender, NetMQSocketEventArgs socket) { // Read and save all frames until we get an empty frame // In this example there is only 1 but it could be more byte[] address = worker.Receive(); byte[] empty = worker.Receive(); byte[] request = worker.Receive(); worker.SendMore(address); worker.SendMore(Encoding.Unicode.GetBytes("")); worker.Send(Encoding.Unicode.GetBytes(Encoding.Unicode.GetString(request) + " WORLD!")); }
/// <summary> /// Pushes data to local storage. /// </summary> public void PushData(DataAdditionRequest request) { if (request.Instrument == null || request.Instrument.ID == null) { RaiseEvent(Error, null, new ErrorArgs(-1, "Instrument must be set and have an ID.")); return; } var ms = new MemoryStream(); lock (_dealerSocketLock) { _dealerSocket.SendMore("HISTPUSH"); _dealerSocket.Send(MyUtils.ProtoBufSerialize(request, ms)); } }
/// <summary> /// Request observer /// </summary> public void Request(Tuple <string, Request> request) { try { var data = request.Item2.Serialize(); if (data == null) { throw new InvalidDataException("Serialize error"); } var id = Encoding.UTF8.GetBytes(request.Item1); router.SendMore(id); router.Send(data); } catch (Exception ex) { SignalService.Logger.Error("Error: {0}", ex.Message); } }
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.SendMore(Topic); socket.Send(expected); }
private void SendEvents(String submissionUri, CancellationToken cancellationToken) { using (NetMQContext context = NetMQContext.Create()) { using (NetMQSocket pushSocket = context.CreatePushSocket()) { pushSocket.IgnoreErrors = true; pushSocket.Connect(submissionUri); Logger.Info("ZeroMQCache: Connected to submissionUri \"{0}\".", submissionUri); while (!cancellationToken.IsCancellationRequested) { try { ZeroMQMessage message; if (mQueue.TryTake(out message, TimeSpan.FromSeconds(1))) { Logger.Debug("ZeroMQCache: Sending -> {0}", message.ToString()); pushSocket.SendMore(message.Topic); pushSocket.SendMore(message.Client); pushSocket.Send(message.Content); } } catch (OperationCanceledException) { // We have been asked to cancel operation break; } catch (Exception ex) { Logger.Error("ZeroMQCache: Error sending message.", ex); } } // Close socket pushSocket.Close(); } context.Terminate(); } }
// 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); }
/// <summary> /// When data arrives from an external data source to the broker, this event is fired. /// </summary> void _broker_RealTimeDataArrived(object sender, RealTimeDataEventArgs e) { using (var ms = new MemoryStream()) { Serializer.Serialize(ms, e); //this lock is needed because this method will be called from //the thread of each DataSource in the broker lock (_pubSocketLock) { _pubSocket.SendMore(BitConverter.GetBytes(e.InstrumentID)); //start by sending the ticker before the data _pubSocket.Send(ms.ToArray()); //then send the serialized bar } } }
public static void Blow(string song, string notes) { new Thread(() => { using (NetMQSocket request_socket = context.CreateRequestSocket()) { request_socket.Connect(string.Format("tcp://{0}:{1}", Address, RequestPort)); request_socket.SendMore("blow").SendMore(song).Send(notes); string response = request_socket.ReceiveString(); Console.WriteLine("[request_socket] Response: {0}", response); request_socket.Close(); request_socket.Dispose(); } }).Start(); }
protected override void DoWork(NetMQSocket socket) { var received = socket.ReceiveMultipartStrings(); for (var i = 0; i < received.Count; i++) { if (i == received.Count - 1) { socket.Send(received[i]); } else { socket.SendMore(received[i]); } } }
/// <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)); } }
/// <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) { 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 string clientIdentity = request.RequesterIdentity; _routerSocket.SendMore(clientIdentity); //2nd message part: the type of reply we're sending _routerSocket.SendMore("HISTREQREP"); //3rd message part: the HistoricalDataRequest object that was used to make the request _routerSocket.SendMore(MyUtils.ProtoBufSerialize(request, ms)); //4th message part: the size of the uncompressed, serialized data. Necessary for decompression on the client end. byte[] uncompressed = MyUtils.ProtoBufSerialize(data, ms); _routerSocket.SendMore(BitConverter.GetBytes(uncompressed.Length)); //5th message part: the compressed serialized data. byte[] compressed = LZ4Codec.EncodeHC(uncompressed, 0, uncompressed.Length); //compress _routerSocket.Send(compressed); } }
// Update is called once per frame // void Update() { AsyncIO.ForceDotNet.Force(); string top; string message; if (client.TryReceiveFrameString(out top)) { if (client.TryReceiveFrameString(out message)) { Debug.Log(message); string[] coord = message.Split(); transX = int.Parse(coord [0]); transY = int.Parse(coord [1]); } } if (Input.GetKeyDown(KeyCode.N)) { calCount++; } if (Input.GetKeyDown(KeyCode.C)) { if (isVisible) { isVisible = false; targetPrefab.SetActive(false); } else { isVisible = true; targetPrefab.SetActive(true); server.SendMore("coord").Send("1180 564"); } } if (calCount != curCount) { calCount = calCount % 10; curCount = calCount; gameObject.GetComponent <Image> ().sprite = calImages [calCount]; } tr = GameObject.FindGameObjectWithTag("RightEyeCamera").transform; vec = (tr.forward) + (tr.right * ((transX - 590) / 1000)) + (tr.up * (((-transY) + 282) / 1000)); //vec = (tr.forward) + (tr.right * (transX)) + (tr.up * ((transY))); Vector3 orig = GameObject.FindGameObjectWithTag("RightEyeCamera").transform.position; RaycastHit hit; Ray ray = new Ray(orig, vec); //Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (isVisible) { if (Physics.Raycast(ray, out hit, 100.0f)) { //Debug.DrawLine (ray.origin, hit.point); targetPrefab.transform.position = hit.point; targetPrefab.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal); targetPrefab.SetActive(true); } else { targetPrefab.SetActive(false); } } }
public void Start() { using (NetMQSocket response_socket = context.CreateResponseSocket()) using (NetMQSocket publisher_socket = context.CreateXPublisherSocket()) { string response_address = string.Format("tcp://{0}:{1}", Address, ResponsePort); string publisher_address = string.Format("tcp://{0}:{1}", Address, PublisherPort); response_socket.Bind(response_address); publisher_socket.Bind(publisher_address); Console.WriteLine("[response_socket] Bound on {0}", response_address); Console.WriteLine("[publisher_socket] Bound on {0}", publisher_address); using (Poller poller = new Poller(response_socket, publisher_socket)) { response_socket.ReceiveReady += delegate(object sender, NetMQSocketEventArgs args) { string message = response_socket.ReceiveString(); if (message.StartsWith("blow")) { string song = ""; string notes = ""; try { song = response_socket.ReceiveString(); notes = response_socket.ReceiveString(); if (song.Length > 64) { song = song.Substring(0, 64); } if (notes.Length > 64) { notes = notes.Substring(0, 64); } cache [song] = notes; Console.WriteLine("[response_socket] Received: song: {0}, notes: {1}", song, notes); } catch (Exception e) { Console.WriteLine("[response_socket] Invalid request: {0}", e.Message); } response_socket.Send("OK"); Console.WriteLine("[response_socket] Sent: OK"); publisher_socket.SendMore(song).Send(notes); Console.WriteLine("[publisher_socket] Sent: song: {0}, notes: {1}", song, notes); return; } if (message.Equals("ping")) { Console.WriteLine("[response_socket] Received: {0}", message); int timestamp = (int)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds; response_socket.Send(timestamp.ToString()); Console.WriteLine("[response_socket] Sent: {0}", timestamp); return; } Console.WriteLine("[response_socket] Invalid request: {0}", message); args.Socket.Send("Meow?"); }; // Send cached notes to new subscribers publisher_socket.ReceiveReady += delegate(object sender, NetMQSocketEventArgs args) { NetMQMessage message = publisher_socket.ReceiveMessage(); // Subscribe == 1, Unsubscibe == 0 if (message.First.Buffer [0] != 1) { return; } string song = message.First.ConvertToString().Substring(1); string cached_notes = (string)cache [song]; if (cached_notes != null) { publisher_socket.SendMore(song).Send(cached_notes); } }; poller.Start(); } } }
private void TransactionDelete() { byte[] transactionIdBytes = m_serverSocket.Receive(); int transactionId = BitConverter.ToInt32(transactionIdBytes, 0); byte[] documentIdBytes = m_serverSocket.Receive(); DocumentId documentId = new DocumentId(documentIdBytes); try { m_db.TransactionDelete(transactionId, documentId); // sending success m_serverSocket.Send(Protocol.Success); } catch (TransactionNotExistException ex) { m_serverSocket.SendMore(Protocol.Failed).Send("Transaction doesn't exist"); } catch (DocumentLockedException) { m_serverSocket.SendMore(Protocol.Failed).Send("Document locked by another transaction"); } }
/// <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"); }
/// <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); } }
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); }
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(); } }
/// <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>()); } } }