1
This commit is contained in:
parent
c37b0c22d9
commit
466448796d
@ -15,7 +15,7 @@ const int MAX_RECV_BUFFERSIZE = 1024 * 64;
|
||||
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_port_ = remote_port;
|
||||
@ -25,9 +25,7 @@ namespace a8
|
||||
remote_port_
|
||||
);
|
||||
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_);
|
||||
new std::thread(&AsioTcpClient::WorkerThreadProc, this);
|
||||
socket_ = std::make_shared<asio::ip::tcp::socket>(io_context);
|
||||
}
|
||||
|
||||
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
|
||||
|
@ -16,7 +16,7 @@ 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;
|
||||
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();
|
||||
const std::string& GetRemoteAddress() { return remote_address_; }
|
||||
int GetRemotePort() { return remote_port_; }
|
||||
@ -31,14 +31,12 @@ namespace a8
|
||||
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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user