forked from warmcat/libwebsockets
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add sd-event support to example «minimal-http-server-eventlib-foreign»
- Loading branch information
Showing
5 changed files
with
105 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
minimal-examples/http-server/minimal-http-server-eventlib-foreign/libsdevent.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* lws-minimal-http-server-eventlib-foreign | ||
* | ||
* Written in 2020 by Christian Fuchs <[email protected]> | ||
* | ||
* This file is made available under the Creative Commons CC0 1.0 | ||
* Universal Public Domain Dedication. | ||
* | ||
* The sdevent specific code | ||
*/ | ||
|
||
#include <libwebsockets.h> | ||
|
||
#include <string.h> | ||
#include <signal.h> | ||
|
||
#include <systemd/sd-event.h> | ||
|
||
#include "private.h" | ||
|
||
static struct sd_event *sd_loop; | ||
|
||
static sd_event_source *sd_timer; | ||
static sd_event_source *sd_signal; | ||
|
||
static int | ||
timer_cb_sd(sd_event_source* source, | ||
uint64_t now, | ||
void* user) | ||
{ | ||
foreign_timer_service(sd_loop); | ||
|
||
if (sd_timer) { | ||
sd_event_source_set_time(sd_timer, now + 1000000); | ||
sd_event_source_set_enabled(sd_timer, SD_EVENT_ON); | ||
} | ||
} | ||
|
||
static int | ||
signal_cb_sd(sd_event_source* source, | ||
const struct signalfd_siginfo *si, | ||
void* user) | ||
{ | ||
signal_cb(si->ssi_signo); | ||
return 0; | ||
} | ||
|
||
static void | ||
foreign_event_loop_init_and_run_libsdevent(void) | ||
{ | ||
/* we create and start our "foreign loop" */ | ||
|
||
sd_event_default(&sd_loop); | ||
sd_event_add_signal(sd_loop, &sd_signal, SIGINT, signal_cb_sd, NULL); | ||
uint64_t now; | ||
sd_event_now(sd_loop, CLOCK_MONOTONIC, &now); | ||
sd_event_add_time(sd_loop, &sd_timer, CLOCK_MONOTONIC, now, (uint64_t) 1000, timer_cb_sd, NULL); | ||
|
||
sd_event_loop(sd_loop); | ||
} | ||
|
||
static void | ||
foreign_event_loop_stop_libsdevent(void) | ||
{ | ||
sd_event_exit(sd_loop, 0); | ||
} | ||
|
||
static void | ||
foreign_event_loop_cleanup_libsdevent(void) | ||
{ | ||
sd_event_source_set_enabled(sd_timer, SD_EVENT_OFF); | ||
sd_timer = sd_event_source_unref(sd_timer); | ||
|
||
sd_event_source_set_enabled(sd_signal, SD_EVENT_OFF); | ||
sd_signal = sd_event_source_unref(sd_signal); | ||
|
||
sd_loop = sd_event_unref(sd_loop); | ||
} | ||
|
||
const struct ops ops_sdevent = { | ||
foreign_event_loop_init_and_run_libsdevent, | ||
foreign_event_loop_stop_libsdevent, | ||
foreign_event_loop_cleanup_libsdevent | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters