1
This commit is contained in:
parent
60f3ef1f7c
commit
ed93a7c237
@ -3,6 +3,7 @@
|
||||
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <iostream>
|
||||
|
||||
#include <a8/a8.h>
|
||||
#include <a8/asiotcpclient.h>
|
||||
@ -14,8 +15,15 @@ const int MAX_RECV_BUFFERSIZE = 1024 * 64;
|
||||
namespace a8
|
||||
{
|
||||
|
||||
AsioTcpClient::AsioTcpClient()
|
||||
AsioTcpClient::AsioTcpClient(const std::string& remote_ip, int remote_port)
|
||||
{
|
||||
remote_address_ = remote_ip;
|
||||
remote_port_ = remote_port;
|
||||
endpoint_ = std::make_shared<asio::ip::tcp::endpoint>
|
||||
(
|
||||
asio::ip::make_address(remote_address_),
|
||||
remote_port_
|
||||
);
|
||||
send_buffer_mutex_ = std::make_shared<std::mutex>();
|
||||
io_context_ = std::make_shared<asio::io_context>();
|
||||
resolver_ = std::make_shared<asio::ip::tcp::resolver>(*io_context_);
|
||||
@ -90,17 +98,15 @@ namespace a8
|
||||
{
|
||||
actived_ = true;
|
||||
connected_ = false;
|
||||
tcp::resolver::results_type endpoints = resolver_->resolve
|
||||
(tcp::v4(),
|
||||
remote_address.c_str(),
|
||||
a8::XValue(remote_port).GetString().c_str());
|
||||
|
||||
#if 0
|
||||
asio::async_connect(*socket_, endpoints,
|
||||
[this] (const asio::error_code& ec,
|
||||
const tcp::endpoint& endpoint)
|
||||
{
|
||||
HandleConnect(ec, endpoint);
|
||||
});
|
||||
#endif
|
||||
}
|
||||
|
||||
void AsioTcpClient::ActiveStop()
|
||||
@ -185,8 +191,12 @@ namespace a8
|
||||
void AsioTcpClient::WorkerThreadProc()
|
||||
{
|
||||
while (true) {
|
||||
io_context_->run();
|
||||
int i = 0;
|
||||
try {
|
||||
io_context_->run();
|
||||
int i = 0;
|
||||
} catch (std::exception& e) {
|
||||
std::cerr << "Exception: " << e.what() << "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -16,11 +16,10 @@ namespace a8
|
||||
std::function<void (a8::AsioTcpClient*)> on_connect;
|
||||
std::function<void (a8::AsioTcpClient*)> on_disconnect;
|
||||
std::function<void (a8::AsioTcpClient*, char*, unsigned int)> on_socketread;
|
||||
std::string remote_address;
|
||||
int remote_port = 0;
|
||||
|
||||
AsioTcpClient();
|
||||
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();
|
||||
@ -35,6 +34,10 @@ namespace a8
|
||||
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::resolver> resolver_;
|
||||
std::shared_ptr<asio::ip::tcp::socket> socket_;
|
||||
|
Loading…
x
Reference in New Issue
Block a user