-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce new node: preconnect client
- Loading branch information
Showing
7 changed files
with
452 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.