Skip to content

Commit 8e7339a

Browse files
authored
Add kcptun (#528)
* Add examples/kcptun * Add README for kcptun
1 parent a4e51af commit 8e7339a

File tree

10 files changed

+1075
-0
lines changed

10 files changed

+1075
-0
lines changed

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,14 @@ mqtt_pub: prepare
191191
mqtt_client_test: prepare
192192
$(MAKEF) TARGET=$@ SRCDIRS="$(CORE_SRCDIRS) mqtt" SRCS="examples/mqtt/mqtt_client_test.cpp"
193193

194+
kcptun: kcptun_client kcptun_server
195+
196+
kcptun_client: prepare
197+
$(MAKEF) TARGET=$@ SRCDIRS="$(CORE_SRCDIRS) examples/kcptun/smux examples/kcptun/client"
198+
199+
kcptun_server: prepare
200+
$(MAKEF) TARGET=$@ SRCDIRS="$(CORE_SRCDIRS) examples/kcptun/smux examples/kcptun/server"
201+
194202
jsonrpc: jsonrpc_client jsonrpc_server
195203

196204
jsonrpc_client: prepare

README-CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ int main(int argc, char** argv) {
429429
- HTTP客户端: [examples/http_client_test.cpp](examples/http_client_test.cpp)
430430
- WebSocket服务端: [examples/websocket_server_test.cpp](examples/websocket_server_test.cpp)
431431
- WebSocket客户端: [examples/websocket_client_test.cpp](examples/websocket_client_test.cpp)
432+
- kcptun隧道: [examples/kcptun](examples/kcptun)
432433
- protobufRPC示例: [examples/protorpc](examples/protorpc)
433434
- Qt中使用libhv示例: [hv-projects/QtDemo](https://github.com/hv-projects/QtDemo)
434435

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ int main(int argc, char** argv) {
369369
- [examples/http_client_test.cpp](examples/http_client_test.cpp)
370370
- [examples/websocket_server_test.cpp](examples/websocket_server_test.cpp)
371371
- [examples/websocket_client_test.cpp](examples/websocket_client_test.cpp)
372+
- [examples/kcptun](examples/kcptun)
372373
- [examples/protorpc](examples/protorpc)
373374
- [hv-projects/QtDemo](https://github.com/hv-projects/QtDemo)
374375

examples/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@ add_executable(jsonrpc_server jsonrpc/jsonrpc_server.c jsonrpc/cJSON.c)
6161
target_compile_definitions(jsonrpc_server PRIVATE CJSON_HIDE_SYMBOLS)
6262
target_link_libraries(jsonrpc_server ${HV_LIBRARIES})
6363

64+
if(WITH_KCP)
65+
glob_headers_and_sources(KCPTUN_SMUX_FILES kcptun/smux)
66+
glob_headers_and_sources(KCPTUN_CLIENT_FILES kcptun/client)
67+
glob_headers_and_sources(KCPTUN_SERVER_FILES kcptun/server)
68+
69+
# kcptun_client
70+
add_executable(kcptun_client ${KCPTUN_SMUX_FILES} ${KCPTUN_CLIENT_FILES})
71+
target_link_libraries(kcptun_client ${HV_LIBRARIES})
72+
73+
# kcptun_server
74+
add_executable(kcptun_server ${KCPTUN_SMUX_FILES} ${KCPTUN_SERVER_FILES})
75+
target_link_libraries(kcptun_server ${HV_LIBRARIES})
76+
77+
list(APPEND EXAMPLES kcptun_client kcptun_server)
78+
endif()
79+
6480
if(WITH_EVPP)
6581
include_directories(../cpputil ../evpp)
6682

examples/kcptun/README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Intro
2+
3+
<img src="kcptun.png" alt="kcptun" height="300px"/>
4+
5+
> *Disclaimer: The picture comes from [github.com/xtaci/kcptun](https://github.com/xtaci/kcptun). Thanks so much.*
6+
7+
# Build
8+
9+
```shell
10+
./configure --with-kcp
11+
make clean
12+
make examples
13+
make kcptun
14+
```
15+
16+
# Usage
17+
18+
```shell
19+
$ bin/kcptun_server -h
20+
21+
Usage: kcptun_server [hvdl:t:m:]
22+
Options:
23+
24+
-h|--help Print this information
25+
-v|--version Print version
26+
-d|--daemon Daemonize
27+
-l|--listen value kcp server listen address (default: ":4000")
28+
-t|--target value target server address (default: "127.0.0.1:8080")
29+
-m|--mode value profiles: fast3, fast2, fast, normal (default: "fast")
30+
--mtu value set maximum transmission unit for UDP packets (default: 1350)
31+
--sndwnd value set send window size(num of packets) (default: 1024)
32+
--rcvwnd value set receive window size(num of packets) (default: 1024)
33+
```
34+
35+
```shell
36+
$ bin/kcptun_client -h
37+
38+
Usage: kcptun_client [hvdl:r:m:]
39+
Options:
40+
41+
-h|--help Print this information
42+
-v|--version Print version
43+
-d|--daemon Daemonize
44+
-l|--localaddr value local listen address (default: ":8388")
45+
-r|--remoteaddr value kcp server address (default: "127.0.0.1:4000")
46+
-m|--mode value profiles: fast3, fast2, fast, normal (default: "fast")
47+
--mtu value set maximum transmission unit for UDP packets (default: 1350)
48+
--sndwnd value set send window size(num of packets) (default: 128)
49+
--rcvwnd value set receive window size(num of packets) (default: 512)
50+
```
51+
52+
# Test
53+
`tcp_client -> kcptun_client -> kcptun_server -> tcp_server`
54+
```shell
55+
tcp_server: bin/tcp_echo_server 1234
56+
kcptun_server: bin/kcptun_server -l :4000 -t 127.0.0.1:1234 --mode fast3
57+
kcptun_client: bin/kcptun_client -l :8388 -r 127.0.0.1:4000 --mode fast3
58+
tcp_client: bin/nc 127.0.0.1 8388
59+
> hello
60+
< hello
61+
```
62+
63+
This kcptun examples does not implement encryption, compression, and fec.<br>
64+
if you want to use [github.com/xtaci/kcptun](https://github.com/xtaci/kcptun), please add `--crypt null --nocomp --ds 0 --ps 0`.<br>
65+
For example:
66+
```shell
67+
golang_kcptun_server -l :4000 -t 127.0.0.1:1234 --mode fast3 --crypt null --nocomp --ds 0 --ps 0
68+
```

0 commit comments

Comments
 (0)