public static bool TrySendFrame(this IOutgoingSocket socket, TimeSpan timeout, byte[] data, int length, bool more = false) { var msg = new Msg(); msg.InitPool(length); data.CopyTo(msg); if (!socket.TrySend(ref msg, timeout, more)) { msg.Close(); return(false); } msg.Close(); return(true); }
/// <summary> /// Attempt to transmit a single frame 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="data">the byte-array of data to send</param> /// <param name="length">the number of bytes to send from <paramref name="data"/>.</param> /// <param name="more">set this flag to true to signal that you will be immediately sending another frame (optional: default is false)</param> /// <returns><c>true</c> if a message was available, otherwise <c>false</c>.</returns> public static bool TrySendFrame([NotNull] this IOutgoingSocket socket, TimeSpan timeout, [NotNull] byte[] data, int length, bool more = false) { var msg = new Msg(); msg.InitPool(length); Buffer.BlockCopy(data, 0, msg.Data, 0, length); if (!socket.TrySend(ref msg, timeout, more)) { msg.Close(); return(false); } msg.Close(); return(true); }
/// <summary> /// Attempt to transmit a status-signal over this socket. /// If signal cannot be sent immediately, return <c>false</c>. /// </summary> /// <param name="socket">the IOutgoingSocket to transmit on</param> /// <param name="status">a byte that contains the status signal to send</param> private static bool TrySignal([NotNull] this IOutgoingSocket socket, byte status) { long signalValue = 0x7766554433221100L + status; Msg msg = new Msg(); msg.InitPool(8); NetworkOrderBitsConverter.PutInt64(signalValue, msg.Data); if (!socket.TrySend(ref msg, TimeSpan.Zero, false)) { msg.Close(); return(false); } msg.Close(); return(true); }
/// <summary> /// Attempt to transmit a single string frame 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">the string to send</param> /// <param name="more">set this flag to true to signal that you will be immediately sending another frame (optional: default is false)</param> /// <returns><c>true</c> if a message was available, otherwise <c>false</c>.</returns> public static bool TrySendFrame([NotNull] this IOutgoingSocket socket, TimeSpan timeout, [NotNull] string message, bool more = false) { var msg = new Msg(); // Count the number of bytes required to encode the string. // Note that non-ASCII strings may not have an equal number of characters // and bytes. The encoding must be queried for this answer. // With this number, request a buffer from the pool. msg.InitPool(SendReceiveConstants.DefaultEncoding.GetByteCount(message)); // Encode the string into the buffer SendReceiveConstants.DefaultEncoding.GetBytes(message, 0, message.Length, msg.Data, 0); if (!socket.TrySend(ref msg, timeout, more)) { msg.Close(); return(false); } msg.Close(); return(true); }
/// <summary> /// Block until the message is can be sent. /// </summary> /// <remarks> /// The call blocks until the message can be sent and cannot be interrupted. /// Wether the message can be sent depends on the socket type. /// </remarks> /// <param name="socket">The socket to send the message on.</param> /// <param name="msg">An object with message's data to send.</param> /// <param name="more">Indicate if another frame is expected after this frame</param> public static void Send(this IOutgoingSocket socket, ref Msg msg, bool more) { var result = socket.TrySend(ref msg, SendReceiveConstants.InfiniteTimeout, more); Debug.Assert(result); }
/// <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 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(); } }