# skywind3000/kcp architecture

> Architecture diagram of the GitHub repository skywind3000/kcp, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-23). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/skywind3000/kcp
- Repository: https://github.com/skywind3000/kcp

## Overview

KCP is a lightweight, reliable ARQ protocol library, not a socket implementation: applications provide packet transport and the clock. The main workflow sends application data through KCP, emits packets via a caller-supplied output callback, accepts received packets back through KCP, and exposes reassembled data to the application. Its core also handles ACKs, retransmission, receive ordering, and configurable flow/congestion control. The excerpts show some internal queue and retransmission behavior; other workflow links are documented in the README.

Main entry points: [`ikcp.h`](https://github.com/skywind3000/kcp/blob/master/ikcp.h) (KCP API), [`ikcp.c`](https://github.com/skywind3000/kcp/blob/master/ikcp.c) (ACK and retransmit)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["Application API"]
  node_api_surface["KCP API<br/>[ikcp.h]"]
end

subgraph group_core["ARQ protocol core"]
  node_session["Session state<br/>[ikcp.c]"]
  node_send_queue["Send queue<br/>[ikcp.c]"]
  node_arq["ACK and retransmit<br/>[ikcp.c]"]
  node_receive["Receive ordering<br/>[ikcp.c]"]
  node_flow_control["Flow control<br/>[ikcp.c]"]
end

subgraph group_host["Host integration"]
  node_output_callback["Output callback<br/>[ikcp.c]"]
  node_scheduler["Update scheduler<br/>[ikcp.c]"]
end

node_application(("Application"))
node_clock(("Host clock"))
node_transport(("Packet transport"))

node_application -->|"sends data"| node_api_surface
node_api_surface -->|"creates session"| node_session
node_api_surface -->|"queues data"| node_send_queue
node_send_queue -.->|"segments data"| node_arq
node_arq -->|"emits packets"| node_output_callback
node_output_callback -->|"sends packets"| node_transport
node_transport -->|"supplies packets"| node_api_surface
node_api_surface -->|"inputs packets"| node_receive
node_receive -.->|"processes ACKs"| node_arq
node_receive -->|"delivers data"| node_application
node_clock -->|"provides time"| node_scheduler
node_scheduler -->|"updates state"| node_arq
node_flow_control -.->|"limits sending"| node_arq
node_api_surface -->|"configures control"| node_flow_control

click node_api_surface "https://github.com/skywind3000/kcp/blob/master/ikcp.h"
click node_session "https://github.com/skywind3000/kcp/blob/master/ikcp.c"
click node_send_queue "https://github.com/skywind3000/kcp/blob/master/ikcp.c"
click node_arq "https://github.com/skywind3000/kcp/blob/master/ikcp.c"
click node_receive "https://github.com/skywind3000/kcp/blob/master/ikcp.c"
click node_flow_control "https://github.com/skywind3000/kcp/blob/master/ikcp.c"
click node_output_callback "https://github.com/skywind3000/kcp/blob/master/ikcp.c"
click node_scheduler "https://github.com/skywind3000/kcp/blob/master/ikcp.c"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_api_surface toneBlue
class node_session,node_send_queue,node_arq,node_receive,node_flow_control toneAmber
class node_output_callback,node_scheduler toneMint
class node_application,node_clock,node_transport toneIndigo
```

## Components

### Application API

- [KCP API](https://github.com/skywind3000/kcp/blob/master/ikcp.h): `ikcp.h`

### ARQ protocol core

- [Session state](https://github.com/skywind3000/kcp/blob/master/ikcp.c): `ikcp.c`
- [Send queue](https://github.com/skywind3000/kcp/blob/master/ikcp.c): `ikcp.c`
- [ACK and retransmit](https://github.com/skywind3000/kcp/blob/master/ikcp.c): `ikcp.c`
- [Receive ordering](https://github.com/skywind3000/kcp/blob/master/ikcp.c): `ikcp.c`
- [Flow control](https://github.com/skywind3000/kcp/blob/master/ikcp.c): `ikcp.c`

### Host integration

- [Output callback](https://github.com/skywind3000/kcp/blob/master/ikcp.c): `ikcp.c`
- [Update scheduler](https://github.com/skywind3000/kcp/blob/master/ikcp.c): `ikcp.c`

### Other components

- **Application**
- **Host clock**
- **Packet transport**

## Connections

- Application → KCP API: sends data (evidence: `README.md`)
- KCP API → Session state: creates session (evidence: `README.md`)
- KCP API → Send queue: queues data (evidence: `README.md`)
- Send queue → ACK and retransmit: segments data (evidence: `ikcp.c`)
- ACK and retransmit → Output callback: emits packets (evidence: `ikcp.c`)
- Output callback → Packet transport: sends packets (evidence: `README.md`)
- Packet transport → KCP API: supplies packets (evidence: `README.md`)
- KCP API → Receive ordering: inputs packets (evidence: `README.md`)
- Receive ordering → ACK and retransmit: processes ACKs (evidence: `ikcp.c`)
- Receive ordering → Application: delivers data (evidence: `README.md`)
- Host clock → Update scheduler: provides time (evidence: `README.md`)
- Update scheduler → ACK and retransmit: updates state (evidence: `README.md`)
- Flow control → ACK and retransmit: limits sending (evidence: `ikcp.c`)
- KCP API → Flow control: configures control (evidence: `README.md`)
