* plugin_event_callback delete 'struct stellar_packet *p'
* plugin_event_callback rename 'uint16_t len' to 'size_t len'
* plugin_event_callback rename 'void **pme' to 'void **ctx'
134 lines
3.1 KiB
C++
134 lines
3.1 KiB
C++
#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 stellar_session_event_extras *info = (struct stellar_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;
|
|
}
|
|
} |