/// <summary> /// Transmit a string over this socket asynchronously. /// </summary> /// <param name="socket">the socket to transmit on</param> /// <param name="group">The group to send the message to.</param> /// <param name="message">the string to send</param> public static ValueTask SendAsync(this IGroupOutSocket socket, string group, string message) { if (socket.TrySend(group, message)) { return(new ValueTask()); } return(new ValueTask(Task.Factory.StartNew(() => Send(socket, group, message), TaskCreationOptions.LongRunning))); }
/// <summary> /// Transmit a byte-array of data over this socket asynchronously. /// </summary> /// <param name="socket">the socket to transmit on</param> /// <param name="group">The group to send the message to.</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> public static ValueTask SendAsync(this IGroupOutSocket socket, string group, byte[] data, int length) { if (socket.TrySend(group, data, length)) { return(new ValueTask()); } return(new ValueTask(Task.Factory.StartNew(() => Send(socket, group, data, length), TaskCreationOptions.LongRunning))); }
/// <summary> /// Attempt to transmit a byte-array of data on <paramref name="socket"/>. /// If message cannot be sent within <paramref name="timeout"/>, return <c>false</c>. /// </summary> /// <param name="socket">the socket to transmit on</param> /// <param name="timeout">The maximum period of time to try to send a message.</param> /// <param name="group">The group to send the message to.</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> /// <returns><c>true</c> if a message was available, otherwise <c>false</c>.</returns> public static bool TrySend(this IGroupOutSocket socket, TimeSpan timeout, string group, byte[] data, int length) { var msg = new Msg(); msg.InitPool(length); msg.Group = group; data.CopyTo(msg); if (!socket.TrySend(ref msg, timeout)) { 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 socket to transmit on</param> /// <param name="timeout">The maximum period of time to try to send a message.</param> /// <param name="group">The group to send the message to.</param> /// <param name="message">the string to send</param> /// <returns><c>true</c> if a message was available, otherwise <c>false</c>.</returns> public static bool TrySend(this IGroupOutSocket socket, TimeSpan timeout, string group, string message) { 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)); msg.Group = group; // Encode the string into the buffer SendReceiveConstants.DefaultEncoding.GetBytes(message, msg); if (!socket.TrySend(ref msg, timeout)) { msg.Close(); return(false); } msg.Close(); return(true); }