Пример #1
0
        private void ProcessHttpRequest(IAsyncResult iaServer)
        {
            HttpListener        server  = iaServer.AsyncState as HttpListener;
            HttpListenerContext context = null;

            try
            {
                context = server.EndGetContext(iaServer);
                Logger.Info("接收请求" + context.Request.Url.ToString());
                //判断上一个操作未完成,即返回服务器正忙,并开启一个新的异步监听
                if (_isRuning)
                {
                    Logger.Info("正在处理请求,已忽略请求" + context.Request.Url.ToString());
                    RetutnResponse(context, _httpImplanter.CreateReturnResult(context, new ReturnCode((int)CommandResult.ServerIsBusy, EnumHelper.GetEnumDescription(CommandResult.ServerIsBusy))));
                    server.BeginGetContext(new AsyncCallback(ProcessHttpRequest), server);
                    return;
                }

                _isRuning = true;
                server.BeginGetContext(new AsyncCallback(ProcessHttpRequest), server);
            }
            catch
            {
                Logger.Warning("服务器已关闭!");
                return;
            }

            string scriptName = new UrlHelper(context.Request.Url).ScriptName;

            byte[] resultBytes = null;
            if (scriptName.ToLower().EndsWith(".html") || scriptName == "favicon.ico")
            {
                string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Web", scriptName);
                if (File.Exists(filePath))
                {
                    resultBytes = File.ReadAllBytes(filePath);
                }
                else
                {
                    resultBytes = _httpImplanter.CreateReturnResult(context, new ReturnCode((int)CommandResult.FileNotExists, EnumHelper.GetEnumDescription(CommandResult.FileNotExists)));
                }
            }
            else
            {
                ReturnCode result = _httpImplanter.ProcessRequest(context);
                resultBytes = _httpImplanter.CreateReturnResult(context, result);
            }
            RetutnResponse(context, resultBytes);
            _isRuning = false;
        }
Пример #2
0
        private void ProcessHttpRequest(IAsyncResult iaServer)
        {
            HttpListener        server  = iaServer.AsyncState as HttpListener;
            HttpListenerContext context = null;

            try
            {
                context = server.EndGetContext(iaServer);
                Logger.Info("接收请求" + context.Request.Url.ToString());

                //判断上一个操作未完成,即返回服务器正忙,并开启一个新的异步监听
                if (_isRuning)
                {
                    Logger.Info("正在处理请求,已忽略请求" + context.Request.Url.ToString());
                    RetutnResponse(context, _httpImplanter.CreateReturnResult(context, new SFReturnCode((int)CommandResult.ServerIsBusy, EnumHelper.GetEnumDescription(CommandResult.ServerIsBusy))));
                    server.BeginGetContext(new AsyncCallback(ProcessHttpRequest), server);
                    return;
                }
                _isRuning = true;
                server.BeginGetContext(new AsyncCallback(ProcessHttpRequest), server);
            }
            catch
            {
                Logger.Warning("服务器已关闭!");
                return;
            }

            string scriptName = new UrlHelper(context.Request.Url).ScriptName;

            byte[]       resultBytes = null;
            SFReturnCode result      = _httpImplanter.ProcessRequest(context);

            resultBytes = _httpImplanter.CreateReturnResult(context, result);

            RetutnResponse(context, resultBytes);
            GC.Collect();
            _isRuning = false;
        }