private void SendDebugMessage(string json) { var msg = new NetMQ.NetMQMessage(); msg.Append(new byte[] { 6 }); msg.Append(json); _socket.SendMultipartMessage(msg); }
public static async Task <NetMQMessage> ReceiveMultipartMessageAsync( [NotNull] this NetMQSocket socket, int expectedFrameCount = 4, CancellationToken cancellationToken = default(CancellationToken)) { var message = new NetMQMessage(expectedFrameCount); while (true) { (byte[] bytes, bool more) = await socket.ReceiveFrameBytesAsync(cancellationToken); message.Append(bytes); if (!more) { break; } } return(message); }
private void _routerSocket_ReceiveReady(object sender, NetMQ.NetMQSocketEventArgs e) { NetMQ.NetMQMessage msg = new NetMQ.NetMQMessage(); if (e.Socket.TryReceiveMultipartMessage(ref msg, 3)) { var address = msg.Pop(); var empty = msg.Pop(); var data = msg.Pop(); var messageData = new MemoryRemoteSocketData() { Address = new StageAddress() { Address = address.ConvertToString() }, Data = data.Buffer }; _inputBuffer.Post(messageData); } }
/// <summary> /// Attempt to receive all frames of the next message from <paramref name="socket"/>. /// If no message is available within <paramref name="timeout"/>, return <c>false</c>. /// </summary> /// <param name="socket">The socket to receive from.</param> /// <param name="timeout">The maximum period of time to wait for a message to become available.</param> /// <param name="message">The received message. Untouched if no message was available.</param> /// <param name="expectedFrameCount">Specifies the initial capacity of the <see cref="List{T}"/> used /// to buffer results. If the number of frames is known, set it here. If more frames arrive than expected, /// an extra allocation will occur, but the result will still be correct.</param> /// <returns><c>true</c> if a message was available, otherwise <c>false</c>.</returns> public static bool TryReceiveMultipartMessage([NotNull] this IReceivingSocket socket, TimeSpan timeout, [CanBeNull] ref NetMQMessage message, int expectedFrameCount = 4) { var msg = new Msg(); msg.InitEmpty(); // Try to read the first frame if (!socket.TryReceive(ref msg, timeout)) { msg.Close(); return(false); } // We have one, so prepare the container if (message == null) { message = new NetMQMessage(expectedFrameCount); } else { message.Clear(); } // Add the frame message.Append(new NetMQFrame(msg.CloneData())); // Rinse and repeat... while (msg.HasMore) { socket.Receive(ref msg); message.Append(new NetMQFrame(msg.CloneData())); } msg.Close(); return(true); }
/// <summary> /// Attempt to transmit a mulitple message on <paramref name="socket"/>. /// If frames cannot be sent immediatly, return <c>false</c>. /// </summary> /// <param name="socket">the IOutgoingSocket to transmit on</param> /// <param name="message">message to transmit</param> public static bool TrySendMultipartMessage([NotNull] this IOutgoingSocket socket, [NotNull] NetMQMessage message) { return(TrySendMultipartMessage(socket, TimeSpan.Zero, message)); }
/// <summary> /// Attempt to transmit a mulitple message on <paramref name="socket"/>. /// If message cannot be sent within <paramref name="timeout"/>, return <c>false</c>. /// </summary> /// <param name="socket">the IOutgoingSocket to transmit on</param> /// <param name="timeout">The maximum period of time to try to send a message.</param> /// <param name="message">message to transmit</param> public static bool TrySendMultipartMessage([NotNull] this IOutgoingSocket socket, TimeSpan timeout, [NotNull] NetMQMessage message) { if (message.FrameCount == 0) { throw new ArgumentException("message is empty", "message"); } else if (message.FrameCount == 1) { return(TrySendFrame(socket, timeout, message[0].Buffer, message[0].MessageSize)); } else { bool sentSuccessfully = TrySendFrame(socket, timeout, message[0].Buffer, message[0].MessageSize, true); if (!sentSuccessfully) { return(false); } } for (int i = 1; i < message.FrameCount - 1; i++) { socket.SendMoreFrame(message[i].Buffer, message[i].MessageSize); } socket.SendFrame(message.Last.Buffer, message.Last.MessageSize); return(true); }
/// <summary> /// Send multiple message on <paramref name="socket"/>, blocking until all entire message is sent. /// </summary> /// <param name="socket">the IOutgoingSocket to transmit on</param> /// <param name="message">message to transmit</param> public static void SendMultipartMessage([NotNull] this IOutgoingSocket socket, [NotNull] NetMQMessage message) { if (message.FrameCount == 0) { throw new ArgumentException("message is empty", "message"); } for (int i = 0; i < message.FrameCount - 1; i++) { socket.SendMoreFrame(message[i].Buffer, message[i].MessageSize); } socket.SendFrame(message.Last.Buffer, message.Last.MessageSize); }
private void OnSocketReady(object sender, NetMQSocketEventArgs e) { NetMQMessage message = m_receiveSocket.ReceiveMultipartMessage(); m_handler(m_receiveSocket, message); }
public DealerRouter() { const int delay = 3000; //Keep dealersocket reference within each three threads var clientSocketPerThread = new ThreadLocal <DealerSocket>(); using (var server = new RouterSocket("tcp://127.0.0.1:5556")) using (var poller = new NetMQPoller()) { for (int i = 0; i < 3; i++) { Task.Factory.StartNew(state => { DealerSocket client = null; if (!clientSocketPerThread.IsValueCreated) { client = new DealerSocket(); client.Options.Identity = Encoding.Unicode.GetBytes(state.ToString()); client.Connect("tcp://127.0.0.1:5556"); client.ReceiveReady += Client_ReceivedReady; clientSocketPerThread.Value = client; poller.Add(client); } else { client = clientSocketPerThread.Value; } //the dealersocket will provide an identity for message when it is created while (true) { var messageToServer = new NetMQMessage(); messageToServer.AppendEmptyFrame(); messageToServer.Append(state.ToString()); Console.WriteLine("======================================"); Console.WriteLine(" OUTGOING MESSAGE TO SERVER "); Console.WriteLine("======================================"); PrintFrames("Client Sending", messageToServer); client.SendMultipartMessage(messageToServer); Thread.Sleep(delay); } }, string.Format("client {0}", i), TaskCreationOptions.LongRunning); } poller.RunAsync(); while (true) { var clientMessage = server.ReceiveMultipartMessage(); Console.WriteLine("======================================"); Console.WriteLine(" INCOMING CLIENT MESSAGE FROM CLIENT "); Console.WriteLine("======================================"); PrintFrames("Server receiving", clientMessage); if (clientMessage.FrameCount == 3) { var clientAddress = clientMessage[0]; var clientOriginalMessage = clientMessage[2].ConvertToString(); string response = string.Format("{0} back from server {1} address {2}", clientOriginalMessage, DateTime.Now.ToLongTimeString(), clientAddress.ConvertToString()); var messageToClient = new NetMQMessage(); messageToClient.Append(clientAddress); messageToClient.AppendEmptyFrame(); messageToClient.Append(response); server.SendMultipartMessage(messageToClient); } } } }
/// <summary> /// Attempt to receive all frames of the next message from <paramref name="socket"/>. /// If no message is immediately available, return <c>false</c>. /// </summary> /// <param name="socket">The socket to receive from.</param> /// <param name="message">The received message. Untouched if no message was available.</param> /// <param name="expectedFrameCount">Specifies the initial capacity of the <see cref="List{T}"/> used /// to buffer results. If the number of frames is known, set it here. If more frames arrive than expected, /// an extra allocation will occur, but the result will still be correct.</param> /// <returns><c>true</c> if a message was available, otherwise <c>false</c>.</returns> public static bool TryReceiveMultipartMessage([NotNull] this IReceivingSocket socket, [CanBeNull] ref NetMQMessage message, int expectedFrameCount = 4) { return(socket.TryReceiveMultipartMessage(TimeSpan.Zero, ref message, expectedFrameCount)); }