Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

webconfig/kcp-annotated

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

13 Commits

Repository files navigation

QQ群

因为 KCP 官方群已经满了, 可以加群 496687140

KCP源码注释

本项目是注释版的kcp源码, 算是另一种的 kcp详解, 方便自己学习也为大家更快的上手, 原始代码来自: https://github.com/skywind3000/kcp , 感谢 skywind3000 带来这么短小精悍的好项目

对 FPS / MOBA 类实时对战游戏开源项目感兴趣的读者还可以移步 realtime-server , 欢迎交流

注 : 项目中使用 tab 缩进且设置了tab = 2 space

几乎每个段落都有注释, 且关键数据结构还带有图解, 比如 :

...
//
//	kcp发送的数据包设计了自己的包结构,包头一共24bytes,包含了一些必要的信息,具体内容和大小如下:
//	
//	|<------------ 4 bytes ------------>|
//	+--------+--------+--------+--------+
//	| conv | conv:Conversation, 会话序号,用于标识收发数据包是否一致
//	+--------+--------+--------+--------+ cmd: Command, 指令类型,代表这个Segment的类型
//	| cmd | frg | wnd | frg: Fragment, 分段序号,分段从大到小,0代表数据包接收完毕
//	+--------+--------+--------+--------+ wnd: Window, 窗口大小
//	| ts | ts: Timestamp, 发送的时间戳
//	+--------+--------+--------+--------+
//	| sn | sn: Sequence Number, Segment序号
//	+--------+--------+--------+--------+
//	| una | una: Unacknowledged, 当前未收到的序号,
//	+--------+--------+--------+--------+ 即代表这个序号之前的包均收到
//	| len | len: Length, 后续数据的长度
//	+--------+--------+--------+--------+
//
...
//---------------------------------------------------------------------
// ...
//	rcv_queue	接收消息的队列, rcv_queue的数据是连续的,rcv_buf可能是间隔的
//	nrcv_que // 接收队列rcv_queue中的Segment数量, 需要小于 rcv_wnd
//	rcv_queue 如下图所示
//	+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
//	... | 2 | 3 | 4 | ............................................... 
//	+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
// ^ 	 ^ 	^ 
// | 	| 	| 
// rcv_nxt 	rcv_nxt + nrcv_que rcv_nxt + rcv_wnd		
//
//	snd_buf 发送消息的缓存
//	snd_buf 如下图所示
//	+---+---+---+---+---+---+---+---+---+---+---+---+---+
//	... | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | ...........
//	+---+---+---+---+---+---+---+---+---+---+---+---+---+
// ^ ^ ^
// | | |
// snd_una snd_nxt snd_una + snd_wnd	
//
//
//	rcv_buf 接收消息的缓存
//	rcv_buf 如下图所示, rcv_queue的数据是连续的,rcv_buf可能是间隔的
//	+---+---+---+---+---+---+---+---+---+---+---+---+---+
//	... | 2 | 4 | 6 | 7 | 8 | 9 | ...........
//	+---+---+---+---+---+---+---+---+---+---+---+---+---+	
//
...

在注释的过程中, 除了少量空格和换行以及一处有无符号比较的调整(为保证高警告级别可编译过)外 :
if ((IUINT32)count >= IKCP_WND_RCV) return -2;
没有对原始代码进行任何其他改动, 最大程度地保证了代码的"原汁原味"。

About

kcp annotated. kcp注释版.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C 100.0%

AltStyle によって変換されたページ (->オリジナル) /