<commit_before>//-----------------------------------
// Copyright <NAME> 2013-2017
//-----------------------------------
#pragma once

#include "Message.h"
#include "TcpForward.h"
#include "Threading.h"
#include "Utils.h"
#include "../OrbitPlugin/OrbitUserData.h"

#include <type_traits>
#include <vector>
#include <atomic>
#include <unordered_map>

//-----------------------------------------------------------------------------
class TcpPacket
{
public:
    TcpPacket(){}
    explicit TcpPacket( const Message & a_Message
                      , const void* a_Payload )
                      : m_Data( new std::vector<char>() )
    {
        m_Data->resize( sizeof( Message ) + a_Message.m_Size + 4 );
        memcpy( m_Data->data(), &a_Message, sizeof( Message ) );

        if( a_Payload )
        {
            memcpy( m_Data->data() + sizeof( Message ), a_Payload, a_Message.m_Size );
        }

        // Footer
        const unsigned int footer = MAGIC_FOOT_MSG;
        memcpy( m_Data->data() + sizeof( Message ) + a_Message.m_Size, &footer, 4 );
    }

    void Dump() const
    {
        std::cout << "TcpPacket [" << std::dec << (uint32_t)m_Data->size() << " bytes]" << std::endl;
        PrintBuffer(m_Data->data(), (uint32_t)m_Data->size());
    }

    std::shared_ptr< std::vector<char> > Data() { return m_Data; };

private:
    std::shared_ptr< std::vector<char> > m_Data;
};

//-----------------------------------------------------------------------------
class TcpEntity
{
public:
    TcpEntity();
    virtual ~TcpEntity();

    virtual void Start();
    void Stop();
    void FlushSendQueue();

    // Note: All Send methods can be called concurrently from multiple threads
    inline void Send(MessageType a_Type) { Message msg(a_Type); SendMsg(msg, nullptr); }
    inline void Send(Message & a_Message, void* a_Data);
    inline void Send(Message & a_Message);
    inline void Send(std::string& a_String);
    inline void Send(OrbitLogEntry& a_Entry);
    inline void Send(Orbit::UserData& a_Entry);
    inline void Send( MessageType a_Type , void* a_Data, size_t a_Size );

    template<class T> void Send( Message & a_Message, const std::vector<T> & a_Vector );
    template<class T> void Send( MessageType a_Type , const std::vector<T> & a_Vector );
    template<class T> void Send( Message & a_Message, const T& a_Item );
    template<class T> void Send( MessageType a_Type , const T& a_Item );

    typedef std::function< void( const Message & ) > MsgCallback;
    void AddCallback( MessageType a_MsgType, MsgCallback a_Callback ) { m_Callbacks[a_MsgType].push_back(a_Callback); }
    void AddMainThreadCallback( MessageType a_MsgType, MsgCallback a_Callback ) { m_MainThreadCallbacks[a_MsgType].push_back(a_Callback); }
    void Callback( const Message& a_Message );
    void ProcessMainThreadCallbacks();
    bool IsValid() const { return m_IsValid; }

protected:
    void SendMsg( Message & a_Message, const void* a_Payload );
    virtual TcpSocket* GetSocket() = 0;
    void SendData();

protected:
    TcpService*                m_TcpService;
    TcpSocket*                 m_TcpSocket;
    std::thread*               m_SenderThread;
    AutoResetEvent             m_ConditionVariable;
    LockFreeQueue< TcpPacket > m_SendQueue;
    std::atomic<uint32_t>      m_NumQueuedEntries;
    std::atomic<bool>          m_ExitRequested;
    std::atomic<bool>          m_FlushRequested;
    std::atomic<uint32_t>      m_NumFlushedItems;
    Mutex                      m_Mutex;
    std::atomic<bool>          m_IsValid = false;

    std::unordered_map< int, std::vector<MsgCallback> > m_Callbacks;
    std::unordered_map< int, std::vector<MsgCallback> > m_MainThreadCallbacks;
    std::unordered_map< int, std::vector<std::shared_ptr<MessageOwner>> > m_MainThreadMessages;
};<commit_msg>Remove atomic member initialization from .h.<commit_after>//-----------------------------------
// Copyright <NAME> 2013-2017
//-----------------------------------
#pragma once

#include "Message.h"
#include "TcpForward.h"
#include "Threading.h"
#include "Utils.h"
#include "../OrbitPlugin/OrbitUserData.h"

#include <type_traits>
#include <vector>
#include <atomic>
#include <unordered_map>

//-----------------------------------------------------------------------------
class TcpPacket
{
public:
    TcpPacket(){}
    explicit TcpPacket( const Message & a_Message
                      , const void* a_Payload )
                      : m_Data( new std::vector<char>() )
    {
        m_Data->resize( sizeof( Message ) + a_Message.m_Size + 4 );
        memcpy( m_Data->data(), &a_Message, sizeof( Message ) );

        if( a_Payload )
        {
            memcpy( m_Data->data() + sizeof( Message ), a_Payload, a_Message.m_Size );
        }

        // Footer
        const unsigned int footer = MAGIC_FOOT_MSG;
        memcpy( m_Data->data() + sizeof( Message ) + a_Message.m_Size, &footer, 4 );
    }

    void Dump() const
    {
        std::cout << "TcpPacket [" << std::dec << (uint32_t)m_Data->size() << " bytes]" << std::endl;
        PrintBuffer(m_Data->data(), (uint32_t)m_Data->size());
    }

    std::shared_ptr< std::vector<char> > Data() { return m_Data; };

private:
    std::shared_ptr< std::vector<char> > m_Data;
};

//-----------------------------------------------------------------------------
class TcpEntity
{
public:
    TcpEntity();
    virtual ~TcpEntity();

    virtual void Start();
    void Stop();
    void FlushSendQueue();

    // Note: All Send methods can be called concurrently from multiple threads
    inline void Send(MessageType a_Type) { Message msg(a_Type); SendMsg(msg, nullptr); }
    inline void Send(Message & a_Message, void* a_Data);
    inline void Send(Message & a_Message);
    inline void Send(std::string& a_String);
    inline void Send(OrbitLogEntry& a_Entry);
    inline void Send(Orbit::UserData& a_Entry);
    inline void Send( MessageType a_Type , void* a_Data, size_t a_Size );

    template<class T> void Send( Message & a_Message, const std::vector<T> & a_Vector );
    template<class T> void Send( MessageType a_Type , const std::vector<T> & a_Vector );
    template<class T> void Send( Message & a_Message, const T& a_Item );
    template<class T> void Send( MessageType a_Type , const T& a_Item );

    typedef std::function< void( const Message & ) > MsgCallback;
    void AddCallback( MessageType a_MsgType, MsgCallback a_Callback ) { m_Callbacks[a_MsgType].push_back(a_Callback); }
    void AddMainThreadCallback( MessageType a_MsgType, MsgCallback a_Callback ) { m_MainThreadCallbacks[a_MsgType].push_back(a_Callback); }
    void Callback( const Message& a_Message );
    void ProcessMainThreadCallbacks();
    bool IsValid() const { return m_IsValid; }

protected:
    void SendMsg( Message & a_Message, const void* a_Payload );
    virtual TcpSocket* GetSocket() = 0;
    void SendData();

protected:
    TcpService*                m_TcpService;
    TcpSocket*                 m_TcpSocket;
    std::thread*               m_SenderThread;
    AutoResetEvent             m_ConditionVariable;
    LockFreeQueue< TcpPacket > m_SendQueue;
    std::atomic<uint32_t>      m_NumQueuedEntries;
    std::atomic<bool>          m_ExitRequested;
    std::atomic<bool>          m_FlushRequested;
    std::atomic<uint32_t>      m_NumFlushedItems;
    Mutex                      m_Mutex;
    std::atomic<bool>          m_IsValid;

    std::unordered_map< int, std::vector<MsgCallback> > m_Callbacks;
    std::unordered_map< int, std::vector<MsgCallback> > m_MainThreadCallbacks;
    std::unordered_map< int, std::vector<std::shared_ptr<MessageOwner>> > m_MainThreadMessages;
};
{'fraction_non_alphanumeric': 0.17414627763967028, 'fraction_numerical': 0.004971869684678791, 'mean_word_length': 2.5388888888888888, 'pattern_counts': {'":': 0, '<': 83, '<?xml version=': 0, '>': 77, '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': '2090748', 'n_tokens_mistral': 2359, 'n_tokens_neox': 2298, 'n_words': 548}