/// <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 frames on <paramref name="socket"/>, blocking until all frames are sent. /// </summary> /// <param name="socket">the IOutgoingSocket to transmit on</param> /// <param name="frames">frames to transmit</param> public static void SendMultipartBytes([NotNull] this IOutgoingSocket socket, IEnumerable <byte[]> frames) { var enumerator = frames.GetEnumerator(); try { // move to the first emlement, if false frames is empty if (!enumerator.MoveNext()) { throw new ArgumentException("frames is empty", "frames"); } var current = enumerator.Current; // we always one item back to make sure we send the last frame without the more flag while (enumerator.MoveNext()) { // this is a more frame socket.SendMoreFrame(current); current = enumerator.Current; } // sending the last frame socket.SendFrame(current); } finally { enumerator.Dispose(); } }
/// <summary> /// Send routing keys over <paramref name="socket"/>, append an empty message at the end of the keys. /// </summary> /// <param name="socket">the IOutgoingSocket to transmit on</param> /// <param name="routingKeys">the routing keys to send</param> public static IOutgoingSocket SendRoutingKeys(this IOutgoingSocket socket, IEnumerable <RoutingKey> routingKeys) { foreach (var routingKey in routingKeys) { socket.SendMoreFrame(routingKey); } socket.SendMoreFrameEmpty(); return(socket); }
/// <summary> /// Send a single routing key over <paramref name="socket"/>, append an empty message afterwards. /// </summary> /// <param name="socket">the IOutgoingSocket to transmit on</param> /// <param name="routingKeys">the routing keys to send</param> public static IOutgoingSocket SendRoutingKeys(this IOutgoingSocket socket, params RoutingKey[] routingKeys) { foreach (var routingKey in routingKeys) { socket.SendMoreFrame(routingKey); } socket.SendMoreFrameEmpty(); return(socket); }
/// <summary> /// Attempt to transmit a mulitple frames on <paramref name="socket"/>. /// If frames 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="frames">frames to transmit</param> public static bool TrySendMultipartBytes([NotNull] this IOutgoingSocket socket, TimeSpan timeout, IEnumerable <byte[]> frames) { var enumerator = frames.GetEnumerator(); try { // move to the first emlement, if false frames is empty if (!enumerator.MoveNext()) { throw new ArgumentException("frames is empty", "frames"); } var current = enumerator.Current; // only the first frame need to be sent with a timeout if (!enumerator.MoveNext()) { return(socket.TrySendFrame(timeout, current)); } else { bool sentSuccessfully = socket.TrySendFrame(timeout, current, true); if (!sentSuccessfully) { return(false); } } // fetching the second frame current = enumerator.Current; // we always one item back to make sure we send the last frame without the more flag while (enumerator.MoveNext()) { // this is a more frame socket.SendMoreFrame(current); current = enumerator.Current; } // sending the last frame socket.SendFrame(current); return(true); } finally { enumerator.Dispose(); } }
/// <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); }
/// <summary> /// Attempt to transmit routing key over <paramref name="socket"/>. /// If message cannot be sent within <paramref name="timeout"/>, return <c>false</c>. /// Routing is always sent as more frame. /// </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="routingKeys">the routing keys to send</param> /// <returns><c>true</c> if a message was available, otherwise <c>false</c>.</returns> public static bool TrySendRoutingKeys(this IOutgoingSocket socket, TimeSpan timeout, IEnumerable <RoutingKey> routingKeys) { var enumerator = routingKeys.GetEnumerator(); // Empty collection, just trying to send the empty message if (!enumerator.MoveNext()) { return(socket.TrySendFrameEmpty(timeout, true)); } if (!socket.TrySendFrame(enumerator.Current)) { return(false); } while (enumerator.MoveNext()) { socket.SendMoreFrame(enumerator.Current); } socket.SendMoreFrameEmpty(); return(true); }
/// <summary> /// Send the Example to the socket. /// </summary> public void Send(IOutgoingSocket output) { if (output is RouterSocket) { output.SendMoreFrame(RoutingId); } int frameSize = 2 + 1; // Signature and message ID switch (Id) { case MessageId.Log: frameSize += Log.GetFrameSize(); break; case MessageId.Structures: frameSize += Structures.GetFrameSize(); break; case MessageId.Binary: frameSize += Binary.GetFrameSize(); break; case MessageId.Types: frameSize += Types.GetFrameSize(); break; } // Now serialize message into the buffer Msg msg = new Msg(); msg.InitPool(frameSize); try { m_offset = 0; m_buffer = msg.Data; // put signature PutNumber2(0xAAA0 | 0); // put message id PutNumber1((byte)Id); switch (Id) { case MessageId.Log: Log.Write(this); break; case MessageId.Structures: Structures.Write(this); break; case MessageId.Binary: Binary.Write(this); break; case MessageId.Types: Types.Write(this); break; } // Send the data frame output.Send(ref msg, false); } finally { m_buffer = null; msg.Close(); } }
/// <summary> /// Send routing key over <paramref name="socket"/>. /// </summary> /// <param name="socket">the IOutgoingSocket to transmit on</param> /// <param name="routingKey">the routing key to send</param> public static void SendMoreFrame([NotNull] this IOutgoingSocket socket, RoutingKey routingKey) { socket.SendMoreFrame(routingKey.Bytes); }
/// <summary> /// Send the Codec to the socket. /// </summary> public void Send(IOutgoingSocket output) { if (output is RouterSocket) { output.SendMoreFrame(RoutingId); } int frameSize = 2 + 1; // Signature and message ID switch (Id) { case MessageId.Message: frameSize += Message.GetFrameSize(); break; case MessageId.ServiceRegister: frameSize += ServiceRegister.GetFrameSize(); break; case MessageId.Error: frameSize += Error.GetFrameSize(); break; } // Now serialize message into the buffer Msg msg = new Msg(); msg.InitPool(frameSize); try { m_offset = 0; m_buffer = msg.Data; // put signature PutNumber2(0xAAA0 | 0); // put message id PutNumber1((byte)Id); switch (Id) { case MessageId.Message: Message.Write(this); break; case MessageId.ServiceRegister: ServiceRegister.Write(this); break; case MessageId.Error: Error.Write(this); break; } // Send the data frame output.Send(ref msg, false); } finally { m_buffer = null; msg.Close(); } }
/// <summary> /// Send the ZreMsg to the socket. /// </summary> public void Send(IOutgoingSocket output) { if (output is RouterSocket) output.SendMoreFrame(RoutingId); int frameSize = 2 + 1; // Signature and message ID switch (Id) { case MessageId.Hello: frameSize += Hello.GetFrameSize(); break; case MessageId.Whisper: frameSize += Whisper.GetFrameSize(); break; case MessageId.Shout: frameSize += Shout.GetFrameSize(); break; case MessageId.Join: frameSize += Join.GetFrameSize(); break; case MessageId.Leave: frameSize += Leave.GetFrameSize(); break; case MessageId.Ping: frameSize += Ping.GetFrameSize(); break; case MessageId.PingOk: frameSize += PingOk.GetFrameSize(); break; } // Now serialize message into the buffer Msg msg = new Msg(); msg.InitPool(frameSize); try { m_offset = 0; m_buffer = msg.Data; // put signature PutNumber2(0xAAA0 | 1); // put message id PutNumber1((byte)Id); switch (Id) { case MessageId.Hello: Hello.Write(this); break; case MessageId.Whisper: Whisper.Write(this); break; case MessageId.Shout: Shout.Write(this); break; case MessageId.Join: Join.Write(this); break; case MessageId.Leave: Leave.Write(this); break; case MessageId.Ping: Ping.Write(this); break; case MessageId.PingOk: PingOk.Write(this); break; } // Send the data frame output.Send(ref msg, false); } finally { m_buffer = null; msg.Close(); } }
/// <summary> /// Send the Codec to the socket. /// </summary> public void Send(IOutgoingSocket output) { if (output is RouterSocket) output.SendMoreFrame(RoutingId); int frameSize = 2 + 1; // Signature and message ID switch (Id) { case MessageId.Message: frameSize += Message.GetFrameSize(); break; case MessageId.ServiceRegister: frameSize += ServiceRegister.GetFrameSize(); break; case MessageId.Error: frameSize += Error.GetFrameSize(); break; } // Now serialize message into the buffer Msg msg = new Msg(); msg.InitPool(frameSize); try { m_offset = 0; m_buffer = msg.Data; // put signature PutNumber2(0xAAA0 | 0); // put message id PutNumber1((byte)Id); switch (Id) { case MessageId.Message: Message.Write(this); break; case MessageId.ServiceRegister: ServiceRegister.Write(this); break; case MessageId.Error: Error.Write(this); break; } // Send the data frame output.Send(ref msg, false); } finally { m_buffer = null; msg.Close(); } }
/// <summary> /// Send the ZreMsg to the socket. /// Warning re WHISPER and SHOUT: The 0MQ spec http://rfc.zeromq.org/spec:36 /// says "message content defined as one 0MQ frame. ZRE does not support multi-frame message contents." /// But this C# implementation allows multi-frame contents. /// </summary> public void Send(IOutgoingSocket output) { if (output is RouterSocket) output.SendMoreFrame(RoutingId); int frameSize = 2 + 1; // Signature and message ID switch (Id) { case MessageId.Hello: frameSize += Hello.GetFrameSize(); break; case MessageId.Whisper: frameSize += Whisper.GetFrameSize(); break; case MessageId.Shout: frameSize += Shout.GetFrameSize(); break; case MessageId.Join: frameSize += Join.GetFrameSize(); break; case MessageId.Leave: frameSize += Leave.GetFrameSize(); break; case MessageId.Ping: frameSize += Ping.GetFrameSize(); break; case MessageId.PingOk: frameSize += PingOk.GetFrameSize(); break; } // Now serialize message into the buffer Msg msg = new Msg(); msg.InitPool(frameSize); try { _offset = 0; _buffer = msg.Data; // put signature PutNumber2(0xAAA0 | 1); // put message id PutNumber1((byte) Id); switch (Id) { case MessageId.Hello: Hello.Write(this); break; case MessageId.Whisper: Whisper.Write(this); break; case MessageId.Shout: Shout.Write(this); break; case MessageId.Join: Join.Write(this); break; case MessageId.Leave: Leave.Write(this); break; case MessageId.Ping: Ping.Write(this); break; case MessageId.PingOk: PingOk.Write(this); break; } // Send the data frame var more = Id == MessageId.Whisper || Id == MessageId.Shout; output.TrySend(ref msg, TimeSpan.Zero, more); // Send message content for types with content switch (Id) { case MessageId.Whisper: if (Whisper.Content == null) { Whisper.Content = new NetMQMessage(); Whisper.Content.PushEmptyFrame(); } output.TrySendMultipartMessage(Whisper.Content); break; case MessageId.Shout: if (Shout.Content == null) { Shout.Content = new NetMQMessage(); Shout.Content.PushEmptyFrame(); } output.TrySendMultipartMessage(Shout.Content); break; } } finally { _buffer = null; msg.Close(); } }
/// <summary> /// Send the ZreMsgOriginal to the socket. /// </summary> public void Send(IOutgoingSocket output) { if (output is RouterSocket) { output.SendMoreFrame(RoutingId); } int frameSize = 2 + 1; // Signature and message ID switch (Id) { case MessageId.Hello: frameSize += Hello.GetFrameSize(); break; case MessageId.Whisper: frameSize += Whisper.GetFrameSize(); break; case MessageId.Shout: frameSize += Shout.GetFrameSize(); break; case MessageId.Join: frameSize += Join.GetFrameSize(); break; case MessageId.Leave: frameSize += Leave.GetFrameSize(); break; case MessageId.Ping: frameSize += Ping.GetFrameSize(); break; case MessageId.PingOk: frameSize += PingOk.GetFrameSize(); break; } // Now serialize message into the buffer Msg msg = new Msg(); msg.InitPool(frameSize); try { m_offset = 0; m_buffer = msg.Data; // put signature PutNumber2(0xAAA0 | 1); // put message id PutNumber1((byte)Id); switch (Id) { case MessageId.Hello: Hello.Write(this); break; case MessageId.Whisper: Whisper.Write(this); break; case MessageId.Shout: Shout.Write(this); break; case MessageId.Join: Join.Write(this); break; case MessageId.Leave: Leave.Write(this); break; case MessageId.Ping: Ping.Write(this); break; case MessageId.PingOk: PingOk.Write(this); break; } // Send the data frame output.Send(ref msg, false); } finally { m_buffer = null; msg.Close(); } }
/// <summary> /// Send the ZreMsg to the socket. /// Warning re WHISPER and SHOUT: The 0MQ spec http://rfc.zeromq.org/spec:36 /// says "message content defined as one 0MQ frame. ZRE does not support multi-frame message contents." /// </summary> public void Send(IOutgoingSocket output) { if (output is RouterSocket) { output.SendMoreFrame(RoutingId); } int frameSize = 2 + 1; // Signature and message ID switch (Id) { case MessageId.Hello: frameSize += Hello.GetFrameSize(); break; case MessageId.Whisper: frameSize += Whisper.GetFrameSize(); break; case MessageId.Shout: frameSize += Shout.GetFrameSize(); break; case MessageId.Join: frameSize += Join.GetFrameSize(); break; case MessageId.Leave: frameSize += Leave.GetFrameSize(); break; case MessageId.Ping: frameSize += Ping.GetFrameSize(); break; case MessageId.PingOk: frameSize += PingOk.GetFrameSize(); break; } // Now serialize message into the buffer Msg msg = new Msg(); msg.InitPool(frameSize); try { m_offset = 0; m_buffer = msg.Data; // put signature PutNumber2(0xAAA0 | 1); // put message id PutNumber1((byte)Id); switch (Id) { case MessageId.Hello: Hello.Write(this); break; case MessageId.Whisper: Whisper.Write(this); break; case MessageId.Shout: Shout.Write(this); break; case MessageId.Join: Join.Write(this); break; case MessageId.Leave: Leave.Write(this); break; case MessageId.Ping: Ping.Write(this); break; case MessageId.PingOk: PingOk.Write(this); break; } // Send the data frame var more = Id == MessageId.Whisper || Id == MessageId.Shout; output.TrySend(ref msg, TimeSpan.Zero, more); // Send message content for types with content switch (Id) { case MessageId.Whisper: if (Whisper.Content == null) { Whisper.Content = new NetMQMessage(); Whisper.Content.PushEmptyFrame(); } output.TrySendMultipartMessage(Whisper.Content); break; case MessageId.Shout: if (Shout.Content == null) { Shout.Content = new NetMQMessage(); Shout.Content.PushEmptyFrame(); } output.TrySendMultipartMessage(Shout.Content); break; } } finally { m_buffer = null; msg.Close(); } }
public static void SendMessage(this IOutgoingSocket socket, SerializedMessage message) { socket .SendMoreFrame(message.RouteName) .SendFrame(message.Data ?? new byte[0]); }