Skip to content

Commit 92ab5a2

Browse files
authored
add binding for kcp (alibaba#1541)
add binding for kcp
1 parent 6d34b35 commit 92ab5a2

13 files changed

Lines changed: 1258 additions & 5 deletions

File tree

.github/workflows/ci.linux.arm.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ jobs:
5454
-D PHOTON_ENABLE_FUSE=ON \
5555
-D PHOTON_ENABLE_URING=ON \
5656
-D PHOTON_ENABLE_LIBCURL=ON \
57+
-D PHOTON_ENABLE_KCP=ON \
5758
-D PHOTON_ENABLE_EXTFS=ON
5859
cmake --build build -j $(nproc) -- VERBOSE=1
5960

.github/workflows/ci.linux.x86_64.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ jobs:
3838
-D PHOTON_ENABLE_FUSE=ON \
3939
-D PHOTON_ENABLE_URING=ON \
4040
-D PHOTON_ENABLE_LIBCURL=ON \
41+
-D PHOTON_ENABLE_KCP=ON \
4142
-D PHOTON_ENABLE_EXTFS=ON
4243
cmake --build build -j $(nproc)
4344
- name: Test epoll

.github/workflows/ci.macos.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ jobs:
5151
-D CMAKE_BUILD_TYPE=MinSizeRel \
5252
-D PHOTON_ENABLE_SASL=ON \
5353
-D PHOTON_ENABLE_LIBCURL=ON \
54+
-D PHOTON_ENABLE_KCP=ON \
5455
-D OPENSSL_ROOT_DIR=${OPENSSL_DIR}
5556
cmake --build ${{github.workspace}}/build -j $(sysctl -n hw.logicalcpu)
5657

CMake/build-from-src.cmake

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,23 @@ function(build_from_src [dep])
395395
set(FSTACK_LIBRARIES ${BINARY_DIR}/lib/libfstack.a PARENT_SCOPE)
396396
set(FSTACK_LIBRARIES ${FSTACK_LIBRARIES} ${DPDK_LIBRARIES} PARENT_SCOPE)
397397

398+
elseif (dep STREQUAL "kcp")
399+
set(BINARY_DIR ${PROJECT_BINARY_DIR}/kcp-build)
400+
ExternalProject_Add(
401+
kcp
402+
URL ${PHOTON_KCP_SOURCE}
403+
UPDATE_DISCONNECTED ON
404+
BUILD_IN_SOURCE ON
405+
CONFIGURE_COMMAND ${CMAKE_COMMAND} -E make_directory ${BINARY_DIR}/lib ${BINARY_DIR}/include
406+
BUILD_COMMAND ${CMAKE_C_COMPILER} -c -O2 -fPIC -I<SOURCE_DIR> <SOURCE_DIR>/ikcp.c -o ikcp.o
407+
COMMAND ${CMAKE_AR} rcs libikcp.a ikcp.o
408+
INSTALL_COMMAND ${CMAKE_COMMAND} -E copy libikcp.a ${BINARY_DIR}/lib/
409+
COMMAND ${CMAKE_COMMAND} -E copy <SOURCE_DIR>/ikcp.h ${BINARY_DIR}/include/
410+
)
411+
ExternalProject_Get_Property(kcp SOURCE_DIR)
412+
set(kcp_SOURCE_DIR ${SOURCE_DIR} PARENT_SCOPE)
413+
set(kcp_static ${BINARY_DIR}/lib/libikcp.a PARENT_SCOPE)
414+
398415
endif ()
399416

400417
list(APPEND actually_built ${dep})

CMakeLists.txt

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ option(PHOTON_ENABLE_RSOCKET "enable rsocket" OFF)
3636
option(PHOTON_ENABLE_LIBCURL "enable libcurl" ON)
3737
set(PHOTON_DEFAULT_LOG_LEVEL "0" CACHE STRING "default log level")
3838
option(PHOTON_BUILD_OCF_CACHE "enable ocf cache" OFF)
39+
option(PHOTON_ENABLE_KCP "enable kcp" OFF)
3940

4041
option(PHOTON_BUILD_DEPENDENCIES "" OFF)
4142
set(PHOTON_AIO_SOURCE "https://pagure.io/libaio/archive/libaio-0.3.113/libaio-0.3.113.tar.gz" CACHE STRING "")
@@ -60,6 +61,7 @@ set(PHOTON_RAPIDYAML_SOURCE "https://github.com/biojppm/rapidyaml/releases/downl
6061
# It's not recommended to build rdmacore from source because it has too many dependencies. Install it to local host.
6162
# The release version we use is https://github.com/linux-rdma/rdma-core/releases/download/v58.0/rdma-core-58.0.tar.gz
6263
set(PHOTON_RDMACORE_SOURCE "" CACHE STRING "")
64+
set(PHOTON_KCP_SOURCE "https://github.com/skywind3000/kcp/archive/refs/tags/2.1.1.tar.gz" CACHE STRING "")
6365

6466
if (NOT (CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64) AND NOT (CMAKE_SYSTEM_PROCESSOR STREQUAL aarch64) AND NOT (CMAKE_SYSTEM_PROCESSOR STREQUAL arm64))
6567
message(FATAL_ERROR "Unknown CPU architecture ${CMAKE_SYSTEM_PROCESSOR}")
@@ -180,6 +182,9 @@ endif ()
180182
if (PHOTON_ENABLE_LIBCURL)
181183
LIST(APPEND dependencies curl)
182184
endif()
185+
if (PHOTON_ENABLE_KCP)
186+
LIST(APPEND dependencies kcp)
187+
endif()
183188
if (PHOTON_BUILD_TESTING)
184189
LIST(APPEND dependencies gflags googletest)
185190
endif ()
@@ -188,7 +193,11 @@ FOREACH (dep ${dependencies})
188193
message(STATUS "Checking dependency ${dep}")
189194
string(TOUPPER ${dep} DEP)
190195
set(source_url "${PHOTON_${DEP}_SOURCE}")
191-
if (PHOTON_BUILD_DEPENDENCIES AND (NOT source_url STREQUAL ""))
196+
if (dep STREQUAL "kcp")
197+
message(STATUS "Will build ${dep} from source")
198+
message(STATUS " URL: ${source_url}")
199+
build_from_src(dep)
200+
elseif (PHOTON_BUILD_DEPENDENCIES AND (NOT source_url STREQUAL ""))
192201
message(STATUS "Will build ${dep} from source")
193202
message(STATUS " URL: ${source_url}")
194203
build_from_src(dep)
@@ -282,6 +291,9 @@ if (PHOTON_ENABLE_ECOSYSTEM)
282291
file(GLOB ECOSYSTEM_SRC ecosystem/*.cpp)
283292
list(APPEND PHOTON_SRC ${ECOSYSTEM_SRC})
284293
endif ()
294+
if (NOT PHOTON_ENABLE_KCP)
295+
list(REMOVE_ITEM PHOTON_SRC net/kcp.cpp)
296+
endif ()
285297
if (PHOTON_ENABLE_RSOCKET)
286298
list(APPEND PHOTON_SRC net/rsocket/rsocket.cpp)
287299
endif ()
@@ -338,6 +350,10 @@ endif()
338350
if (PHOTON_DEFAULT_LOG_LEVEL)
339351
target_compile_definitions(photon_obj PRIVATE DEFAULT_LOG_LEVEL=${PHOTON_DEFAULT_LOG_LEVEL})
340352
endif()
353+
if (PHOTON_ENABLE_KCP)
354+
target_include_directories(photon_obj PRIVATE ${kcp_SOURCE_DIR})
355+
target_compile_definitions(photon_obj PRIVATE PHOTON_KCP=on)
356+
endif()
341357

342358
if (actually_built)
343359
add_dependencies(photon_obj ${actually_built})
@@ -397,6 +413,10 @@ if (PHOTON_BUILD_OCF_CACHE)
397413
endif()
398414
list(APPEND static_deps ${OPENSSL_LIBRARIES} ${ZLIB_LIBRARIES})
399415

416+
if (PHOTON_ENABLE_KCP)
417+
list(APPEND static_deps ${kcp_static})
418+
endif()
419+
400420

401421
# Find out dynamic libs and append to `shared_deps`.
402422
# Because if not built from source, we won't know the local packages are static or shared.

include/photon/net/kcp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../net/kcp.h

net/datagram_socket.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class DatagramSocketBase : public IDatagramSocket {
6565
virtual uint64_t max_message_size() override { return m_max_msg_size; }
6666

6767
int do_connect(const sockaddr_storage* s) {
68-
return DOIO_ONCE(::connect(fd, s->get_sockaddr(), s->get_socklen()), wait_for_fd_writable(fd));
68+
return DOIO_ONCE(::connect(fd, s->get_sockaddr(), s->get_socklen()), wait_for_fd_writable(fd, m_timeout));
6969
}
7070

7171
int do_bind(const sockaddr_storage* s) {
@@ -84,7 +84,7 @@ class DatagramSocketBase : public IDatagramSocket {
8484
.msg_flags = 0,
8585
};
8686
return DOIO_ONCE(::sendmsg(fd, &hdr, MSG_DONTWAIT | flags),
87-
wait_for_fd_writable(fd));
87+
wait_for_fd_writable(fd, m_timeout));
8888
}
8989
ssize_t do_recv(const iovec* iov, int iovcnt, sockaddr* addr,
9090
size_t* addrlen, int flags) {
@@ -98,7 +98,7 @@ class DatagramSocketBase : public IDatagramSocket {
9898
.msg_flags = 0,
9999
};
100100
auto ret = DOIO_ONCE(::recvmsg(fd, &hdr, MSG_DONTWAIT | flags),
101-
wait_for_fd_readable(fd));
101+
wait_for_fd_readable(fd, m_timeout));
102102
if (addrlen) *addrlen = hdr.msg_namelen;
103103
return ret;
104104
}

net/datagram_socket.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class UDPSocket : public IDatagramSocket {
6666
public:
6767
using base::recv;
6868
using base::send;
69+
using base::timeout;
6970
int connect(const EndPoint& ep) { return connect((Addr*)&ep, sizeof(ep)); }
7071
int bind(const EndPoint& ep) { return bind((Addr*)&ep, sizeof(ep)); }
7172
int bind(uint16_t port = 0) { return bind_v4any(0); }

0 commit comments

Comments
 (0)