plugin_manager adapt session_manager API and rewrite plugin_manger gtest

This commit is contained in:
luwenpeng
2022-08-26 17:33:03 +08:00
parent e3ea4d0729
commit 0ed9a0ccca
52 changed files with 1161 additions and 1575 deletions

View File

@@ -1,9 +1,4 @@
add_library(custom_event_plugin SHARED
custom_event_plugin.cpp
add_library(http_plugin SHARED
http_plugin.cpp
)
set_target_properties(custom_event_plugin PROPERTIES PREFIX "")
add_library(http_event_plugin SHARED
http_event_plugin.cpp
)
set_target_properties(http_event_plugin PROPERTIES PREFIX "")
set_target_properties(http_plugin PROPERTIES PREFIX "")

View File

@@ -1,134 +0,0 @@
#include "session.h"
#include "packet.h"
#include "plugin.h"
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
static char *g_handler = NULL;
static void *custom_decode(const char *payload, size_t len, void **ctx)
{
return NULL;
}
struct tcp_session_ctx
{
char data[16];
/* data */
};
struct custom_session_ctx
{
char data[16];
/* data */
};
static struct tcp_session_ctx *tcp_session_ctx_create()
{
struct tcp_session_ctx *ctx = (struct tcp_session_ctx *)calloc(1, sizeof(struct tcp_session_ctx));
return ctx;
}
static void tcp_session_ctx_destory(struct tcp_session_ctx *ctx)
{
if (ctx)
{
free(ctx);
ctx = NULL;
}
}
static struct custom_session_ctx *custom_session_ctx_create()
{
struct custom_session_ctx *ctx = (struct custom_session_ctx *)calloc(1, sizeof(struct custom_session_ctx));
return ctx;
}
static void custom_session_ctx_destory(struct custom_session_ctx *ctx)
{
if (ctx)
{
free(ctx);
ctx = NULL;
}
}
extern "C" void custom_event_plugin_tcp_entry(const struct stellar_session *session, enum session_event_type event, const char *payload, size_t len, void **ctx)
{
struct tcp_session_ctx **per_tcp_session_ctx = (struct tcp_session_ctx **)ctx;
printf("RUN custom_event_plugin_tcp_entry, event: %d\n", event);
if (event & SESSION_EVENT_OPENING)
{
if (*per_tcp_session_ctx == NULL)
{
struct tcp_session_ctx *cur_ctx = tcp_session_ctx_create();
memcpy(cur_ctx->data, "custom_event_plugin_tcp_entry", strlen("custom_event_plugin_tcp_entry"));
*per_tcp_session_ctx = *&cur_ctx;
}
}
if (event & SESSION_EVENT_ORDPKT)
{
struct session_event_extras *info = (struct session_event_extras *)custom_decode(payload, len, ctx);
struct stellar_session *new_session = session_manager_session_derive(session, "CUSTOM");
session_manager_trigger_event(new_session, SESSION_EVENT_OPENING, info);
session_manager_trigger_event(new_session, SESSION_EVENT_META, info);
}
if (event & SESSION_EVENT_CLOSING)
{
tcp_session_ctx_destory(*per_tcp_session_ctx);
}
}
extern "C" void custom_event_plugin_custom_entry(const struct stellar_session *session, enum session_event_type event, const char *payload, size_t len, void **ctx)
{
struct custom_session_ctx **per_custom_session_ctx = (struct custom_session_ctx **)ctx;
printf("RUN custom_event_plugin_custom_entry, event: %d\n", event);
if (event & SESSION_EVENT_OPENING)
{
if (*per_custom_session_ctx == NULL)
{
struct custom_session_ctx *cur_ctx = custom_session_ctx_create();
memcpy(cur_ctx->data, "custom_event_plugin_custom_entry", strlen("custom_event_plugin_custom_entry"));
*per_custom_session_ctx = *&cur_ctx;
}
}
if (event & SESSION_EVENT_CLOSING)
{
custom_session_ctx_destory(*per_custom_session_ctx);
}
}
extern "C" int custom_event_plugin_init(void)
{
printf("RUN custom_event_plugin_init\n");
if (g_handler == NULL)
{
g_handler = (char *)malloc(1024);
snprintf(g_handler, 1024, "222222");
}
return 0;
}
extern "C" void custom_event_plugin_exit(void)
{
printf("RUN custom_event_plugin_exit\n");
if (g_handler)
{
free(g_handler);
g_handler = NULL;
}
}

View File

@@ -1,93 +0,0 @@
#include "session.h"
#include "packet.h"
#include "plugin.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
static char *g_handler = NULL;
struct http_session_ctx
{
char data[16];
/* data */;
};
static struct http_session_ctx *http_session_ctx_create()
{
struct http_session_ctx *ctx = (struct http_session_ctx *)calloc(1, sizeof(struct http_session_ctx));
return ctx;
}
static void http_session_ctx_destory(struct http_session_ctx *ctx)
{
if (ctx)
{
free(ctx);
ctx = NULL;
}
}
extern "C" void http_event_plugin_entry(const struct stellar_session *session, enum session_event_type event, const char *payload, size_t len, void **ctx)
{
struct http_session_ctx **per_http_session_ctx = (struct http_session_ctx **)ctx;
printf("RUN http_event_plugin_entry, event: %d\n", event);
if (event & SESSION_EVENT_OPENING)
{
if (*per_http_session_ctx == NULL)
{
struct http_session_ctx *cur_ctx = http_session_ctx_create();
memcpy(cur_ctx->data, "http_event_plugin_entry", strlen("http_event_plugin_entry"));
*per_http_session_ctx = *&cur_ctx;
}
}
if (event & SESSION_EVENT_RAWPKT)
{
// TODO
pm_session_dettach_me(session);
}
if (event & SESSION_EVENT_ORDPKT)
{
// TODO
pm_session_take_over(session);
}
if (event & SESSION_EVENT_META)
{
// TODO
}
if (event & SESSION_EVENT_CLOSING)
{
http_session_ctx_destory(*per_http_session_ctx);
}
}
extern "C" int http_event_plugin_init(void)
{
printf("RUN http_event_plugin_init\n");
if (g_handler == NULL)
{
g_handler = (char *)malloc(1024);
snprintf(g_handler, 1024, "111111");
}
return 0;
}
extern "C" void http_event_plugin_exit(void)
{
printf("RUN http_event_plugin_exit\n");
if (g_handler)
{
free(g_handler);
g_handler = NULL;
}
}

View File

@@ -0,0 +1,70 @@
#include <stdlib.h>
#include "session.h"
static char *g_handler = NULL;
struct session_ctx
{
char data[64];
};
static struct session_ctx *session_ctx_create()
{
struct session_ctx *ctx = (struct session_ctx *)calloc(1, sizeof(struct session_ctx));
return ctx;
}
static void session_ctx_destory(struct session_ctx *ctx)
{
if (ctx)
{
free(ctx);
ctx = NULL;
}
}
extern "C" void http_plugin_entry(struct stellar_session *session, enum session_state state, int thread_id, void **ctx)
{
struct session_ctx **sctx = (struct session_ctx **)ctx;
if (state & SESSION_STATE_OPENING)
{
if (*sctx == NULL)
{
struct session_ctx *cur_ctx = session_ctx_create();
memcpy(cur_ctx->data, "http_plugin_entry", 17);
*sctx = *&cur_ctx;
}
}
if (state & SESSION_STATE_ACTIVE)
{
// TODO
}
if (state & SESSION_STATE_CLOSING)
{
session_ctx_destory(*sctx);
*sctx = NULL;
}
}
extern "C" int http_plugin_init(void)
{
if (g_handler == NULL)
{
g_handler = (char *)malloc(1024);
}
return 0;
}
extern "C" void http_plugin_exit(void)
{
if (g_handler)
{
free(g_handler);
g_handler = NULL;
}
}

3
sdk/example/plugins.inf Normal file
View File

@@ -0,0 +1,3 @@
# Relative path, relative to the installation path of stellar
./plugins/http_plugin.inf

View File

@@ -1,14 +0,0 @@
[PLUGINFO]
INIT_FUNC="custom_event_plugin_init"
EXIT_FUNC="custom_event_plugin_exit"
LIBRARY_PATH="./plugins/custom_event_plugin/custom_event_plugin.so"
# Support SESSION_EVENT_TYPE: "SESSION_EVENT_OPENING", "SESSION_EVENT_RAWPKT", "SESSION_EVENT_ORDPKT", "SESSION_EVENT_META", "SESSION_EVENT_CLOSING", "SESSION_EVENT_ALL"
[SESSION_NAME.TCP]
SESSION_EVENT_TYPE=["SESSION_EVENT_ALL"]
SESSION_EVENT_CALLBACK="custom_event_plugin_tcp_entry"
[SESSION_NAME.CUSTOM]
SESSION_EVENT_TYPE=["SESSION_EVENT_OPENING","SESSION_EVENT_ORDPKT","SESSION_EVENT_CLOSING"]
SESSION_EVENT_CALLBACK="custom_event_plugin_custom_entry"

View File

@@ -1,10 +0,0 @@
[PLUGINFO]
INIT_FUNC="http_event_plugin_init"
EXIT_FUNC="http_event_plugin_exit"
LIBRARY_PATH="./plugins/http_event_plugin/http_event_plugin.so"
# Support SESSION_EVENT_TYPE: "SESSION_EVENT_OPENING", "SESSION_EVENT_RAWPKT", "SESSION_EVENT_ORDPKT", "SESSION_EVENT_META", "SESSION_EVENT_CLOSING", "SESSION_EVENT_ALL"
[SESSION_NAME.HTTP]
SESSION_EVENT_TYPE=["SESSION_EVENT_ALL"]
SESSION_EVENT_CALLBACK="http_event_plugin_entry"

View File

@@ -0,0 +1,10 @@
[PLUGINFO]
INIT_FUNC="http_plugin_init"
EXIT_FUNC="http_plugin_exit"
LIBRARY_PATH="./plugins/http_plugin/http_plugin.so"
# Support SESSION_STATE: "SESSION_STATE_OPENING", "SESSION_STATE_ACTIVE", "SESSION_STATE_CLOSING", "SESSION_STATE_ALL"
[SESSION_NAME.HTTP]
SESSION_STATE=["SESSION_STATE_ALL"]
SESSION_ENTRY_CALLBACK="http_plugin_entry"

View File

@@ -1,4 +0,0 @@
# Relative path, relative to the installation path of stellar
./plugins/http_event_plugin/http_event_plugin.inf
./plugins/custom_event_plugin/custom_event_plugin.inf

View File

@@ -14,7 +14,7 @@ extern "C"
* Public API: For build in
******************************************************************************/
void http_entry(const struct stellar_session *session, enum session_event_type event, const char *payload, size_t len, void **ctx);
void http_entry(struct stellar_session *session, enum session_state state, int thread_id, void **ctx);
/******************************************************************************
* Public API: For http consumer
@@ -56,7 +56,7 @@ struct http_status_line
int minor_version;
};
struct http_decoder *http_session_get_decoder(const struct stellar_session *http_session);
struct http_decoder *http_session_get_decoder(struct stellar_session *http_session);
enum http_event http_decoder_get_event(struct http_decoder *decoder);
/*
@@ -73,7 +73,7 @@ void http_decoder_fetch_next_header(struct http_decoder *decoder, int *iter_inde
* Example: How to implement http consumer
******************************************************************************/
/*
void http_consumer_entry_example(const struct stellar_session *http_session, enum session_event_type event, const char *payload, size_t len, void **ctx)
void http_consumer_entry_example(struct stellar_session *http_session, enum session_event_type event, const char *payload, size_t len, void **ctx)
{
if (event & SESSION_EVENT_OPENING)
{

View File

@@ -12,7 +12,7 @@ extern "C"
typedef int plugin_init_callback(void);
typedef void plugin_exit_callback(void);
typedef void plugin_event_callback(const struct stellar_session *session, enum session_state state, int thread_id, void **ctx);
typedef void plugin_entry_callback(struct stellar_session *session, enum session_state state, int thread_id, void **ctx);
/******************************************************************************
* Public API For Plugin
@@ -22,7 +22,7 @@ typedef void plugin_event_callback(const struct stellar_session *session, enum s
* The pm_session_dettach_me just sets the flag to disable this plugin and no longer call this event callback.
* Before calling pm_session_dettach_me, the current plugin must release related resources for the current session.
*/
void pm_session_dettach_me(const struct stellar_session *session);
void pm_session_dettach_me(struct stellar_session *session);
/*
* The current plugin(cb2) takes over the current session, the pm_session_take_over setting flag disables other plugins,
@@ -38,7 +38,7 @@ void pm_session_dettach_me(const struct stellar_session *session);
* A plugin(cb1/cb3/cb4) that is taken over, if the plugin was called before being taken over and has a registered SESSION_EVENT_CLOSING event,
* it will be called again when the SESSION_EVENT_CLOSING event comes. Otherwise, the plugin will not be called.
*/
void pm_session_take_over(const struct stellar_session *session);
void pm_session_take_over(struct stellar_session *session);
#ifdef __cpluscplus
}

View File

@@ -38,43 +38,27 @@ enum session_type
SESSION_TYPE_MAX,
};
//http decoder
enum http_stage
{
HTTP_STAGE_REQ_HDR=1<<1,
HTTP_STAGE_REQ_BODY_BEGIN=1<<2,
HTTP_STAGE_REQ_BODY_CONTINUE=1<<3,
HTTP_STAGE_REQ_BODY_END,
HTTP_STAGE_RESP_HDR,
HTTP_STAGE_RESP_BODY
};
//dns decoder
enum dns_stage
{
DNS_STAGE_REQUEST,
DNS_STAGE_RESPONSE
};
enum session_state
{
SESSION_STATE_OPENING,
SESSION_STATE_ACTIVE,
SESSION_STATE_CLOSING
SESSION_STATE_INVALID = 0,
SESSION_STATE_OPENING = 1 << 1,
SESSION_STATE_ACTIVE = 1 << 2,
SESSION_STATE_CLOSING = 1 << 3,
SESSION_STATE_ALL = (1 << 1 | 1 << 2 | 1 << 3),
};
struct stellar_session;
struct stellar_session *session_derive(const struct stellar_session *this_session, const char *session_name);
void session_close(const struct stellar_session *session);
struct stellar_session *session_derive(struct stellar_session *this_session, const char *session_name);
void session_close(struct stellar_session *session);
const char *session_get_name(const struct stellar_session *session);
const char *session_get_name(struct stellar_session *session);
uint8_t session_get_direction(const struct stellar_session *session); // tcp or udp
uint8_t session_get_current_direction(const struct stellar_session *session); // tcp or udp
uint8_t session_get_direction(struct stellar_session *session); // tcp or udp
uint8_t session_get_current_direction(struct stellar_session *session); // tcp or udp
uint64_t session_get_createtime_ms(const struct stellar_session *session);
uint64_t session_get_lasttime_ms(const struct stellar_session *session);
enum session_type session_get_type(const struct stellar_session *session);
uint64_t session_get_createtime_ms(struct stellar_session *session);
uint64_t session_get_lasttime_ms(struct stellar_session *session);
enum session_type session_get_type(struct stellar_session *session);
struct stellar_packet *session_get_packet(struct stellar_session *session);
enum session_state session_get_state(struct stellar_session *session);
const char *session_get_payload(struct stellar_session *session);