<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Hadouken.Framework.Rpc.Hosting
{
    public class HttpJsonRpcServer : IJsonRpcServer
    {
        private readonly IJsonRpcHandler _rpcHandler;
        private readonly HttpListener _httpListener = new HttpListener();

        public HttpJsonRpcServer(string listenUri, IJsonRpcHandler rpcHandler)
        {
            _rpcHandler = rpcHandler;
            _httpListener.Prefixes.Add(listenUri);
        }

        public void Open()
        {
            _httpListener.Start();
            _httpListener.BeginGetContext(GetContext, null);
        }

        public void Close()
        {
            _httpListener.Close();
        }

        private void GetContext(IAsyncResult ar)
        {
            try
            {
                var context = _httpListener.EndGetContext(ar);
                Task.Run(() => ProcessContext(context));
            }
            catch (ObjectDisposedException)
            {
                return;
            }

            _httpListener.BeginGetContext(GetContext, null);
        }

        private void ProcessContext(HttpListenerContext context)
        {
            using (var reader = new StreamReader(context.Request.InputStream))
            using (var writer = new StreamWriter(context.Response.OutputStream))
            {
                var content = reader.ReadToEnd();

                try
                {
                    var response = _rpcHandler.HandleAsync(content).Result;
<commit_msg>The HttpJsonRpcServer now uses the async/await model instead of the old Begin/End.<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Hadouken.Framework.Rpc.Hosting
{
    public class HttpJsonRpcServer : IJsonRpcServer
    {
        private readonly IJsonRpcHandler _rpcHandler;
        private readonly HttpListener _httpListener = new HttpListener();
        private readonly CancellationTokenSource _cancellationToken = new CancellationTokenSource();
        private readonly Task _workerTask;

        public HttpJsonRpcServer(string listenUri, IJsonRpcHandler rpcHandler)
        {
            _rpcHandler = rpcHandler;
            _httpListener.Prefixes.Add(listenUri);
            _workerTask = new Task(ct => Run(_cancellationToken.Token), _cancellationToken.Token);
        }

        public void Open()
        {
            _workerTask.Start();
        }

        public void Close()
        {
            _cancellationToken.Cancel();
            _workerTask.Wait();
        }

        private async void Run(CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            _httpListener.Start();

            while (!_cancellationToken.IsCancellationRequested)
            {
                var context = await _httpListener.GetContextAsync();

                if (cancellationToken.IsCancellationRequested)
                    break;

                ProcessContext(context);
            }

            _httpListener.Close();
        }

        private async void ProcessContext(HttpListenerContext context)
        {
            using (var reader = new StreamReader(context.Request.InputStream))
            using (var writer = new StreamWriter(context.Response.OutputStream))
            {
                var content = reader.ReadToEnd();

                try
                {
                    var response = await _rpcHandler.HandleAsync(content);

{'fraction_non_alphanumeric': 0.07281553398058252, 'fraction_numerical': 0.0, 'mean_word_length': 2.008110300081103, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7059377', 'n_tokens_mistral': 908, 'n_tokens_neox': 900, 'n_words': 200}