Skip to content

Commit

Permalink
重构以支持更多底层的传输协议
Browse files Browse the repository at this point in the history
  • Loading branch information
lanthora committed Dec 21, 2024
1 parent 4c808d9 commit 3fe4619
Show file tree
Hide file tree
Showing 52 changed files with 2,509 additions and 4,042 deletions.
16 changes: 11 additions & 5 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,23 @@ jobs:
run: docker build .

macos:
runs-on: macos-13
runs-on: macos-latest
steps:
- name: depends
run: brew install spdlog poco
run: brew install openssl fmt spdlog
- name: checkout
uses: actions/checkout@v4
- name: build
run: |
export CPATH=/usr/local/include
export LIBRARY_PATH=/usr/local/lib
cmake -B build -DCANDY_STATIC_POCO=1 -DCMAKE_BUILD_TYPE=Release && cmake --build build
if [ "$RUNNER_ARCH" == "ARM64" ]; then
export CPATH=/opt/homebrew/include
export LIBRARY_PATH=/opt/homebrew/lib
else
export CPATH=/usr/local/include
export LIBRARY_PATH=/usr/local/lib
fi
cmake -B build -DCANDY_STATIC_POCO=1 -DCMAKE_BUILD_TYPE=Release
cmake --build build
windows:
runs-on: windows-latest
Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)

if (${CANDY_STATIC})
set(CMAKE_SKIP_BUILD_RPATH TRUE)
if (${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")
endif()
set(CANDY_STATIC_OPENSSL 1)
set(CANDY_STATIC_FMT 1)
set(CANDY_STATIC_SPDLOG 1)
Expand Down
10 changes: 6 additions & 4 deletions src/cffi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ if (${CANDY_STATIC_SPDLOG})
target_link_libraries(${CANDY_LIBRARY_NAME} PRIVATE spdlog::spdlog)
else()
find_package(PkgConfig REQUIRED)
pkg_check_modules(DEPS REQUIRED spdlog)
add_definitions(${DEPS_CFLAGS})
include_directories(${DEPS_INCLUDEDIR})
target_link_libraries(${CANDY_LIBRARY_NAME} PRIVATE ${DEPS_LIBRARIES})
pkg_check_modules(SPDLOG REQUIRED spdlog)
add_definitions(${SPDLOG_CFLAGS})
include_directories(${SPDLOG_INCLUDEDIR})
target_link_libraries(${CANDY_LIBRARY_NAME} PRIVATE ${SPDLOG_LIBRARIES})
endif()

if (${CANDY_STATIC_OPENSSL})
target_link_libraries(${CANDY_LIBRARY_NAME} PRIVATE ${OPENSSL_LIB_CRYPTO} ${OPENSSL_LIB_SSL})
else()
find_package(OpenSSL REQUIRED)
endif()

if (${CANDY_STATIC_POCO})
Expand Down
65 changes: 32 additions & 33 deletions src/cffi/candy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,82 +18,82 @@ void candy_client_release(void *candy) {
delete c;
}

int candy_client_set_name(void *candy, const char *name) {
void candy_client_set_name(void *candy, const char *name) {
Candy::Client *c = static_cast<Candy::Client *>(candy);
return c->setName(name);
c->setName(name);
}

int candy_client_set_password(void *candy, const char *password) {
void candy_client_set_password(void *candy, const char *password) {
Candy::Client *c = static_cast<Candy::Client *>(candy);
return c->setPassword(password);
c->setPassword(password);
}

int candy_client_set_websocket_server(void *candy, const char *server) {
void candy_client_set_websocket(void *candy, const char *server) {
Candy::Client *c = static_cast<Candy::Client *>(candy);
return c->setWebSocketServer(server);
c->setWebSocket(server);
}

int candy_client_set_tun_address(void *candy, const char *cidr) {
void candy_client_set_tun_address(void *candy, const char *cidr) {
Candy::Client *c = static_cast<Candy::Client *>(candy);
return c->setTunAddress(cidr);
c->setTunAddress(cidr);
}

int candy_client_set_expected_address(void *candy, const char *cidr) {
void candy_client_set_expt_tun_address(void *candy, const char *cidr) {
Candy::Client *c = static_cast<Candy::Client *>(candy);
return c->setExpectedAddress(cidr);
c->setExptTunAddress(cidr);
}

int candy_client_set_virtual_mac(void *candy, const char *vmac) {
void candy_client_set_virtual_mac(void *candy, const char *vmac) {
Candy::Client *c = static_cast<Candy::Client *>(candy);
return c->setVirtualMac(vmac);
c->setVirtualMac(vmac);
}

int candy_client_set_stun(void *candy, const char *stun) {
void candy_client_set_stun(void *candy, const char *stun) {
Candy::Client *c = static_cast<Candy::Client *>(candy);
return c->setStun(stun);
c->setStun(stun);
}

int candy_client_set_discovery_interval(void *candy, int interval) {
void candy_client_set_discovery_interval(void *candy, int interval) {
Candy::Client *c = static_cast<Candy::Client *>(candy);
return c->setDiscoveryInterval(interval);
c->setDiscoveryInterval(interval);
}

int candy_client_set_route_cost(void *candy, int cost) {
void candy_client_set_route_cost(void *candy, int cost) {
Candy::Client *c = static_cast<Candy::Client *>(candy);
return c->setRouteCost(cost);
c->setRouteCost(cost);
}

int candy_client_set_mtu(void *candy, int mtu) {
void candy_client_set_mtu(void *candy, int mtu) {
Candy::Client *c = static_cast<Candy::Client *>(candy);
return c->setMtu(mtu);
c->setMtu(mtu);
}

int candy_client_set_address_update_callback(void *candy, void (*callback)(const char *, const char *)) {
void candy_client_set_tun_update_callback(void *candy, void (*callback)(const char *, const char *)) {
Candy::Client *c = static_cast<Candy::Client *>(candy);
return c->setAddressUpdateCallback([=](const std::string &address) {
return c->setTunUpdateCallback([=](const std::string &address) {
callback(c->getName().c_str(), address.c_str());
return 0;
});
}

int candy_client_set_udp_bind_port(void *candy, int port) {
void candy_client_set_port(void *candy, int port) {
Candy::Client *c = static_cast<Candy::Client *>(candy);
return c->setUdpBindPort(port);
c->setPort(port);
}

int candy_client_set_localhost(void *candy, const char *ip) {
void candy_client_set_localhost(void *candy, const char *ip) {
Candy::Client *c = static_cast<Candy::Client *>(candy);
return c->setLocalhost(ip);
c->setLocalhost(ip);
}

int candy_client_run(void *candy) {
void candy_client_run(void *candy) {
Candy::Client *c = static_cast<Candy::Client *>(candy);
return c->run();
c->run();
}

int candy_client_shutdown(void *candy) {
void candy_client_shutdown(void *candy) {
Candy::Client *c = static_cast<Candy::Client *>(candy);
return c->shutdown();
c->shutdown();
}

namespace {
Expand All @@ -110,13 +110,12 @@ void shutdown(Client *c) {
}
} // namespace Candy

int candy_client_set_error_cb(void (*callback)(void *)) {
void candy_client_set_error_cb(void (*callback)(void *)) {
client_error_cb = callback;
return 0;
}

void candy_use_system_time() {
Candy::Time::useSystemTime = true;
Candy::useSystemTime = true;
}

void candy_set_log_path(const char *path) {
Expand Down
32 changes: 16 additions & 16 deletions src/cffi/candy.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ extern "C" {

void candy_init();
void *candy_client_create();
int candy_client_set_name(void *candy, const char *name);
int candy_client_set_password(void *candy, const char *password);
int candy_client_set_websocket_server(void *candy, const char *server);
int candy_client_set_tun_address(void *candy, const char *cidr);
int candy_client_set_expected_address(void *candy, const char *cidr);
int candy_client_set_virtual_mac(void *candy, const char *vmac);
int candy_client_set_stun(void *candy, const char *stun);
int candy_client_set_discovery_interval(void *candy, int interval);
int candy_client_set_route_cost(void *candy, int cost);
int candy_client_set_mtu(void *candy, int mtu);
int candy_client_set_udp_bind_port(void *candy, int port);
int candy_client_set_localhost(void *candy, const char *ip);
int candy_client_set_address_update_callback(void *candy, void (*callback)(const char *, const char *));
int candy_client_set_error_cb(void (*callback)(void *));
int candy_client_run(void *candy);
int candy_client_shutdown(void *candy);
void candy_client_set_name(void *candy, const char *name);
void candy_client_set_password(void *candy, const char *password);
void candy_client_set_websocket(void *candy, const char *server);
void candy_client_set_tun_address(void *candy, const char *cidr);
void candy_client_set_expt_tun_address(void *candy, const char *cidr);
void candy_client_set_virtual_mac(void *candy, const char *vmac);
void candy_client_set_stun(void *candy, const char *stun);
void candy_client_set_discovery_interval(void *candy, int interval);
void candy_client_set_route_cost(void *candy, int cost);
void candy_client_set_mtu(void *candy, int mtu);
void candy_client_set_port(void *candy, int port);
void candy_client_set_localhost(void *candy, const char *ip);
void candy_client_set_address_update_callback(void *candy, void (*callback)(const char *, const char *));
void candy_client_set_error_cb(void (*callback)(void *));
void candy_client_run(void *candy);
void candy_client_shutdown(void *candy);
void candy_client_release(void *candy);
void candy_use_system_time();
void candy_set_log_path(const char *path);
Expand Down
18 changes: 10 additions & 8 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,26 @@ if (${CANDY_STATIC_FMT})
target_link_libraries(core PRIVATE fmt::fmt)
else()
find_package(PkgConfig REQUIRED)
pkg_check_modules(DEPS REQUIRED fmt)
add_definitions(${DEPS_CFLAGS})
include_directories(${DEPS_INCLUDEDIR})
target_link_libraries(core PRIVATE ${DEPS_LIBRARIES})
pkg_check_modules(FMT REQUIRED fmt)
add_definitions(${FMT_CFLAGS})
include_directories(${FMT_INCLUDEDIR})
target_link_libraries(core PRIVATE ${FMT_LIBRARIES})
endif()

if (${CANDY_STATIC_SPDLOG})
target_link_libraries(core PRIVATE spdlog::spdlog)
else()
find_package(PkgConfig REQUIRED)
pkg_check_modules(DEPS REQUIRED spdlog)
add_definitions(${DEPS_CFLAGS})
include_directories(${DEPS_INCLUDEDIR})
target_link_libraries(core PRIVATE ${DEPS_LIBRARIES})
pkg_check_modules(SPDLOG REQUIRED spdlog)
add_definitions(${SPDLOG_CFLAGS})
include_directories(${SPDLOG_INCLUDEDIR})
target_link_libraries(core PRIVATE ${SPDLOG_LIBRARIES})
endif()

if (${CANDY_STATIC_OPENSSL})
target_link_libraries(core PRIVATE ${OPENSSL_LIB_CRYPTO} ${OPENSSL_LIB_SSL})
else()
find_package(OpenSSL REQUIRED)
endif()

if (${CANDY_STATIC_POCO})
Expand Down
Loading

0 comments on commit 3fe4619

Please sign in to comment.