xref: /freebsd/contrib/libevent/sample/hello-world.c (revision c43e99fd14c915adcb7173dd49c31e803ceadfe0)
1*c43e99fdSEd Maste /*
2*c43e99fdSEd Maste   This example program provides a trivial server program that listens for TCP
3*c43e99fdSEd Maste   connections on port 9995.  When they arrive, it writes a short message to
4*c43e99fdSEd Maste   each client connection, and closes each connection once it is flushed.
5*c43e99fdSEd Maste 
6*c43e99fdSEd Maste   Where possible, it exits cleanly in response to a SIGINT (ctrl-c).
7*c43e99fdSEd Maste */
8*c43e99fdSEd Maste 
9*c43e99fdSEd Maste 
10*c43e99fdSEd Maste #include <string.h>
11*c43e99fdSEd Maste #include <errno.h>
12*c43e99fdSEd Maste #include <stdio.h>
13*c43e99fdSEd Maste #include <signal.h>
14*c43e99fdSEd Maste #ifndef _WIN32
15*c43e99fdSEd Maste #include <netinet/in.h>
16*c43e99fdSEd Maste # ifdef _XOPEN_SOURCE_EXTENDED
17*c43e99fdSEd Maste #  include <arpa/inet.h>
18*c43e99fdSEd Maste # endif
19*c43e99fdSEd Maste #include <sys/socket.h>
20*c43e99fdSEd Maste #endif
21*c43e99fdSEd Maste 
22*c43e99fdSEd Maste #include <event2/bufferevent.h>
23*c43e99fdSEd Maste #include <event2/buffer.h>
24*c43e99fdSEd Maste #include <event2/listener.h>
25*c43e99fdSEd Maste #include <event2/util.h>
26*c43e99fdSEd Maste #include <event2/event.h>
27*c43e99fdSEd Maste 
28*c43e99fdSEd Maste static const char MESSAGE[] = "Hello, World!\n";
29*c43e99fdSEd Maste 
30*c43e99fdSEd Maste static const int PORT = 9995;
31*c43e99fdSEd Maste 
32*c43e99fdSEd Maste static void listener_cb(struct evconnlistener *, evutil_socket_t,
33*c43e99fdSEd Maste     struct sockaddr *, int socklen, void *);
34*c43e99fdSEd Maste static void conn_writecb(struct bufferevent *, void *);
35*c43e99fdSEd Maste static void conn_eventcb(struct bufferevent *, short, void *);
36*c43e99fdSEd Maste static void signal_cb(evutil_socket_t, short, void *);
37*c43e99fdSEd Maste 
38*c43e99fdSEd Maste int
39*c43e99fdSEd Maste main(int argc, char **argv)
40*c43e99fdSEd Maste {
41*c43e99fdSEd Maste 	struct event_base *base;
42*c43e99fdSEd Maste 	struct evconnlistener *listener;
43*c43e99fdSEd Maste 	struct event *signal_event;
44*c43e99fdSEd Maste 
45*c43e99fdSEd Maste 	struct sockaddr_in sin;
46*c43e99fdSEd Maste #ifdef _WIN32
47*c43e99fdSEd Maste 	WSADATA wsa_data;
48*c43e99fdSEd Maste 	WSAStartup(0x0201, &wsa_data);
49*c43e99fdSEd Maste #endif
50*c43e99fdSEd Maste 
51*c43e99fdSEd Maste 	base = event_base_new();
52*c43e99fdSEd Maste 	if (!base) {
53*c43e99fdSEd Maste 		fprintf(stderr, "Could not initialize libevent!\n");
54*c43e99fdSEd Maste 		return 1;
55*c43e99fdSEd Maste 	}
56*c43e99fdSEd Maste 
57*c43e99fdSEd Maste 	memset(&sin, 0, sizeof(sin));
58*c43e99fdSEd Maste 	sin.sin_family = AF_INET;
59*c43e99fdSEd Maste 	sin.sin_port = htons(PORT);
60*c43e99fdSEd Maste 
61*c43e99fdSEd Maste 	listener = evconnlistener_new_bind(base, listener_cb, (void *)base,
62*c43e99fdSEd Maste 	    LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_FREE, -1,
63*c43e99fdSEd Maste 	    (struct sockaddr*)&sin,
64*c43e99fdSEd Maste 	    sizeof(sin));
65*c43e99fdSEd Maste 
66*c43e99fdSEd Maste 	if (!listener) {
67*c43e99fdSEd Maste 		fprintf(stderr, "Could not create a listener!\n");
68*c43e99fdSEd Maste 		return 1;
69*c43e99fdSEd Maste 	}
70*c43e99fdSEd Maste 
71*c43e99fdSEd Maste 	signal_event = evsignal_new(base, SIGINT, signal_cb, (void *)base);
72*c43e99fdSEd Maste 
73*c43e99fdSEd Maste 	if (!signal_event || event_add(signal_event, NULL)<0) {
74*c43e99fdSEd Maste 		fprintf(stderr, "Could not create/add a signal event!\n");
75*c43e99fdSEd Maste 		return 1;
76*c43e99fdSEd Maste 	}
77*c43e99fdSEd Maste 
78*c43e99fdSEd Maste 	event_base_dispatch(base);
79*c43e99fdSEd Maste 
80*c43e99fdSEd Maste 	evconnlistener_free(listener);
81*c43e99fdSEd Maste 	event_free(signal_event);
82*c43e99fdSEd Maste 	event_base_free(base);
83*c43e99fdSEd Maste 
84*c43e99fdSEd Maste 	printf("done\n");
85*c43e99fdSEd Maste 	return 0;
86*c43e99fdSEd Maste }
87*c43e99fdSEd Maste 
88*c43e99fdSEd Maste static void
89*c43e99fdSEd Maste listener_cb(struct evconnlistener *listener, evutil_socket_t fd,
90*c43e99fdSEd Maste     struct sockaddr *sa, int socklen, void *user_data)
91*c43e99fdSEd Maste {
92*c43e99fdSEd Maste 	struct event_base *base = user_data;
93*c43e99fdSEd Maste 	struct bufferevent *bev;
94*c43e99fdSEd Maste 
95*c43e99fdSEd Maste 	bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
96*c43e99fdSEd Maste 	if (!bev) {
97*c43e99fdSEd Maste 		fprintf(stderr, "Error constructing bufferevent!");
98*c43e99fdSEd Maste 		event_base_loopbreak(base);
99*c43e99fdSEd Maste 		return;
100*c43e99fdSEd Maste 	}
101*c43e99fdSEd Maste 	bufferevent_setcb(bev, NULL, conn_writecb, conn_eventcb, NULL);
102*c43e99fdSEd Maste 	bufferevent_enable(bev, EV_WRITE);
103*c43e99fdSEd Maste 	bufferevent_disable(bev, EV_READ);
104*c43e99fdSEd Maste 
105*c43e99fdSEd Maste 	bufferevent_write(bev, MESSAGE, strlen(MESSAGE));
106*c43e99fdSEd Maste }
107*c43e99fdSEd Maste 
108*c43e99fdSEd Maste static void
109*c43e99fdSEd Maste conn_writecb(struct bufferevent *bev, void *user_data)
110*c43e99fdSEd Maste {
111*c43e99fdSEd Maste 	struct evbuffer *output = bufferevent_get_output(bev);
112*c43e99fdSEd Maste 	if (evbuffer_get_length(output) == 0) {
113*c43e99fdSEd Maste 		printf("flushed answer\n");
114*c43e99fdSEd Maste 		bufferevent_free(bev);
115*c43e99fdSEd Maste 	}
116*c43e99fdSEd Maste }
117*c43e99fdSEd Maste 
118*c43e99fdSEd Maste static void
119*c43e99fdSEd Maste conn_eventcb(struct bufferevent *bev, short events, void *user_data)
120*c43e99fdSEd Maste {
121*c43e99fdSEd Maste 	if (events & BEV_EVENT_EOF) {
122*c43e99fdSEd Maste 		printf("Connection closed.\n");
123*c43e99fdSEd Maste 	} else if (events & BEV_EVENT_ERROR) {
124*c43e99fdSEd Maste 		printf("Got an error on the connection: %s\n",
125*c43e99fdSEd Maste 		    strerror(errno));/*XXX win32*/
126*c43e99fdSEd Maste 	}
127*c43e99fdSEd Maste 	/* None of the other events can happen here, since we haven't enabled
128*c43e99fdSEd Maste 	 * timeouts */
129*c43e99fdSEd Maste 	bufferevent_free(bev);
130*c43e99fdSEd Maste }
131*c43e99fdSEd Maste 
132*c43e99fdSEd Maste static void
133*c43e99fdSEd Maste signal_cb(evutil_socket_t sig, short events, void *user_data)
134*c43e99fdSEd Maste {
135*c43e99fdSEd Maste 	struct event_base *base = user_data;
136*c43e99fdSEd Maste 	struct timeval delay = { 2, 0 };
137*c43e99fdSEd Maste 
138*c43e99fdSEd Maste 	printf("Caught an interrupt signal; exiting cleanly in two seconds.\n");
139*c43e99fdSEd Maste 
140*c43e99fdSEd Maste 	event_base_loopexit(base, &delay);
141*c43e99fdSEd Maste }
142