#ifndef A8_TCPSESSION2_H #define A8_TCPSESSION2_H #ifdef A8_TCP_SESSION2 #include namespace a8 { struct SendQueueNode; class TcpListener; class TcpSession { public: bool is_activite = false; time_t create_time = 0; a8::TcpListener* master = nullptr; unsigned long saddr = 0; std::string remote_address; int remote_port = 0; int socket_handle = -1; int epoll_fd = 0; list_head session_entry; public: TcpSession(); virtual ~TcpSession(); void SetMaxPacketLen(int max_packet_len); int Socket(); virtual void OnError(int); virtual void OnConnect(); virtual void OnDisConnect(); virtual void Destory(); bool Alive(); protected: virtual void SendBuff(const char* buff, unsigned int bufflen); void SendText(const std::string& text); void SetSocket(int sock); bool AllocRecvBuf(); virtual void Reset(); void _ForceClose(); virtual void OnSocketRead(char* buf, unsigned int buflen); virtual void DecodePacket(char* buf, int& offset, unsigned int buflen); virtual void DecodeUserPacket(char* buf, int& offset, unsigned int buflen) = 0; void DoClientRecv(); void DoClientSend(); void Close(); private: void ClearSendBuff(); void ClearWorkBuff(); void AsyncSend(); void NotifyEpollSend(); int DirectSend(a8::SendQueueNode* node); protected: char *recv_buff_ = nullptr; int recv_bufflen_ = 0; int max_packet_len_ = 0; volatile long long epoll_out_times = 0; volatile long long epoll_in_times = 0; volatile long long direct_send_times = 0; private: int socket_ = 0; volatile bool sending_ = false; a8::SendQueueNode* top_node_ = nullptr; a8::SendQueueNode* bot_node_ = nullptr; a8::SendQueueNode* work_node_ = nullptr; std::mutex send_buffer_mutex_; friend class TcpListener; friend class TcpListenerImpl; }; } #endif #endif