public void Run()
        {
            while (m_running)
            {
                PollServiceHttpRequest req = m_request.Dequeue();
                try
                {
                    if (req.PollServiceArgs.Valid())
                    {
                        if (req.PollServiceArgs.HasEvents(req.RequestID, req.PollServiceArgs.Id))
                        {
                            StreamReader str;
                            try
                            {
                                str = new StreamReader(req.Request.Body);
                            }
                            catch (ArgumentException)
                            {
                                // Stream was not readable means a child agent
                                // was closed due to logout, leaving the
                                // Event Queue request orphaned.
                                continue;
                            }

                            Hashtable responsedata = req.PollServiceArgs.GetEvents(req.RequestID, req.PollServiceArgs.Id,
                                                                                   str.ReadToEnd());
                            var request = new OSHttpRequest(req.HttpContext, req.Request);
                            m_server.MessageHandler.SendGenericHTTPResponse(
                                responsedata,
                                request.MakeResponse(System.Net.HttpStatusCode.OK, "OK"),
                                request
                                );
                        }
                        else
                        {
                            if ((Environment.TickCount - req.RequestTime) > m_timeout)
                            {
                                var request = new OSHttpRequest(req.HttpContext, req.Request);
                                m_server.MessageHandler.SendGenericHTTPResponse(
                                    req.PollServiceArgs.NoEvents(req.RequestID, req.PollServiceArgs.Id),
                                    request.MakeResponse(System.Net.HttpStatusCode.OK, "OK"),
                                    request);
                            }
                            else
                            {
                                ReQueuePollServiceItem reQueueItem = ReQueue;
                                if (reQueueItem != null)
                                {
                                    reQueueItem(req);
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    MainConsole.Instance.ErrorFormat("Exception in poll service thread: " + e);
                }
            }
        }
Example #2
0
        public void Run()
        {
            while (m_running)
            {
                PollServiceHttpRequest req = m_request.Dequeue();

                Watchdog.UpdateThread();

                try
                {
                    if (req.PollServiceArgs.HasEvents(req.RequestID, req.PollServiceArgs.Id))
                    {
                        StreamReader str;
                        try
                        {
                            str = new StreamReader(req.Request.Body);
                        }
                        catch (System.ArgumentException)
                        {
                            // Stream was not readable means a child agent
                            // was closed due to logout, leaving the
                            // Event Queue request orphaned.
                            continue;
                        }

                        Hashtable responsedata = req.PollServiceArgs.GetEvents(req.RequestID, req.PollServiceArgs.Id, str.ReadToEnd());
                        DoHTTPGruntWork(m_server, req, responsedata);
                    }
                    else
                    {
                        if ((Environment.TickCount - req.RequestTime) > m_timeout)
                        {
                            DoHTTPGruntWork(
                                m_server,
                                req,
                                req.PollServiceArgs.NoEvents(req.RequestID, req.PollServiceArgs.Id));
                        }
                        else
                        {
                            ReQueuePollServiceItem reQueueItem = ReQueue;
                            if (reQueueItem != null)
                            {
                                reQueueItem(req);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    m_log.ErrorFormat("Exception in poll service thread: " + e.ToString());
                }
            }
        }
        public void Run()
        {
            while (m_running)
            {
                PollServiceHttpRequest req = m_request.Dequeue();

                try
                {
                    byte[] buffer = null;
                    if (req.PollServiceArgs.HasEvents(req.RequestID, req.PollServiceArgs.Id))
                    {
                        StreamReader str;
                        try
                        {
                            str = new StreamReader(req.Context.Request.InputStream);
                        }
                        catch (System.ArgumentException)
                        {
                            // Stream was not readable means a child agent
                            // was closed due to logout, leaving the
                            // Event Queue request orphaned.
                            continue;
                        }

                        OSHttpResponse response = new OSHttpResponse(req.Context);

                        buffer = req.PollServiceArgs.GetEvents(req.RequestID, req.PollServiceArgs.Id,
                                                               str.ReadToEnd(), response);
                    }
                    else
                    {
                        if ((Environment.TickCount - req.RequestTime) > m_timeout)
                        {
                            OSHttpResponse response = new OSHttpResponse(req.Context);

                            buffer = req.PollServiceArgs.NoEvents(req.RequestID, req.PollServiceArgs.Id, response);
                        }
                        else
                        {
                            ReQueuePollServiceItem reQueueItem = ReQueue;
                            if (reQueueItem != null)
                            {
                                reQueueItem(req);
                            }
                        }
                    }
                    if (buffer != null)
                    {
                        req.Context.Response.ContentEncoding = Encoding.UTF8;

                        try
                        {
                            if (req.Context.Request.ProtocolVersion.Minor == 0)
                            {
                                //HTTP 1.0... no chunking
                                req.Context.Response.ContentLength64 = buffer.Length;
                                using (Stream stream = req.Context.Response.OutputStream)
                                {
                                    HttpServerHandlerHelpers.WriteNonChunked(stream, buffer);
                                }
                            }
                            else
                            {
                                req.Context.Response.SendChunked = true;
                                using (Stream stream = req.Context.Response.OutputStream)
                                {
                                    HttpServerHandlerHelpers.WriteChunked(stream, buffer);
                                }
                            }
                            req.Context.Response.Close();
                        }
                        catch (Exception ex)
                        {
                            if (!(ex is HttpListenerException) ||
                                !HttpListenerManager.IGNORE_ERROR_CODES.Contains(((HttpListenerException)ex).ErrorCode))
                            {
                                MainConsole.Instance.WarnFormat("[POLL SERVICE WORKER THREAD]: Failed to write all data to the stream: {0}", ex.ToString());
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    MainConsole.Instance.ErrorFormat("Exception in poll service thread: {0}", e.ToString());
                }
            }
        }
Example #4
0
        public void Run()
        {
            while (m_running)
            {
                PollServiceHttpRequest req = m_request.Dequeue();

                try
                {
                    byte[] buffer = null;
                    if (req.PollServiceArgs.HasEvents(req.RequestID, req.PollServiceArgs.Id))
                    {
                        StreamReader str;
                        try
                        {
                            str = new StreamReader(req.Context.Request.InputStream);
                        }
                        catch (System.ArgumentException)
                        {
                            // Stream was not readable means a child agent
                            // was closed due to logout, leaving the
                            // Event Queue request orphaned.
                            continue;
                        }

                        OSHttpResponse response = new OSHttpResponse(req.Context);

                        buffer = req.PollServiceArgs.GetEvents(req.RequestID, req.PollServiceArgs.Id,
                                                               str.ReadToEnd(), response);
                    }
                    else
                    {
                        if ((Environment.TickCount - req.RequestTime) > m_timeout)
                        {
                            OSHttpResponse response = new OSHttpResponse(req.Context);

                            buffer = req.PollServiceArgs.NoEvents(req.RequestID, req.PollServiceArgs.Id, response);
                        }
                        else
                        {
                            ReQueuePollServiceItem reQueueItem = ReQueue;
                            if (reQueueItem != null)
                            {
                                reQueueItem(req);
                            }
                        }
                    }
                    if (buffer != null)
                    {
                        req.Context.Response.SendChunked     = false;
                        req.Context.Response.ContentEncoding = Encoding.UTF8;

                        try
                        {
                            req.Context.Response.ContentLength64 = buffer.LongLength;
                            req.Context.Response.Close(buffer, true);
                        }
                        catch (Exception ex)
                        {
                            MainConsole.Instance.WarnFormat("[POLL SERVICE WORKER THREAD]: Error: {0}", ex.ToString());
                        }
                    }
                }
                catch (Exception e)
                {
                    MainConsole.Instance.ErrorFormat("Exception in poll service thread: {0}", e.ToString());
                }
            }
        }