49 lines
1.3 KiB
C++
49 lines
1.3 KiB
C++
#ifndef A8_ASYNC_TCPCLIENT_H
|
|
#define A8_ASYNC_TCPCLIENT_H
|
|
|
|
namespace a8
|
|
{
|
|
class AsyncTcpClient
|
|
{
|
|
public:
|
|
std::function<void (a8::AsyncTcpClient*, int)> on_error;
|
|
std::function<void (a8::AsyncTcpClient*)> on_connect;
|
|
std::function<void (a8::AsyncTcpClient*)> on_disconnect;
|
|
std::function<void (a8::AsyncTcpClient*, char*, unsigned int)> on_socketread;
|
|
std::string remote_address;
|
|
int remote_port = 0;
|
|
|
|
AsyncTcpClient();
|
|
virtual ~AsyncTcpClient();
|
|
|
|
void Open();
|
|
void Close();
|
|
bool IsActive();
|
|
bool Connected();
|
|
void SendBuff(const char* buff, unsigned int bufflen);
|
|
|
|
void DoConnect();
|
|
void DoRecv();
|
|
void DoSend();
|
|
void DoDisConnect();
|
|
|
|
private:
|
|
volatile int epoll_fd = a8::INVALID_FD;
|
|
volatile int socket_ = a8::INVALID_SOCKET;
|
|
volatile bool sending_ = false;
|
|
volatile bool connected_ = false;
|
|
std::mutex* send_buffer_mutex_ = nullptr;
|
|
SendQueueNode *top_node_ = nullptr;
|
|
SendQueueNode *bot_node_ = nullptr;
|
|
SendQueueNode *work_node_ = nullptr;
|
|
|
|
void SetActive(bool active);
|
|
bool ActiveStart();
|
|
void ActiveStop();
|
|
void NotifyEpollSend();
|
|
void AsyncSend();
|
|
};
|
|
}
|
|
|
|
#endif
|