Skip to content

Instantly share code, notes, and snippets.

@frozencemetery
Created August 25, 2017 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frozencemetery/3c34c6979cd3dfa5642838fc21e9c66e to your computer and use it in GitHub Desktop.
Save frozencemetery/3c34c6979cd3dfa5642838fc21e9c66e to your computer and use it in GitHub Desktop.
#include <tevent.h>
#include <stdio.h>
#include <unistd.h>
#ifndef TEVENT_FD_ERROR
#define TEVENT_FD_ERROR 0
#endif /* TEVENT_FD_ERROR */
static int fds[2];
struct tevent_fd *tfd;
static void timer_cb(struct tevent_context *c, struct tevent_timer *e,
struct timeval ct, void *data) {
printf("[timer_cb]; error!\n");
exit(-1);
}
static void error_cb(struct tevent_context *c, struct tevent_fd *e,
uint16_t fl, void *data) {
printf("blue skied an' clear\n");
exit(0);
}
static void read_cb(struct tevent_context *c, struct tevent_fd *e,
uint16_t fl, void *data) {
int len;
char buf[5];
struct tevent_fd *tfd_new;
printf("[read_cb]\n");
/* verto_fire */
len = read(fds[0], buf, 5);
close(fds[0]);
fds[0] = -1;
tfd_new = tevent_add_fd(c, c, fds[1], TEVENT_FD_ERROR, read_cb,
(void *) c);
if (!tfd_new) {
printf("tevent_add_fd\n");
exit(-1);
}
tevent_fd_set_flags(tfd_new, TEVENT_FD_WRITE);
/* delete old CB */
talloc_free(tfd);
tfd = tfd_new;
}
static void write_cb(struct tevent_context *c, struct tevent_fd *e,
uint16_t fl, void *data) {
struct tevent_fd *tfd_new;
printf("[write_cb]\n");
/* verto_fire */
write(fds[1], "hello", 5);
tfd_new = tevent_add_fd(c, c, fds[0], TEVENT_FD_ERROR, read_cb,
(void *) c);
if (!tfd_new) {
printf("tevent_add_fd\n");
exit(-1);
}
tevent_fd_set_flags(tfd_new, TEVENT_FD_READ);
/* delete old CB */
talloc_free(tfd);
tfd = tfd_new;
}
int main() {
int ret;
struct tevent_context *c;
struct timeval tv;
struct tevent_timer *timer;
fds[0] = -1;
fds[1] = -1;
ret = pipe(fds);
if (ret != 0) {
printf("pipe\n");
goto done;
}
c = tevent_context_init(NULL);
if (!c) {
printf("tevent_context_init\n");
goto done;
}
/* verto_add_timeout */
tv = tevent_timeval_current_ofs(1, 1000);
timer = tevent_add_timer(c, c, tv, timer_cb, (void *) c);
if (!timer) {
printf("tevent_add_timer\n");
goto done;
}
/* verto_add_io */
tfd = tevent_add_fd(c, c, fds[1], TEVENT_FD_ERROR, write_cb, (void *) c);
if (!tfd) {
printf("tevent_add_fd\n");
goto done;
}
tevent_fd_set_flags(tfd, TEVENT_FD_WRITE);
while (1) {
printf("[tevent_loop_once]\n");
ret = tevent_loop_once(c);
if (ret) {
printf("tevent_loop_once\n");
goto done;
}
}
done:
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment