Esempio n. 1
0
        /// <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(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();
            }
        }
Esempio n. 2
0
        public static bool TrySendFrameBytes(this IOutgoingSocket socket, string Message)
        {
            var bytes  = Encoding.UTF8.GetBytes(Message);
            var result = socket.TrySendFrame(bytes);

            return(result);
        }
Esempio n. 3
0
        public bool Send(Message message)
        {
            var hmac = _signatureValidator.CreateSignature(message);

            foreach (var ident in message.Identifiers)
            {
                _socket.TrySendFrame(ident, true);
            }

            Send(Constants.DELIMITER, _socket);
            Send(hmac, _socket);
            Send(message.Header.ToJson(), _socket);
            Send(message.ParentHeader.ToJson(), _socket);
            Send(message.MetaData.ToJson(), _socket);
            Send(message.Content.ToJson(), _socket, false);

            return(true);
        }
Esempio n. 4
0
        public void Send(Message message)
        {
            var hmac = _signatureValidator.CreateSignature(message);

            if (message.Identifiers is not null)
            {
                foreach (var ident in message.Identifiers)
                {
                    _socket.TrySendFrame(ident.ToArray(), true);
                }
            }

            Send(Constants.DELIMITER, _socket);
            Send(hmac, _socket);
            Send(message.Header.ToJson(), _socket);
            Send((message.ParentHeader ?? new object()).ToJson(), _socket);
            Send((message.MetaData ?? new object()).ToJson(), _socket);
            Send(message.Content.ToJson(), _socket, false);
        }
Esempio n. 5
0
        public static async Task SendFrameAsync(
            this IOutgoingSocket socket,
            byte[] data,
            int?timeout = null,
            int delay   = 100)
        {
            int elapsed = 0;

            while (!socket.TrySendFrame(data))
            {
                await Task.Delay(delay);

                elapsed += 100;
                if (elapsed > timeout)
                {
                    throw new TimeoutException(
                              $"The operation exceeded the specified time[{timeout}].");
                }
            }
        }
        public static async Task SendFrameAsync(
            this IOutgoingSocket socket,
            byte[] data,
            TimeSpan?timeout = null,
            TimeSpan?delay   = null,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            TimeSpan delayNotNull = delay ?? TimeSpan.FromMilliseconds(100);
            TimeSpan elapsed      = TimeSpan.Zero;

            while (!socket.TrySendFrame(data))
            {
                await Task.Delay(delayNotNull, cancellationToken);

                elapsed += delayNotNull;
                if (elapsed > timeout)
                {
                    throw new TimeoutException(
                              $"The operation exceeded the specified time[{timeout}].");
                }
            }
        }
        /// <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>
 /// 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="routingKey">the routing key to send</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, RoutingKey routingKey)
 {
     return(socket.TrySendFrame(timeout, routingKey.Bytes, true));
 }
Esempio n. 9
0
 public static bool TrySendFrame(this IOutgoingSocket socket, RoutingKey routingKey)
 {
     return(socket.TrySendFrame(routingKey.Bytes, true));
 }
Esempio n. 10
0
        /// <summary>
        /// Tries to send object as JSON-serialized data through given socket.
        /// Returns false if operation times out.
        /// </summary>
        public static bool TrySendAsJson(this IOutgoingSocket socket, object obj, int timeoutMs)
        {
            var json = JsonConvert.SerializeObject(obj);

            return(socket.TrySendFrame(TimeSpan.FromMilliseconds(timeoutMs), json));
        }
Esempio n. 11
0
 /// <summary>
 ///     Attempt to transmit an empty frame on <paramref name="socket" />.
 ///     If message cannot be sent immediately, return <c>false</c>.
 /// </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>
 /// <returns><c>true</c> if a message was available, otherwise <c>false</c>.</returns>
 public static bool TrySendFrameEmpty([NotNull] this IOutgoingSocket socket, bool more = false)
 {
     return(socket.TrySendFrame(EmptyArray <byte> .Instance, more));
 }
Esempio n. 12
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 IOutgoingSocket to transmit on</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, [NotNull] string message, bool more = false)
 {
     return(socket.TrySendFrame(TimeSpan.Zero, message, more));
 }
Esempio n. 13
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 IOutgoingSocket to transmit on</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, [NotNull] byte[] data, int length,
                                 bool more = false)
 {
     return(socket.TrySendFrame(TimeSpan.Zero, data, length, more));
 }
Esempio n. 14
0
 /// <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="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, bool more = false)
 {
     return(socket.TrySendFrame(timeout, data, data.Length, more));
 }