From 181809578c42f507b1972350cf1f546072595995 Mon Sep 17 00:00:00 2001 From: freeeyes Date: Sat, 29 Jun 2024 19:58:09 +0800 Subject: [PATCH] update kcp code. --- PSS_ASIO/UdpSession/KcpServer.cpp | 11 ++++++++++- PSS_ASIO/UdpSession/KcpServer.h | 16 +++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/PSS_ASIO/UdpSession/KcpServer.cpp b/PSS_ASIO/UdpSession/KcpServer.cpp index f8be371..e41b974 100644 --- a/PSS_ASIO/UdpSession/KcpServer.cpp +++ b/PSS_ASIO/UdpSession/KcpServer.cpp @@ -74,6 +74,11 @@ void CKcpServer::do_receive_from(std::error_code ec, std::size_t length) { //查询当前的connect_id auto connect_id = add_udp_endpoint(recv_endpoint_, length, max_send_size_); + if (-1 == connect_id) + { + //初始化kcp函数失败 + return; + } auto session_kcp = find_udp_endpoint_by_id(connect_id); @@ -336,7 +341,11 @@ uint32 CKcpServer::add_udp_endpoint(const udp::endpoint& recv_endpoint, size_t l kcp_output_func output_func = kcp_udpOutPut; //创建KCP - session_info->init_kcp(connect_id, max_recv_size_, max_send_size_, output_func, this); + auto init_ret = session_info->init_kcp(connect_id, max_recv_size_, max_send_size_, output_func, this); + if (false == init_ret) + { + return -1; + } //刷新一下时间 ikcp_update(session_info->kcpcb_, iclock()); diff --git a/PSS_ASIO/UdpSession/KcpServer.h b/PSS_ASIO/UdpSession/KcpServer.h index dc72e76..c0566af 100644 --- a/PSS_ASIO/UdpSession/KcpServer.h +++ b/PSS_ASIO/UdpSession/KcpServer.h @@ -59,7 +59,7 @@ class CKcp_Session_Info ikcpcb* kcpcb_ = nullptr; template - void init_kcp(uint32 kcp_id, uint32 max_send_size, uint32 max_recv_size, kcp_output_func output_func, T* run_class) + bool init_kcp(uint32 kcp_id, uint32 max_send_size, uint32 max_recv_size, kcp_output_func output_func, T* run_class) { //建立kcp kcp_id_ = kcp_id; @@ -67,6 +67,7 @@ class CKcp_Session_Info if (nullptr == kcpcb_) { PSS_LOGGER_DEBUG("[init_kcp]connect_id={0} kcp is null.", kcp_id_); + return false; } else { @@ -75,10 +76,19 @@ class CKcp_Session_Info if (ptr) { kcpcb_->output = *ptr; + + ikcp_nodelay(kcpcb_, 0, 10, 0, 0); + ikcp_wndsize(kcpcb_, max_send_size, max_recv_size); + return true; + } + else + { + PSS_LOGGER_DEBUG("[init_kcp]connect_id={0} Failed to convert output_func to function pointer.", kcp_id_); + ikcp_release(kcpcb_); + kcpcb_ = nullptr; + return false; } - ikcp_nodelay(kcpcb_, 0, 10, 0, 0); - ikcp_wndsize(kcpcb_, max_send_size, max_recv_size); } }