<commit_before>﻿using MultiMiner.Xgminer.Api.Parsers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace MultiMiner.Xgminer.Api
{
    public class ApiContext
    {
        private TcpClient tcpClient;
        public ApiContext(int port)
        {
            tcpClient = new TcpClient("127.0.0.1", port);
        }

        public List<DeviceInformation> GetDeviceInformation()
        {
            string textResponse = GetResponse(ApiVerb.Devs);
            List<DeviceInformation> result = new List<DeviceInformation>();
            DeviceInformationParser.ParseTextForDeviceInformation(textResponse, result);
            return result;
        }

        public void QuitMining()
        {
            GetResponse(ApiVerb.Quit);
        }

        private string GetResponse(string apiVerb)
        {
            NetworkStream stream = tcpClient.GetStream();
            Byte[] request = System.Text.Encoding.ASCII.GetBytes(apiVerb);
            stream.Write(request, 0, request.Length);

            Byte[] responseBuffer = new Byte[4096];
            string response = string.Empty;
            int bytesRead = stream.Read(responseBuffer, 0, responseBuffer.Length);
            response = System.Text.Encoding.ASCII.GetString(responseBuffer, 0, bytesRead);<commit_msg>Improve TCP reading code to read more than 4096 bytes if available<commit_after>﻿using MultiMiner.Xgminer.Api.Parsers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace MultiMiner.Xgminer.Api
{
    public class ApiContext
    {
        private TcpClient tcpClient;
        public ApiContext(int port)
        {
            tcpClient = new TcpClient("127.0.0.1", port);
        }

        public List<DeviceInformation> GetDeviceInformation()
        {
            string textResponse = GetResponse(ApiVerb.Devs);
            List<DeviceInformation> result = new List<DeviceInformation>();
            DeviceInformationParser.ParseTextForDeviceInformation(textResponse, result);
            return result;
        }

        public void QuitMining()
        {
            GetResponse(ApiVerb.Quit);
        }

        private string GetResponse(string apiVerb)
        {
            NetworkStream tcpStream = tcpClient.GetStream();

            Byte[] request = Encoding.ASCII.GetBytes(apiVerb);
            tcpStream.Write(request, 0, request.Length);

            Byte[] responseBuffer = new Byte[4096];
            string response = string.Empty;
            do
            {
                int bytesRead = tcpStream.Read(responseBuffer, 0, responseBuffer.Length);
                response = response + Encoding.ASCII.GetString(responseBuffer, 0, bytesRead);
            } while (tcpStream.DataAvailable);

{'fraction_non_alphanumeric': 0.08826583592938733, 'fraction_numerical': 0.010384215991692628, 'mean_word_length': 2.424170616113744, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '4721701', 'n_tokens_mistral': 778, 'n_tokens_neox': 758, 'n_words': 166}