###########################################
# build and install libafbwsc
###########################################
-ADD_LIBRARY(afbwsc SHARED afb-ws.c afb-ws-client.c afb-wsj1.c websock.c afb-common.c)
+ADD_LIBRARY(afbwsc SHARED afb-ws.c afb-ws-client.c afb-wsj1.c websock.c)
SET_TARGET_PROPERTIES(afbwsc PROPERTIES
VERSION ${LIBAFBWSC_VERSION}
SOVERSION ${LIBAFBWSC_SOVERSION})
/* entry function */
int main(int ac, char **av, char **env)
{
+ int rc;
+ sd_event *loop;
+
/* check the argument count */
if (ac != 2 && ac != 4 && ac != 5)
usage(1, av[0]);
if (!strcmp(av[1], "-h") || !strcmp(av[1], "--help"))
usage(0, av[0]);
+ /* get the default event loop */
+ rc = sd_event_default(&loop);
+ if (rc < 0) {
+ fprintf(stderr, "connection to default event loop failed: %s\n", strerror(-rc));
+ return 1;
+ }
+
/* connect the websocket wsj1 to the uri given by the first argument */
- wsj1 = afb_ws_client_connect_wsj1(av[1], &itf, NULL);
+ wsj1 = afb_ws_client_connect_wsj1(loop, av[1], &itf, NULL);
if (wsj1 == NULL) {
fprintf(stderr, "connection to %s failed: %m\n", av[1]);
return 1;
if (ac == 2) {
/* get requests from stdin */
fcntl(0, F_SETFL, O_NONBLOCK);
- sd_event_add_io(afb_ws_client_get_event_loop(), &evsrc, 0, EPOLLIN, io_event_callback, NULL);
+ sd_event_add_io(loop, &evsrc, 0, EPOLLIN, io_event_callback, NULL);
} else {
/* the request is defined by the arguments */
exonrep = 1;
/* loop until end */
for(;;)
- sd_event_run(afb_ws_client_get_event_loop(), 30000000);
+ sd_event_run(loop, 30000000);
return 0;
}
#include <fcntl.h>
#include "afb-wsj1.h"
-#include "afb-common.h"
/**************** WebSocket handshake ****************************/
errno = EINVAL;
error:
return -1;
-
}
static const char *proto_json1[2] = { "x-afb-ws-json1", NULL };
-struct afb_wsj1 *afb_ws_client_connect_wsj1(const char *uri, struct afb_wsj1_itf *itf, void *closure)
+struct afb_wsj1 *afb_ws_client_connect_wsj1(struct sd_event *eloop, const char *uri, struct afb_wsj1_itf *itf, void *closure)
{
int rc, fd;
char *host, *service, xhost[32];
if (rc == 0) {
rc = negociate(fd, proto_json1, path, xhost);
if (rc == 0) {
- result = afb_wsj1_create(fd, itf, closure);
+ result = afb_wsj1_create(eloop, fd, itf, closure);
if (result != NULL) {
fcntl(fd, F_SETFL, O_NONBLOCK);
break;
}
#endif
-/*
- *
- * Returns the internal event loop coming from afb-common
- *
- * Returns the handle to the event loop
- */
-struct sd_event *afb_ws_client_get_event_loop()
-{
- return afb_common_get_event_loop();
-}
-
struct afb_wsj1;
struct afb_wsj1_itf;
+struct sd_event;
/*
* Makes the WebSocket handshake at the 'uri' and if successful
* instanciate a wsj1 websocket for this connection using 'itf' and 'closure'.
* (see afb_wsj1_create).
+ * The systemd event loop 'eloop' is used to handle the websocket.
* Returns NULL in case of failure with errno set appriately.
*/
-extern struct afb_wsj1 *afb_ws_client_connect_wsj1(const char *uri, struct afb_wsj1_itf *itf, void *closure);
-
-struct sd_event;
-extern struct sd_event *afb_ws_client_get_event_loop();
-
+extern struct afb_wsj1 *afb_ws_client_connect_wsj1(struct sd_event *eloop, const char *uri, struct afb_wsj1_itf *itf, void *closure);
#include "afb-wsj1.h"
#include "afb-ws-json1.h"
+#include "afb-common.h"
#include "afb-msg-json.h"
#include "session.h"
#include "afb-apis.h"
if (result->session == NULL)
goto error2;
- result->wsj1 = afb_wsj1_create(fd, &wsj1_itf, result);
+ result->wsj1 = afb_wsj1_create(afb_common_get_event_loop(), fd, &wsj1_itf, result);
if (result->wsj1 == NULL)
goto error3;
* Creates the afb_ws structure for the file descritor
* 'fd' and the callbacks described by the interface 'itf'
* and its 'closure'.
+ * When the creation is a success, the systemd event loop 'eloop' is
+ * used for handling event for 'fd'.
*
* Returns the handle for the afb_ws created or NULL on error.
*/
-struct afb_ws *afb_ws_create(int fd, const struct afb_ws_itf *itf, void *closure)
+struct afb_ws *afb_ws_create(struct sd_event *eloop, int fd, const struct afb_ws_itf *itf, void *closure)
{
int rc;
struct afb_ws *result;
goto error2;
/* creates the evsrc */
- rc = sd_event_add_io(afb_common_get_event_loop(), &result->evsrc, result->fd, EPOLLIN, io_event_callback, result);
+ rc = sd_event_add_io(eloop, &result->evsrc, result->fd, EPOLLIN, io_event_callback, result);
if (rc < 0) {
errno = -rc;
goto error3;
#pragma once
struct afb_ws;
+struct sd_event;
struct afb_ws_itf
{
void (*on_hangup) (void *); /* optional, it is safe too call afb_ws_destroy within the callback */
};
-extern struct afb_ws *afb_ws_create(int fd, const struct afb_ws_itf *itf, void *closure);
+extern struct afb_ws *afb_ws_create(struct sd_event *eloop, int fd, const struct afb_ws_itf *itf, void *closure);
extern void afb_ws_destroy(struct afb_ws *ws);
extern void afb_ws_hangup(struct afb_ws *ws);
extern int afb_ws_close(struct afb_ws *ws, uint16_t code, const char *reason);
struct wsj1_call *calls;
};
-struct afb_wsj1 *afb_wsj1_create(int fd, struct afb_wsj1_itf *itf, void *closure)
+struct afb_wsj1 *afb_wsj1_create(struct sd_event *eloop, int fd, struct afb_wsj1_itf *itf, void *closure)
{
struct afb_wsj1 *result;
if (result->tokener == NULL)
goto error2;
- result->ws = afb_ws_create(fd, &wsj1_itf, result);
+ result->ws = afb_ws_create(eloop, fd, &wsj1_itf, result);
if (result->ws == NULL)
goto error3;
struct afb_wsj1_msg;
struct json_object;
+struct sd_event;
/*
* Interface for callback functions.
/*
* Creates the afb_wsj1 socket connected to the file descriptor 'fd'
* and having the callback interface defined by 'itf' for the 'closure'.
+ * When the creation is a success, the systemd event loop 'eloop' is
+ * used for handling event for 'fd'.
* Returns the created wsj1 websocket or NULL in case of error.
*/
-extern struct afb_wsj1 *afb_wsj1_create(int fd, struct afb_wsj1_itf *itf, void *closure);
+extern struct afb_wsj1 *afb_wsj1_create(struct sd_event *eloop, int fd, struct afb_wsj1_itf *itf, void *closure);
/*
* Increases by one the count of reference to 'wsj1'
{
global:
afb_ws_client_connect_wsj1;
- afb_ws_client_get_event_loop;
afb_wsj1_*;
afb_common_*;
local: