Add experimental C-API for evaluation

This commit is contained in:
Alex Hultman
2021-02-07 20:11:29 +01:00
parent 8dcca10d07
commit 0a44518fc0
4 changed files with 88 additions and 0 deletions
+6
View File
@@ -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"
+34
View File
@@ -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<false> *uwsRes = (uWS::HttpResponse<false> *) 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);
});
}
}
+19
View File
@@ -0,0 +1,19 @@
#include "libuwebsockets.h"
#include <stdio.h>
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);
}
+29
View File
@@ -0,0 +1,29 @@
#ifndef LIBUWS_CAPI_HEADER
#define LIBUS_CAPI_HEADER
#include <stddef.h>
#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