Beispiel #1
0
        /// <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)));
        }
Beispiel #2
0
        /// <summary>
        /// Transmit a byte-array of data over this socket, block until frame is sent.
        /// </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 void Send(this IGroupOutSocket socket, string group, byte[] data, int length)
        {
            var msg = new Msg();

            msg.InitPool(length);
            msg.Group = group;
            data.Slice(0, length).CopyTo(msg);
            socket.Send(ref msg);
            msg.Close();
        }
Beispiel #3
0
        /// <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)));
        }
Beispiel #4
0
        /// <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);
        }
Beispiel #5
0
        /// <summary>
        /// Transmit a string over this socket, block until message is sent.
        /// </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 void Send(this IGroupOutSocket socket, 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);

            socket.Send(ref msg);
            msg.Close();
        }
Beispiel #6
0
 /// <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>
 /// <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)
 {
     return(TrySend(socket, timeout, group, data, data.Length));
 }
Beispiel #7
0
 /// <summary>
 /// Transmit a byte-array of data over this socket, block until message is sent.
 /// </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>
 public static void Send(this IGroupOutSocket socket, string group, byte[] data)
 {
     Send(socket, group, data, data.Length);
 }
Beispiel #8
0
 /// <summary>
 /// Attempt to transmit a single string frame on <paramref name="socket"/>.
 /// If message cannot be sent immediately, return <c>false</c>.
 /// </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>
 /// <returns><c>true</c> if a message was available, otherwise <c>false</c>.</returns>
 public static bool TrySend(this IGroupOutSocket socket, string group, string message)
 {
     return(TrySend(socket, TimeSpan.Zero, group, message));
 }
Beispiel #9
0
 /// <summary>
 /// Attempt to transmit a single frame on <paramref name="socket"/>.
 /// If message cannot be sent immediately, return <c>false</c>.
 /// </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>
 /// <returns><c>true</c> if a message was available, otherwise <c>false</c>.</returns>
 public static bool TrySend(this IGroupOutSocket socket, string group, byte[] data, int length)
 {
     return(TrySend(socket, TimeSpan.Zero, group, data, length));
 }