static async Task ProcessListenerContext(HttpListenerContext listenerContext, HttpAsyncHost host) { try { // Get the response action to take: Stopwatch sw = Stopwatch.StartNew(); var requestContext = new HttpRequestContext(host._hostContext, listenerContext.Request, listenerContext.User); var action = await host._handler.Execute(requestContext); sw.Stop(); if (sw.ElapsedMilliseconds <= 20) { Interlocked.Increment(ref _20s); } else if (sw.ElapsedMilliseconds <= 50) { Interlocked.Increment(ref _50s); } else if (sw.ElapsedMilliseconds <= 100) { Interlocked.Increment(ref _100s); } else if (sw.ElapsedMilliseconds <= 200) { Interlocked.Increment(ref _200s); } else if (sw.ElapsedMilliseconds <= 500) { Interlocked.Increment(ref _500s); } else if (sw.ElapsedMilliseconds <= 1000) { Interlocked.Increment(ref _1000s); } else if (sw.ElapsedMilliseconds <= 2000) { Interlocked.Increment(ref _2000s); } else if (sw.ElapsedMilliseconds <= 5000) { Interlocked.Increment(ref _5000s); } else { TraceLog.WriteError("Http request [{0}] timeout {1}ms", listenerContext.Request.RawUrl, sw.ElapsedMilliseconds); Interlocked.Increment(ref _up); } if (action != null) { // Take the action and await its completion: var responseContext = new HttpRequestResponseContext(requestContext, listenerContext.Response); var task = action.Execute(responseContext); if (task != null) { await task; } } // Close the response and send it to the client: listenerContext.Response.Close(); } catch (HttpListenerException) { } catch (Exception ex) { TraceLog.WriteError("Http ProcessListenerContext {0}", ex); } }
static async Task ProcessListenerContext(HttpListenerContext listenerContext, HttpAsyncHost host) { try { // Get the response action to take: Stopwatch sw = Stopwatch.StartNew(); var requestContext = new HttpRequestContext(host._hostContext, listenerContext.Request, listenerContext.User); var action = await host._handler.Execute(requestContext); sw.Stop(); if (sw.ElapsedMilliseconds <= 20) Interlocked.Increment(ref _20s); else if (sw.ElapsedMilliseconds <= 50) Interlocked.Increment(ref _50s); else if (sw.ElapsedMilliseconds <= 100) Interlocked.Increment(ref _100s); else if (sw.ElapsedMilliseconds <= 200) Interlocked.Increment(ref _200s); else if (sw.ElapsedMilliseconds <= 500) Interlocked.Increment(ref _500s); else if (sw.ElapsedMilliseconds <= 1000) Interlocked.Increment(ref _1000s); else if (sw.ElapsedMilliseconds <= 2000) Interlocked.Increment(ref _2000s); else if (sw.ElapsedMilliseconds <= 5000) Interlocked.Increment(ref _5000s); else { TraceLog.WriteError("Http request [{0}] timeout {1}ms", listenerContext.Request.RawUrl, sw.ElapsedMilliseconds); Interlocked.Increment(ref _up); } if (action != null) { // Take the action and await its completion: var responseContext = new HttpRequestResponseContext(requestContext, listenerContext.Response); var task = action.Execute(responseContext); if (task != null) await task; } // Close the response and send it to the client: listenerContext.Response.Close(); } catch (HttpListenerException) { } catch (Exception ex) { TraceLog.WriteError("Http ProcessListenerContext {0}", ex); } }
/// <summary> /// /// </summary> protected GameHttpHost(IHttpAsyncHandler handler) { listenUrls = new List<string>(); httpListener = new HttpAsyncHost(handler, 16); }
static async Task ProcessListenerContext(HttpListenerContext listenerContext, HttpAsyncHost host) { Stopwatch sw = Stopwatch.StartNew(); string rawUrl = string.Empty; string userHostAddress = string.Empty; long actionTime = 0; string identity = string.Empty; try { rawUrl = listenerContext.Request.RawUrl; userHostAddress = host.HttpCdnAddress.GetUserHostAddress(listenerContext.Request.RemoteEndPoint, key => listenerContext.Request.Headers[key]); // Get the response action to take: var requestContext = new HttpRequestContext(host._hostContext, listenerContext.Request, listenerContext.User, userHostAddress); var action = await host._handler.Execute(requestContext); actionTime = sw.ElapsedMilliseconds; if (action != null) { if (listenerContext.Request.HttpMethod == "POST") { rawUrl += action.RequestParams; } identity = action.Identity; // Take the action and await its completion: var responseContext = new HttpRequestResponseContext(requestContext, listenerContext.Response); var task = action.Execute(responseContext); if (task != null) { await task; } } // Close the response and send it to the client: listenerContext.Response.Close(); sw.Stop(); } catch (Exception ex) { TraceLog.WriteError("Http ProcessListenerContext {0}\r\nUrl:{1}\r\nHost:{2}, User:{3}", ex, rawUrl, userHostAddress, identity); } finally { if (sw.ElapsedMilliseconds >= 5) { TraceLog.Write("Http request action time:{0}ms, total:{1}ms.\r\nRawUrl:{2}\r\nHost:{3}, User:{4}", actionTime, sw.ElapsedMilliseconds, rawUrl, userHostAddress, identity); } if (sw.ElapsedMilliseconds == 0) { } else if (sw.ElapsedMilliseconds <= 20) { Interlocked.Increment(ref _20s); } else if (sw.ElapsedMilliseconds <= 50) { Interlocked.Increment(ref _50s); } else if (sw.ElapsedMilliseconds <= 100) { Interlocked.Increment(ref _100s); } else if (sw.ElapsedMilliseconds <= 200) { Interlocked.Increment(ref _200s); } else if (sw.ElapsedMilliseconds <= 500) { Interlocked.Increment(ref _500s); } else if (sw.ElapsedMilliseconds <= 1000) { Interlocked.Increment(ref _1000s); } else if (sw.ElapsedMilliseconds <= 2000) { Interlocked.Increment(ref _2000s); } else if (sw.ElapsedMilliseconds <= 5000) { Interlocked.Increment(ref _5000s); } else { TraceLog.WriteError("Http request action timeout:{0}ms, total:{1}ms.\r\nRawUrl:{2}\r\nHost:{3}, User:{4}", actionTime, sw.ElapsedMilliseconds, rawUrl, userHostAddress, identity); Interlocked.Increment(ref _up); } } }
static void Main(string[] args) { try { Console.CancelKeyPress += OnCancelKeyPress; try { HandlerManager.Init(Assembly.GetExecutingAssembly()); } catch (Exception ex) { TraceLog.WriteError("Application_Start error:{0}", ex); } HttpAsyncHandler.PageListen["/"] = OnService;//default page HttpAsyncHandler.PageListen["/default.ashx"] = OnService; var listenUrls = new List<string>(); var httpListener = new HttpAsyncHost(HttpAsyncHandler.Default, 16); var section = ConfigManager.Configger.GetFirstOrAddConfig<ProtocolSection>(); var httpHost = section.HttpHost; var httpPort = section.HttpPort; var httpName = section.HttpName; if (!string.IsNullOrEmpty(httpHost)) { var hosts = httpHost.Split(','); foreach (var point in hosts) { var addressList = point.Split(':'); string host = addressList[0]; int port = httpPort; if (addressList.Length > 1) { int.TryParse(addressList[1], out port); } string address = host.StartsWith("http", StringComparison.InvariantCultureIgnoreCase) ? host : "http://" + host; if (listenUrls.Count == 0) listenUrls.Add(string.Format("{0}:{1}/", address, port)); listenUrls.Add(string.Format("{0}:{1}/{2}/", address, port, httpName)); } } httpListener.Run(listenUrls.ToArray()); foreach (var listenUrl in listenUrls) { TraceLog.WriteLine("{0} Http service:{1} is started.", DateTime.Now.ToString("HH:mm:ss"), listenUrl.TrimEnd('/')); } if (httpListener.Error != null) { throw httpListener.Error; } TraceLog.WriteLine("{0} AccountServer has started successfully!", DateTime.Now.ToString("HH:mm:ss")); } catch (Exception ex) { TraceLog.WriteLine("{0} AccountServer failed to start!", DateTime.Now.ToString("HH:mm:ss")); TraceLog.WriteError("Start Error:{0}", ex); } try { TraceLog.WriteLine("# Server exit command \"Ctrl+C\" or \"Ctrl+Break\"."); RunWait().Wait(); } finally { runCompleteEvent.Set(); } }
static async Task ProcessListenerContext(HttpListenerContext listenerContext, HttpAsyncHost host) { Stopwatch sw = Stopwatch.StartNew(); string rawUrl = string.Empty; string userHostAddress = string.Empty; long actionTime = 0; string identity = string.Empty; try { rawUrl = listenerContext.Request.RawUrl; userHostAddress = host.HttpCdnAddress.GetUserHostAddress(listenerContext.Request.RemoteEndPoint, key => listenerContext.Request.Headers[key]); // Get the response action to take: var requestContext = new HttpRequestContext(host._hostContext, listenerContext.Request, listenerContext.User, userHostAddress); var action = await host._handler.Execute(requestContext); actionTime = sw.ElapsedMilliseconds; if (action != null) { if (listenerContext.Request.HttpMethod == "POST") { rawUrl += action.RequestParams; } identity = action.Identity; // Take the action and await its completion: var responseContext = new HttpRequestResponseContext(requestContext, listenerContext.Response); var task = action.Execute(responseContext); if (task != null) await task; } // Close the response and send it to the client: listenerContext.Response.Close(); sw.Stop(); } catch (Exception ex) { TraceLog.WriteError("Http ProcessListenerContext {0}\r\nUrl:{1}\r\nHost:{2}, User:{3}", ex, rawUrl, userHostAddress, identity); } finally { if (sw.ElapsedMilliseconds >= 5) { TraceLog.Write("Http request action time:{0}ms, total:{1}ms.\r\nRawUrl:{2}\r\nHost:{3}, User:{4}", actionTime, sw.ElapsedMilliseconds, rawUrl, userHostAddress, identity); } if (sw.ElapsedMilliseconds == 0) { } else if (sw.ElapsedMilliseconds <= 20) Interlocked.Increment(ref _20s); else if (sw.ElapsedMilliseconds <= 50) Interlocked.Increment(ref _50s); else if (sw.ElapsedMilliseconds <= 100) Interlocked.Increment(ref _100s); else if (sw.ElapsedMilliseconds <= 200) Interlocked.Increment(ref _200s); else if (sw.ElapsedMilliseconds <= 500) Interlocked.Increment(ref _500s); else if (sw.ElapsedMilliseconds <= 1000) Interlocked.Increment(ref _1000s); else if (sw.ElapsedMilliseconds <= 2000) Interlocked.Increment(ref _2000s); else if (sw.ElapsedMilliseconds <= 5000) Interlocked.Increment(ref _5000s); else { TraceLog.WriteError("Http request action timeout:{0}ms, total:{1}ms.\r\nRawUrl:{2}\r\nHost:{3}, User:{4}", actionTime, sw.ElapsedMilliseconds, rawUrl, userHostAddress, identity); Interlocked.Increment(ref _up); } } }