Skip to content

Commit

Permalink
update kcp code.
Browse files Browse the repository at this point in the history
  • Loading branch information
freeeyes committed Jun 29, 2024
1 parent 65ed4b2 commit 1818095
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
11 changes: 10 additions & 1 deletion PSS_ASIO/UdpSession/KcpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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());
Expand Down
16 changes: 13 additions & 3 deletions PSS_ASIO/UdpSession/KcpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,15 @@ class CKcp_Session_Info
ikcpcb* kcpcb_ = nullptr;

template<class T>
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;
kcpcb_ = ikcp_create(kcp_id_, run_class);
if (nullptr == kcpcb_)
{
PSS_LOGGER_DEBUG("[init_kcp]connect_id={0} kcp is null.", kcp_id_);
return false;
}
else
{
Expand All @@ -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);
}
}

Expand Down

0 comments on commit 1818095

Please sign in to comment.