66 lines
2.1 KiB
C++
66 lines
2.1 KiB
C++
#pragma once
|
|
|
|
#include <asio.hpp>
|
|
|
|
using asio::ip::tcp;
|
|
|
|
namespace f8
|
|
{
|
|
namespace internal
|
|
{
|
|
|
|
class AsioTcpClient
|
|
{
|
|
public:
|
|
std::function<void (f8::internal::AsioTcpClient*, int)> on_error;
|
|
std::function<void (f8::internal::AsioTcpClient*)> on_connect;
|
|
std::function<void (f8::internal::AsioTcpClient*, int)> on_disconnect;
|
|
std::function<void (f8::internal::AsioTcpClient*, char*, int)> on_read;
|
|
|
|
AsioTcpClient(std::shared_ptr<asio::io_context> io_context,
|
|
const std::string& remote_ip,
|
|
int remote_port);
|
|
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, int bufflen);
|
|
|
|
private:
|
|
void Init(std::shared_ptr<asio::io_context> io_context,
|
|
const std::string& remote_ip,
|
|
int remote_port);
|
|
void HandleConnect(const asio::error_code& err);
|
|
void DoRead();
|
|
void DoSend();
|
|
|
|
private:
|
|
std::shared_ptr<asio::io_context> io_context_;
|
|
std::string remote_address_;
|
|
int remote_port_ = 0;
|
|
|
|
std::shared_ptr<asio::ip::tcp::endpoint> endpoint_;
|
|
std::shared_ptr<asio::ip::tcp::socket> socket_;
|
|
volatile bool actived_ = false;
|
|
volatile bool connected_ = false;
|
|
std::shared_ptr<std::mutex> send_buffer_mutex_;
|
|
a8::SendQueueNode *top_node_ = nullptr;
|
|
a8::SendQueueNode *bot_node_ = nullptr;
|
|
volatile a8::SendQueueNode *work_node_ = nullptr;
|
|
volatile bool sending_ = false;
|
|
std::array<char, 1024 * 64> buffer_;
|
|
|
|
void SetActive(bool active);
|
|
void ActiveStart();
|
|
void ActiveStop();
|
|
};
|
|
|
|
}
|
|
|
|
}
|