Esempio n. 1
0
        /// <summary>
        /// Sends a message to all of the connections.
        /// </summary>
        /// <param name="connections">The connections to send to.</param>
        /// <param name="msg">The message to send.</param>
        /// <exception cref="ArgumentNullException">
        /// <para><paramref name="connections"/> is <c>null</c>.</para><para>--or--</para>
        /// <para><paramref name="msg"/> is <c>null</c>.</para>
        /// </exception>
        public static void Send(this IEnumerable<IConnection> connections, Message msg)
        {
            if (connections == null)
                throw new ArgumentNullException ("connections");
            if (msg == null)
                throw new ArgumentNullException ("msg");

            foreach (IConnection connection in connections)
                connection.SendAsync (msg);
        }
Esempio n. 2
0
        public void Receive(Message message)
        {
            if (message == null)
                throw new ArgumentNullException ("message");
            if (message.Header == null)
                throw new ArgumentNullException ("message", "Message.Header is null");
            if (!message.Header.IsResponse)
                throw new ArgumentException ("message is not a response message", "message");

            TaskCompletionSource<Message> tcs;
            if (!this.messageResponses.TryRemove (message.Header.ResponseMessageId, out tcs))
                return;

            tcs.TrySetResult (message);

            int timeout;
            this.timeouts.TryRemove (message.Header.MessageId, out timeout);
        }
Esempio n. 3
0
        private Task<Message> SendForCore(Message message, Func<Message, Task<bool>> sender = null, Task<bool> sendTask = null)
        {
            int id = message.Header.MessageId;

            var tcs = new TaskCompletionSource<Message> (message);

            if (!this.messageResponses.TryAdd (id, tcs))
                throw new InvalidOperationException ("Message already waiting response");

            if (sender != null)
                sendTask = sender (message);

            sendTask.ContinueWith (t => {
                if (!t.IsFaulted && t.Result)
                    return;

                TaskCompletionSource<Message> cancelTcs;
                if (this.messageResponses.TryRemove (id, out cancelTcs))
                    cancelTcs.TrySetCanceled();
            }, TaskScheduler.Default);

            return tcs.Task;
        }
Esempio n. 4
0
        public Task<Message> SendFor(Message message, Task<bool> sendTask, CancellationToken cancelToken)
        {
            if (message == null)
                throw new ArgumentNullException ("message");
            if (message.Header == null)
                throw new ArgumentNullException ("message", "Message.Header is null");
            if (sendTask == null)
                throw new ArgumentNullException ("sendTask");

            int id = message.Header.MessageId;
            cancelToken.Register (() => {
                TaskCompletionSource<Message> cancelTcs;
                if (this.messageResponses.TryRemove (id, out cancelTcs))
                    cancelTcs.TrySetCanceled();
            });

            return SendForCore (message, sendTask: sendTask);
        }
Esempio n. 5
0
        public Task<Message> SendFor(Message message, Task<bool> sendTask, int timeout)
        {
            if (message == null)
                throw new ArgumentNullException ("message");
            if (message.Header == null)
                throw new ArgumentNullException ("message", "Message.Header is null");

            if (timeout > 0) {
                if (!this.timeouts.TryAdd (message.Header.MessageId, timeout))
                    throw new InvalidOperationException ("Message already waiting response");
            }

            return SendForCore (message, sendTask: sendTask);
        }
Esempio n. 6
0
        public Task<Message> SendFor(Message message, Task<bool> sendTask)
        {
            if (message == null)
                throw new ArgumentNullException ("message");
            if (message.Header == null)
                throw new ArgumentNullException ("message", "Message.Header is null");

            return SendForCore (message, sendTask: sendTask);
        }
Esempio n. 7
0
        /// <summary>
        /// Creates a new instance of <see cref="MessageEventArgs"/>.
        /// </summary>
        /// <param name="connection">The connection of the event.</param>
        /// <param name="message">The message received.</param>
        /// <exception cref="ArgumentNullException"><paramref name="connection"/> or <paramref name="message"/> is <c>null</c>.</exception>
        public MessageEventArgs(IConnection connection, Message message)
            : base(connection)
        {
            if (message == null)
                throw new ArgumentNullException ("message");

            Message = message;
        }
Esempio n. 8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ConnectionlessMessageEventArgs"/> class.
        /// </summary>
        /// <param name="message">The message that was received connectionlessly.</param>
        /// <param name="from">The <see cref="Target"/> the message came from.</param>
        /// <param name="messenger">The <see cref="IConnectionlessMessenger"/> the message was received from.</param>
        /// <exception cref="ArgumentNullException"><paramref name="message"/> or <paramref name="from"/> is <c>null</c>.</exception>
        public ConnectionlessMessageEventArgs(Message message, Target from, IConnectionlessMessenger messenger)
        {
            if (message == null)
                throw new ArgumentNullException ("message");
            if (from == null)
                throw new ArgumentNullException ("from");
            if (messenger == null)
                throw new ArgumentNullException ("messenger");

            Message = message;
            From = from;
            Messenger = messenger;
        }
Esempio n. 9
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ConnectionlessMessageEventArgs"/> class.
        /// </summary>
        /// <param name="message">The message received connectionlessly.</param>
        /// <param name="from">Where the message came from.</param>
        /// <exception cref="ArgumentNullException"><paramref name="message"/> or <paramref name="from"/> is <c>null</c>.</exception>
        public ConnectionlessMessageEventArgs(Message message, EndPoint from)
        {
            if (message == null)
                throw new ArgumentNullException ("message");
            if (from == null)
                throw new ArgumentNullException ("from");

            Message = message;
            From = from;
        }