public void Stop()
        {
            m_running = false;
            foreach (object o in m_requests)
            {
                PollServiceHttpRequest req = (PollServiceHttpRequest)o;

                OSHttpResponse response = new OSHttpResponse(req.Context);

                byte[] buffer = req.PollServiceArgs.NoEvents(req.RequestID, req.PollServiceArgs.Id, response);


                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());
                }
            }

            m_requests.Clear();

            foreach (Thread t in m_workerThreads)
            {
                t.Abort();
            }
        }
        private void OnRequest(HttpListenerContext context)
        {
            try
            {
                PollServiceEventArgs psEvArgs;

                if (TryGetPollServiceHTTPHandler(context.Request.Url.AbsolutePath, out psEvArgs))
                {
                    if (context.Request.HttpMethod == "HEAD")
                    {
                        return;
                    }
                    PollServiceHttpRequest psreq = new PollServiceHttpRequest(psEvArgs, context);

                    if (psEvArgs.Request != null)
                    {
                        OSHttpRequest req = new OSHttpRequest(context);
                        psEvArgs.Request(psreq.RequestID, req);
                    }

                    m_PollServiceManager.Enqueue(psreq);
                }
                else
                {
                    HandleRequest(context);
                }
            }
            catch (Exception e)
            {
                MainConsole.Instance.ErrorFormat("[BASE HTTP SERVER]: OnRequest() failed: {0} ", e.ToString());
            }
        }
 public void Enqueue(PollServiceHttpRequest req)
 {
     lock (m_requests) {
         m_requests.Enqueue (req);
         lock (m_queueSync)
             Monitor.Pulse (m_queueSync);
     }
 }
 public void Enqueue(PollServiceHttpRequest req)
 {
     lock (m_requests) {
         m_requests.Enqueue(req);
         lock (m_queueSync)
             Monitor.Pulse(m_queueSync);
     }
 }
 internal void Enqueue(PollServiceHttpRequest pPollServiceHttpRequest)
 {
     m_request.Enqueue(pPollServiceHttpRequest);
 }
Beispiel #6
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 (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());
                }
            }
        }
Beispiel #7
0
 internal void Enqueue(PollServiceHttpRequest pPollServiceHttpRequest)
 {
     m_request.Enqueue(pPollServiceHttpRequest);
 }
 internal void ReQueueEvent(PollServiceHttpRequest req)
 {
     // Do accounting stuff here
     Enqueue(req);
 }
 internal void ReQueueEvent(PollServiceHttpRequest req)
 {
     // Do accounting stuff here
     Enqueue (req);
 }
        void OnRequest(HttpListenerContext context)
        {
            try {
                PollServiceEventArgs psEvArgs;

                if (TryGetPollServiceHTTPHandler (context.Request.Url.AbsolutePath, out psEvArgs)) {
                    if (context.Request.HttpMethod == "HEAD")
                        return;
                    PollServiceHttpRequest psreq = new PollServiceHttpRequest (psEvArgs, context);

                    if (psEvArgs.Request != null) {
                        OSHttpRequest req = new OSHttpRequest (context);
                        psEvArgs.Request (psreq.RequestID, req);
                    }

                    m_PollServiceManager.Enqueue (psreq);
                } else {
                    HandleRequest (context);
                }
            } catch (Exception e) {
                MainConsole.Instance.ErrorFormat ("[Base HTTP server]: OnRequest() failed: {0} ", e.ToString ());
            }
        }