private static Action <HttpListenerContext> CreateSyncCallback(ServerArguments parsedArguments) { return(context => { var currentRequestId = Interlocked.Increment(ref RequestsCount); log.InfoFormat("Thread #{0} received request #{1} at {2}", Thread.CurrentThread.ManagedThreadId, currentRequestId, DateTime.Now.TimeOfDay); Thread.Sleep(parsedArguments.MethodDuration); var encryptedBytes = GetBase64HashBytes(context.Request.QueryString["query"], Encoding.UTF8); context.Response.OutputStream.Write(encryptedBytes, 0, encryptedBytes.Length); log.InfoFormat("Thread #{0} sent response #{1} at {2}", Thread.CurrentThread.ManagedThreadId, currentRequestId, DateTime.Now.TimeOfDay); }); }
private static Func <HttpListenerContext, Task> CreateAsyncCallback(ServerArguments parsedArguments) { return(async context => { var currentRequestNum = Interlocked.Increment(ref RequestsCount); log.InfoFormat("Thread #{0} received request #{1} at {2}", Thread.CurrentThread.ManagedThreadId, currentRequestNum, DateTime.Now.TimeOfDay); await Task.Delay(parsedArguments.MethodDuration, CancellationToken.None); var encryptedBytes = GetBase64HashBytes(context.Request.QueryString["query"], Encoding.UTF8); await context.Response.OutputStream.WriteAsync(encryptedBytes, 0, encryptedBytes.Length); log.InfoFormat("Thread #{0} sent response #{1} at {2}", Thread.CurrentThread.ManagedThreadId, currentRequestNum, DateTime.Now.TimeOfDay); }); }
private static Func <HttpListenerContext, Task> CreateAsyncCallbackOk(ServerArguments parsedArguments) { return(async(context) => { var time = Environment.TickCount; var id = context.Request.QueryString["id"]; Console.WriteLine("Start cancelling \"{0}\" ", id); if (Cancellations.TryGetValue(id, out var ctx)) { ctx.Cancel(); Cancellations.TryRemove(id, out _); var answer = Encoding.UTF8.GetBytes("STOPED"); await context.Response.OutputStream.WriteAsync(answer, 0, answer.Length); } Console.WriteLine("End cancelling \"{0}\" in {1}", id, Environment.TickCount - time); }); }
// private static ConcurrentBag<string> StopedIds { get; } public static void Main(string[] args) { XmlConfigurator.Configure(); try { ServerArguments parsedArguments; if (!ServerArguments.TryGetArguments(args, out parsedArguments)) { return; } var listener = new HttpListener { Prefixes = { string.Format("http://+:{0}/{1}/", parsedArguments.Port, parsedArguments.MethodName) } }; log.InfoFormat("Server is starting listening prefixes: {0}", string.Join(";", listener.Prefixes)); if (parsedArguments.Async) { log.InfoFormat("Press ENTER to stop listening"); listener.StartProcessingRequestsAsync(CreateAsyncCallback(parsedArguments), CreateAsyncCallbackOk(parsedArguments)); Console.ReadLine(); log.InfoFormat("Server stopped!"); } else { listener.StartProcessingRequestsSync(CreateSyncCallback(parsedArguments)); } } catch (Exception e) { Log.Fatal(e); } }
private static Func <HttpListenerContext, Task> CreateAsyncCallback(ServerArguments parsedArguments) { var tokenSources = new ConcurrentDictionary <string, CancellationTokenSource>(); return(async context => { var currentRequestNum = Interlocked.Increment(ref RequestsCount); log.InfoFormat("Thread #{0} received request #{1} at {2}", Thread.CurrentThread.ManagedThreadId, currentRequestNum, DateTime.Now.TimeOfDay); CancellationTokenSource ts; if (context.Request.QueryString.AllKeys.Contains("abort")) { if (tokenSources.TryRemove(context.Request.UserHostAddress + ":" + context.Request.QueryString["guid"], out ts)) { ts.Cancel(); } log.InfoFormat("Thread #{0} canceled request.", Thread.CurrentThread.ManagedThreadId); await context.Response.OutputStream.WriteAsync(new byte[] {}, 0, 0); return; } ts = new CancellationTokenSource(); var token = ts.Token; tokenSources[context.Request.UserHostAddress + ":" + context.Request.QueryString["guid"]] = ts; await Task.Delay(parsedArguments.MethodDuration, ts.Token); // Thread.Sleep(parsedArguments.MethodDuration); var encryptedBytes = GetBase64HashBytes(context.Request.QueryString["query"], Encoding.UTF8); await context.Response.OutputStream.WriteAsync(encryptedBytes, 0, encryptedBytes.Length); log.InfoFormat("Thread #{0} sent response #{1} at {2}", Thread.CurrentThread.ManagedThreadId, currentRequestNum, DateTime.Now.TimeOfDay); }); }
public static bool TryGetArguments(string[] args, out ServerArguments parsedArguments) { var argumentsParser = new FluentCommandLineParser <ServerArguments>(); argumentsParser.Setup(a => a.Ports) .As('p', "ports") .Required(); argumentsParser.Setup(a => a.MethodName) .As('n', "name") .Required(); argumentsParser.Setup(a => a.MethodDuration) .As('d', "duration") .WithDescription("Server will return his response in <duration> ms") .Required(); argumentsParser.Setup(a => a.Async) .As('a', "async") .SetDefault(false); argumentsParser.SetupHelp("?", "h", "help") .Callback(text => Console.WriteLine(text)); var parsingResult = argumentsParser.Parse(args); if (parsingResult.HasErrors) { argumentsParser.HelpOption.ShowHelp(argumentsParser.Options); parsedArguments = null; return(false); } parsedArguments = argumentsParser.Object; return(!parsingResult.HasErrors); }