Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add kcptun #528

Merged
merged 2 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,14 @@ mqtt_pub: prepare
mqtt_client_test: prepare
$(MAKEF) TARGET=$@ SRCDIRS="$(CORE_SRCDIRS) mqtt" SRCS="examples/mqtt/mqtt_client_test.cpp"

kcptun: kcptun_client kcptun_server

kcptun_client: prepare
$(MAKEF) TARGET=$@ SRCDIRS="$(CORE_SRCDIRS) examples/kcptun/smux examples/kcptun/client"

kcptun_server: prepare
$(MAKEF) TARGET=$@ SRCDIRS="$(CORE_SRCDIRS) examples/kcptun/smux examples/kcptun/server"

jsonrpc: jsonrpc_client jsonrpc_server

jsonrpc_client: prepare
Expand Down
1 change: 1 addition & 0 deletions README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ int main(int argc, char** argv) {
- HTTP客户端: [examples/http_client_test.cpp](examples/http_client_test.cpp)
- WebSocket服务端: [examples/websocket_server_test.cpp](examples/websocket_server_test.cpp)
- WebSocket客户端: [examples/websocket_client_test.cpp](examples/websocket_client_test.cpp)
- kcptun隧道: [examples/kcptun](examples/kcptun)
- protobufRPC示例: [examples/protorpc](examples/protorpc)
- Qt中使用libhv示例: [hv-projects/QtDemo](https://github.com/hv-projects/QtDemo)

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ int main(int argc, char** argv) {
- [examples/http_client_test.cpp](examples/http_client_test.cpp)
- [examples/websocket_server_test.cpp](examples/websocket_server_test.cpp)
- [examples/websocket_client_test.cpp](examples/websocket_client_test.cpp)
- [examples/kcptun](examples/kcptun)
- [examples/protorpc](examples/protorpc)
- [hv-projects/QtDemo](https://github.com/hv-projects/QtDemo)

Expand Down
16 changes: 16 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ add_executable(jsonrpc_server jsonrpc/jsonrpc_server.c jsonrpc/cJSON.c)
target_compile_definitions(jsonrpc_server PRIVATE CJSON_HIDE_SYMBOLS)
target_link_libraries(jsonrpc_server ${HV_LIBRARIES})

if(WITH_KCP)
glob_headers_and_sources(KCPTUN_SMUX_FILES kcptun/smux)
glob_headers_and_sources(KCPTUN_CLIENT_FILES kcptun/client)
glob_headers_and_sources(KCPTUN_SERVER_FILES kcptun/server)

# kcptun_client
add_executable(kcptun_client ${KCPTUN_SMUX_FILES} ${KCPTUN_CLIENT_FILES})
target_link_libraries(kcptun_client ${HV_LIBRARIES})

# kcptun_server
add_executable(kcptun_server ${KCPTUN_SMUX_FILES} ${KCPTUN_SERVER_FILES})
target_link_libraries(kcptun_server ${HV_LIBRARIES})

list(APPEND EXAMPLES kcptun_client kcptun_server)
endif()

if(WITH_EVPP)
include_directories(../cpputil ../evpp)

Expand Down
68 changes: 68 additions & 0 deletions examples/kcptun/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Intro

<img src="kcptun.png" alt="kcptun" height="300px"/>

> *Disclaimer: The picture comes from [github.com/xtaci/kcptun](https://github.com/xtaci/kcptun). Thanks so much.*

# Build

```shell
./configure --with-kcp
make clean
make examples
make kcptun
```

# Usage

```shell
$ bin/kcptun_server -h

Usage: kcptun_server [hvdl:t:m:]
Options:

-h|--help Print this information
-v|--version Print version
-d|--daemon Daemonize
-l|--listen value kcp server listen address (default: ":4000")
-t|--target value target server address (default: "127.0.0.1:8080")
-m|--mode value profiles: fast3, fast2, fast, normal (default: "fast")
--mtu value set maximum transmission unit for UDP packets (default: 1350)
--sndwnd value set send window size(num of packets) (default: 1024)
--rcvwnd value set receive window size(num of packets) (default: 1024)
```

```shell
$ bin/kcptun_client -h

Usage: kcptun_client [hvdl:r:m:]
Options:

-h|--help Print this information
-v|--version Print version
-d|--daemon Daemonize
-l|--localaddr value local listen address (default: ":8388")
-r|--remoteaddr value kcp server address (default: "127.0.0.1:4000")
-m|--mode value profiles: fast3, fast2, fast, normal (default: "fast")
--mtu value set maximum transmission unit for UDP packets (default: 1350)
--sndwnd value set send window size(num of packets) (default: 128)
--rcvwnd value set receive window size(num of packets) (default: 512)
```

# Test
`tcp_client -> kcptun_client -> kcptun_server -> tcp_server`
```shell
tcp_server: bin/tcp_echo_server 1234
kcptun_server: bin/kcptun_server -l :4000 -t 127.0.0.1:1234 --mode fast3
kcptun_client: bin/kcptun_client -l :8388 -r 127.0.0.1:4000 --mode fast3
tcp_client: bin/nc 127.0.0.1 8388
> hello
< hello
```

This kcptun examples does not implement encryption, compression, and fec.<br>
if you want to use [github.com/xtaci/kcptun](https://github.com/xtaci/kcptun), please add `--crypt null --nocomp --ds 0 --ps 0`.<br>
For example:
```shell
golang_kcptun_server -l :4000 -t 127.0.0.1:1234 --mode fast3 --crypt null --nocomp --ds 0 --ps 0
```
Loading
Loading