a8/a8/tcpsession.h
2018-08-26 20:34:01 +08:00

74 lines
1.8 KiB
C++

#ifndef A8_TCPSESSION_H
#define A8_TCPSESSION_H
#include <mutex>
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();
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();
virtual void Destory();
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 AsyncSend(bool is_first_package);
protected:
char *recv_buff_ = nullptr;
int recv_bufflen_ = 0;
int max_packet_len_ = 0;
private:
int socket_ = 0;
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