1 Star 0 Fork 3

damoio / kcp-cpp

forked from wkh / kcp-cpp 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

🌟 kcp-cpp

A C++11 header-only kcp library,It has been heavily optimized to support native heartbeat packets and multithreading


There are a lot of instructions in KcpOpt on how to configure it.

Configure speed mode

    kcp::KcpOpt opt;
    opt.conv                = conv;
    opt.interval            = 10;
    opt.nodelay             = true;
    opt.trigger_fast_resend = 5;

Use KCP with a single thread

    kcp::Kcp<> kcp(opt);

Call Update every 10 - 100ms repeatedly

    kcp.Update(current);

💥 multi-thread

Benefit by the c++11 feature, kcp-cpp can be used with multiple threads, but with no additional overhead when used in only single thread.

You only need to set the template parameter to true to use it in multi threads.

    kcp::Kcp<true> kcp(opt);

Thie means you can call Update,Send,Recv,Input with multi threads

// thread1
   while(1){ kcp.Update(current);}
// thread2
   kcp.Send(buf,size);

🚀 ChatRoom

In the example directory, a chat room example shows you how to write a concurrent kcpServer on Linux

Build it

./autobuild

Start ChatServer

cd bin
./ChatServer server_ip server_port

Start ChatClient

./ChatClient server_ip server_port

This example will teach you how to use kcp-cpp to write an application, use conv to identify the client without reconnecting even if the client's network changes

🎆 Congesition Control

In order to make it more convenient for users to customize the congestion control algorithm, so I separate its interface

    class CongestionControl{
        //@brief: slow start algorithm 
        virtual void SlowStart(uint32_t &cwnd, uint32_t &ssthresh);
        //@brief: algorithm when packet loss occurs
        virtual void Lost(uint32_t &cwnd, uint32_t &ssthresh);
        //@brief: congesion avoidance algorithm
        virtual void CongestionAvoidance(uint32_t &cwnd, uint32_t &ssthresh);
        //@brief: quick recover algorithm 
        virtual void QuickRecover(uint32_t &cwnd, uint32_t &ssthresh,uint32_t fast_resend_trigger);
    };

Users can easily customize congestion control by simply inheriting it and implementing these interfaces

    class Control : public CongestionContron{};
    KcpOpt opt;
    opt.use_congestion = true;
    Kcp<> kcp(opt);
    kcp.SetControl(new Control());

🌔 Sliding Window

In order to slove the problem of reliable transmission and packet disorder,every Kcp object contains a send window and a receive window.Next,let's demonstate the whole sending and receiving process.

In the initial state,the sender send the packets 1 to 4.The left of the send window boundary is snd_unack_,which points to packet 1.The right of the send window boundary is snd_next_,which points to packet 5 to be sent.

image

Unfortunately,packet 1 was lost during transmission,so the receiver only received packets 2 to 4.the recevier inserts packets 2 to 4 into corresponding position and replies ack to sender.The left boundary of receive window can't move,because packet 1 wasn't received.

image

After the sender receive the ack of packets 2 to 4,it wiil delete packets 2 to 4 in buffer.However packet 1 will be resent due to timeout.

image

The winow of receiver can move right after receive packet 1 and hand the data to application orderly.Then start the transmission next.

image

MIT License Copyright (c) 2021 wkh Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

基于c++11实现kcp协议 展开 收起
C++
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/damoio/kcp-cpp.git
git@gitee.com:damoio/kcp-cpp.git
damoio
kcp-cpp
kcp-cpp
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891