* udp步进
This commit is contained in:
+62
-25
@@ -144,12 +144,14 @@ public:
|
||||
class ServerUdpImp
|
||||
{
|
||||
public:
|
||||
ServerUdp* _parent;
|
||||
asio::io_context _ioContext;
|
||||
asio::ip::udp::socket _socket;
|
||||
asio::ip::udp::endpoint _endpoint; // 接收到后被填充
|
||||
Buffer _buf;
|
||||
|
||||
ServerUdpImp() :
|
||||
ServerUdpImp(ServerUdp* parent) :
|
||||
_parent{ parent },
|
||||
_socket{ _ioContext }
|
||||
{
|
||||
_buf.resize(DEFAULT_BUFFER_SIZE);
|
||||
@@ -195,7 +197,12 @@ public:
|
||||
对于不可靠的协议(例如 UDP),多余的数据会丢失;
|
||||
对于可靠协议,数据由服务提供商保留,直到通过调用具有足够大缓冲区的 RECV 成功读取数据。*/
|
||||
|
||||
// todo
|
||||
UdpMessage msg;
|
||||
msg._buf = _buf;
|
||||
msg._ip = _endpoint.address().to_string();
|
||||
msg._port = _endpoint.port();
|
||||
_parent->_listMsg.Add(msg);
|
||||
|
||||
#ifdef DL_NETWORK_DEBUG_INFO
|
||||
log_msg("ServerUDP Recv << [{}:{}]:\n{}", _endpoint.address().to_string(), _endpoint.port(),
|
||||
to_string_auto(CreateBufferView(_buf, length)));
|
||||
@@ -259,7 +266,7 @@ bool Client::Connent()
|
||||
|
||||
void Client::Send(BufferView buf)
|
||||
{
|
||||
_bufList.Add(buf);
|
||||
_bufList.Add(CreateBuffer(buf));
|
||||
}
|
||||
|
||||
Client::Client() :
|
||||
@@ -283,25 +290,24 @@ void Client::_thread_run(void* p)
|
||||
|
||||
while (!c->_bEnd)
|
||||
{
|
||||
if(!c->_bufList.Begin())
|
||||
Buffer* buf = c->_bufList.Beg();
|
||||
if(!buf)
|
||||
continue;
|
||||
|
||||
//
|
||||
std::error_code ec;
|
||||
|
||||
Buffer& buf = c->_bufList.Get();
|
||||
MsgHead head;
|
||||
head._magic = NETWORK_MESSAGE_TAG;
|
||||
head._size = (uint32_t)buf.size();
|
||||
head._size = (uint32_t)buf->size();
|
||||
head._check = 0;
|
||||
head._compress = 0;
|
||||
|
||||
asio::write(c->_imp->_socket, asio::buffer(&head, sizeof(MsgHead)));
|
||||
size_t size = asio::write(c->_imp->_socket, asio::buffer(buf));
|
||||
assert(size == buf.size());
|
||||
size_t size = asio::write(c->_imp->_socket, asio::buffer(*buf));
|
||||
assert(size == buf->size());
|
||||
|
||||
c->_bufList.End();
|
||||
//std::this_thread::sleep_for(std::chrono::milliseconds(0));
|
||||
}
|
||||
}
|
||||
//--------------------------------------Tcp Server
|
||||
@@ -359,7 +365,8 @@ void Server::_thread_run(void* p)
|
||||
|
||||
ClientUdp::ClientUdp() :
|
||||
_bEnd{ false },
|
||||
_t{ nullptr }
|
||||
_tSend{ nullptr },
|
||||
_tRecv{ nullptr }
|
||||
{
|
||||
_imp = new ClientUdpImp();
|
||||
}
|
||||
@@ -367,7 +374,8 @@ ClientUdp::ClientUdp() :
|
||||
ClientUdp::~ClientUdp()
|
||||
{
|
||||
_bEnd = true;
|
||||
delete _t;
|
||||
delete _tSend;
|
||||
delete _tRecv;
|
||||
delete _imp;
|
||||
}
|
||||
|
||||
@@ -380,31 +388,62 @@ void ClientUdp::SetAddress(std::string_view ip, uint16_t port, bool broadcast /*
|
||||
|
||||
void ClientUdp::Send(BufferView buf)
|
||||
{
|
||||
if (!_t)
|
||||
if (!_tSend)
|
||||
{
|
||||
_t = g_thread->Create();
|
||||
_t->Start(_thread_run, this);
|
||||
_tSend = g_thread->Create();
|
||||
_tSend->Start(_thread_send, this);
|
||||
|
||||
_tRecv = g_thread->Create();
|
||||
_tRecv->Start(_thread_recv, this);
|
||||
}
|
||||
_bufList.Add(buf);
|
||||
_bufListSend.Add(CreateBuffer(buf));
|
||||
}
|
||||
|
||||
void ClientUdp::_thread_run(void* p)
|
||||
void ClientUdp::_thread_send(void* p)
|
||||
{
|
||||
// 子线程
|
||||
ClientUdp* c = (ClientUdp*)p;
|
||||
|
||||
while (!c->_bEnd)
|
||||
{
|
||||
if(!c->_bufList.Begin())
|
||||
Buffer* buf = c->_bufListSend.Beg();
|
||||
if(!buf)
|
||||
continue;
|
||||
//
|
||||
std::error_code ec;
|
||||
|
||||
Buffer& buf = c->_bufList.Get();
|
||||
size_t size;
|
||||
try
|
||||
{
|
||||
size = c->_imp->_socket.send_to(asio::buffer(buf), c->_imp->_endpoint);
|
||||
size = c->_imp->_socket.send_to(asio::buffer(*buf), c->_imp->_endpoint);
|
||||
}
|
||||
catch (std::system_error& e)
|
||||
{
|
||||
log_err0(e);
|
||||
}
|
||||
assert(size == buf->size());
|
||||
|
||||
#ifdef DL_NETWORK_DEBUG_INFO
|
||||
log_msg("ClientUdp Send >> [{}:{}]:\n{}", c->_imp->_endpoint.address().to_string(), c->_imp->_endpoint.port(),
|
||||
to_string_auto(*buf));
|
||||
#endif
|
||||
|
||||
c->_bufListSend.End();
|
||||
}
|
||||
}
|
||||
|
||||
void ClientUdp::_thread_recv(void* p)
|
||||
{
|
||||
// 子线程
|
||||
ClientUdp* c = (ClientUdp*)p;
|
||||
|
||||
while (!c->_bEnd)
|
||||
{
|
||||
std::error_code ec;
|
||||
Buffer buf;
|
||||
size_t size;
|
||||
try
|
||||
{
|
||||
size = c->_imp->_socket.receive_from(asio::buffer(buf), c->_imp->_endpoint);
|
||||
}
|
||||
catch (std::system_error& e)
|
||||
{
|
||||
@@ -413,12 +452,10 @@ void ClientUdp::_thread_run(void* p)
|
||||
assert(size == buf.size());
|
||||
|
||||
#ifdef DL_NETWORK_DEBUG_INFO
|
||||
log_msg("ClientUdp Send >> [{}:{}]:\n{}", c->_imp->_endpoint.address().to_string(), c->_imp->_endpoint.port(),
|
||||
log_msg("ClientUdp Recv << [{}:{}]:\n{}", c->_imp->_endpoint.address().to_string(), c->_imp->_endpoint.port(),
|
||||
to_string_auto(buf));
|
||||
#endif
|
||||
|
||||
c->_bufList.End();
|
||||
//std::this_thread::sleep_for(std::chrono::milliseconds(0));
|
||||
c->_bufListRecv.Add(buf);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -434,7 +471,7 @@ void ServerUdp::Start(std::string_view ip, uint16_t port)
|
||||
|
||||
ServerUdp::ServerUdp()
|
||||
{
|
||||
_imp = new ServerUdpImp;
|
||||
_imp = new ServerUdpImp(this);
|
||||
}
|
||||
|
||||
ServerUdp::~ServerUdp()
|
||||
|
||||
+35
-21
@@ -31,25 +31,29 @@ struct MsgHead
|
||||
bool _compress;
|
||||
};
|
||||
|
||||
struct BufferList
|
||||
struct UdpMessage
|
||||
{
|
||||
std::string _ip;
|
||||
uint16_t _port;
|
||||
Buffer _buf;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct ListMultiThread
|
||||
{
|
||||
std::mutex _mutex;// 队列锁
|
||||
std::list<Buffer> _list;// 先入先出队列
|
||||
std::list<Buffer> _listTemp;// 主线程缓冲
|
||||
std::list<T> _list;// 先入先出队列
|
||||
std::list<T> _listTemp;// 主线程缓冲
|
||||
|
||||
bool Begin()
|
||||
T* Beg()
|
||||
{
|
||||
_mutex.lock();
|
||||
if (_list.empty())
|
||||
{
|
||||
_mutex.unlock();
|
||||
return false;
|
||||
return nullptr;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
Buffer& Get()
|
||||
{
|
||||
return _list.front();
|
||||
return &_list.front();
|
||||
}
|
||||
void End()
|
||||
{
|
||||
@@ -57,18 +61,21 @@ struct BufferList
|
||||
_mutex.unlock();
|
||||
}
|
||||
|
||||
void Add(BufferView buf)
|
||||
// todo 左值引用
|
||||
void Add(const T& buf)
|
||||
{
|
||||
if (_mutex.try_lock())
|
||||
{
|
||||
_list.splice(_list.begin(), _listTemp);
|
||||
_list.push_back(CreateBuffer(buf));
|
||||
_list.push_back(buf);
|
||||
_mutex.unlock();
|
||||
return;
|
||||
}
|
||||
_listTemp.push_back(CreateBuffer(buf));
|
||||
_listTemp.push_back(buf);
|
||||
}
|
||||
};
|
||||
using BufferList = ListMultiThread<Buffer>;
|
||||
using UdpMessageList = ListMultiThread<UdpMessage>;
|
||||
|
||||
class ClientImp;
|
||||
class Client
|
||||
@@ -111,32 +118,39 @@ class ClientUdp
|
||||
public:
|
||||
void SetAddress(std::string_view ip, uint16_t port, bool broadcast = false);
|
||||
void Send(BufferView buf);
|
||||
size_t GetMsgCount() { return 0; }
|
||||
Buffer& GetPopMsg() { return _bufList._list.front(); }
|
||||
void PopMsg() {}
|
||||
Buffer* GetPopMsg() { return _bufListRecv.Beg(); }
|
||||
void PopMsg() { _bufListRecv.End(); }
|
||||
|
||||
ClientUdp();
|
||||
~ClientUdp();
|
||||
private:
|
||||
ClientUdpImp* _imp;
|
||||
Thread* _t;
|
||||
ClientUdpImp* _imp;
|
||||
std::atomic_bool _bEnd;
|
||||
BufferList _bufList;
|
||||
Thread* _tSend;
|
||||
Thread* _tRecv;
|
||||
BufferList _bufListSend;
|
||||
BufferList _bufListRecv;
|
||||
|
||||
static void _thread_run(void*);// 主循环
|
||||
static void _thread_send(void*);
|
||||
static void _thread_recv(void*);
|
||||
};
|
||||
|
||||
class ServerUdpImp;
|
||||
class ServerUdp
|
||||
{
|
||||
friend class ServerUdpImp;
|
||||
public:
|
||||
void Start(std::string_view ip, uint16_t port);
|
||||
|
||||
UdpMessage* GetPopMsg() { return _listMsg.Beg(); }
|
||||
void PopMsg() { _listMsg.End(); }
|
||||
|
||||
ServerUdp();
|
||||
~ServerUdp();
|
||||
private:
|
||||
ServerUdpImp* _imp;
|
||||
Thread* _t;
|
||||
Thread* _t;
|
||||
UdpMessageList _listMsg;
|
||||
|
||||
static void _thread_run(void*);
|
||||
};
|
||||
|
||||
+1
-1
Submodule project/dl_chess updated: 43e05b09ba...a12a429d39
Reference in New Issue
Block a user