1
This commit is contained in:
parent
c37b0c22d9
commit
466448796d
@ -15,7 +15,7 @@ const int MAX_RECV_BUFFERSIZE = 1024 * 64;
|
|||||||
namespace a8
|
namespace a8
|
||||||
{
|
{
|
||||||
|
|
||||||
AsioTcpClient::AsioTcpClient(const std::string& remote_ip, int remote_port)
|
AsioTcpClient::AsioTcpClient(asio::io_context& io_context, const std::string& remote_ip, int remote_port)
|
||||||
{
|
{
|
||||||
remote_address_ = remote_ip;
|
remote_address_ = remote_ip;
|
||||||
remote_port_ = remote_port;
|
remote_port_ = remote_port;
|
||||||
@ -25,9 +25,7 @@ namespace a8
|
|||||||
remote_port_
|
remote_port_
|
||||||
);
|
);
|
||||||
send_buffer_mutex_ = std::make_shared<std::mutex>();
|
send_buffer_mutex_ = std::make_shared<std::mutex>();
|
||||||
io_context_ = std::make_shared<asio::io_context>();
|
socket_ = std::make_shared<asio::ip::tcp::socket>(io_context);
|
||||||
socket_ = std::make_shared<asio::ip::tcp::socket>(*io_context_);
|
|
||||||
new std::thread(&AsioTcpClient::WorkerThreadProc, this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AsioTcpClient::~AsioTcpClient()
|
AsioTcpClient::~AsioTcpClient()
|
||||||
@ -184,19 +182,6 @@ namespace a8
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void AsioTcpClient::WorkerThreadProc()
|
|
||||||
{
|
|
||||||
while (true) {
|
|
||||||
try {
|
|
||||||
io_context_->run();
|
|
||||||
io_context_->reset();
|
|
||||||
int i = 0;
|
|
||||||
} catch (std::exception& e) {
|
|
||||||
std::cerr << "Exception: " << e.what() << "\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -16,7 +16,7 @@ namespace a8
|
|||||||
std::function<void (a8::AsioTcpClient*)> on_connect;
|
std::function<void (a8::AsioTcpClient*)> on_connect;
|
||||||
std::function<void (a8::AsioTcpClient*)> on_disconnect;
|
std::function<void (a8::AsioTcpClient*)> on_disconnect;
|
||||||
std::function<void (a8::AsioTcpClient*, char*, unsigned int)> on_socketread;
|
std::function<void (a8::AsioTcpClient*, char*, unsigned int)> on_socketread;
|
||||||
AsioTcpClient(const std::string& remote_ip, int remote_port);
|
AsioTcpClient(asio::io_context& io_context, const std::string& remote_ip, int remote_port);
|
||||||
virtual ~AsioTcpClient();
|
virtual ~AsioTcpClient();
|
||||||
const std::string& GetRemoteAddress() { return remote_address_; }
|
const std::string& GetRemoteAddress() { return remote_address_; }
|
||||||
int GetRemotePort() { return remote_port_; }
|
int GetRemotePort() { return remote_port_; }
|
||||||
@ -31,14 +31,12 @@ namespace a8
|
|||||||
void HandleConnect(const asio::error_code& err);
|
void HandleConnect(const asio::error_code& err);
|
||||||
void DoRead();
|
void DoRead();
|
||||||
void DoSend();
|
void DoSend();
|
||||||
void WorkerThreadProc();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string remote_address_;
|
std::string remote_address_;
|
||||||
int remote_port_ = 0;
|
int remote_port_ = 0;
|
||||||
|
|
||||||
std::shared_ptr<asio::ip::tcp::endpoint> endpoint_;
|
std::shared_ptr<asio::ip::tcp::endpoint> endpoint_;
|
||||||
std::shared_ptr<asio::io_context> io_context_;
|
|
||||||
std::shared_ptr<asio::ip::tcp::socket> socket_;
|
std::shared_ptr<asio::ip::tcp::socket> socket_;
|
||||||
volatile bool actived_ = false;
|
volatile bool actived_ = false;
|
||||||
volatile bool connected_ = false;
|
volatile bool connected_ = false;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user