summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authoryangwei <[email protected]>2023-08-17 21:54:41 +0800
committeryangwei <[email protected]>2023-08-23 12:25:28 +0800
commitf85c57d2002f11fac34cfbaff3af0058c0e7757f (patch)
tree297373e93fc7c9c3c0ada31116659ecc3a8d500d /examples
parent8fffa5090139f72ae53fcba6b346fda389eef2ee (diff)
✨ feat(redefine sdk/include):
Diffstat (limited to 'examples')
-rw-r--r--examples/.gitkeep0
-rw-r--r--examples/sapp_plugin/CMakeLists.txt6
-rw-r--r--examples/sapp_plugin/simple_loader.inf13
-rw-r--r--examples/sapp_plugin/simple_sapp_entry.c154
-rw-r--r--examples/sapp_plugin/simple_stellar_plugin.c151
-rw-r--r--examples/sapp_plugin/simple_stellar_plugin.h11
6 files changed, 335 insertions, 0 deletions
diff --git a/examples/.gitkeep b/examples/.gitkeep
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/examples/.gitkeep
diff --git a/examples/sapp_plugin/CMakeLists.txt b/examples/sapp_plugin/CMakeLists.txt
new file mode 100644
index 0000000..fc524a0
--- /dev/null
+++ b/examples/sapp_plugin/CMakeLists.txt
@@ -0,0 +1,6 @@
+add_definitions(-fPIC)
+
+add_library(simple_loader SHARED simple_sapp_entry.c simple_stellar_plugin.c)
+target_link_libraries(simple_loader adapter)
+set_target_properties(simple_loader PROPERTIES PREFIX "")
+include_directories(${CMAKE_SOURCE_DIR}/src) \ No newline at end of file
diff --git a/examples/sapp_plugin/simple_loader.inf b/examples/sapp_plugin/simple_loader.inf
new file mode 100644
index 0000000..142b3ec
--- /dev/null
+++ b/examples/sapp_plugin/simple_loader.inf
@@ -0,0 +1,13 @@
+[PLUGINFO]
+PLUGNAME=simple_loader
+SO_PATH=./plug/business/simple_loader/simple_loader.so
+INIT_FUNC=LOADER_INIT
+DESTROY_FUNC=LOADER_EXIT
+
+[TCP_ALL]
+FUNC_FLAG=ALL
+FUNC_NAME=loader_tcpall_stream_entry
+
+[UDP]
+FUNC_FLAG=ALL
+FUNC_NAME=loader_udp_stream_entry \ No newline at end of file
diff --git a/examples/sapp_plugin/simple_sapp_entry.c b/examples/sapp_plugin/simple_sapp_entry.c
new file mode 100644
index 0000000..aee7576
--- /dev/null
+++ b/examples/sapp_plugin/simple_sapp_entry.c
@@ -0,0 +1,154 @@
+#include "stellar/session.h"
+#include "stellar/session_mq.h"
+#include "stellar/utils.h"
+
+#include "simple_stellar_plugin.h"
+
+#include "adapter/adapter.h"
+#include "adapter/session_manager.h"
+
+#include <MESA/stream.h>
+#include <stdio.h>
+
+struct plugin_specific g_plugin_schema[] =
+{
+ {
+ .init_cb = simple_stellar_event_plugin_init,
+ .exit_cb = simple_stellar_event_plugin_exit,
+ },
+ {
+ .init_cb = simple_stellar_mq_plugin_init,
+ .exit_cb = simple_stellar_mq_plugin_exit,
+ },
+};
+
+struct simple_stream_ctx
+{
+ uint32_t c2s_pkts;
+ uint32_t c2s_bytes;
+ uint32_t s2c_pkts;
+ uint32_t s2c_bytes;
+ struct session *sess;
+};
+
+static void session_mq_topic_free(void *data, void *cb_arg)
+{
+ FREE(data);
+ return;
+}
+
+static int session_mq_loader_read(struct session *sess, int topic_id, const void *data, void *cb_arg)
+{
+ struct simple_stream_ctx *ctx =(struct simple_stream_ctx *)data;
+ printf("loader_read_message(topic:%d)-----------%20s", topic_id, session_get0_readable_addr(sess));
+ printf("server-pkt=%u, server-count=%u, client-pkt=%u, client-count=%u, ",
+ ctx->c2s_pkts, ctx->c2s_bytes,
+ ctx->s2c_pkts, ctx->s2c_bytes);
+ printf("total-pkt=%u, ", ctx->c2s_pkts+ctx->s2c_pkts);
+ printf("total-count=%u\n", ctx->c2s_bytes+ctx->s2c_bytes);
+ return 0;
+}
+
+void *g_stellar=NULL;
+int g_topic_id=-1;
+int LOADER_INIT()
+{
+ g_stellar = stellar_init(g_plugin_schema, 2);
+ if(g_stellar==NULL)return -1;
+ int t_topic_id=session_mq_get_topic_id(g_stellar, "SIMPLE_MQ_TOPIC");
+ if(t_topic_id >= 0)
+ {
+ session_mq_update_topic(g_stellar, t_topic_id, session_mq_topic_free, NULL);
+ g_topic_id=t_topic_id;
+ }
+ else
+ {
+ g_topic_id=session_mq_create_topic(g_stellar, "SIMPLE_MQ_TOPIC", session_mq_topic_free, NULL);
+ }
+ session_mq_subscribe_topic(g_stellar , g_topic_id, session_mq_loader_read, NULL);
+ return 0;
+}
+
+void LOADER_EXIT(void)
+{
+
+ session_mq_destroy_topic(g_stellar, g_topic_id);
+ stellar_exit(g_stellar);
+ return;
+}
+
+
+static void print_stream_ctx(struct streaminfo *pstream, struct simple_stream_ctx *ctx)
+{
+ printf("stream-----------%20s: ", printaddr(&(pstream->addr), pstream->threadnum));
+ printf("server-pkt=%u, server-count=%u, client-pkt=%u, client-count=%u, ",
+ ctx->c2s_pkts, ctx->c2s_bytes,
+ ctx->s2c_pkts, ctx->s2c_bytes);
+ printf("total-pkt=%u, ", ctx->c2s_pkts+ctx->s2c_pkts);
+ printf("total-count=%u\n", ctx->c2s_bytes+ctx->s2c_bytes);
+ return;
+}
+
+static struct simple_stream_ctx *stream_ctx_dup(const struct simple_stream_ctx *origin)
+{
+ struct simple_stream_ctx *ctx=CALLOC(struct simple_stream_ctx,1);
+ memcpy(ctx, origin, sizeof(struct simple_stream_ctx));
+ return ctx;
+}
+
+static void loader_transfer_stream_entry(struct streaminfo *pstream, UCHAR state, void **pme, int thread_seq,void *a_packet)
+{
+ struct simple_stream_ctx *ctx=(struct simple_stream_ctx *)*pme;
+ struct tcpdetail *pdetail=(struct tcpdetail *)pstream->pdetail;
+ if(*pme==NULL)
+ {
+ *pme=CALLOC(struct simple_stream_ctx,1);
+ ctx=(struct simple_stream_ctx *)*pme;
+ }
+
+ if(a_packet!=NULL)
+ {
+ if(DIR_C2S == pstream->curdir){
+ ctx->c2s_bytes += pdetail->datalen;
+ ctx->c2s_pkts++;
+ }else{
+ ctx->s2c_bytes += pdetail->datalen;
+ ctx->s2c_pkts++;
+ }
+ }
+ struct simple_stream_ctx *msg;
+ switch (state)
+ {
+ case OP_STATE_PENDING:
+ ctx->sess=adapter_session_open(g_stellar, pstream, a_packet);
+ break;
+ case OP_STATE_DATA:
+ msg = stream_ctx_dup((const struct simple_stream_ctx *)*pme);
+ if(session_mq_publish_message(ctx->sess, g_topic_id, msg) < 0)
+ {
+ FREE(msg);
+ }
+ adapter_session_active(pstream, ctx->sess, a_packet);
+ break;
+ case OP_STATE_CLOSE:
+ adapter_session_close(pstream, ctx->sess, a_packet);
+ print_stream_ctx(pstream, ctx);
+ FREE(*pme);
+ break;
+ default:
+ break;
+ }
+ return;
+}
+
+char loader_udp_stream_entry(struct streaminfo *pstream,void **pme, int thread_seq,void *a_packet)
+{
+ loader_transfer_stream_entry(pstream, pstream->opstate, pme, thread_seq, a_packet);
+ return APP_STATE_GIVEME;
+}
+
+char loader_tcpall_stream_entry(struct streaminfo *pstream,void **pme, int thread_seq,void *a_packet)
+{
+ loader_transfer_stream_entry(pstream, pstream->pktstate, pme, thread_seq, a_packet);
+ return APP_STATE_GIVEME;
+}
diff --git a/examples/sapp_plugin/simple_stellar_plugin.c b/examples/sapp_plugin/simple_stellar_plugin.c
new file mode 100644
index 0000000..7084ceb
--- /dev/null
+++ b/examples/sapp_plugin/simple_stellar_plugin.c
@@ -0,0 +1,151 @@
+#include "simple_stellar_plugin.h"
+
+#include "stellar/stellar.h"
+#include "stellar/utils.h"
+#include "stellar/session_exdata.h"
+#include "stellar/session_mq.h"
+
+#include <stdio.h>
+
+struct simple_stellar_plugin_ctx
+{
+ int plugin_id;
+ int exdata_idx;
+ struct stellar *st;
+};
+
+struct mq_message_stat
+{
+ uint32_t c2s_pkts;
+ uint32_t c2s_bytes;
+ uint32_t s2c_pkts;
+ uint32_t s2c_bytes;
+};
+
+extern int simple_mq_plugin_entry(struct session *sess, int events, const struct packet *pkt, void *cb_arg);
+
+static int session_mq_plugin_sub_fn(struct session *sess, int topic_id, const void *data, void *cb_arg)
+{
+ struct mq_message_stat *ctx =(struct mq_message_stat *)data;
+ struct simple_stellar_plugin_ctx *plugin_ctx = (struct simple_stellar_plugin_ctx *)cb_arg;
+ printf("%s(topic:%d->plug:%d)-----------%20s: ", __FUNCTION__, topic_id, plugin_ctx->plugin_id, session_get0_readable_addr(sess));
+ printf("server-pkt=%u, server-count=%u, client-pkt=%u, client-count=%u, ",
+ ctx->c2s_pkts, ctx->c2s_bytes,
+ ctx->s2c_pkts, ctx->s2c_bytes);
+ printf("total-pkt=%u, ", ctx->c2s_pkts+ctx->s2c_pkts);
+ printf("total-count=%u\n", ctx->c2s_bytes+ctx->s2c_bytes);
+
+ struct session_event *i_ev=session_get_intrinsic_event(sess, plugin_ctx->plugin_id);
+ session_event_assign(i_ev, plugin_ctx->st, sess, (SESS_EV_TCP|SESS_EV_UDP|SESS_EV_OPENING|SESS_EV_PACKET|SESS_EV_CLOSING), simple_mq_plugin_entry, plugin_ctx);
+ printf("%s(plug:%d)session_event_assign-----------%20s\n", __FUNCTION__, plugin_ctx->plugin_id, session_get0_readable_addr(sess));
+
+ return 0;
+}
+
+static void print_session_ctx(struct session *sess, struct mq_message_stat *ctx, int plugin_id)
+{
+ printf("%s(plug:%d)-----------%20s: ", __FUNCTION__, plugin_id, session_get0_readable_addr(sess));
+ printf("server-pkt=%u, server-count=%u, client-pkt=%u, client-count=%u, ",
+ ctx->c2s_pkts, ctx->c2s_bytes,
+ ctx->s2c_pkts, ctx->s2c_bytes);
+ printf("total-pkt=%u, ", ctx->c2s_pkts+ctx->s2c_pkts);
+ printf("total-count=%u\n", ctx->c2s_bytes+ctx->s2c_bytes);
+ return;
+}
+
+int simple_event_plugin_entry(struct session *sess, int events, const struct packet *pkt, void *cb_arg)
+{
+ if(cb_arg == NULL)return -1;
+ struct simple_stellar_plugin_ctx *plugin_ctx=(struct simple_stellar_plugin_ctx *)cb_arg;
+ struct mq_message_stat *mg_stat = (struct mq_message_stat *)session_get_ex_data(sess, plugin_ctx->exdata_idx);
+ if (mg_stat == NULL)
+ {
+ mg_stat = CALLOC(struct mq_message_stat, 1);
+ session_set_ex_data(sess, plugin_ctx->exdata_idx, mg_stat);
+ }
+
+ if (pkt)
+ {
+ size_t payload_len = 0;
+ session_get0_current_payload(sess, &payload_len);
+ int dir = session_get_direction(sess);
+ if (dir==SESSION_DIRECTION_IN)
+ {
+ mg_stat->c2s_bytes += payload_len;
+ mg_stat->c2s_pkts += 1;
+ }
+ if (dir==SESSION_DIRECTION_OUT)
+ {
+ mg_stat->s2c_bytes += payload_len;
+ mg_stat->s2c_pkts += 1;
+ }
+ }
+ if (mg_stat != NULL && (events & SESS_EV_CLOSING))
+ {
+ print_session_ctx(sess, mg_stat, plugin_ctx->plugin_id);
+ }
+ return 0;
+}
+
+int simple_mq_plugin_entry(struct session *sess, int events, const struct packet *pkt, void *cb_arg)
+{
+ if(cb_arg == NULL)return -1;
+ struct simple_stellar_plugin_ctx *plugin_ctx=(struct simple_stellar_plugin_ctx *)cb_arg;
+ struct session_event *i_ev=session_get_intrinsic_event(sess, plugin_ctx->plugin_id);
+ session_event_assign(i_ev, plugin_ctx->st, sess, 0, simple_mq_plugin_entry, plugin_ctx);
+ printf("%s(plug:%d)session_event_assign-----------%20s: \n", __FUNCTION__, plugin_ctx->plugin_id, session_get0_readable_addr(sess));
+ return 0;
+}
+
+static void simple_exdata_free(struct session *sess, int idx, void *ex_ptr, void *arg)
+{
+ if(ex_ptr)
+ {
+ FREE(ex_ptr);
+ }
+ return;
+}
+
+void *simple_stellar_event_plugin_init(struct stellar *st)
+{
+ struct simple_stellar_plugin_ctx *ctx = CALLOC(struct simple_stellar_plugin_ctx, 1);
+ ctx->st = st;
+ ctx->exdata_idx = stellar_session_get_ex_new_index(st, "SIMPLE_EVENT_PLUGIN", simple_exdata_free, NULL);
+ int plugin_id=stellar_plugin_register(st, (SESS_EV_TCP|SESS_EV_UDP|SESS_EV_OPENING|SESS_EV_PACKET|SESS_EV_CLOSING), simple_event_plugin_entry, ctx);
+ if(plugin_id >= 0)
+ {
+ ctx->plugin_id=plugin_id;
+ }
+ return ctx;
+}
+
+void simple_stellar_event_plugin_exit(void *ctx)
+{
+ if(ctx)FREE(ctx);
+ return;
+}
+
+void *simple_stellar_mq_plugin_init(struct stellar *st)
+{
+ struct simple_stellar_plugin_ctx *ctx = CALLOC(struct simple_stellar_plugin_ctx, 1);
+ ctx->st = st;
+ ctx->exdata_idx = stellar_session_get_ex_new_index(st, "SIMPLE_MQ_PLUGIN", simple_exdata_free, NULL);
+ int topic_id=session_mq_get_topic_id(st, "SIMPLE_MQ_TOPIC");
+ if(topic_id < 0)
+ {
+ topic_id=session_mq_create_topic(st, "SIMPLE_MQ_TOPIC", NULL, NULL);
+ }
+ session_mq_subscribe_topic(st, topic_id, session_mq_plugin_sub_fn, ctx);
+ int plugin_id=stellar_plugin_register(st, 0, simple_mq_plugin_entry, ctx);
+ if(plugin_id >= 0)
+ {
+ ctx->plugin_id=plugin_id;
+ }
+ return ctx;
+}
+
+void simple_stellar_mq_plugin_exit(void *ctx)
+{
+ if(ctx)FREE(ctx);
+ return;
+} \ No newline at end of file
diff --git a/examples/sapp_plugin/simple_stellar_plugin.h b/examples/sapp_plugin/simple_stellar_plugin.h
new file mode 100644
index 0000000..1e7e7ba
--- /dev/null
+++ b/examples/sapp_plugin/simple_stellar_plugin.h
@@ -0,0 +1,11 @@
+#pragma once
+
+#include "stellar/stellar.h"
+#include "stellar/session.h"
+
+void *simple_stellar_event_plugin_init(struct stellar *st);
+void simple_stellar_event_plugin_exit(void *plugin_ctx);
+
+void *simple_stellar_mq_plugin_init(struct stellar *st);
+void simple_stellar_mq_plugin_exit(void *plugin_ctx);
+