Example #1
0
        /// <summary>
        ///     Processes the message queue and triggers the <see cref="MessageSent"/> event every time a message is sent to the server.
        /// </summary>
        /// <returns></returns>
        private async Task ProcessMessageQueueAsync()
        {
            Tuple <IrcClient, string> tuple = null;
            long delay = 0;

            while (_messageQueue.TryDequeue(out tuple))
            {
                IrcClient client  = tuple.Item1;
                string    message = tuple.Item2;
                delay = GetRemainingDelay();

                if (delay > 0)
                {
                    await Task.Delay((int)delay);
                }

                await client.WriteLineAsync(message);

                HandleMessageSent();

                var e = new IrcRawMessageEventArgs(message);
                client.OnRawMessageSent(e);
            }

            _isProcessingMessageQueue = false;
        }
Example #2
0
        /// <summary>
        ///     Sends the message to the server using the passed <see cref="IrcClient"/> instance.
        ///     If the message queue is being processed during the time this method is called, the message is queued up instead.
        ///     A new asynchronous task processing the message queue is started if the message queue is empty at the time this method is called and
        ///     <see cref="GetRemainingDelay"/> returns a value greater than zero.
        /// </summary>
        /// <param name="client">The client sending the message.</param>
        /// <param name="message">The message to be sent</param>
        /// <returns></returns>
        public async Task SendMessageAsync(IrcClient client, string message)
        {
            if (!_isProcessingMessageQueue)
            {
                if (GetRemainingDelay() == 0)
                {
                    await client.WriteLineAsync(message);

                    var e = new IrcRawMessageEventArgs(message);
                    client.OnRawMessageSent(e);

                    HandleMessageSent();
                }
                else
                {
                    _messageQueue.Enqueue(new Tuple <IrcClient, string>(client, message));
                    _isProcessingMessageQueue = true;
                    await Task.Factory.StartNew(ProcessMessageQueueAsync);
                }
            }
            else
            {
                _messageQueue.Enqueue(new Tuple <IrcClient, string>(client, message));
            }
        }