<commit_before>#include "TcpClient.h"
#include <stdio.h>
#include <string.h>
#include <string>
#include <sys/socket.h>
#include <unistd.h>


#define MAX_INTERNAL_TX_COUNT 4000
using namespace Dumais::WebServer;
using namespace Dumais::Utils;


TcpClient::TcpClient(ITcpClientContext* ctx, IFramingStrategy* fs)
{
    this->mFailed = false;
    this->observer = 0;
    this->framingStrategy = fs;
    this->context = ctx;
    this->currentSendBuffer.buffer = 0;
    this->currentMessage.complete = false;
    this->currentMessage.buffer = 0;
    this->currentMessage.size = 0;
    this->currentMessage.currentIndex = 0;
    this->txList = new MPSCRingBuffer<SendBuffer>(MAX_INTERNAL_TX_COUNT);
    this->mSocket = 0;
}

TcpClient::~TcpClient()
{
    SendBuffer s;
    while (this->txList->get(s))
    {
        delete[] s.buffer;
    }
    delete this->txList;
    if (this->context != 0)  delete this->context;
    if (this->mSocket) delete this->mSocket;
}

ISocket* TcpClient::getSocket()
{
    return this->mSocket;
}

void TcpClient::setObserver(TcpClientObserver* obs)
{
    this->observer = obs;
}


bool TcpClient::onReceive(char *data, size_t size)
{
    if (this->mFailed) return false;

    while (size > 0)
    { 
        int ret = this->framingStrategy->processData(data, size, this->currentMessage);
        if (ret < 0)
        {
            observer->onFramingError(this,ret);
            this->mFailed = true;<commit_msg>Fixed socket leaks<commit_after>#include "TcpClient.h"
#include <stdio.h>
#include <string.h>
#include <string>
#include <sys/socket.h>
#include <unistd.h>


#define MAX_INTERNAL_TX_COUNT 4000
using namespace Dumais::WebServer;
using namespace Dumais::Utils;


TcpClient::TcpClient(ITcpClientContext* ctx, IFramingStrategy* fs)
{
    this->mFailed = false;
    this->observer = 0;
    this->framingStrategy = fs;
    this->context = ctx;
    this->currentSendBuffer.buffer = 0;
    this->currentMessage.complete = false;
    this->currentMessage.buffer = 0;
    this->currentMessage.size = 0;
    this->currentMessage.currentIndex = 0;
    this->txList = new MPSCRingBuffer<SendBuffer>(MAX_INTERNAL_TX_COUNT);
    this->mSocket = 0;
}

TcpClient::~TcpClient()
{
    SendBuffer s;
    while (this->txList->get(s))
    {
        delete[] s.buffer;
    }
    delete this->txList;
    if (this->context != 0)  delete this->context;
    if (this->mSocket)
    {
        this->mSocket->close();
        delete this->mSocket;
    }
}

ISocket* TcpClient::getSocket()
{
    return this->mSocket;
}

void TcpClient::setObserver(TcpClientObserver* obs)
{
    this->observer = obs;
}


bool TcpClient::onReceive(char *data, size_t size)
{
    if (this->mFailed) return false;

    while (size > 0)
    { 
        int ret = this->framingStrategy->processData(data, size, this->currentMessage);
        if (ret < 0)
        {
            observer->onFramingError(this,ret);
            this->mFailed = true;
{'fraction_non_alphanumeric': 0.13954281815080177, 'fraction_numerical': 0.008870692596383487, 'mean_word_length': 3.2991202346041058, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 71, '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': '5360934', 'n_tokens_mistral': 1031, 'n_tokens_neox': 988, 'n_words': 198}