Skip to content

Commit

Permalink
introduce new node: preconnect client
Browse files Browse the repository at this point in the history
  • Loading branch information
radkesvat committed Apr 3, 2024
1 parent ab03963 commit 989f493
Show file tree
Hide file tree
Showing 7 changed files with 452 additions and 2 deletions.
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ option(INCLUDE_REVERSE_SERVER "link ReverseServer staticly to the core" TRUE)
option(INCLUDE_REVERSE_CLIENT "link ReverseClient staticly to the core" TRUE)
option(INCLUDE_HEADER_SERVER "link HeaderServer staticly to the core" TRUE)
option(INCLUDE_HEADER_CLIENT "link HeaderClient staticly to the core" TRUE)
option(INCLUDE_PRECONNECT_CLIENT "link PreConnectClient staticly to the core" TRUE)


# create project
project(Waterwall VERSION 0.1)
Expand Down Expand Up @@ -227,7 +229,13 @@ target_link_directories(Waterwall PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/tunnels/cli
target_link_libraries(Waterwall HeaderClient)
endif()


#preconnect client
if (INCLUDE_PRECONNECT_CLIENT)
target_compile_definitions(Waterwall PUBLIC INCLUDE_PRECONNECT_CLIENT=1)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tunnels/client/preconnect)
target_link_directories(Waterwall PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/tunnels/client/preconnect)
target_link_libraries(Waterwall PreConnectClient)
endif()



Expand Down
8 changes: 7 additions & 1 deletion core/static_tunnels.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@
#include "tunnels/client/header/header_client.h"
#endif

#ifdef INCLUDE_PRECONNECT_CLIENT
#include "tunnels/client/preconnect/preconnect_client.h"
#endif



void loadStaticTunnelsIntoCore()
Expand Down Expand Up @@ -159,7 +163,9 @@ void loadStaticTunnelsIntoCore()
USING(HeaderClient);
#endif


#ifdef INCLUDE_PRECONNECT_CLIENT
USING(PreConnectClient);
#endif


}
37 changes: 37 additions & 0 deletions tunnels/client/preconnect/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
add_library(PreConnectClient STATIC
preconnect_client.c

)



#ww api
target_include_directories(PreConnectClient PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../../ww)
target_link_libraries(PreConnectClient PUBLIC ww)
# target_compile_options(ww PUBLIC -fPIC)


# add dependencies
include(${CMAKE_BINARY_DIR}/cmake/CPM.cmake)


CPMAddPackage(
NAME stc
GIT_REPOSITORY https://github.com/stclib/STC
GIT_TAG 09790f024ad29fca6fe60528461eeb589d4a917b
DOWNLOAD_ONLY True
)


if(stc_ADDED)
target_include_directories(PreConnectClient PUBLIC ${stc_SOURCE_DIR}/include)
endif()



target_compile_definitions(PreConnectClient PRIVATE STC_STATIC=1 PreConnectClient_VERSION=0.1)


if(CMAKE_BUILD_TYPE STREQUAL "Debug")
target_compile_definitions(PreConnectClient PRIVATE DEBUG=1)
endif()
102 changes: 102 additions & 0 deletions tunnels/client/preconnect/helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#pragma once
#include "types.h"
#include "loggers/network_logger.h"

#define STATE(x) ((preconnect_client_state_t *)((x)->state))
#define CSTATE(x) ((preconnect_client_con_state_t *)((((x)->line->chains_state)[self->chain_index])))
#define CSTATE_MUT(x) ((x)->line->chains_state)[self->chain_index]
#define ISALIVE(x) (((((x)->line->chains_state)[self->chain_index])) != NULL)
#define PRECONNECT_DELAY 100
#undef max
#undef min
static inline size_t min(size_t x, size_t y) { return (((x) < (y)) ? (x) : (y)); }
static inline size_t max(size_t x, size_t y) { return (((x) < (y)) ? (y) : (x)); }

static void add_connection(thread_box_t *box,
preconnect_client_con_state_t *con)
{
con->next = box->root.next;
box->root.next = con;
con->prev = &box->root;
if (con->next)
{
con->next->prev = con;
}
box->length += 1;
}
static void remove_connection(thread_box_t *box, preconnect_client_con_state_t *con)
{

con->prev->next = con->next;
if (con->next)
{
con->next->prev = con->prev;
}
box->length -= 1;
}

static preconnect_client_con_state_t *create_cstate(int tid)
{
preconnect_client_con_state_t *cstate = malloc(sizeof(preconnect_client_con_state_t));
memset(cstate, 0, sizeof(preconnect_client_con_state_t));
cstate->u = newLine(tid);

return cstate;
}

static void destroy_cstate(preconnect_client_con_state_t *cstate)
{
destroyLine(cstate->u);

free(cstate);
}
static void do_connect(struct connect_arg *cg)
{
tunnel_t *self = cg->t;
preconnect_client_state_t *state = STATE(self);
preconnect_client_con_state_t *cstate = create_cstate(cg->tid);
free(cg);
(cstate->u->chains_state)[self->chain_index] = cstate;
self->up->upStream(self->up, newInitContext(cstate->u));
}

static void connect_timer_finished(htimer_t *timer)
{
do_connect(hevent_userdata(timer));
htimer_del(timer);
}
static void before_connect(hevent_t *ev)
{
struct connect_arg *cg = hevent_userdata(ev);
htimer_t *connect_timer = htimer_add(loops[cg->tid], connect_timer_finished, PRECONNECT_DELAY, 1);
hevent_set_userdata(connect_timer, cg);
}

static void initiateConnect(tunnel_t *t)
{
if (STATE(t)->unused_cons >= STATE(t)->min_unused_cons)
return;

int tid = 0;
if (threads_count > 0)
{
tid = atomic_fetch_add_explicit(&(STATE(t)->round_index), 1, memory_order_relaxed);

if (tid >= threads_count)
{
atomic_store_explicit(&(STATE(t)->round_index), 0, memory_order_relaxed);
tid = 0;
}
}

hloop_t *worker_loop = loops[tid];
hevent_t ev;
memset(&ev, 0, sizeof(ev));
ev.loop = worker_loop;
ev.cb = before_connect;
struct connect_arg *cg = malloc(sizeof(struct connect_arg));
cg->t = t;
cg->tid = tid;
ev.userdata = cg;
hloop_post_event(worker_loop, &ev);
}
Loading

0 comments on commit 989f493

Please sign in to comment.