-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
176 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
add_library(PreConnectServer STATIC | ||
preconnect_server.c | ||
|
||
) | ||
|
||
|
||
|
||
#ww api | ||
target_include_directories(PreConnectServer PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../../ww) | ||
target_link_libraries(PreConnectServer 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(PreConnectServer PUBLIC ${stc_SOURCE_DIR}/include) | ||
endif() | ||
|
||
|
||
|
||
target_compile_definitions(PreConnectServer PRIVATE STC_STATIC=1 PreConnectServer_VERSION=0.1) | ||
|
||
|
||
if(CMAKE_BUILD_TYPE STREQUAL "Debug") | ||
target_compile_definitions(PreConnectServer 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,124 @@ | ||
#include "preconnect_server.h" | ||
#include "buffer_stream.h" | ||
#include "hv/hsocket.h" | ||
#include "loggers/network_logger.h" | ||
|
||
#define STATE(x) ((preconnect_server_state_t *)((x)->state)) | ||
#define CSTATE(x) ((preconnect_server_con_state_t *)((((x)->line->chains_state)[self->chain_index]))) | ||
#define CSTATE_MUT(x) ((x)->line->chains_state)[self->chain_index] | ||
#define ISALIVE(x) (CSTATE(x) != NULL) | ||
|
||
typedef struct preconnect_server_state_s | ||
{ | ||
|
||
} preconnect_server_state_t; | ||
|
||
typedef struct preconnect_server_con_state_s | ||
{ | ||
bool init_sent; | ||
|
||
} preconnect_server_con_state_t; | ||
|
||
static void upStream(tunnel_t *self, context_t *c) | ||
{ | ||
|
||
if (c->payload != NULL) | ||
{ | ||
preconnect_server_con_state_t *cstate = CSTATE(c); | ||
if (c->first) | ||
{ | ||
cstate->init_sent = true; | ||
self->up->upStream(self->up, newInitContext(c->line)); | ||
if (!ISALIVE(c)) | ||
{ | ||
DISCARD_CONTEXT(c); | ||
destroyContext(c); | ||
return; | ||
} | ||
} | ||
} | ||
else if (c->init) | ||
{ | ||
preconnect_server_con_state_t *cstate = malloc(sizeof(preconnect_server_con_state_t)); | ||
cstate->init_sent = false; | ||
CSTATE_MUT(c) = cstate; | ||
destroyContext(c); | ||
return; | ||
} | ||
else if (c->fin) | ||
{ | ||
preconnect_server_con_state_t *cstate = CSTATE(c); | ||
bool send_fin = cstate->init_sent; | ||
free(cstate); | ||
CSTATE_MUT(c) = NULL; | ||
if (send_fin) | ||
self->up->upStream(self->up, c); | ||
else | ||
destroyContext(c); | ||
|
||
return; | ||
} | ||
self->up->upStream(self->up, c); | ||
} | ||
|
||
static inline void downStream(tunnel_t *self, context_t *c) | ||
{ | ||
|
||
if (c->fin) | ||
{ | ||
free(cstate); | ||
CSTATE_MUT(c) = NULL; | ||
} | ||
|
||
self->dw->downStream(self->dw, c); | ||
} | ||
|
||
static void preConnectServerUpStream(tunnel_t *self, context_t *c) | ||
{ | ||
upStream(self, c); | ||
} | ||
static void preConnectServerPacketUpStream(tunnel_t *self, context_t *c) | ||
{ | ||
upStream(self, c); | ||
} | ||
static void preConnectServerDownStream(tunnel_t *self, context_t *c) | ||
{ | ||
downStream(self, c); | ||
} | ||
static void preConnectServerPacketDownStream(tunnel_t *self, context_t *c) | ||
{ | ||
downStream(self, c); | ||
} | ||
|
||
tunnel_t *newPreConnectServer(node_instance_context_t *instance_info) | ||
{ | ||
|
||
preconnect_server_state_t *state = malloc(sizeof(preconnect_server_state_t)); | ||
memset(state, 0, sizeof(preconnect_server_state_t)); | ||
|
||
tunnel_t *t = newTunnel(); | ||
t->state = state; | ||
t->upStream = &preConnectServerUpStream; | ||
t->packetUpStream = &preConnectServerPacketUpStream; | ||
t->downStream = &preConnectServerDownStream; | ||
t->packetDownStream = &preConnectServerPacketDownStream; | ||
atomic_thread_fence(memory_order_release); | ||
|
||
return t; | ||
} | ||
|
||
api_result_t apiPreConnectServer(tunnel_t *self, char *msg) | ||
{ | ||
LOGE("preconnect-client API NOT IMPLEMENTED"); | ||
return (api_result_t){0}; // TODO | ||
} | ||
|
||
tunnel_t *destroyPreConnectServer(tunnel_t *self) | ||
{ | ||
LOGE("preconnect-client DESTROY NOT IMPLEMENTED"); // TODO | ||
return NULL; | ||
} | ||
tunnel_metadata_t getMetadataPreConnectServer() | ||
{ | ||
return (tunnel_metadata_t){.version = 0001, .flags = 0x0}; | ||
} |
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,15 @@ | ||
#pragma once | ||
#include "api.h" | ||
|
||
// | ||
// con <------> PreConnectServer (initiate upstream after we've got some data) <-------> con | ||
// | ||
// | ||
|
||
tunnel_t *newPreConnectServer(node_instance_context_t *instance_info); | ||
api_result_t apiPreConnectServer(tunnel_t *self, char *msg); | ||
tunnel_t *destroyPreConnectServer(tunnel_t *self); | ||
tunnel_metadata_t getMetadataPreConnectServer(); | ||
|
||
|
||
|