summary |
shortlog |
log |
commit | commitdiff |
review |
tree
raw |
patch |
inline | side by side (from parent 1:
1d0f869)
The use of the event loop where previously hidden
in internal deep places of the websocket modules.
This commits enforce the client of the library to
explicitely tell what event loop must be used.
This has 3 effects:
- you know that the systemd event loop is used
- you tell the event loop to use (no confusion)
- you don't depend on afb-common.c
Change-Id: Id13d8a96f981183c299cde414d9bb0cd77fe3daa
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
###########################################
# build and install libafbwsc
###########################################
###########################################
# 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})
SET_TARGET_PROPERTIES(afbwsc PROPERTIES
VERSION ${LIBAFBWSC_VERSION}
SOVERSION ${LIBAFBWSC_SOVERSION})
/* entry function */
int main(int ac, char **av, char **env)
{
/* 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]);
/* 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]);
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 */
/* 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 (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);
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;
} else {
/* the request is defined by the arguments */
exonrep = 1;
/* loop until end */
for(;;)
/* loop until end */
for(;;)
- sd_event_run(afb_ws_client_get_event_loop(), 30000000);
+ sd_event_run(loop, 30000000);
#include <fcntl.h>
#include "afb-wsj1.h"
#include <fcntl.h>
#include "afb-wsj1.h"
/**************** WebSocket handshake ****************************/
/**************** WebSocket handshake ****************************/
errno = EINVAL;
error:
return -1;
errno = EINVAL;
error:
return -1;
static const char *proto_json1[2] = { "x-afb-ws-json1", NULL };
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];
{
int rc, fd;
char *host, *service, xhost[32];
if (rc == 0) {
rc = negociate(fd, proto_json1, path, xhost);
if (rc == 0) {
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;
if (result != NULL) {
fcntl(fd, F_SETFL, O_NONBLOCK);
break;
-/*
- *
- * 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 afb_wsj1;
struct afb_wsj1_itf;
/*
* 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).
/*
* 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.
*/
* 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-wsj1.h"
#include "afb-ws-json1.h"
#include "afb-msg-json.h"
#include "session.h"
#include "afb-apis.h"
#include "afb-msg-json.h"
#include "session.h"
#include "afb-apis.h"
if (result->session == NULL)
goto error2;
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;
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'.
* 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.
*/
*
* 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;
{
int rc;
struct afb_ws *result;
goto error2;
/* creates the evsrc */
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;
if (rc < 0) {
errno = -rc;
goto error3;
#pragma once
struct afb_ws;
#pragma once
struct afb_ws;
void (*on_hangup) (void *); /* optional, it is safe too call afb_ws_destroy within the callback */
};
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);
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 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;
{
struct afb_wsj1 *result;
if (result->tokener == NULL)
goto error2;
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;
if (result->ws == NULL)
goto error3;
struct afb_wsj1_msg;
struct json_object;
struct afb_wsj1_msg;
struct json_object;
/*
* Interface for callback functions.
/*
* 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'.
/*
* 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.
*/
* 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'
/*
* Increases by one the count of reference to 'wsj1'
{
global:
afb_ws_client_connect_wsj1;
{
global:
afb_ws_client_connect_wsj1;
- afb_ws_client_get_event_loop;
afb_wsj1_*;
afb_common_*;
local:
afb_wsj1_*;
afb_common_*;
local: