Implementation of kcp protocol based on c++11

Overview

🌟 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

You might also like...
Ideas, thoughts, and notes on a typeclass/interface based polymorphism pattern for standard C

Polymorphism through Typeclasses / Interface / Traits Ideas, thoughts, and notes on an action based polymorphism pattern for good ol' C. Originally us

Cmusic source code, based on the CARL 0.2.0 distribution

CMUSIC These are the sources and makefiles for a build of the classic CARL cmusic and related programs. The build has been tested on OSX and Linux, an

Stack based programming language

stacky WIP stack-based compiled concatenative programming language. Currently it's somewhere between B and C programming languages in universe where B

a header-only crossplatform type-safe dynamic compiler generator based on C++ 17.
a header-only crossplatform type-safe dynamic compiler generator based on C++ 17.

Mu Compiler Generator MuCompilerGenerator(MuCplGen) a Header-Only dynamic compiler generator based on C++ 17. Why MuCplGen? header-only cross-platform

Connect 4 clone written with c++ with the RSGL library. Based on my connect 4 clone written in python/pygame and my SDL port of that same repo. Along with 3DS support by SaCode

RSGL-Connect-4 Building linux git clone https://github.com/RSGL-Org/RSGL-Connect-4.git cd RSGL-Connect-4 make ./Connect4 Bulding 3ds (3ds support

A simple token-based programming language written in C++.
A simple token-based programming language written in C++.

Fun Assembly Programming Language About It's a simple token-based programming language written in C++ by Soham Malakar and Sanket Tarafder. Know About

:zap: KCP - A Fast and Reliable ARQ Protocol
:zap: KCP - A Fast and Reliable ARQ Protocol

KCP - A Fast and Reliable ARQ Protocol README in English 简介 KCP是一个快速可靠协议,能以比 TCP 浪费 10%-20% 的带宽的代价,换取平均延迟降低 30%-40%,且最大延迟降低三倍的传输效果。纯算法实现,并不负责底层协议(如UDP

The implementation of the Domino Network protocol based program.

spacex The implementation of the Domino distributed storage network protocol for the Smart Ecology. With a fully featured and well documented that pro

xeus-wren is a Jupyter kernel for wren based on the native implementation of the Jupyter protocol xeus.

xeus-wren is a Jupyter kernel for wren based on the native implementation of the Jupyter protocol xeus. Installation xeus-wren has not been packaged f

Protocol Buffers implementation in C

Overview This is protobuf-c, a C implementation of the Google Protocol Buffers data serialization format. It includes libprotobuf-c, a pure C library

An implementation of the Mobile Adapter GB protocol, to connect to custom game servers.

libmobile Library that implements the Mobile Adapter GB protocol, in a way that should be easy to integrate into a plethora of different emulators/har

Simple HTTP protocol implementation in C

C-SimpleHTTP A simple HTTP protocol implementation in C How do i run it? First of all, this project is made in c, then you will need GCC Compiler. The

This project is implementation of TFTP protocol from scratch

TFTP This project is implementation of TFTP protocol from scratch

A language server protocol implementation

A barebone LSP implementation Starting point for server implementations of the LSP protocol. Provides the infrastructure to easily hook in functionali

mvfst is a client and server implementation of IETF QUIC protocol in C++ by Facebook.
mvfst is a client and server implementation of IETF QUIC protocol in C++ by Facebook.

mvfst (Pronounced move fast) is a client and server implementation of IETF QUIC protocol in C++ by Facebook.

Official Go implementation of the Fixcoin protocol

XFSGO Official Go implementation of the XFS protocol. Usage To learn more about the available xfsgo commands, use xfsgo help or type a command followe

Server side minimalistic HTTP protocol implementation for the Arduino platform.

ArduinoHttpServer Server side minimalistic Object Oriented HTTP protocol implementation for the Arduino platform. ArduinoHttpServer is a simple HTTP r

PTP daemon (PTPd) is an implementation the Precision Time Protocol (PTP) version 2

PTPd PTP daemon (PTPd) is an implementation the Precision Time Protocol (PTP) version 2 as defined by 'IEEE Std 1588-2008'. PTP provides precise time

XQUIC Library released by Alibaba is a cross-platform implementation of QUIC and HTTP/3 protocol.
XQUIC Library released by Alibaba is a cross-platform implementation of QUIC and HTTP/3 protocol.

XQUIC 简体中文文档 README-zh-CN Introduction XQUIC Library released by Alibaba is … … a client and server implementation of QUIC and HTTP/3 as specified by

Owner
KH
 KH
LRU implementation exercises

LRU implementation exercises

Tsoding 12 Jul 3, 2022
Implementation using C in programming lab class when being Teacher Assistant.

C Programming Lab Implementation using C in programming lab class when being Teacher Assistant. The problems are here Problems and Description Week2 w

Tsuen Hsueh 1 Aug 16, 2022
This is a simple UNITEST to test the implementation of the the various container types of the C++ standard template library

ft_container UNITest. This is a simple UNITEST to test the implementation of the the various container types of the C++ standard template library that

Mohamed AMOUSSAOUI 46 Dec 27, 2022
Jinja2 C++ (and for C++) almost full-conformance template engine implementation

Jinja2С++ C++ implementation of the Jinja2 Python template engine. This library brings support of powerful Jinja2 template features into the C++ world

Jinja2C++ project 385 Dec 17, 2022
Imu_initialization - Implementation of "An Analytical Solution to the IMU Initialization Problem for Visual-Inertial Systems"

An Analytical Solution to the IMU Initialization Problem for Visual-Inertial Systems Implementation of "An Analytical Solution to the IMU Initializati

David Zuniga-Noel 94 Nov 23, 2022
wideint is a C++ implementation of wide exact-width integer types.

wideint - wide exact-width integer types Copyright (c) 2022 Joergen Ibsen About wideint is a C++ implementation of wide exact-width integer types. #in

Jørgen Ibsen 3 Jan 27, 2022
Code profiler based on Frida

Code Profiler Based on Frida This repository contains the code to profile LIEF functions with Frida. Get Started Make sure to download the right versi

LIEF 26 Sep 12, 2022
A curated list of project-based tutorials in C

A list of tutorials that work towards the making of small to large projects in C.

R 8.3k Feb 13, 2022
Ubpa small flat containers based on C++20

USmallFlat Ubpa small flat containers based on C++20 Containers basic_flat_map basic_flat_multimap basic_flat_multiset basic_flat_set basic_small_vect

Ubpa 20 Nov 8, 2022
λQ: A Simple Quantum Programming Language based on QWIRE.

λQ λQ: A Simple Language based on QWIRE which can be compiled to QASM. The name λQ means lambda calculus with quantum circuits. This is a term project

Wenhao Tang 5 Jul 11, 2021