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

47 lines
1.3 KiB
C++

#ifndef A8_TCPCLIENT_H
#define A8_TCPCLIENT_H
namespace a8
{
class TcpClient
{
public:
std::function<void (a8::TcpClient*, int)> on_error;
std::function<void (a8::TcpClient*)> on_connect;
std::function<void (a8::TcpClient*)> on_disconnect;
std::function<void (a8::TcpClient*, char*, unsigned int)> on_socketread;
std::string remote_address;
int remote_port = 0;
TcpClient();
virtual ~TcpClient();
void Open();
void Close();
bool IsActive();
bool Connected();
void SendBuff(const char* buff, unsigned int bufflen);
private:
volatile int socket_ = a8::INVALID_SOCKET;
volatile bool connected_ = false;
volatile bool sender_thread_shutdown_ = false;
volatile bool worker_thread_shutdown_ = false;
std::thread* worker_thread_ = nullptr;
std::mutex* send_buffer_mutex_ = nullptr;
SendQueueNode *top_node_ = nullptr;
SendQueueNode *bot_node_ = nullptr;
std::mutex *send_cond_mutex_ = nullptr;
std::condition_variable *send_cond_ = nullptr;
void SetActive(bool active);
bool ActiveStart();
void ActiveStop();
void WorkerThreadProc();
void SenderThreadProc();
void NotifySendCond();
};
}
#endif