private static void SendMessage(IOutgoingSocket socket, string eventName, params object[] arguments) { var message = eventName + "|" + string.Join("|", arguments); socket.SendFrame(message); Console.WriteLine("Sent: {0}", message); }
/// <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> /// 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); }
public static void SendMessage(this IOutgoingSocket socket, Message message, Kernel kernel = null) { var header = JsonConvert.SerializeObject(message.Header); var parentHeader = JsonConvert.SerializeObject(message.ParentHeader); var metadata = JsonConvert.SerializeObject(message.MetaData); var content = JsonConvert.SerializeObject(message.Content); if (string.IsNullOrEmpty(message.Signature) && kernel != null) { message.Signature = kernel.SignMessage(header, parentHeader, metadata, content); } // Console.WriteLine($"< [{message.Header.MessageType}] {metadata} {content}"); foreach (var identity in message.Identities) { socket.SendFrame(identity, true); } socket.SendFrame(Delimiter, true); socket.SendFrame(message.Signature, true); socket.SendFrame(header, true); socket.SendFrame(parentHeader, true); socket.SendFrame(metadata, true); socket.SendFrame(content); }
/// <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> /// Sends object as JSON-serialized data through given socket. /// </summary> public static void SendAsJson(this IOutgoingSocket socket, object obj) { var json = JsonConvert.SerializeObject(obj); socket.SendFrame(json); }
private static void Send(string message, IOutgoingSocket socket, bool sendMore = true) { socket.SendFrame(message, sendMore); }
public static IOutgoingSocket SendMoreFrame([NotNull] this IOutgoingSocket socket, [NotNull] byte[] data, int length) { socket.SendFrame(data, length, true); return(socket); }
public static IOutgoingSocket SendMoreFrameEmpty([NotNull] this IOutgoingSocket socket) { socket.SendFrame(EmptyArray <byte> .Instance, true); return(socket); }
/// <summary> /// Transmit an empty frame over this socket, block until frame is sent. /// </summary> /// <param name="socket">the IOutgoingSocket to transmit on</param> /// <param name="more"> /// set this flag to true to signal that you will be immediately sending another frame (optional: /// default is false) /// </param> public static void SendFrameEmpty([NotNull] this IOutgoingSocket socket, bool more = false) { socket.SendFrame(EmptyArray <byte> .Instance, more); }
/// <summary> /// Transmit a byte-array of data over this socket, block until frame is sent. /// </summary> /// <param name="socket">the IOutgoingSocket to transmit on</param> /// <param name="data">the byte-array of data 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> public static void SendFrame([NotNull] this IOutgoingSocket socket, [NotNull] byte[] data, bool more = false) { socket.SendFrame(data, data.Length, more); }
public static IOutgoingSocket SendMoreFrame([NotNull] this IOutgoingSocket socket, [NotNull] string message) { socket.SendFrame(message, true); return(socket); }