<commit_before>#ifndef __CUBE_TCP_CONNECTION_H__
#define __CUBE_TCP_CONNECTION_H__

#include <memory>

#include "base/buffer.h"

#include "callbacks.h"
#include "inet_addr.h"

namespace cube {

namespace net {

class EventLoop;
class Eventor;
class Socket;

class TcpConnection : public std::enable_shared_from_this<TcpConnection> {
    public:
        enum ConnState {
            ConnState_Connecting,
            ConnState_Connected,
            ConnState_Disconnected,
        };

        TcpConnection(EventLoop *event_loop, int sockfd, const InetAddr &local_addr, const InetAddr &peer_addr);
        ~TcpConnection();

        // callback settor
        void SetConnectCallback(const ConnectCallback &cb) { m_connect_callback = cb; }
        void SetDisconnectCallback(const DisconnectCallback &cb) { m_disconnect_callback = cb; }
        //void SetWriteCompleteCallback(const WriteCompleteCallback &cb) { m_write_complete_callback = cb; }

        uint64_t Id() const { return m_conn_id; }
        const InetAddr &LocalAddr() const { return m_local_addr; }
        const InetAddr &PeerAddr() const { return m_peer_addr; }

        void Initialize();

        // run callback when the read data's length >= read_bytes
        void ReadBytes(size_t read_bytes, const ReadCallback &cb);
        // run callback when the read data contains delimiter
        void ReadUntil(const std::string &delimiter, const ReadCallback &cb);
        // run callback when the read data's length >= 1
        void ReadAny(const ReadCallback &cb);

        bool Write(const std::string &str);
        bool Write(const char *data, size_t len);
        bool Write(const std::string &str, const WriteCompleteCallback &cb);
        bool Write(const char *data, size_t len, const WriteCompleteCallback &cb);

        void Close();   // close the connection
        void CloseAfter(int64_t delay_ms);
        bool Closed() const { return m_state == ConnState_Disconnected; }

        void EnableReading();
        void DisableReading();

        time_t LastActiveTime() const { return m_last_active_time; }

        const std::string &ErrMsg() const { return m_err_msg; }
    private:

        void OnRead();

        void HandleEvents(int revents);
        int HandleConnect();
        void HandleRead();
        void HandleWrite();
        void HandleError();
        void HandleClose();

    private:

        static uint64_t m_next_conn_id; // atomic

        EventLoop *m_event_loop;

        const uint64_t m_conn_id;<commit_msg>fix compile error<commit_after>#ifndef __CUBE_TCP_CONNECTION_H__
#define __CUBE_TCP_CONNECTION_H__

#include <memory>

#include "base/buffer.h"

#include "callbacks.h"
#include "inet_addr.h"

namespace cube {

namespace net {

class EventLoop;
class Eventor;
class Socket;

class TcpConnection : public std::enable_shared_from_this<TcpConnection> {
    public:
        enum ConnState {
            ConnState_Connecting,
            ConnState_Connected,
            ConnState_Disconnected,
        };

        TcpConnection(EventLoop *event_loop, int sockfd, const InetAddr &local_addr, const InetAddr &peer_addr);
        ~TcpConnection();

        // callback settor
        void SetConnectCallback(const ConnectCallback &cb) { m_connect_callback = cb; }
        void SetDisconnectCallback(const DisconnectCallback &cb) { m_disconnect_callback = cb; }
        //void SetWriteCompleteCallback(const WriteCompleteCallback &cb) { m_write_complete_callback = cb; }

        uint64_t Id() const { return m_conn_id; }
        const InetAddr &LocalAddr() const { return m_local_addr; }
        const InetAddr &PeerAddr() const { return m_peer_addr; }

        void Initialize();

        // run callback when the read data's length >= read_bytes
        void ReadBytes(size_t read_bytes, const ReadCallback &cb);
        // run callback when the read data contains delimiter
        void ReadUntil(const std::string &delimiter, const ReadCallback &cb);
        // run callback when the read data's length >= 1
        void ReadAny(const ReadCallback &cb);

        bool Write(const std::string &str);
        bool Write(const char *data, size_t len);
        bool Write(const std::string &str, const WriteCompleteCallback &cb);
        bool Write(const char *data, size_t len, const WriteCompleteCallback &cb);

        void Close();   // close the connection
        void CloseAfter(int64_t delay_ms);
        bool Closed() const { return m_state == ConnState_Disconnected; }

        void EnableReading();
        void DisableReading();

        time_t LastActiveTime() const { return m_last_active_time; }

        const std::string &ErrMsg() const { return m_err_msg; }

        friend class Connector;

    private:

        void OnRead();

        void HandleEvents(int revents);
        int HandleConnect();
        void HandleRead();
        void HandleWrite();
        void HandleError();
        void HandleClose();

    private:

        static uint64_t m_next_conn_id; // atomic

        EventLoop *m_event_loop;

        const uint64_t m_conn_id;
{'fraction_non_alphanumeric': 0.08504340962904498, 'fraction_numerical': 0.003551696921862668, 'mean_word_length': 2.899230769230769, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '4768093', 'n_tokens_mistral': 1476, 'n_tokens_neox': 1406, 'n_words': 412}