Пример #1
0
        private void Proncess_BeginGetContext(HttpListener httpListen)
        {
            if (Interlocked.Read(ref _isStarted) != 1)
            {
                return;
            }
            if (Interlocked.CompareExchange(ref _isRecvice, 1, 0) != 0)
            {
                return;
            }

            try
            {
                httpListen.BeginGetContext(iar =>
                {
                    HttpListenerContext context = null;
                    try
                    {
                        context = httpListen.EndGetContext(iar);
                    }
                    catch (System.Exception)
                    {
                        //this.Dispose();
                    }
                    if (Interlocked.CompareExchange(ref _isRecvice, 0, 1) == 1)
                    {
                        Proncess_BeginGetContext(httpListen);
                    }
                    if (context != null)
                    {
                        ThreadPool.UnsafeQueueUserWorkItem(r =>
                        {
                            try
                            {
                                var c = new WebServerRequestContext(context);
                                try
                                {
                                    Proncess_EndGetContext(c);
                                }
                                catch (System.Exception ex)
                                {
                                    c.WriteLine(GetErrorFormatString(ex));
                                    c.Close();
                                }
                            }
                            catch //(Exception ex)
                            {
                            }
                        }, null);
                    }
                }, null);
            }
            catch (System.Exception)
            {
                this.Dispose();
            }
        }
Пример #2
0
 private void Proncess_EndGetContext(WebServerRequestContext context)
 {
     context.Context.Response.ContentEncoding    = Encoding.UTF8;
     context.Context.Response.Headers["charset"] = "utf-8";
     context.ContentType = "application/json";
     NewRequest(this, context);
     context.Flush();
     //结束处理,发送响应
     context.Close();
 }