* net增加接口
This commit is contained in:
+26
-18
@@ -76,13 +76,12 @@ public:
|
||||
std::list<ServerConnect*> _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<std::string> 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;
|
||||
|
||||
+5
-2
@@ -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:
|
||||
|
||||
+1
-1
Submodule project/dl_chess updated: c2482952c2...40b261e30a
+1
-1
Submodule project/dl_chess_server updated: 4191b8e1e7...10538dcd2b
Reference in New Issue
Block a user