Skip to content

Commit

Permalink
add preconnect server
Browse files Browse the repository at this point in the history
  • Loading branch information
radkesvat committed Apr 3, 2024
1 parent 8dee1bb commit e8ff7a6
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 0 deletions.
37 changes: 37 additions & 0 deletions tunnels/server/preconnect/CMakeLists.txt
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()
124 changes: 124 additions & 0 deletions tunnels/server/preconnect/preconnect_server.c
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};
}
15 changes: 15 additions & 0 deletions tunnels/server/preconnect/preconnect_server.h
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();



0 comments on commit e8ff7a6

Please sign in to comment.