<commit_before>#include "mocca/net/stream/TCPConnection.h"

#include "mocca/base/StringTools.h"

namespace mocca {
namespace net {

TCPConnection::TCPConnection(std::unique_ptr<IVDA::ConnectionSocket> socket)
    : socket_(move(socket)) {
    socket->SetNonBlocking(true);
    socket->SetNoSigPipe(true); // disable sigpipe to prevent crashes
    socket->SetNoDelay(true); // disable Nagle algorithm for low-latency transmission

    std::string localIP, peerIP;
    uint16_t localPort, peerPort;
    socket_->GetLocalNetworkAddress().GetAddress(localIP, localPort);
    socket_->GetPeerNetworkAddress().GetAddress(peerIP, peerPort);
    connectionID_ = std::make_shared<ConnectionID>(Endpoint("tcp", localIP, std::to_string(localPort)),
                                                   Endpoint("tcp", peerIP, std::to_string(peerPort)));
}

TCPConnection::~TCPConnection() {
    if (socket_ != nullptr) {
        try {
            socket_->Disconnect();
            socket_->Close();
        } catch (IVDA::SocketException&) {
            // ignore exception; exceptions must not escape the destructor!
        }
    }
}

std::shared_ptr<const ConnectionID> TCPConnection::connectionID() const {
    return connectionID_;
}

bool TCPConnection::isConnected() const {
    return socket_->IsConnected();
}

void TCPConnection::send(ByteArray message, std::chrono::milliseconds timeout) const {
    try {
        socket_->SendData((const int8_t*)message.data(), message.size(), static_cast<uint32_t>(timeout.count()));
    } catch (const IVDA::SocketConnectionException& err) {
        throw ConnectionClosedError("Connection to peer " + connectionID_->peerEndpoint.toString() +
                                        " lost during send operation (internal error: " + err.what() + ")",
                                    *connectionID_, __FILE__, __LINE__);
    } catch (const IVDA::SocketException& err) {
        std::string internalError = mocca::joinString(err.what(), ", ", err.internalError());
        throw NetworkError("Network error in send operation (internal error: " + internalError + ")", __FILE__, __LINE__);
    }
}

ByteArray TCPConnection::readFromStream(uint32_t maxSize, std::chrono::milliseconds timeout) const {
    try {
        ByteArray message(maxSize);
        auto bytesRead = socket_->ReceiveData((int8_t*)message.data(), maxSize, static_cast<uint32_t>(timeout.count()));
        message.setSize(bytesRead);
        return message;
    } catch (const IVDA::SocketConnectionException& err) {
        throw ConnectionClosedError("Connection to peer " + socket_->GetPeerAddress() + " lost during receive operation (internal error: " +
                                        err.what() + ")",
                                    *connectionID(), __FILE__, __LINE__);
    } catch (const IVDA::SocketException& err) {
        std::string internalError = mocca::joinString(err.what(), ", ", err.internalError());
        throw NetworkError("Network error in receive operation (internal error: " + internalError + ")", __FILE__, __LINE__);
    }
}
}
}<commit_msg>disable sigpipe to prevent crashes under unix<commit_after>#include "mocca/net/stream/TCPConnection.h"

#include "mocca/base/StringTools.h"

namespace mocca {
namespace net {

TCPConnection::TCPConnection(std::unique_ptr<IVDA::ConnectionSocket> socket)
    : socket_(move(socket)) {
    socket_->SetNonBlocking(true);
    socket_->SetNoSigPipe(true); // disable sigpipe to prevent crashes
    socket_->SetNoDelay(true); // disable Nagle algorithm for low-latency transmission

    std::string localIP, peerIP;
    uint16_t localPort, peerPort;
    socket_->GetLocalNetworkAddress().GetAddress(localIP, localPort);
    socket_->GetPeerNetworkAddress().GetAddress(peerIP, peerPort);
    connectionID_ = std::make_shared<ConnectionID>(Endpoint("tcp", localIP, std::to_string(localPort)),
                                                   Endpoint("tcp", peerIP, std::to_string(peerPort)));
}

TCPConnection::~TCPConnection() {
    if (socket_ != nullptr) {
        try {
            socket_->Disconnect();
            socket_->Close();
        } catch (IVDA::SocketException&) {
            // ignore exception; exceptions must not escape the destructor!
        }
    }
}

std::shared_ptr<const ConnectionID> TCPConnection::connectionID() const {
    return connectionID_;
}

bool TCPConnection::isConnected() const {
    return socket_->IsConnected();
}

void TCPConnection::send(ByteArray message, std::chrono::milliseconds timeout) const {
    try {
        socket_->SendData((const int8_t*)message.data(), message.size(), static_cast<uint32_t>(timeout.count()));
    } catch (const IVDA::SocketConnectionException& err) {
        throw ConnectionClosedError("Connection to peer " + connectionID_->peerEndpoint.toString() +
                                        " lost during send operation (internal error: " + err.what() + ")",
                                    *connectionID_, __FILE__, __LINE__);
    } catch (const IVDA::SocketException& err) {
        std::string internalError = mocca::joinString(err.what(), ", ", err.internalError());
        throw NetworkError("Network error in send operation (internal error: " + internalError + ")", __FILE__, __LINE__);
    }
}

ByteArray TCPConnection::readFromStream(uint32_t maxSize, std::chrono::milliseconds timeout) const {
    try {
        ByteArray message(maxSize);
        auto bytesRead = socket_->ReceiveData((int8_t*)message.data(), maxSize, static_cast<uint32_t>(timeout.count()));
        message.setSize(bytesRead);
        return message;
    } catch (const IVDA::SocketConnectionException& err) {
        throw ConnectionClosedError("Connection to peer " + socket_->GetPeerAddress() + " lost during receive operation (internal error: " +
                                        err.what() + ")",
                                    *connectionID(), __FILE__, __LINE__);
    } catch (const IVDA::SocketException& err) {
        std::string internalError = mocca::joinString(err.what(), ", ", err.internalError());
        throw NetworkError("Network error in receive operation (internal error: " + internalError + ")", __FILE__, __LINE__);
    }
}
}
}
{'fraction_non_alphanumeric': 0.12787888548880658, 'fraction_numerical': 0.003221130616846513, 'mean_word_length': 3.2389078498293515, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6704522', 'n_tokens_mistral': 1648, 'n_tokens_neox': 1633, 'n_words': 354}