Beispiel #1
0
        /// <summary>
        /// Monitors the queue of pending commands and causes the next pending request
        /// to be sent to Cornerstone.
        /// </summary>
        private void MonitorQueue()
        {
            while (true)
            {
                //Wait until we have a request in the queue
                _requestEnqueued.WaitOne();

                //Check to see if we are already processing another command. We do not
                //want to process the next command until a response has been returned for
                //command currently being processed.
                if (_pendingCommand == null)
                {
                    //Prepare to send this command. Grab it off of the queue.
                    SendDataEventArgs eventArgs;
                    if (_pendingCommands.TryDequeue(out eventArgs))
                    {
                        //Make sure the command has data to be sent.
                        var data = eventArgs.Data;
                        if (data != null)
                        {
                            //If the command does not already have an cookie, we will give
                            //it a GUID as its cookie.
                            if (String.IsNullOrEmpty(eventArgs.Cookie))
                            {
                                eventArgs.Cookie = Guid.NewGuid().ToString();
                            }

                            if (data.StartsWith("<"))
                            {
                                var document = XDocument.Parse(data);
                                //Add on the cookie ID and the current request culture to the data XML.
                                document.Root.SetAttributeValue("Cookie", eventArgs.Cookie);
                                document.Root.SetAttributeValue("Culture", RequestCulture);
                                data = document.ToString();
                            }

                            //Keep track of this command.
                            _pendingCommand = eventArgs;

                            //Fire the command off to Cornerstone.
                            SendData(EncodingToUse.GetBytes(data));

                            //Let the sender know that the data went out.
                            if (eventArgs.Sender != null)
                            {
                                eventArgs.Sender.TrafficOut(data);
                            }
                        }
                    }
                }

                if (_pendingCommands.Count > 0)
                {
                    _requestEnqueued.Set();
                }
            }
        }