Created
August 16, 2013 19:04
-
-
Save mistotebe/6252608 to your computer and use it in GitHub Desktop.
Changing bufferevent callbacks when data is pending.
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
#include <signal.h> | |
#include <stdio.h> | |
#include <event2/event.h> | |
#include <event2/bufferevent.h> | |
struct event_base *base; | |
void signal_cb(evutil_socket_t signal, short action, void *arg) | |
{ | |
printf("Sorry\n"); | |
event_base_loopexit(base, NULL); | |
} | |
void read2(struct bufferevent *bev, void *arg) | |
{ | |
printf("Success!\n"); | |
event_base_loopexit(base, NULL); | |
} | |
void switch_cb(evutil_socket_t fd, short action, void *arg) | |
{ | |
struct bufferevent *bev = arg; | |
printf("Step 2\n"); | |
bufferevent_setcb(bev, read2, NULL, NULL, NULL); | |
//bufferevent_setwatermark(bev, EV_READ, 0, 16384); | |
//bufferevent_disable(bev, EV_READ); | |
//bufferevent_enable(bev, EV_READ); | |
// works with BEV_FLUSH for _pair but some implementations ignore flush | |
//bufferevent_flush(bev, EV_READ, BEV_NORMAL); | |
} | |
void read1(struct bufferevent *bev, void *arg) | |
{ | |
struct event *event = arg; | |
printf("Step 1\n"); | |
event_active(event, 0, 0); | |
} | |
int main() | |
{ | |
struct bufferevent *pair[2]; | |
struct event *event; | |
base = event_base_new(); | |
bufferevent_pair_new(base, 0, pair); | |
event = event_new(base, -1, 0, switch_cb, pair[1]); | |
bufferevent_setcb(pair[1], read1, NULL, NULL, event); | |
bufferevent_enable(pair[1], EV_READ); | |
bufferevent_write(pair[0], " ", 1); | |
event = event_new(base, SIGINT, EV_SIGNAL, signal_cb, NULL); | |
event_add(event, NULL); | |
event_base_dispatch(base); | |
printf("Goodbye!\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment