public static async Task <NetMQMessage> ReceiveMultipartMessageAsync( this IReceivingSocket socket, int?timeout = null, int delay = 100) { NetMQMessage message = new NetMQMessage(); int elapsed = 0; while (!socket.TryReceiveMultipartMessage(ref message)) { await Task.Delay(delay); elapsed += delay; if (elapsed > timeout) { throw new TimeoutException( $"The operation exceeded the specified time[{timeout}]."); } } return(message); }
/// <summary> /// Attempt to receive all frames of the next message from <paramref name="socket" />, then ignore their contents. /// 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> /// <returns><c>true</c> if a frame was received and ignored, otherwise <c>false</c>.</returns> public static bool TrySkipMultipartMessage([NotNull] this IReceivingSocket socket, TimeSpan timeout) { Msg msg = new Msg(); msg.InitEmpty(); // Try to read the first frame if (!socket.TryReceive(ref msg, timeout)) { msg.Close(); return(false); } // Rinse and repeat... while (msg.HasMore) { socket.Receive(ref msg); } msg.Close(); return(true); }
public static Message ReceiveMessage(this IReceivingSocket socket, Kernel kernel = null) { var message = new Message(); while (true) { var bytes = socket.ReceiveFrameBytes(); if (bytes.SequenceEqual(Delimiter)) { break; } message.Identities.Add(bytes); } message.Signature = socket.ReceiveFrameString(); var header = socket.ReceiveFrameString(); var parentHeader = socket.ReceiveFrameString(); var metadata = socket.ReceiveFrameString(); var content = socket.ReceiveFrameString(); if (kernel != null) { var sig = kernel.SignMessage(header, parentHeader, metadata, content); if (sig != message.Signature) { Console.WriteLine("! WARNING: Invalid message signature, ignoring the message"); message.Signature = null; } } message.Header = JsonConvert.DeserializeObject <Header>(header); message.ParentHeader = JsonConvert.DeserializeObject <Header>(parentHeader); message.MetaData = JObject.Parse(metadata); message.Content = JObject.Parse(content); // Console.WriteLine($"> [{message.Header.MessageType}] {metadata} {content}"); return(message); }
/// <summary> /// Attempt to receive a valid signal 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="signal"><c>true</c> if the received signal was zero, otherwise <c>false</c>. If no signal received, <c>false</c>.</param> /// <returns><c>true</c> if a valid signal was observed, otherwise <c>false</c>.</returns> public static bool TryReceiveSignal([NotNull] this IReceivingSocket socket, TimeSpan timeout, out bool signal) { var msg = new Msg(); msg.InitEmpty(); // TODO use clock to enforce timeout across multiple loop iterations — if invalid messages are received regularly, the method may not return once the timeout elapses while (true) { if (!socket.TryReceive(ref msg, timeout)) { signal = false; msg.Close(); return(false); } var isMultiFrame = msg.HasMore; while (msg.HasMore) { socket.Receive(ref msg); } if (isMultiFrame || msg.Size != 8) { continue; } var signalValue = NetworkOrderBitsConverter.ToInt64(msg.Data); if ((signalValue & 0x7FFFFFFFFFFFFF00L) == 0x7766554433221100L) { signal = (signalValue & 255) == 0; msg.Close(); return(true); } } }
/// <summary> /// Attempt to receive a single frame from <paramref name="socket"/>, and decode as a string using <paramref name="encoding"/>. /// 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="encoding">The encoding used to convert the frame's data to a string.</param> /// <param name="frameString">The content of the received message frame as a string, or <c>null</c> if no message was available.</param> /// <param name="more"><c>true</c> if another frame of the same message follows, otherwise <c>false</c>.</param> /// <returns><c>true</c> if a message was available, otherwise <c>false</c>.</returns> public static bool TryReceiveFrameString([NotNull] this IReceivingSocket socket, TimeSpan timeout, [NotNull] Encoding encoding, out string frameString, out bool more) { var msg = new Msg(); msg.InitEmpty(); if (socket.TryReceive(ref msg, timeout)) { more = msg.HasMore; frameString = msg.Size > 0 ? encoding.GetString(msg.Data, msg.Offset, msg.Size) : string.Empty; msg.Close(); return(true); } frameString = null; more = false; msg.Close(); return(false); }
// custom proxy function public static void ProxyBetweenAction(this IntermediaryServer server, IReceivingSocket from, IOutgoingSocket to) { var msg = new Msg(); msg.InitEmpty(); while (true) { from.Receive(ref msg); var more = msg.HasMore; server.queue.Enqueue(new IntermediaryMsg { Msg = msg, From = from, To = to }); if (!more) { break; } } }
public static string ReceiveString([NotNull] this IReceivingSocket socket, [NotNull] Encoding encoding, SendReceiveOptions options, out bool hasMore) { if (encoding == null) { throw new ArgumentNullException("encoding"); } var msg = new Msg(); msg.InitEmpty(); socket.Receive(ref msg, options); hasMore = msg.HasMore; string data = msg.Size > 0 ? encoding.GetString(msg.Data, 0, msg.Size) : string.Empty; msg.Close(); return(data); }
/// <summary> /// Receive all frames of the next message from <paramref name="socket"/>, blocking until a message arrives. /// </summary> /// <param name="socket">The socket to receive from.</param> /// <param name="frames">Reference to a list for return values. If <c>null</c> a new instance will be assigned, otherwise the provided list will be cleared and populated.</param> /// <param name="expectedFrameCount">Optional initial <see cref="List{T}.Capacity"/> for the returned <see cref="List{T}"/>.</param> public static void ReceiveMultipartBytes([NotNull] this IReceivingSocket socket, ref List <byte[]> frames, int expectedFrameCount = 4) { if (frames == null) { frames = new List <byte[]>(expectedFrameCount); } else { frames.Clear(); } var msg = new Msg(); msg.InitEmpty(); do { socket.Receive(ref msg); frames.Add(msg.CloneData()); }while (msg.HasMore); msg.Close(); }
/// <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="frames">Reference to a list for return values. If <c>null</c> a new instance will be assigned, otherwise the provided list will be cleared and populated.</param> /// <param name="expectedFrameCount">Optional initial <see cref="List{T}.Capacity"/> for the returned <see cref="List{T}"/>.</param> public static bool TryReceiveMultipartBytes([NotNull] this IReceivingSocket socket, TimeSpan timeout, ref List <byte[]> frames, 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 (frames == null) { frames = new List <byte[]>(expectedFrameCount); } else { frames.Clear(); } // Add the frame frames.Add(msg.CloneData()); // Rinse and repeat... while (msg.HasMore) { socket.Receive(ref msg); frames.Add(msg.CloneData()); } msg.Close(); return(true); }
public static async Task <NetMQMessage> ReceiveMultipartMessageAsync( this IReceivingSocket socket, TimeSpan?timeout = null, TimeSpan?delay = null, CancellationToken cancellationToken = default(CancellationToken)) { NetMQMessage message = new NetMQMessage(); TimeSpan delayNotNull = delay ?? TimeSpan.FromMilliseconds(100); TimeSpan elapsed = TimeSpan.Zero; while (!socket.TryReceiveMultipartMessage(ref message)) { await Task.Delay(delayNotNull, cancellationToken); elapsed += delayNotNull; if (elapsed > timeout) { throw new TimeoutException( $"The operation exceeded the specified time[{timeout}]."); } } return(message); }
/// <summary> /// 将所有消息帧原封不动的发送至另外一端 /// </summary> /// <param name="from"></param> /// <param name="to"></param> /// <param name="control"></param> private static void ProxyBetween(IReceivingSocket from, IOutgoingSocket to, IOutgoingSocket control) { Msg msg = default(Msg); msg.InitEmpty(); Msg msg2 = default(Msg); msg2.InitEmpty(); bool hasMore; do { from.Receive(ref msg); hasMore = msg.HasMore; if (control != null) { msg2.Copy(ref msg); control.Send(ref msg2, hasMore); } to.Send(ref msg, hasMore); }while (hasMore); msg2.Close(); msg.Close(); }
/// <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 receive all frames of the next message from <paramref name="socket"/>, and decode them as strings using <paramref name="encoding"/>. /// 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="encoding">The encoding used to convert the frame's data to a string.</param> /// <param name="frames">The frames of the received message as strings. 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 TryReceiveMultipartStrings([NotNull] this IReceivingSocket socket, TimeSpan timeout, [NotNull] Encoding encoding, [CanBeNull] ref List <string> frames, 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 (frames == null) { frames = new List <string>(expectedFrameCount); } else { frames.Clear(); } // Add the frame frames.Add(encoding.GetString(msg.Data, msg.Offset, msg.Size)); // Rinse and repeat... while (msg.HasMore) { socket.Receive(ref msg); frames.Add(encoding.GetString(msg.Data, msg.Offset, msg.Size)); } msg.Close(); return(true); }
/// <summary> /// Attempt to receive all frames of the next message from <paramref name="socket"/>, and decode them as strings using <see cref="SendReceiveConstants.DefaultEncoding"/>. /// 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="frames">The frames of the received message as strings. 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 TryReceiveMultipartStrings([NotNull] this IReceivingSocket socket, TimeSpan timeout, [CanBeNull] ref List <string> frames, int expectedFrameCount = 4) { return(TryReceiveMultipartStrings(socket, timeout, SendReceiveConstants.DefaultEncoding, ref frames, expectedFrameCount)); }
/// <summary> /// Attempt to receive all frames of the next message from <paramref name="socket"/>, and decode them as strings using <paramref name="encoding"/>. /// If no message is immediately available, return <c>false</c>. /// </summary> /// <param name="socket">The socket to receive from.</param> /// <param name="encoding">The encoding used to convert the frame's data to a string.</param> /// <param name="frames">The frames of the received message as strings. 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 TryReceiveMultipartStrings([NotNull] this IReceivingSocket socket, [NotNull] Encoding encoding, [CanBeNull] ref List <string> frames, int expectedFrameCount = 4) { return(socket.TryReceiveMultipartStrings(TimeSpan.Zero, encoding, ref frames, expectedFrameCount)); }
public static List <string> ReceiveMultipartStrings([NotNull] this IReceivingSocket socket, int expectedFrameCount = 4) { return(ReceiveMultipartStrings(socket, SendReceiveConstants.DefaultEncoding, expectedFrameCount)); }
/// <summary> /// Receive a ZreMsg from the socket. /// </summary> public void Receive(IReceivingSocket input) { bool more; if (input is RouterSocket) { Msg routingIdMsg = new Msg(); routingIdMsg.InitEmpty(); try { input.Receive(ref routingIdMsg); if (!routingIdMsg.HasMore) { throw new MessageException("No routing id"); } if (m_routingId == null || m_routingId.Length == routingIdMsg.Size) m_routingId = new byte[routingIdMsg.Size]; Buffer.BlockCopy(routingIdMsg.Data, 0, m_routingId, 0, m_routingId.Length); } finally { routingIdMsg.Close(); } } else { RoutingId = null; } Msg msg = new Msg(); msg.InitEmpty(); try { input.Receive(ref msg); m_offset = 0; m_buffer = msg.Data; more = msg.HasMore; UInt16 signature = GetNumber2(); if (signature != (0xAAA0 | 1)) { throw new MessageException("Invalid signature"); } // Get message id and parse per message type Id = (MessageId)GetNumber1(); switch (Id) { case MessageId.Hello: Hello.Read(this); break; case MessageId.Whisper: Whisper.Read(this); break; case MessageId.Shout: Shout.Read(this); break; case MessageId.Join: Join.Read(this); break; case MessageId.Leave: Leave.Read(this); break; case MessageId.Ping: Ping.Read(this); break; case MessageId.PingOk: PingOk.Read(this); break; default: throw new MessageException("Bad message id"); } } finally { m_buffer = null; msg.Close(); } }
public static byte[] ReceiveFrameBytes([NotNull] this IReceivingSocket socket) { return(socket.ReceiveFrameBytes(out bool more)); }
/// <summary> /// Receive a ZreMsg from the socket. /// Message is ignored if the message signature doesn't start with %xAA %xA1 (returns false). /// It appears "real" input will always be a RouterSocket, but DealerSocket is used during unit testing by zeromq/zyre and by this implementation. /// </summary> /// <param name="input">the socket</param> /// <returns>true if successful</returns> public bool Receive(IReceivingSocket input) { if (input is RouterSocket) { Msg routingIdMsg = new Msg(); routingIdMsg.InitEmpty(); try { input.Receive(ref routingIdMsg); if (!routingIdMsg.HasMore) { throw new MessageException("No routing id"); } if (_routingId == null || _routingId.Length == routingIdMsg.Size) _routingId = new byte[routingIdMsg.Size]; Buffer.BlockCopy(routingIdMsg.Data, 0, _routingId, 0, _routingId.Length); } finally { routingIdMsg.Close(); } } else { RoutingId = null; } Msg msg = new Msg(); msg.InitEmpty(); try { input.Receive(ref msg); _offset = 0; _buffer = msg.Data; UInt16 signature = GetNumber2(); if (signature != (0xAAA0 | 1)) { // spec:36 A node SHALL silently discard any message received that does not start with these two octets. return false; } // Get message id and parse per message type Id = (MessageId) GetNumber1(); switch (Id) { case MessageId.Hello: Hello.Read(this); break; case MessageId.Whisper: Whisper.Read(this); break; case MessageId.Shout: Shout.Read(this); break; case MessageId.Join: Join.Read(this); break; case MessageId.Leave: Leave.Read(this); break; case MessageId.Ping: Ping.Read(this); break; case MessageId.PingOk: PingOk.Read(this); break; default: throw new MessageException("Bad message id"); } // Receive message content for types with content switch (Id) { case MessageId.Whisper: Whisper.Content = input.ReceiveMultipartMessage(); break; case MessageId.Shout: Shout.Content = input.ReceiveMultipartMessage(); break; } } finally { _buffer = null; msg.Close(); } return true; }
/// <summary> /// Receive a Example from the socket. /// </summary> public void Receive(IReceivingSocket input) { bool more; if (input is RouterSocket) { Msg routingIdMsg = new Msg(); routingIdMsg.InitEmpty(); try { input.Receive(ref routingIdMsg); if (!routingIdMsg.HasMore) { throw new MessageException("No routing id"); } if (m_routingId == null || m_routingId.Length == routingIdMsg.Size) { m_routingId = new byte[routingIdMsg.Size]; } Buffer.BlockCopy(routingIdMsg.Data, 0, m_routingId, 0, m_routingId.Length); } finally { routingIdMsg.Close(); } } else { RoutingId = null; } Msg msg = new Msg(); msg.InitEmpty(); try { input.Receive(ref msg); m_offset = 0; m_buffer = msg.Data; more = msg.HasMore; UInt16 signature = GetNumber2(); if (signature != (0xAAA0 | 0)) { throw new MessageException("Invalid signature"); } // Get message id and parse per message type Id = (MessageId)GetNumber1(); switch (Id) { case MessageId.Log: Log.Read(this); break; case MessageId.Structures: Structures.Read(this); break; case MessageId.Binary: Binary.Read(this); break; case MessageId.Types: Types.Read(this); break; default: throw new MessageException("Bad message id"); } } finally { m_buffer = null; msg.Close(); } }
/// <summary> /// Receive a Example from the socket. /// </summary> public void Receive(IReceivingSocket input) { bool more; if (input is RouterSocket) { RoutingId = input.Receive(out more); if (!more) { throw new MessageException("No routing id"); } } else { RoutingId = null; } Msg msg = new Msg(); msg.InitEmpty(); try { input.Receive(ref msg, SendReceiveOptions.None); m_offset = 0; m_buffer = msg.Data; more = msg.HasMore; UInt16 signature = GetNumber2(); if (signature != (0xAAA0 | 0)) { throw new MessageException("Invalid signature"); } // Get message id and parse per message type Id = (MessageId)GetNumber1(); int listSize; int hashSize; int chunkSize; byte[] guidBytes; UInt16 version; switch (Id) { case MessageId.Log: Sequence = GetNumber2(); version = GetNumber2(); if (version != 3) { throw new MessageException("Version is invalid"); } Level = GetNumber1(); Event = GetNumber1(); Node = GetNumber2(); Peer = GetNumber2(); Time = GetNumber8(); Host = GetString(); Data = GetLongString(); break; case MessageId.Structures: Sequence = GetNumber2(); listSize = (int)GetNumber4(); Aliases = new List<string>(listSize); while (listSize-- > 0) { string s = GetLongString(); Aliases.Add(s); } hashSize = (int)GetNumber4(); Headers = new Dictionary<string, string>(); while (hashSize-- > 0) { string key = GetString(); string value = GetLongString(); Headers.Add(key, value); } break; case MessageId.Binary: Sequence = GetNumber2(); GetOctets(Flags, 4); chunkSize = (int)GetNumber4(); if (m_offset + chunkSize > m_buffer.Length) { throw new MessageException("PublicKey is missing data"); } PublicKey = new byte[chunkSize]; GetOctets(PublicKey, chunkSize); if (m_offset + 16 > m_buffer.Length) { throw new MessageException("Identifier is invalid"); } guidBytes = new byte[16]; GetOctets(guidBytes, 16); Identifier = new Guid(guidBytes); // Get next frame off socket if (!more) { throw new MessageException("Address is missing"); } Address = input.Receive(out more); // Get zero or more remaining frames if (more) Content = input.ReceiveMessage(); else Content = new NetMQMessage(); break; case MessageId.Types: Sequence = GetNumber2(); ClientForename = GetString(); ClientSurname = GetString(); ClientMobile = GetString(); ClientEmail = GetString(); SupplierForename = GetString(); SupplierSurname = GetString(); SupplierMobile = GetString(); SupplierEmail = GetString(); break; default: throw new MessageException("Bad message id"); break; } } finally { m_buffer = null; msg.Close(); } }
/// <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="frames">Reference to a list for return values. If <c>null</c> a new instance will be assigned, otherwise the provided list will be cleared and populated.</param> /// <param name="expectedFrameCount">Optional initial <see cref="List{T}.Capacity"/> for the returned <see cref="List{T}"/>.</param> public static bool TryReceiveMultipartBytes([NotNull] this IReceivingSocket socket, ref List <byte[]> frames, int expectedFrameCount = 4) { return(socket.TryReceiveMultipartBytes(TimeSpan.Zero, ref frames, expectedFrameCount)); }
public static string ReceiveString(this IReceivingSocket socket) { bool hasMore; return(socket.ReceiveString(false, out hasMore)); }
/// <summary> /// Attempt to receive a single frame 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="bytes">The content of the received message frame, or <c>null</c> if no message was available.</param> /// <returns><c>true</c> if a message was available, otherwise <c>false</c>.</returns> public static bool TryReceiveFrameBytes([NotNull] this IReceivingSocket socket, out byte[] bytes) { bool more; return(socket.TryReceiveFrameBytes(out bytes, out more)); }
/// <summary> /// Attempt to receive a single frame from <paramref name="socket"/>, and decode as a string using <paramref name="encoding"/>. /// If no message is immediately available, return <c>false</c>. /// </summary> /// <param name="socket">The socket to receive from.</param> /// <param name="encoding">The encoding used to convert the frame's data to a string.</param> /// <param name="frameString">The content of the received message frame as a string, or <c>null</c> if no message was available.</param> /// <returns><c>true</c> if a message was available, otherwise <c>false</c>.</returns> public static bool TryReceiveFrameString([NotNull] this IReceivingSocket socket, [NotNull] Encoding encoding, out string frameString) { bool more; return(socket.TryReceiveFrameString(TimeSpan.Zero, encoding, out frameString, out more)); }
/// <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)); }
/// <summary> /// Block until the next message arrives, then make the message's data available via <paramref name="msg"/>. /// </summary> /// <remarks> /// The call blocks until the next message arrives, and cannot be interrupted. This a convenient and safe when /// you know a message is available, such as for code within a <see cref="NetMQSocket.ReceiveReady"/> callback. /// </remarks> /// <param name="socket">The socket to receive from.</param> /// <param name="msg">An object to receive the message's data into.</param> public static void Receive(this IReceivingSocket socket, ref Msg msg) { var result = socket.TryReceive(ref msg, SendReceiveConstants.InfiniteTimeout); Debug.Assert(result); }
/// <summary> /// Attempt to receive a valid signal 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="signal"><c>true</c> if the received signal was zero, otherwise <c>false</c>. If no signal received, <c>false</c>.</param> /// <returns><c>true</c> if a valid signal was observed, otherwise <c>false</c>.</returns> public static bool TryReceiveSignal([NotNull] this IReceivingSocket socket, out bool signal) { return(socket.TryReceiveSignal(TimeSpan.Zero, out signal)); }
public static string ReceiveFrameString([NotNull] this IReceivingSocket socket, out bool more) { return(socket.ReceiveFrameString(SendReceiveConstants.DefaultEncoding, out more)); }
/// <summary> /// Attempt to receive a single frame from <paramref name="socket"/>. /// If no message is immediately available, return <c>false</c>. /// Indicate whether further frames exist via <paramref name="more"/>. /// </summary> /// <param name="socket">The socket to receive from.</param> /// <param name="more"><c>true</c> if another frame of the same message follows, otherwise <c>false</c>.</param> /// <param name="bytes">The content of the received message frame, or <c>null</c> if no message was available.</param> /// <returns><c>true</c> if a message was available, otherwise <c>false</c>.</returns> public static bool TryReceiveFrameBytes([NotNull] this IReceivingSocket socket, out byte[] bytes, out bool more) { return(socket.TryReceiveFrameBytes(TimeSpan.Zero, out bytes, out more)); }
/// <summary> /// Attempt to receive a single frame from <paramref name="socket"/>, and decode as a string using <see cref="SendReceiveConstants.DefaultEncoding"/>. /// 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="frameString">The content of the received message frame as a string, or <c>null</c> if no message was available.</param> /// <param name="more"><c>true</c> if another frame of the same message follows, otherwise <c>false</c>.</param> /// <returns><c>true</c> if a message was available, otherwise <c>false</c>.</returns> public static bool TryReceiveFrameString([NotNull] this IReceivingSocket socket, TimeSpan timeout, out string frameString, out bool more) { return(socket.TryReceiveFrameString(timeout, SendReceiveConstants.DefaultEncoding, out frameString, out more)); }
private static void ProxyBetween(IReceivingSocket from, IOutgoingSocket to, [CanBeNull] IOutgoingSocket control) { var msg = new Msg(); msg.InitEmpty(); var copy = new Msg(); copy.InitEmpty(); while (true) { from.Receive(ref msg); var more = msg.HasMore; if (control != null) { copy.Copy(ref msg); control.Send(ref copy, more); } to.Send(ref msg, more); if (!more) break; } copy.Close(); msg.Close(); }
/// <summary> /// Attempt to receive a single frame from <paramref name="socket"/>, and decode as a string using <paramref name="encoding"/>. /// 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="encoding">The encoding used to convert the frame's data to a string.</param> /// <param name="frameString">The content of the received message frame as a string, or <c>null</c> if no message was available.</param> /// <returns><c>true</c> if a message was available, otherwise <c>false</c>.</returns> public static bool TryReceiveFrameString([NotNull] this IReceivingSocket socket, TimeSpan timeout, [NotNull] Encoding encoding, out string frameString) { return(socket.TryReceiveFrameString(timeout, encoding, out frameString, out bool more)); }
public static string ReceiveFrameString([NotNull] this IReceivingSocket socket, [NotNull] Encoding encoding) { return(socket.ReceiveFrameString(encoding, out bool more)); }
public static ZreMessage Receive(IReceivingSocket input) { NetMQMessage message = input.ReceiveMultipartMessage(); if (message == null) { return null; } NetMQFrame routingId = null; if (input is RouterSocket) { routingId = message.Pop(); if (routingId == null || message.IsEmpty) { return null; } } var zreMsg = Decode(message); if (zreMsg != null && input is RouterSocket) { zreMsg._routingId = routingId; } return zreMsg; }