summaryrefslogtreecommitdiff
path: root/sdk/example/custom_event_plugin.cpp
blob: 6419eb9932c0decb51325e571c9e407d5c12663f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#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;
	}
}