59 lines
1.6 KiB
C++
59 lines
1.6 KiB
C++
#pragma once
|
|
|
|
#ifdef USE_ASIO
|
|
|
|
#include <asio.hpp>
|
|
|
|
using asio::ip::tcp;
|
|
|
|
namespace a8
|
|
{
|
|
|
|
class AsioTcpClient
|
|
{
|
|
public:
|
|
std::function<void (a8::AsioTcpClient*, int)> on_error;
|
|
std::function<void (a8::AsioTcpClient*)> on_connect;
|
|
std::function<void (a8::AsioTcpClient*)> on_disconnect;
|
|
std::function<void (a8::AsioTcpClient*, char*, unsigned int)> on_socketread;
|
|
AsioTcpClient(const std::string& remote_ip, int remote_port);
|
|
virtual ~AsioTcpClient();
|
|
const std::string& GetRemoteAddress() { return remote_address_; }
|
|
int GetRemotePort() { return remote_port_; }
|
|
|
|
void Open();
|
|
void Close();
|
|
bool IsActive();
|
|
bool Connected();
|
|
void SendBuff(const char* buff, unsigned int bufflen);
|
|
|
|
private:
|
|
void HandleConnect(const asio::error_code& err);
|
|
void DoRead();
|
|
void DoSend();
|
|
void WorkerThreadProc();
|
|
|
|
private:
|
|
std::string remote_address_;
|
|
int remote_port_ = 0;
|
|
|
|
std::shared_ptr<asio::ip::tcp::endpoint> endpoint_;
|
|
std::shared_ptr<asio::io_context> io_context_;
|
|
std::shared_ptr<asio::ip::tcp::socket> socket_;
|
|
volatile bool actived_ = false;
|
|
volatile bool connected_ = false;
|
|
std::shared_ptr<std::mutex> send_buffer_mutex_;
|
|
SendQueueNode *top_node_ = nullptr;
|
|
SendQueueNode *bot_node_ = nullptr;
|
|
volatile SendQueueNode *work_node_ = nullptr;
|
|
std::array<char, 1024 * 64> buffer_;
|
|
|
|
void SetActive(bool active);
|
|
void ActiveStart();
|
|
void ActiveStop();
|
|
};
|
|
|
|
}
|
|
|
|
#endif
|