<commit_before>#include <SenseKit/sensekit_types.h>
#include "StreamConnection.h"
#include "StreamBin.h"
#include "Stream.h"
#include <cassert>

namespace sensekit {

    StreamConnection::StreamConnection(Stream* stream)
        : m_stream(stream)
    {
        assert (stream != nullptr);

        m_connection.handle =
            reinterpret_cast<sensekit_streamconnection_handle_t>(this);

        m_connection.desc = stream->get_description();

        m_binFrontBufferReadyCallback =
            [this](StreamBin* b, sensekit_frame_index_t frameIndex)
            {
                this->on_bin_front_buffer_ready(b, frameIndex);
            };
    }

    StreamConnection::~StreamConnection()
    {
        clear_pending_parameter_result();
        set_bin(nullptr);
    }

    sensekit_bin_t StreamConnection::get_bin_handle()
    {
        if (m_bin != nullptr)
        {
            return m_bin->get_handle();
        }
        else
        {
            return nullptr;
        }
    }

    const sensekit_stream_desc_t& StreamConnection::get_description() const
    {
        return m_stream->get_description();
    }

    sensekit_frame_t* StreamConnection::lock()
    {
        if (m_locked)
            return m_currentFrame;

        if (m_bin != nullptr)
        {
            m_currentFrame = m_bin->lock_front_buffer();
        }
        else
        {
            m_currentFrame = nullptr;
        }

        m_locked = true;

        return m_currentFrame;
    }

    void StreamConnection::unlock()
    {
        if (!m_locked)
            return;

        if (m_bin != nullptr)
            m_bin->unlock_front_buffer();

        m_locked = false;
    }

    void StreamConnection::start()
    {
        if (m_started)
            return;

        m_started = true;
    }

    void StreamConnection::stop()
    {
        if (!m_started)
            return;

        m_started = false;
    }

    void StreamConnection::set_bin(StreamBin* bin)
    {
        if (m_bin != nullptr)
        {
            m_bin->unregister_front_buffer_ready_callback(m_binFrontBufferReadyCallbackId);
            m_bin->dec_connected();<commit_msg>Add check for StreamConnection to not close a frame if it didn't properly open one<commit_after>#include <SenseKit/sensekit_types.h>
#include "StreamConnection.h"
#include "StreamBin.h"
#include "Stream.h"
#include <cassert>

namespace sensekit {

    StreamConnection::StreamConnection(Stream* stream)
        : m_stream(stream)
    {
        assert (stream != nullptr);

        m_connection.handle =
            reinterpret_cast<sensekit_streamconnection_handle_t>(this);

        m_connection.desc = stream->get_description();

        m_binFrontBufferReadyCallback =
            [this](StreamBin* b, sensekit_frame_index_t frameIndex)
            {
                this->on_bin_front_buffer_ready(b, frameIndex);
            };
    }

    StreamConnection::~StreamConnection()
    {
        clear_pending_parameter_result();
        set_bin(nullptr);
    }

    sensekit_bin_t StreamConnection::get_bin_handle()
    {
        if (m_bin != nullptr)
        {
            return m_bin->get_handle();
        }
        else
        {
            return nullptr;
        }
    }

    const sensekit_stream_desc_t& StreamConnection::get_description() const
    {
        return m_stream->get_description();
    }

    sensekit_frame_t* StreamConnection::lock()
    {
        if (m_locked)
            return m_currentFrame;

        if (m_bin != nullptr)
        {
            m_currentFrame = m_bin->lock_front_buffer();
        }
        else
        {
            m_currentFrame = nullptr;
        }

        m_locked = true;

        return m_currentFrame;
    }

    void StreamConnection::unlock()
    {
        if (!m_locked)
            return;

        if (m_currentFrame != nullptr && m_bin != nullptr)
            m_bin->unlock_front_buffer();

        m_currentFrame = nullptr;

        m_locked = false;
    }

    void StreamConnection::start()
    {
        if (m_started)
            return;

        m_started = true;
    }

    void StreamConnection::stop()
    {
        if (!m_started)
            return;

        m_started = false;
    }

    void StreamConnection::set_bin(StreamBin* bin)
    {
        if (m_bin != nullptr)
        {
            m_bin->unregister_front_buffer_ready_callback(m_binFrontBufferReadyCallbackId);
            m_bin->dec_connected();
{'fraction_non_alphanumeric': 0.09533468559837728, 'fraction_numerical': 0.0, 'mean_word_length': 1.9645958583834335, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 25, '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': '7129497', 'n_tokens_mistral': 1325, 'n_tokens_neox': 1276, 'n_words': 215}