This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
stellar-stellar-2022/sdk/example/http_event_plugin.cpp

103 lines
2.3 KiB
C++
Raw Normal View History

#include "session.h"
#include "packet.h"
#include "plugin.h"
2022-07-27 18:32:22 +08:00
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
2022-07-27 18:32:22 +08:00
static char *g_handler = NULL;
struct http_session_pme
{
2022-07-27 18:32:22 +08:00
char data[16];
/* data */;
2022-07-27 18:32:22 +08:00
};
static struct http_session_pme *http_session_pme_create()
2022-07-27 18:32:22 +08:00
{
struct http_session_pme *pme = (struct http_session_pme *)calloc(1, sizeof(struct http_session_pme));
return pme;
}
static void http_session_pme_destory(struct http_session_pme *pme)
{
if (pme)
{
free(pme);
pme = NULL;
}
}
extern "C" void http_event_plugin_error(const struct stellar_session *session, enum error_event_type event, void **pme)
{
if (strcmp(stellar_session_get_name(session), "HTTP") == 0)
{
struct http_session_pme **per_http_session_pme = (struct http_session_pme **)pme;
printf("RUN http_event_plugin_error, session_name: 'HTTP', error_event_type: %d, pme->data: %s\n", event, (*per_http_session_pme) == NULL ? "NULL" : (*per_http_session_pme)->data);
http_session_pme_destory(*per_http_session_pme);
}
}
extern "C" void http_event_plugin_entry(const struct stellar_session *session, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme)
{
struct http_session_pme **per_http_session_pme = (struct http_session_pme **)pme;
2022-07-27 18:32:22 +08:00
printf("RUN http_event_plugin_entry, event: %d\n", event);
if (event & SESSION_EVENT_OPENING)
{
if (*per_http_session_pme == NULL)
2022-07-27 18:32:22 +08:00
{
struct http_session_pme *cur_ctx = http_session_pme_create();
memcpy(cur_ctx->data, "http_event_plugin_entry", strlen("http_event_plugin_entry"));
*per_http_session_pme = *&cur_ctx;
2022-07-27 18:32:22 +08:00
}
}
if (event & SESSION_EVENT_RAWPKT)
{
// TODO
pm_session_dettach_me(session);
2022-07-27 18:32:22 +08:00
}
if (event & SESSION_EVENT_ORDPKT)
{
// TODO
pm_session_dettach_others(session);
2022-07-27 18:32:22 +08:00
}
if (event & SESSION_EVENT_META)
{
// TODO
2022-07-27 18:32:22 +08:00
}
if (event & SESSION_EVENT_CLOSING)
{
http_session_pme_destory(*per_http_session_pme);
2022-07-27 18:32:22 +08:00
}
}
2022-07-27 18:32:22 +08:00
extern "C" int http_event_plugin_init(void)
{
2022-07-27 18:32:22 +08:00
printf("RUN http_event_plugin_init\n");
if (g_handler == NULL)
{
g_handler = (char *)malloc(1024);
snprintf(g_handler, 1024, "111111");
}
return 0;
}
2022-07-27 18:32:22 +08:00
extern "C" void http_event_plugin_exit(void)
{
2022-07-27 18:32:22 +08:00
printf("RUN http_event_plugin_exit\n");
if (g_handler)
{
free(g_handler);
g_handler = NULL;
}
}