/// <summary> /// Receive frames from <paramref name="socket"/>, blocking until a valid signal arrives. /// </summary> /// <param name="socket">The socket to receive from.</param> /// <returns><c>true</c> if the received signal was zero, otherwise <c>false</c>.</returns> public static bool ReceiveSignal([NotNull] this IReceivingSocket socket) { var msg = new Msg(); msg.InitEmpty(); while (true) { socket.Receive(ref msg); 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) { msg.Close(); return((signalValue & 255) == 0); } } }
public static string ReceiveString(this IReceivingSocket socket, [NotNull] Encoding encoding, SendReceiveOptions options, out bool hasMore) { if (ReferenceEquals(encoding, null)) { throw new ArgumentNullException("encoding"); } Msg msg = new Msg(); msg.InitEmpty(); socket.Receive(ref msg, options); hasMore = msg.HasMore; string data = string.Empty; if (msg.Size > 0) { data = encoding.GetString(msg.Data, 0, msg.Size); } msg.Close(); return(data); }
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> /// Receive a single frame from <paramref name="socket"/>, blocking until one arrives, then ignore its content. /// </summary> /// <param name="socket">The socket to receive from.</param> public static void SkipFrame([NotNull] this IReceivingSocket socket) { var msg = new Msg(); msg.InitEmpty(); socket.Receive(ref msg); msg.Close(); }
/// <summary> /// Receive a single frame from <paramref name="socket"/>, blocking until one arrives, then ignore its content. /// 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> public static void SkipFrame([NotNull] this IReceivingSocket socket, out bool more) { var msg = new Msg(); msg.InitEmpty(); socket.Receive(ref msg); more = msg.HasMore; msg.Close(); }
public static IEnumerable <byte[]> ReceiveMessages(this IReceivingSocket socket) { bool hasMore = true; while (hasMore) { yield return(socket.Receive(false, out hasMore)); } }
public static byte[] Receive([NotNull] this IReceivingSocket socket, bool dontWait, out bool hasMore) { var options = SendReceiveOptions.None; if (dontWait) { options |= SendReceiveOptions.DontWait; } return(socket.Receive(options, out hasMore)); }
/// <summary> /// Receive all frames of the next message from <paramref name="socket"/>, blocking until a message arrives, then ignore their contents. /// </summary> /// <param name="socket">The socket to receive from.</param> public static void SkipMultipartMessage([NotNull] this IReceivingSocket socket) { var msg = new Msg(); msg.InitEmpty(); do { socket.Receive(ref msg); }while (msg.HasMore); msg.Close(); }
public static void ReceiveMessage(this IReceivingSocket socket, NetMQMessage message, bool dontWait = false) { message.Clear(); bool more = true; while (more) { byte[] buffer = socket.Receive(dontWait, out more); message.Append(buffer); } }
public static List <byte[]> ReceiveMessages([NotNull] this IReceivingSocket socket, int expectedFrameCount = 4) { var frames = new List <byte[]>(capacity: expectedFrameCount); bool hasMore = true; while (hasMore) { frames.Add(socket.Receive(false, out hasMore)); } return(frames); }
public ReadOnlyMemory <byte> ReceiveFrame(IReceivingSocket socket, out bool more) { if (msg.IsInitialised) { msg.Close(); } msg.InitEmpty(); socket.Receive(ref msg); more = msg.HasMore; return(msg.Data.AsMemory(msg.Offset, msg.Size)); }
public static byte[] ReceiveFrameBytes([NotNull] this IReceivingSocket socket, out bool more) { var msg = new Msg(); msg.InitEmpty(); socket.Receive(ref msg); var data = msg.CloneData(); more = msg.HasMore; msg.Close(); return(data); }
public static NetMQMessage ReceiveMultipartMessage([NotNull] this IReceivingSocket socket, int expectedFrameCount = 4) { var msg = new Msg(); msg.InitEmpty(); var message = new NetMQMessage(expectedFrameCount); do { socket.Receive(ref msg); message.Append(msg.CloneData()); }while (msg.HasMore); msg.Close(); return(message); }
public static List <string> ReceiveMultipartStrings([NotNull] this IReceivingSocket socket, [NotNull] Encoding encoding, int expectedFrameCount = 4) { var frames = new List <string>(expectedFrameCount); var msg = new Msg(); msg.InitEmpty(); do { socket.Receive(ref msg); frames.Add(encoding.GetString(msg.Data, msg.Offset, msg.Size)); }while (msg.HasMore); msg.Close(); return(frames); }
public static string ReceiveFrameString([NotNull] this IReceivingSocket socket, [NotNull] Encoding encoding, out bool more) { var msg = new Msg(); msg.InitEmpty(); socket.Receive(ref msg); more = msg.HasMore; var str = msg.Size > 0 ? encoding.GetString(msg.Data, msg.Offset, msg.Size) : string.Empty; msg.Close(); return(str); }
public static byte[] Receive(this IReceivingSocket socket, TimeSpan timeout) { var s = socket as NetMQSocket; if (s == null) { throw new InvalidCastException(string.Format("Expected a NetMQSocket but got a {0}", socket.GetType())); } var result = s.Poll(PollEvents.PollIn, timeout); if (!result.HasFlag(PollEvents.PollIn)) { return(null); } return(socket.Receive()); }
public static string ReceiveString(this IReceivingSocket socket, Encoding encoding, SendReceiveOptions options, out bool hasMore) { Msg msg = new Msg(); msg.InitEmpty(); socket.Receive(ref msg, options); hasMore = msg.HasMore; string data = string.Empty; if (msg.Size > 0) { data = encoding.GetString(msg.Data, 0, msg.Size); } msg.Close(); return(data); }
public static byte[] Receive([NotNull] this IReceivingSocket socket, SendReceiveOptions options, out bool hasMore) { var msg = new Msg(); msg.InitEmpty(); socket.Receive(ref msg, options); var data = new byte[msg.Size]; if (msg.Size > 0) { Buffer.BlockCopy(msg.Data, 0, data, 0, msg.Size); } hasMore = msg.HasMore; msg.Close(); return(data); }
/// <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) { var 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); }
/// <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); } } }
// 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="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"/>. /// 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); }
/// <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> /// 将所有消息帧原封不动的发送至另外一端 /// </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> /// 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> /// 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(); } }
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(); }
public static byte[] Receive(this IReceivingSocket socket) { bool hasMore; return(socket.Receive(false, out hasMore)); }
/// <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; }
public static string ReceiveString(this IReceivingSocket socket, bool dontWait, out bool hasMore) { byte[] data = socket.Receive(dontWait, out hasMore); return(Encoding.ASCII.GetString(data)); }
/// <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(); } }