public void ProcessRquestData(byte[] data, TaskCompletionSource <Message> responseRawDataAvailableTcs, TaskCompletionSource <object> responseSentTcs)
        {
            var           applicationMessage = _messageFactory.CreateApplicationMessage(data);
            IInboundReply reply = null;

            var messageAvailable = applicationMessage.Message != null;

            reply = _replyFactory.CreateIInboundReply(responseRawDataAvailableTcs, responseSentTcs);

            var circuitBreaker = _policyRegistry.Get <CircuitBreakerPolicy <Message> >(applicationMessage.Backend);

            var policy = Policy.HandleResult <Message>(r => r.IsFault).
                         Fallback(applicationMessage.Message, (x) =>
            {
                reply.Reply(applicationMessage.ErrorMessage, TimeSpan.FromSeconds(1));
            }).Wrap(circuitBreaker);

            policy.Execute(() =>
            {
                if (messageAvailable)
                {
                    _inboundQueue.Enqueue(new Tuple <bool, Message, IInboundReply>(messageAvailable, applicationMessage.Message, reply));
                }

                return(applicationMessage.Message);
            });
        }
        /// <summary>
        /// Tries to receive a message within a specified interval of time. 
        /// </summary>
        public bool TryReceive(TimeSpan timeout, out System.ServiceModel.Channels.Message message, out IInboundReply reply)
        {
            reply = null;
            message = null;

            if (queue.IsCompleted)
                return false;

            FileItem item = null;
            bool result = queue.TryTake(out item, (int)Math.Min(timeout.TotalMilliseconds, (long)int.MaxValue), cancelSource.Token);

            if (result)
            {
                Uri originalUri = connectionUri.Uri;

                message = ByteStreamMessage.CreateMessage(item.Stream);
                message.Headers.Action = new UriBuilder(originalUri.Scheme, originalUri.Host, originalUri.Port, item.Path).Uri.ToString();

                reply = new FtpAdapterInboundReply(item.Client, item.Path, item.Stream);
            }

            return result;
        }
        /// <summary>
        /// Tries to receive a message within a specified interval of time.
        /// </summary>
        /// <param name="timeout">A timeout period within which to receive a message</param>
        /// <param name="message">The Message instance that has been received</param>
        /// <param name="reply">The reply object instance for sending reply messages</param>
        /// <returns>A boolean indicating whether a message was received during the defined timeout</returns>
        public bool TryReceive(TimeSpan timeout, out System.ServiceModel.Channels.Message message, out IInboundReply reply)
        {
            reply = null;

            message = null;
            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);

            System.Diagnostics.Debug.WriteLine("Starting the loop over the internal queue");

            while (true)
            {
                lock (this.inboundQueueSyncLock)
                {
                    if (this.inboundQueue == null)
                    {
                        System.Diagnostics.Debug.WriteLine("Inbound queue is null");

                        // listener has been closed
                        return(false);
                    }

                    if (this.inboundQueue.Count != 0)
                    {
                        System.Diagnostics.Debug.WriteLine("Inbound queue contains messages");
                        var msgHelper = this.inboundQueue.Dequeue();

                        if (msgHelper != null)
                        {
                            // Assigning the message that was received
                            message = msgHelper.Message;

                            // Creating the proper reply instance
                            reply = new MockAdapterInboundReply(
                                this.pipeServer,
                                msgHelper.ConnectionId,
                                Encoding.GetEncoding(
                                    this.Connection.ConnectionFactory.Adapter.Encoding));

                            System.Diagnostics.Debug.WriteLine("Message dequeued from the inbound queue");

                            return(true);
                        }
                    }
                }

                if (timeoutHelper.IsExpired)
                {
                    System.Diagnostics.Debug.WriteLine("The reception timed out");

                    return(false);
                }

                // Wait for sometime, and check again
                System.Threading.Thread.Sleep(500);
            }
        }
        /// <summary>
        /// Tries to receive a message within a specified interval of time.
        /// </summary>
        public bool TryReceive(TimeSpan timeout, out System.ServiceModel.Channels.Message message, out IInboundReply reply)
        {
            var tuple = _handlerService.TryReceiveAsync(timeout).Result;

            message = tuple.Item2;
            reply   = tuple.Item3;

            return(tuple.Item1);
        }
        /// <summary>
        /// Tries to receive a message within a specified interval of time. 
        /// </summary>
        public bool TryReceive(TimeSpan timeout, out System.ServiceModel.Channels.Message message, out IInboundReply reply)
        {
            reply = null;
            message = null;

            if (queue.IsCompleted)
                return false;

            MessageItem item = null;
            bool result = queue.TryTake(out item, (int)Math.Min(timeout.TotalMilliseconds, (long)int.MaxValue), cancelSource.Token);

            if (result)
            {
                message = item.Message;
                reply = new AdoNetAdapterInboundReply(item.Connection, item.Transaction, item.EndOperationStatement);
            }

            return result;
        }
        /// <summary>
        /// Tries to receive a message within a specified interval of time. 
        /// </summary>
        public bool TryReceive(TimeSpan timeout, out System.ServiceModel.Channels.Message message, out IInboundReply reply)
        {
            reply = new ScheduleAdapterInboundReply();
            message = null;

            if (queue.IsCompleted)
                return false;

            object scheduleObject = null;
            bool result = queue.TryTake(out scheduleObject, (int)Math.Min(timeout.TotalMilliseconds, (long)int.MaxValue), cancelSource.Token);

            if (result)
            {
                if (scheduleObject == null)
                    return false;

                message = Message.CreateMessage(MessageVersion.Default, action, scheduleObject);
            }

            return result;
        }