diff --git a/dl/io/dl_net.cpp b/dl/io/dl_net.cpp index 69dbc53f..daffeead 100644 --- a/dl/io/dl_net.cpp +++ b/dl/io/dl_net.cpp @@ -76,13 +76,12 @@ public: std::list _allConnect; ServerImp() : - _acceptor{ _ioContext , asio::ip::tcp::endpoint{asio::ip::tcp::v4(), 7000} } + _acceptor{ _ioContext } { } ~ServerImp() { - _ioContext.stop(); for (auto& iter : _allConnect) { delete iter; @@ -146,7 +145,7 @@ public: ServerUdp* _parent; asio::io_context _ioContext; asio::ip::udp::socket _socket; - asio::ip::udp::endpoint _endpoint; // 接收到后被填充 + asio::ip::udp::endpoint _endpoint; // 目标,接收到后被填充 Buffer _buf; ServerUdpImp(ServerUdp* parent) : @@ -184,7 +183,7 @@ public: } catch (std::exception& e) { - log_err("{}", e.what()); + log_err0(e); } return false; } @@ -207,11 +206,6 @@ public: #endif _read(); } - - void _stop() - { - _ioContext.stop(); - } }; //---------------------------------------Implement-------------------------------------- @@ -244,16 +238,16 @@ std::vector Network::GetIpAddressLocal(bool ipv4 /*= true*/) } //--------------------------------------Tcp Client -bool Client::Connent() +bool Client::Connent(std::string_view ip, uint16_t port) { try { asio::ip::tcp::resolver r{ _imp->_ioContext }; - asio::ip::tcp::resolver::query q{ "127.0.0.1", "7000" }; + asio::ip::tcp::resolver::query q{ ip.data(), std::to_string(port) }; asio::ip::tcp::resolver::results_type vec_ed = r.resolve(q); - connect(_imp->_socket, vec_ed); + asio::connect(_imp->_socket, vec_ed); _t = g_thread->Create(); _t->Start(_thread_run, this); @@ -262,7 +256,7 @@ bool Client::Connent() } catch (std::exception& e) { - log_err("{}", dl::CodeCvt::Utf8ToMultiByte(e.what())); + log_err0(e); } return false; } @@ -314,10 +308,12 @@ void Client::_thread_run(void* p) } } //--------------------------------------Tcp Server -bool Server::Accept() +bool Server::Accept(uint16_t port) { try { + _imp->_acceptor = asio::ip::tcp::acceptor{ _imp->_ioContext, + asio::ip::tcp::endpoint{asio::ip::tcp::v4(), port} }; _imp->_accept(); return true; } @@ -334,6 +330,16 @@ void Server::Run() _t->Start(_thread_run, this); } +std::string Server::GetIp() +{ + return _imp->_acceptor.local_endpoint().address().to_string(); +} + +uint16_t Server::GetPort() +{ + return _imp->_acceptor.local_endpoint().port(); +} + Server::Server() : _t{ nullptr } { @@ -342,8 +348,10 @@ Server::Server() : Server::~Server() { - delete _imp; + _imp->_ioContext.stop(); + // 先删除线程,等待执行完后,再删自己 delete _t; + delete _imp; } void Server::_thread_run(void* p) @@ -476,12 +484,12 @@ void ServerUdp::Start(std::string_view ip, uint16_t port) std::string ServerUdp::GetIp() { - return _imp->_endpoint.address().to_string(); + return _imp->_socket.local_endpoint().address().to_string(); } uint16_t ServerUdp::GetPort() { - return _imp->_endpoint.port(); + return _imp->_socket.local_endpoint().port(); } ServerUdp::ServerUdp() @@ -491,7 +499,7 @@ ServerUdp::ServerUdp() ServerUdp::~ServerUdp() { - _imp->_stop(); + _imp->_ioContext.stop(); // 先删除线程,等待执行完后,再删自己 delete _t; delete _imp; diff --git a/dl/io/dl_net.h b/dl/io/dl_net.h index bc63ce1d..647b3560 100644 --- a/dl/io/dl_net.h +++ b/dl/io/dl_net.h @@ -82,7 +82,7 @@ class ClientImp; class Client { public: - bool Connent(); + bool Connent(std::string_view ip, uint16_t port); void Send(BufferView buf); Client(); @@ -102,9 +102,12 @@ class ServerImp; class Server { public: - bool Accept(); + bool Accept(uint16_t port); void Run(); + std::string GetIp(); + uint16_t GetPort(); + Server(); ~Server(); private: diff --git a/project/dl_chess b/project/dl_chess index c2482952..40b261e3 160000 --- a/project/dl_chess +++ b/project/dl_chess @@ -1 +1 @@ -Subproject commit c2482952c29123589cac1b96626128120ea2ef47 +Subproject commit 40b261e30a0166bd735592c3819d72971e991525 diff --git a/project/dl_chess_server b/project/dl_chess_server index 4191b8e1..10538dcd 160000 --- a/project/dl_chess_server +++ b/project/dl_chess_server @@ -1 +1 @@ -Subproject commit 4191b8e1e7de8b3cad1bec30a83f02b3acabc87b +Subproject commit 10538dcd2baab96036268318db6c6e11f61d7112