diff --git a/Makefile b/Makefile index d393dc0..0fcee21 100644 --- a/Makefile +++ b/Makefile @@ -40,6 +40,12 @@ examples: for FILE in $(THREADED_EXAMPLE_FILES); do $(CXX) -pthread -flto -O3 $(CXXFLAGS) examples/$$FILE.cpp -o $$FILE $(LDFLAGS) & done; \ wait +.PHONY: capi +capi: + $(MAKE) -C uSockets + $(CXX) -shared -fPIC -flto -O3 $(CXXFLAGS) capi/App.cpp -o capi.so $(LDFLAGS) + $(CXX) capi/example.c -O3 capi.so -o example + install: mkdir -p "$(DESTDIR)$(prefix)/include/uWebSockets/f2" cp -r src/* "$(DESTDIR)$(prefix)/include/uWebSockets" diff --git a/capi/App.cpp b/capi/App.cpp new file mode 100644 index 0000000..a38ab45 --- /dev/null +++ b/capi/App.cpp @@ -0,0 +1,34 @@ +#include "libuwebsockets.h" +#include "App.h" + +extern "C" { + + uws_app_t *uws_create_app() { + return (uws_app_t *) new uWS::App(); + } + + void uws_app_get(uws_app_t *app, const char *pattern, void (*handler)(uws_res_t *, uws_req_t *)) { + uWS::App *uwsApp = (uWS::App *) app; + uwsApp->get(pattern, [handler](auto *res, auto *req) { + handler((uws_res_t *) res, (uws_req_t *) req); + }); + } + + void uws_app_run(uws_app_t *app) { + uWS::App *uwsApp = (uWS::App *) app; + uwsApp->run(); + } + + void uws_res_end(uws_res_t *res, const char *data, size_t length) { + uWS::HttpResponse *uwsRes = (uWS::HttpResponse *) res; + uwsRes->end(data, length); + } + + void uws_app_listen(uws_app_t *app, int port, void (*handler)(void *)) { + uWS::App *uwsApp = (uWS::App *) app; + uwsApp->listen(port, [handler](struct us_listen_socket_t *listen_socket) { + handler((void *) listen_socket); + }); + } + +} \ No newline at end of file diff --git a/capi/example.c b/capi/example.c new file mode 100644 index 0000000..111ee4a --- /dev/null +++ b/capi/example.c @@ -0,0 +1,19 @@ +#include "libuwebsockets.h" +#include + +void get_handler(uws_res_t *res, uws_req_t *req) { + uws_res_end(res, "Hello CAPI!", 11); +} + +void listen_handler(void *listen_socket) { + if (listen_socket) { + printf("Listening on port now\n"); + } +} + +int main() { + uws_app_t *app = uws_create_app(); + uws_app_get(app, "/*", get_handler); + uws_app_listen(app, 3000, listen_handler); + uws_app_run(app); +} \ No newline at end of file diff --git a/capi/libuwebsockets.h b/capi/libuwebsockets.h new file mode 100644 index 0000000..7e87133 --- /dev/null +++ b/capi/libuwebsockets.h @@ -0,0 +1,29 @@ +#ifndef LIBUWS_CAPI_HEADER +#define LIBUS_CAPI_HEADER + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +struct uws_app_s; +struct uws_req_s; +struct uws_res_s; +typedef struct uws_app_s uws_app_t; +typedef struct uws_req_s uws_req_t; +typedef struct uws_res_s uws_res_t; + +uws_app_t *uws_create_app(); +void uws_app_get(uws_app_t *app, const char *pattern, void (*handler)(uws_res_t *, uws_req_t *)); +void uws_app_run(uws_app_t *); + +void uws_app_listen(uws_app_t *app, int port, void (*handler)(void *)); + +void uws_res_end(uws_res_t *res, const char *data, size_t length); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file