{"id":13693409,"url":"https://github.com/KumoKyaku/KCP","last_synced_at":"2025-05-02T21:32:01.231Z","repository":{"id":43612506,"uuid":"168439896","full_name":"KumoKyaku/kcp","owner":"KumoKyaku","description":"KCP C#版。线程安全，运行时无alloc，对gc无压力。","archived":false,"fork":false,"pushed_at":"2023-11-16T02:20:29.000Z","size":2240,"stargazers_count":907,"open_issues_count":0,"forks_count":142,"subscribers_count":25,"default_branch":"master","last_synced_at":"2025-04-10T06:24:51.943Z","etag":null,"topics":["csharp-library","dotnet-standard","il2cpp","kcp","no-allocation","nuget","unity"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/KumoKyaku.png","metadata":{"files":{"readme":"README.md","changelog":"ChangeLog.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null},"funding":{"github":null,"custom":["https://github.com/KumoKyaku/KumoKyaku.github.io/blob/develop/source/_posts/%E5%9B%BE%E5%BA%8A/alipay.png","https://paypal.me/kumokyaku?country.x=C2\u0026locale.x=zh_XC"]}},"created_at":"2019-01-31T01:00:22.000Z","updated_at":"2025-04-09T10:14:39.000Z","dependencies_parsed_at":"2023-02-09T09:46:32.773Z","dependency_job_id":"b2f82e2e-d4bf-4f0f-9bdd-b9aac58c3ea9","html_url":"https://github.com/KumoKyaku/kcp","commit_stats":null,"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KumoKyaku%2Fkcp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KumoKyaku%2Fkcp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KumoKyaku%2Fkcp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KumoKyaku%2Fkcp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/KumoKyaku","download_url":"https://codeload.github.com/KumoKyaku/kcp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252108949,"owners_count":21696165,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp-library","dotnet-standard","il2cpp","kcp","no-allocation","nuget","unity"],"created_at":"2024-08-02T17:01:09.694Z","updated_at":"2025-05-02T21:32:00.301Z","avatar_url":"https://github.com/KumoKyaku.png","language":"C#","funding_links":["https://github.com/KumoKyaku/KumoKyaku.github.io/blob/develop/source/_posts/%E5%9B%BE%E5%BA%8A/alipay.png","https://paypal.me/kumokyaku?country.x=C2\u0026locale.x=zh_XC"],"categories":["NetWork","C#"],"sub_categories":[],"readme":"# KCP C#版\n开箱即用。也可以使用 [Nuget](https://www.nuget.org/packages?q=kcp) 搜索。\n\n[![Nuget](https://img.shields.io/nuget/v/Kcp)](https://www.nuget.org/packages/Kcp/)\n\n## Feature：\n\n- 异步API标准接口 IKcpIO.cs\n  - ValueTask Recv(IBufferWriter\u003cbyte\u003e writer, object option = null);\n  - ValueTask Output(IBufferWriter\u003cbyte\u003e writer, object option = null);\n  - 附带一个基本实现。KcpIO.cs\n- kcpSegment泛型化，可由用户自定义高性能实现。\n  - `KcpCore\u003cSegment\u003e`  where Segment : IKcpSegment\n  - `KcpIO\u003cSegment\u003e` : `KcpCore\u003cSegment\u003e`, IKcpIO  where Segment : IKcpSegment\n  - `Kcp\u003cSegment\u003e` : `KcpCore\u003cSegment\u003e` where Segment:IKcpSegment\n\n## 链接：\n\nc: skywind3000 [KCP](https://github.com/skywind3000/kcp)  \ngo: xtaci [kcp-go](https://github.com/xtaci/kcp-go)  \n\n## 说明：\n\n- 内部使用了unsafe代码和非托管内存，不会对gc造成压力。\n- 支持用户自定义内存管理方式,如果不想使用unsafe模式,可以使用内存池.\n- 对于output回调和TryRecv函数。使用RentBuffer回调，从外部分配内存。请参考[IMemoryOwner](https://docs.microsoft.com/en-us/dotnet/standard/memory-and-spans/memory-t-usage-guidelines)用法。\n- 支持`Span\u003cbyte\u003e`\n\n## 线程安全\n简单的说：  \n不能在线程1调用Recv/Update时，线程2也在调用Recv/Update。函数内部使用大量共享数据结构，如果加锁严重影响性能。    \n可以在线程1调用Send/Input时，线程2也在调用Send/Input。函数内部有锁。  \n\n- 可以在任意多线程同时调用Send 和 Input。  \n  多线程同时发送消息是安全的，可以放心的在异步函数中发送消息。    \n- 但`不可以`多个线程同时调用Recv 和 Update。  \n  同名方法仅支持一个线程同时调用，否则会导致多线程错误。  \n\n## 测试：\n在UnitTestProject1路径下执行 dotnet test 可进行多框架测试。（需要安装dotnetcoreSDK）\n\n## 相对C版的一些变化：\n\n| 差异变化         | C版            | C#版                                                  |\n| ---------------- | -------------- | ----------------------------------------------------- |\n| 数据结构         |                |                                                       |\n| acklist          | 数组           | ConcurrentQueue                                       |\n| snd_queue        | 双向链表       | ConcurrentQueue                                       |\n| snd_buf          | 双向链表       | LinkedList                                            |\n| rcv_buf          | 双向链表       | LinkedList                                            |\n| rcv_queue        | 双向链表       | List                                                  |\n| --------------   | -------------- | --------------                                        |\n| 回调函数         |                | 增加了RentBuffer回调，当KCP需要时可以从外部申请内存。 |\n| 多线程           |                | 增加了线程安全。                                      |\n| 流模式           |                | 由于数据结构变动，流模式不会填充当前未发送的最末的数据包。 |\n| interval最小间隔 | 10ms           | 0ms(在特殊形况下允许CPU满负荷运转)                    |\n| --------------   | -------------- | --------------                                        |\n| API变动          |                |                                                       |\n|                  |                | 增加大小端编码设置。默认小端编码。                    |\n|                  |                | 增加TryRecv函数，当可以Recv时只peeksize一次。         |\n|                  | ikcp_ack_push  | 删除了此函数（已内联）                                |\n|                  | ikcp_ack_get   | 删除了此函数（已内联）                                |\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FKumoKyaku%2FKCP","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FKumoKyaku%2FKCP","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FKumoKyaku%2FKCP/lists"}