<commit_before>﻿using Newtonsoft.Json.Linq;
using System;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace Espera.Services
{
    internal class EsperaNetworkClient : IEsperaNetworkClient
    {
        private readonly TcpClient client;

        public EsperaNetworkClient(TcpClient client)
        {
            if (client == null)
                throw new ArgumentNullException("client");

            this.client = client;
        }

        public void Dispose()
        {
            this.client.Close();
        }

        public async Task<byte[]> ReceiveAsync(int length)
        {
            var buffer = new byte[length];
            int received = 0;

            while (received < length)
            {
                int bytesRecieved = await this.client.GetStream().ReadAsync(buffer, received, buffer.Length - received);
                received += bytesRecieved;
            }

            return buffer;
        }

        public async Task<JObject> ReceiveMessage()
        {
            byte[] buffer = await this.ReceiveAsync(42);

            string header = Encoding.Unicode.GetString(buffer);

            if (header != "espera-client-message")
                throw new Exception("Holy batman, something went terribly wrong!");

            buffer = await this.ReceiveAsync(4);

            int length = BitConverter.ToInt32(buffer, 0);<commit_msg>Run network receive entirely on a separate thread<commit_after>﻿using Newtonsoft.Json.Linq;
using System;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace Espera.Services
{
    internal class EsperaNetworkClient : IEsperaNetworkClient
    {
        private readonly TcpClient client;

        public EsperaNetworkClient(TcpClient client)
        {
            if (client == null)
                throw new ArgumentNullException("client");

            this.client = client;
        }

        public void Dispose()
        {
            this.client.Close();
        }

        public async Task<byte[]> ReceiveAsync(int length)
        {
            return await Task.Run(async () =>
            {
                var buffer = new byte[length];
                int received = 0;

                while (received < length)
                {
                    int bytesRecieved = await this.client.GetStream().ReadAsync(buffer, received, buffer.Length - received);
                    received += bytesRecieved;
                }

                return buffer;
            });
        }

        public async Task<JObject> ReceiveMessage()
        {
            byte[] buffer = await this.ReceiveAsync(42);

            string header = Encoding.Unicode.GetString(buffer);

            if (header != "espera-client-message")
                throw new Exception("Holy batman, something went terribly wrong!");

            buffer = await this.ReceiveAsync(4);

            int length = BitConverter.ToInt32(buffer, 0);
{'fraction_non_alphanumeric': 0.0883940620782726, 'fraction_numerical': 0.004723346828609987, 'mean_word_length': 1.8130929791271346, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 8, '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': '924481', 'n_tokens_mistral': 773, 'n_tokens_neox': 726, 'n_words': 189}