1
This commit is contained in:
parent
60f3ef1f7c
commit
ed93a7c237
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
#include <a8/a8.h>
|
#include <a8/a8.h>
|
||||||
#include <a8/asiotcpclient.h>
|
#include <a8/asiotcpclient.h>
|
||||||
@ -14,8 +15,15 @@ const int MAX_RECV_BUFFERSIZE = 1024 * 64;
|
|||||||
namespace a8
|
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>();
|
send_buffer_mutex_ = std::make_shared<std::mutex>();
|
||||||
io_context_ = std::make_shared<asio::io_context>();
|
io_context_ = std::make_shared<asio::io_context>();
|
||||||
resolver_ = std::make_shared<asio::ip::tcp::resolver>(*io_context_);
|
resolver_ = std::make_shared<asio::ip::tcp::resolver>(*io_context_);
|
||||||
@ -90,17 +98,15 @@ namespace a8
|
|||||||
{
|
{
|
||||||
actived_ = true;
|
actived_ = true;
|
||||||
connected_ = false;
|
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,
|
asio::async_connect(*socket_, endpoints,
|
||||||
[this] (const asio::error_code& ec,
|
[this] (const asio::error_code& ec,
|
||||||
const tcp::endpoint& endpoint)
|
const tcp::endpoint& endpoint)
|
||||||
{
|
{
|
||||||
HandleConnect(ec, endpoint);
|
HandleConnect(ec, endpoint);
|
||||||
});
|
});
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void AsioTcpClient::ActiveStop()
|
void AsioTcpClient::ActiveStop()
|
||||||
@ -185,8 +191,12 @@ namespace a8
|
|||||||
void AsioTcpClient::WorkerThreadProc()
|
void AsioTcpClient::WorkerThreadProc()
|
||||||
{
|
{
|
||||||
while (true) {
|
while (true) {
|
||||||
|
try {
|
||||||
io_context_->run();
|
io_context_->run();
|
||||||
int i = 0;
|
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_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;
|
||||||
std::string remote_address;
|
AsioTcpClient(const std::string& remote_ip, int remote_port);
|
||||||
int remote_port = 0;
|
|
||||||
|
|
||||||
AsioTcpClient();
|
|
||||||
virtual ~AsioTcpClient();
|
virtual ~AsioTcpClient();
|
||||||
|
const std::string& GetRemoteAddress() { return remote_address_; }
|
||||||
|
int GetRemotePort() { return remote_port_; }
|
||||||
|
|
||||||
void Open();
|
void Open();
|
||||||
void Close();
|
void Close();
|
||||||
@ -35,6 +34,10 @@ namespace a8
|
|||||||
void WorkerThreadProc();
|
void WorkerThreadProc();
|
||||||
|
|
||||||
private:
|
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::io_context> io_context_;
|
||||||
std::shared_ptr<asio::ip::tcp::resolver> resolver_;
|
std::shared_ptr<asio::ip::tcp::resolver> resolver_;
|
||||||
std::shared_ptr<asio::ip::tcp::socket> socket_;
|
std::shared_ptr<asio::ip::tcp::socket> socket_;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user