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"); } }
/// <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 = LZ4Codec.EncodeHC(uncompressed, 0, uncompressed.Length); // compress _socket.SendFrame(compressed); } } } }
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 RequestSocketReceiveReady(object sender, NetMQSocketEventArgs e) { lock (_requestConnectionString) { if (_requestSocket != null) { var requestType = _requestSocket.ReceiveFrameString(); if (requestType == null) { return; } // Handle ping requests if (requestType.Equals(MessageType.Ping, StringComparison.InvariantCultureIgnoreCase)) { _requestSocket.SendFrame(MessageType.Pong); return; } // Handle real time data requests if (requestType.Equals(MessageType.RTDRequest, StringComparison.InvariantCultureIgnoreCase)) // Two part message: first, "RTD" string. Then the RealTimeDataRequest object. { HandleRealTimeDataRequest(); } // Manage cancellation requests // Three part message: first: MessageType.CancelRTD. Second: the instrument ID. Third: frequency. if (requestType.Equals(MessageType.CancelRTD, StringComparison.InvariantCultureIgnoreCase)) { HandleRealTtimeDataCancelRequest(); } } } }
void Receive(object _, NetMQSocketEventArgs e) { var message = new Message(); message.MergeFrom(Socket.ReceiveMultipartBytes().SelectMany(x => x).ToArray()); if (message.MessageType == MessageType.PingRequest) { Socket.SendFrame(new PingResponse().Wrap(message, MessageType.PingResponse).ToByteArray()); return; } Listener?.OnMessage(message); }
protected override void DoServer(NetMQSocket socket, int messageSize) { for (int i = 0; i < Iterations; i++) { byte[] message = socket.ReceiveFrameBytes(); socket.SendFrame(message); } }
protected override void DoClient(int id, NetMQSocket socket) { const string value = "Hello World"; var expected = value + " " + id; Console.WriteLine("({0}) Pushing: {1}", id, expected); socket.SendFrame(expected); }
/// <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 } } }
/// <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 } } }
public void Start() { while (true) { var msg = frontend.ReceiveMultipartMessage(); backend.SendMultipartMessage(msg); frontend.SendFrame("Success"); } }
public bool Send <T, C>(Message <T> request, C content, string msgType) { var ioPubMessage = new Message <C> { Identifiers = request.Identifiers, Delimiter = request.Delimiter, ParentHeader = request.Header, Header = new Header() { UserName = request.Header.UserName, Session = request.Header.Session, Date = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ"), MessageId = Guid.NewGuid().ToString(), MessageType = msgType, Version = request.Header.Version }, Metadata = request.Metadata, Content = content }; Console.WriteLine($"{msgType}: [{JsonConvert.SerializeObject(ioPubMessage.Content)}]"); var encoder = new UTF8Encoding(); List <string> messages = new List <string>(); var signature = Sign(_key, ioPubMessage, messages, _iopub); // send foreach (var id in request.Identifiers) { _iopub.TrySendFrame(id, true); } _iopub.SendFrame(ioPubMessage.Delimiter, true); _iopub.SendFrame(signature, true); for (int i = 0; i < messages.Count; i++) { _iopub.SendFrame(messages[i], i < messages.Count - 1); } return(true); }
// Accept a real time data request private void HandleRealTimeDataRequest() { var buffer = requestSocket.ReceiveFrameBytes(); using (var ms = new MemoryStream(buffer)) { var request = MyUtils.ProtoBufDeserialize <RealTimeDataRequest>(ms); // Make sure the ID and data sources are set 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(BitConverter.GetBytes((byte)DataRequestMessageType.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}"); } } }
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 static void Server(NetMQContext context) { using (NetMQSocket serverSocket = context.CreateResponseSocket()) { serverSocket.Bind("tcp://127.0.0.1:5555"); while (true) { string message1 = serverSocket.ReceiveFrameString(); Console.WriteLine("Receive message :\r\n{0}\r\n", message1); string[] msg = message1.Split(':'); string message = msg[1]; #region 根据接收到的消息,返回不同的信息 if (message == "Hello") { serverSocket.SendFrame("World"); } else if (message == "ni hao ") { serverSocket.SendFrame("你好!"); } else if (message == "hi") { serverSocket.SendFrame("HI"); } else { serverSocket.SendFrame(message); } #endregion if (message == "exit") { break; } } } }
public void OnMessageReceived(NetMQSocket handler, ReadOnlySpan <byte> payload) { if (_initiator) { return; } var data = payload.ToArray(); handler.SendFrame(data); _out?.WriteInfoLine($"{_id}: Sent: '{Encoding.UTF8.GetString(payload)}'"); }
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()); }
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"); } }
protected override ReceiveStatus SendPointValues() { byte[] message = null; bool recievedmessage = false; DateTime lastMessageTime = DateTime.Now; int index = 0; while (true) { recievedmessage = socket.TryReceiveFrameBytes(out message); if (recievedmessage) { lastMessageTime = DateTime.Now; Debug.Log("Message Received"); //Debug.Log(BitConverter.ToString(message)); if (index >= byte_dataList.Count) { Debug.Log("Transfer Finished"); socket.SendFrame("PointData Finished"); return(ReceiveStatus.SUCCESS); } string convert = System.Text.Encoding.UTF8.GetString(message, 0, message.Length); if (convert == "PointCollumn Echo") { Debug.Log("length of " + index + " : " + dataList[index].Length); Debug.Log("length of " + index + " : " + byte_dataList[index].Length); socket.SendFrame(byte_dataList[index]); lastMessageTime = DateTime.Now; index++; } } DateTime currentTime = DateTime.Now; DateTime stopTime = lastMessageTime.AddSeconds(TimeLimit); if (stopTime < currentTime) { return(ReceiveStatus.TIMEOUT); } } }
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.SendFrame(msg); socket.SkipFrame(); // ignore response } return(watch.ElapsedTicks); }
public static bool SendMessage(this NetMQSocket socket, Message message) { Logger?.LogTrace("Sending Message: {0}", JsonConvert.SerializeObject(message)); if (message.Header.MessageType == MessageType.Input) { lastParentHeader = message.ParentHeader; } if (string.IsNullOrEmpty(message.UUID)) { message.UUID = SessionId; } if (string.IsNullOrEmpty(message.Header.Session)) { message.Header.Session = SessionId; message.ParentHeader = lastParentHeader; } var messageFrames = new[] { JsonConvert.SerializeObject(message.Header), JsonConvert.SerializeObject(message.ParentHeader), JsonConvert.SerializeObject(message.MetaData), JsonConvert.SerializeObject(message.Content) }; string hmac = Validator.CreateSignature(messageFrames); if (message.Identifiers != null && message.Identifiers.Count > 0) { // Send ZMQ identifiers from the message we're responding to. // This is important when we're dealing with ROUTER sockets, like the shell socket, // because the message won't be sent unless we manually include these. foreach (var ident in message.Identifiers) { socket.TrySendFrame(ident, true); } } else { // This is just a normal message so send the UUID socket.SendFrame(message.UUID, true); } socket.SendFrame(Constants.DELIMITER, true); socket.SendFrame(hmac, true); socket.SendFrame(messageFrames[0], true); socket.SendFrame(messageFrames[1], true); socket.SendFrame(messageFrames[2], true); socket.SendFrame(messageFrames[3], false); return(true); }
protected override void DoClient(int id, NetMQSocket socket) { const string value = "Hello World"; var expected = value + " " + id; Console.WriteLine("Client: {0} sending: {1}", id, expected); socket.SendFrame(expected); Thread.Sleep(Random.Next(1, 50)); var response = socket.ReceiveMultipartStrings(); Assert.AreEqual(1, response.Count); Assert.AreEqual(expected, response[0]); Console.WriteLine("Client: {0} received: {1}", id, response[0]); }
public void Ipv6ToIpv4() { using (var context = NetMQContext.Create()) using (var localDealer = context.CreateDealerSocket()) using (NetMQSocket connectingDealer = context.CreateDealerSocket()) { localDealer.Options.IPv4Only = false; var port = localDealer.BindRandomPort(string.Format("tcp://*")); connectingDealer.Connect(string.Format("tcp://{0}:{1}", IPAddress.Loopback, port)); connectingDealer.SendFrame("test"); Assert.AreEqual("test", localDealer.ReceiveFrameString()); } }
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]); } } }
static void Server(NetMQContext context) { using (NetMQSocket serverSocket = context.CreateResponseSocket()) { serverSocket.Bind("tcp://*:5555"); while (true) { string message = serverSocket.ReceiveFrameString(); Console.WriteLine("Receive message {0}", message); serverSocket.SendFrame("World"); if (message == "exit") { break; } } } }
static void Client(NetMQContext context) { using (NetMQSocket clientSocket = context.CreateRequestSocket()) { clientSocket.Connect("tcp://127.0.0.1:5555"); while (true) { Console.WriteLine("Please enter your message:"); string message = Console.ReadLine(); clientSocket.SendFrame(message); string answer = clientSocket.ReceiveFrameString(); Console.WriteLine("Answer from server: {0}", answer); if (message == "exit") { break; } } } }
protected override ReceiveStatus ReceivePointValues() { long timeSinceLastMessage; while (true) { socket.SendFrame("PointCollumn Echo"); byte[] message = null; bool recievedmessage = false; timeSinceLastMessage = 0; while (true) { recievedmessage = socket.TryReceiveFrameBytes(out message); if (recievedmessage) { timeSinceLastMessage = 0; Debug.Log("Message Received"); string convert = System.Text.Encoding.UTF8.GetString(message, 0, message.Length); if (convert == "PointData Finished") { if (dataList.Count < 1) { return(ReceiveStatus.INVALID_FORMAT); } else { return(ReceiveStatus.SUCCESS); } } if (message.Length < 1) { return(ReceiveStatus.INVALID_FORMAT); } var array = new float[message.Length / sizeof(float)]; try { Buffer.BlockCopy(message, 0, array, 0, message.Length); } catch { return(ReceiveStatus.BYTE_CONVERSION_ERROR); } if (dataList.Count != 0 && array.Length != dataList[dataList.Count - 1].Length) { return(ReceiveStatus.INVALID_FORMAT); } dataList.Add(array); break; } else { timeSinceLastMessage++; } if (timeSinceLastMessage > timeLimit) { //Doesn't seem very efficient, should probably find another way later. return(ReceiveStatus.TIMEOUT); } } } }
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); } }
private void ThreadProcessRequest(NetMQSocket socket) { string response = string.Empty; bool receiveok = false; try { if (socket.TryReceiveFrameString(out var msg)) { receiveok = true; var json = JObject.Parse(msg); if (Logger.IsDebugEnabled) { Logger.Debug( $"Thread:{Thread.CurrentThread.ManagedThreadId}:{Thread.CurrentThread.IsThreadPoolThread}->{msg}"); } var name = json.Value <string>("Name"); var serverconfig = json["Config"].ToObject <ScheduleHost.ScheduleDistrictConfig>(); if (!TaskDic.ContainsKey(name)) { return; } var type = TaskDic[name]; if (!(Activator.CreateInstance(type) is IGameJobTask task)) { return; } task.ConfigJson = ConfigJson; try { var sw = new Stopwatch(); sw.Start(); task.Execute(_districtContainer, serverconfig); sw.Stop(); response = JsonConvert.SerializeObject(new { Host = Dns.GetHostName(), Name = name, Msg = "done", Id = serverconfig.Id, sw.ElapsedMilliseconds }); } catch (Exception ex) { if (Logger.IsErrorEnabled) { Logger.Error(ex); } response = JsonConvert.SerializeObject(new { Host = Dns.GetHostName(), Name = name, Id = serverconfig.Id, Msg = "error", Error = ex }); } } else { return; } } catch (Exception exception) { Logger.Error(exception); } finally { if (receiveok) { socket.SendFrame(response); } } }
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; } }
private void Send(string message, NetMQSocket socket, bool sendMore = true) { socket.SendFrame(message, sendMore); }
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.SendFrame(msg); socket.SkipFrame(); // ignore response } return watch.ElapsedTicks; }