Skip to content

Instantly share code, notes, and snippets.

@azat
Created February 19, 2017 11:42
Show Gist options
  • Save azat/298b62d4a1290270f4dd721b6cfb54f4 to your computer and use it in GitHub Desktop.
Save azat/298b62d4a1290270f4dd721b6cfb54f4 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <event2/event.h>
#include <event2/bufferevent.h>
struct arg
{
int i;
int *i_ptr;
};
int events, events_max = 2;
struct bufferevent *pair[2];
static void arg_inc(struct arg *arg, const char *pref)
{
arg->i_ptr = malloc(sizeof(arg->i));
*arg->i_ptr = arg->i;
++*arg->i_ptr;
printf("%s(i)=%i\n", pref, *arg->i_ptr);
}
static void read_cb(struct bufferevent *be, void *_arg)
{
struct arg *arg = _arg;
arg_inc(arg, "read_cb");
/** pass i_ptr next, but do not forget to free it */
}
static void write_cb(struct bufferevent *be, void *_arg)
{
struct arg *arg = _arg;
arg_inc(arg, "write_cb");
if (++events < events_max) {
bufferevent_write(pair[1], "foo", 3);
}
/** pass i_ptr next, but do not forget to free it */
}
int main()
{
struct arg arg = { 0 };
struct event_base *base = event_base_new();
bufferevent_pair_new(base, 0, pair);
bufferevent_enable(pair[0], EV_READ);
bufferevent_setcb(pair[0], read_cb, NULL, NULL, &arg);
bufferevent_setcb(pair[1], NULL, write_cb, NULL, &arg);
bufferevent_write(pair[1], "foo", 3);
event_base_dispatch(base);
/** XXX: leaks */
return 0;
}
#include <stdio.h>
#include <event2/event.h>
#include <event2/bufferevent.h>
struct arg
{
int i;
};
int events, events_max = 2;
struct bufferevent *pair[2];
static void read_cb(struct bufferevent *be, void *_arg)
{
struct arg *arg = _arg;
++arg->i;
printf("read_cb(i)=%i\n", arg->i);
}
static void write_cb(struct bufferevent *be, void *_arg)
{
struct arg *arg = _arg;
++arg->i;
printf("write_cb(i)=%i\n", arg->i);
if (++events < events_max) {
bufferevent_write(pair[1], "foo", 3);
}
}
int main()
{
struct arg arg = { 0 };
struct event_base *base = event_base_new();
bufferevent_pair_new(base, 0, pair);
bufferevent_enable(pair[0], EV_READ);
bufferevent_setcb(pair[0], read_cb, NULL, NULL, &arg);
bufferevent_setcb(pair[1], NULL, write_cb, NULL, &arg);
bufferevent_write(pair[1], "foo", 3);
event_base_dispatch(base);
/** XXX: leaks */
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment