From 30c1c1b9c8c7473df753c0a145daaf69d6d05e76 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Mon, 11 Feb 2019 18:56:52 +0100 Subject: [PATCH] WIP event core + GLib --- CMakeLists.txt | 9 ++++++++- src/evt_core.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/evt_core.h | 29 +++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 src/evt_core.c create mode 100644 src/evt_core.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 4108794..516e327 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,6 @@ cmake_minimum_required(VERSION 2.6) project(donar) - list(APPEND CSOURCES src/net_tools.h src/net_tools.c @@ -15,10 +14,18 @@ list(APPEND CSOURCES src/donar_client.c src/donar_server.h src/donar_server.c + src/evt_core.h + src/evt_core.c ) add_executable(donar-proxy ${CSOURCES} src/donar_proxy.c) +find_package(PkgConfig REQUIRED) +pkg_search_module(GLIB REQUIRED glib-2.0) +target_include_directories(donar-proxy PRIVATE ${GLIB_INCLUDE_DIRS}) +target_link_libraries(donar-proxy ${GLIB_LDFLAGS}) + + install(TARGETS donar-proxy RUNTIME DESTINATION bin LIBRARY DESTINATION lib) \ No newline at end of file diff --git a/src/evt_core.c b/src/evt_core.c new file mode 100644 index 0000000..af2ffa9 --- /dev/null +++ b/src/evt_core.c @@ -0,0 +1,46 @@ +#include "evt_core.h" + +void free_int(void* v) { + free(v); +} + +void free_char(void* c) { + free(c); +} + +void free_cat(void* vcat) { + struct evt_core_cat* cat = (struct evt_core_cat*) vcat; + cat->free_app_ctx(cat->app_ctx); +} + +void evt_core_init(struct evt_core_ctx* ctx) { + ctx->epollfd = epoll_create1(0); + if (ctx->epollfd == -1) { + perror("Failed to create epoll file descriptor epoll_create1"); + exit(EXIT_FAILURE); + } + + ctx->catlist = g_hash_table_new_full(g_str_hash, g_str_equal,free_char,free_cat); + ctx->socklist = g_hash_table_new_full(g_int_hash, g_int_equal,free_int, NULL); +} + +void evt_core_add_cat(struct evt_core_cat* cat) { + struct evt_core_cat* dyn = NULL; + dyn = malloc(sizeof(struct evt_core_cat)); + if (dyn == NULL) { + fprintf(stderr, "Failed to alloc memory"); + exit(EXIT_FAILURE); + } + dyn->app_ctx = cat->app_ctx; + dyn->free_app_ctx = cat->free_app_ctx; + dyn->cb = cat->cb; + dyn->name = strdup(cat->name); + dyn->flags = cat->flags; + + if (dyn->name == NULL) { + perror("Unable to allocate memory for category name via strdup"); + exit(EXIT_FAILURE); + } + + +} diff --git a/src/evt_core.h b/src/evt_core.h new file mode 100644 index 0000000..f434c4a --- /dev/null +++ b/src/evt_core.h @@ -0,0 +1,29 @@ +#pragma once +#include +#include +#include +#include +#include +#include + +struct evt_core_ctx; +struct evt_core_cat; + +typedef void (*evt_core_free_app_ctx)(void*); +typedef void (*evt_core_cb)(struct evt_core_ctx*, struct evt_core_cat*); + +struct evt_core_cat { + void* app_ctx; + evt_core_free_app_ctx free_app_ctx; + evt_core_cb cb; + char* name; + int flags; +}; + +struct evt_core_ctx { + int epollfd; + GHashTable* catlist; + GHashTable* socklist; +}; + +void evt_core_init(struct evt_core_ctx* ctx);